DomDocumentConfiguration.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* DomDocumentConfiguration.java --
  2. Copyright (C) 2004 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.xml.dom;
  32. import java.util.Arrays;
  33. import java.util.List;
  34. import org.w3c.dom.DOMConfiguration;
  35. import org.w3c.dom.DOMErrorHandler;
  36. import org.w3c.dom.DOMException;
  37. import org.w3c.dom.DOMStringList;
  38. /**
  39. * Document configuration, used to store normalization and other parameters.
  40. *
  41. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  42. */
  43. class DomDocumentConfiguration
  44. implements DOMConfiguration, DOMStringList
  45. {
  46. private static final List SUPPORTED_PARAMETERS =
  47. Arrays.asList(new String[] { "cdata-sections",
  48. "comments",
  49. "element-content-whitespace",
  50. "entities",
  51. "error-handler",
  52. "namespace-declarations",
  53. "split-cdata-sections",
  54. "infoset"});
  55. boolean cdataSections = true;
  56. boolean comments = true;
  57. boolean elementContentWhitespace = true;
  58. boolean entities = true;
  59. DOMErrorHandler errorHandler;
  60. boolean namespaceDeclarations = true;
  61. boolean splitCdataSections = true;
  62. public void setParameter(String name, Object value)
  63. throws DOMException
  64. {
  65. name = name.toLowerCase();
  66. if ("cdata-sections".equals(name))
  67. {
  68. cdataSections = "true".equals(value.toString());
  69. }
  70. else if ("comments".equals(name))
  71. {
  72. comments = "true".equals(value.toString());
  73. }
  74. else if ("element-content-whitespace".equals(name))
  75. {
  76. elementContentWhitespace = "true".equals(value.toString());
  77. }
  78. else if ("entities".equals(name))
  79. {
  80. entities = "true".equals(value.toString());
  81. }
  82. else if ("error-handler".equals(name))
  83. {
  84. try
  85. {
  86. errorHandler = (DOMErrorHandler) value;
  87. }
  88. catch (ClassCastException e)
  89. {
  90. throw new DomDOMException(DOMException.TYPE_MISMATCH_ERR,
  91. value.getClass().getName(), null, 0);
  92. }
  93. }
  94. else if ("namespace-declarations".equals(name))
  95. {
  96. namespaceDeclarations = "true".equals(value.toString());
  97. }
  98. else if ("split-cdata-sections".equals(name))
  99. {
  100. comments = "true".equals(value.toString());
  101. }
  102. else if ("infoset".equals(name))
  103. {
  104. if ("true".equals(value.toString()))
  105. {
  106. entities = false;
  107. cdataSections = false;
  108. namespaceDeclarations = true;
  109. elementContentWhitespace = true;
  110. comments = true;
  111. }
  112. }
  113. else if (("canonical-form".equals(name) ||
  114. "check-character-normalization".equals(name) ||
  115. "datatype-normalization".equals(name) ||
  116. "normalize-characters".equals(name) ||
  117. "validate".equals(name) ||
  118. "validate-if-schema".equals(name)) &&
  119. "false".equals(value.toString()))
  120. {
  121. // NOOP
  122. }
  123. else if (("namespaces".equals(name) ||
  124. "well-formed".equals(name)) &&
  125. "true".equals(value.toString()))
  126. {
  127. // NOOP
  128. }
  129. else
  130. {
  131. throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR,
  132. name, null, 0);
  133. }
  134. }
  135. public Object getParameter(String name)
  136. throws DOMException
  137. {
  138. name = name.toLowerCase();
  139. if ("cdata-sections".equals(name))
  140. {
  141. return cdataSections ? Boolean.TRUE : Boolean.FALSE;
  142. }
  143. else if ("comments".equals(name))
  144. {
  145. return comments ? Boolean.TRUE : Boolean.FALSE;
  146. }
  147. else if ("element-content-whitespace".equals(name))
  148. {
  149. return elementContentWhitespace ? Boolean.TRUE : Boolean.FALSE;
  150. }
  151. else if ("entities".equals(name))
  152. {
  153. return entities ? Boolean.TRUE : Boolean.FALSE;
  154. }
  155. else if ("error-handler".equals(name))
  156. {
  157. return errorHandler;
  158. }
  159. else if ("namespace-declarations".equals(name))
  160. {
  161. return namespaceDeclarations ? Boolean.TRUE : Boolean.FALSE;
  162. }
  163. else if ("split-cdata-sections".equals(name))
  164. {
  165. return comments ? Boolean.TRUE : Boolean.FALSE;
  166. }
  167. else if ("canonical-form".equals(name) ||
  168. "check-character-normalization".equals(name) ||
  169. "datatype-normalization".equals(name) ||
  170. "normalize-characters".equals(name) ||
  171. "validate".equals(name) ||
  172. "validate-if-schema".equals(name))
  173. {
  174. return Boolean.FALSE;
  175. }
  176. else if ("namespaces".equals(name) ||
  177. "well-formed".equals(name))
  178. {
  179. return Boolean.TRUE;
  180. }
  181. else if ("infoset".equals(name))
  182. {
  183. return (entities == false &&
  184. cdataSections == false &&
  185. namespaceDeclarations == true &&
  186. comments == true) ? Boolean.TRUE : Boolean.FALSE;
  187. }
  188. throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR, name, null, 0);
  189. }
  190. public boolean canSetParameter(String name, Object value)
  191. {
  192. name = name.toLowerCase();
  193. if ("error-handler".equals(name))
  194. {
  195. return (value == null || value instanceof DOMErrorHandler);
  196. }
  197. else if (contains(name))
  198. {
  199. return true;
  200. }
  201. else if ("canonical-form".equals(name) ||
  202. "check-character-normalization".equals(name) ||
  203. "datatype-normalization".equals(name) ||
  204. "normalize-characters".equals(name) ||
  205. "validate".equals(name) ||
  206. "validate-if-schema".equals(name))
  207. {
  208. return "false".equals(value.toString());
  209. }
  210. else if ("namespaces".equals(name) ||
  211. "well-formed".equals(name))
  212. {
  213. return "true".equals(value.toString());
  214. }
  215. return false;
  216. }
  217. public DOMStringList getParameterNames()
  218. {
  219. return this;
  220. }
  221. public String item(int i)
  222. {
  223. try
  224. {
  225. return (String) SUPPORTED_PARAMETERS.get(i);
  226. }
  227. catch (IndexOutOfBoundsException e)
  228. {
  229. return null;
  230. }
  231. }
  232. public int getLength()
  233. {
  234. return SUPPORTED_PARAMETERS.size();
  235. }
  236. public boolean contains(String str)
  237. {
  238. str = str.toLowerCase();
  239. return SUPPORTED_PARAMETERS.contains(str);
  240. }
  241. }