You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultConfiguration.java 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id: Accessibility.java 1343632 2012-05-29 09:48:03Z vhennebert $ */
  18. package org.apache.fop.configuration;
  19. import java.io.StringWriter;
  20. import java.util.ArrayList;
  21. import java.util.Collections;
  22. import java.util.List;
  23. import javax.xml.parsers.DocumentBuilder;
  24. import javax.xml.parsers.DocumentBuilderFactory;
  25. import javax.xml.parsers.ParserConfigurationException;
  26. import javax.xml.transform.OutputKeys;
  27. import javax.xml.transform.Transformer;
  28. import javax.xml.transform.TransformerException;
  29. import javax.xml.transform.TransformerFactory;
  30. import javax.xml.transform.dom.DOMSource;
  31. import javax.xml.transform.stream.StreamResult;
  32. import org.w3c.dom.Document;
  33. import org.w3c.dom.Element;
  34. import org.w3c.dom.NamedNodeMap;
  35. import org.w3c.dom.Node;
  36. import org.w3c.dom.NodeList;
  37. public class DefaultConfiguration implements Configuration {
  38. static final DocumentBuilderFactory DBF = DocumentBuilderFactory.newInstance();
  39. static {
  40. DBF.setNamespaceAware(false);
  41. DBF.setValidating(false);
  42. DBF.setIgnoringComments(true);
  43. DBF.setIgnoringElementContentWhitespace(true);
  44. DBF.setExpandEntityReferences(true);
  45. }
  46. /**
  47. * @deprecated For debug only.
  48. */
  49. public static String toString(Document document) {
  50. try {
  51. Transformer transformer = TransformerFactory.newInstance().newTransformer();
  52. transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  53. transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
  54. //initialize StreamResult with File object to save to file
  55. StreamResult result = new StreamResult(new StringWriter());
  56. DOMSource source = new DOMSource(document);
  57. transformer.transform(source, result);
  58. return result.getWriter().toString();
  59. } catch (TransformerException e) {
  60. throw new IllegalStateException(e);
  61. }
  62. }
  63. private Element element;
  64. public DefaultConfiguration(String key) {
  65. DocumentBuilder builder = null;
  66. try {
  67. builder = DBF.newDocumentBuilder();
  68. } catch (ParserConfigurationException e) {
  69. e.printStackTrace();
  70. throw new IllegalStateException(e);
  71. }
  72. Document doc = builder.newDocument();
  73. // create the root element node
  74. element = doc.createElement(key);
  75. doc.appendChild(element);
  76. }
  77. DefaultConfiguration(Element element) {
  78. this.element = element;
  79. }
  80. Element getElement() {
  81. return element;
  82. }
  83. public void addChild(DefaultConfiguration configuration) {
  84. Element node = (Element) element.getOwnerDocument().importNode(configuration.getElement(), true);
  85. element.appendChild(node);
  86. }
  87. String getValue0() {
  88. String result = element.getTextContent();
  89. if (result == null) {
  90. result = "";
  91. }
  92. return result;
  93. }
  94. @Override
  95. public Configuration getChild(String key) {
  96. NodeList nl = element.getElementsByTagName(key);
  97. for (int i = 0; i < nl.getLength(); ++i) {
  98. Node n = nl.item(i);
  99. if (n.getNodeName().equals(key)) {
  100. return new DefaultConfiguration((Element) n);
  101. }
  102. }
  103. return NullConfiguration.INSTANCE;
  104. }
  105. @Override
  106. public Configuration getChild(String key, boolean required) {
  107. Configuration result = getChild(key);
  108. if (!required && result == NullConfiguration.INSTANCE) {
  109. return null;
  110. }
  111. if (required && (result == null || result == NullConfiguration.INSTANCE)) {
  112. // throw new IllegalStateException("No child '" + key + "'");
  113. return NullConfiguration.INSTANCE;
  114. }
  115. return result;
  116. }
  117. @Override
  118. public Configuration[] getChildren(String key) {
  119. NodeList nl = element.getElementsByTagName(key);
  120. Configuration[] result = new Configuration[nl.getLength()];
  121. for (int i = 0; i < nl.getLength(); ++i) {
  122. Node n = nl.item(i);
  123. result[i] = new DefaultConfiguration((Element) n);
  124. }
  125. return result;
  126. }
  127. @Override
  128. public String[] getAttributeNames() {
  129. NamedNodeMap nnm = element.getAttributes();
  130. String[] result = new String[nnm.getLength()];
  131. for (int i = 0; i < nnm.getLength(); ++i) {
  132. Node n = nnm.item(i);
  133. result[i] = n.getNodeName();
  134. }
  135. return result;
  136. }
  137. @Override
  138. public String getAttribute(String key) {
  139. String result = element.getAttribute(key);
  140. if ("".equals(result)) {
  141. result = null;
  142. }
  143. return result;
  144. }
  145. @Override
  146. public String getAttribute(String key, String defaultValue) {
  147. String result = getAttribute(key);
  148. if (result == null || "".equals(result)) {
  149. result = defaultValue;
  150. }
  151. return result;
  152. }
  153. @Override
  154. public boolean getAttributeAsBoolean(String key, boolean defaultValue) {
  155. String result = getAttribute(key);
  156. if (result == null || "".equals(result)) {
  157. return defaultValue;
  158. }
  159. return "true".equalsIgnoreCase(result) || "yes".equalsIgnoreCase(result);
  160. }
  161. @Override
  162. public float getAttributeAsFloat(String key) throws ConfigurationException {
  163. return Float.parseFloat(getAttribute(key));
  164. }
  165. @Override
  166. public float getAttributeAsFloat(String key, float defaultValue) {
  167. String result = getAttribute(key);
  168. if (result == null || "".equals(result)) {
  169. return defaultValue;
  170. }
  171. return Float.parseFloat(result);
  172. }
  173. @Override
  174. public int getAttributeAsInteger(String key, int defaultValue) {
  175. String result = getAttribute(key);
  176. if (result == null || "".equals(result)) {
  177. return defaultValue;
  178. }
  179. return Integer.parseInt(result);
  180. }
  181. @Override
  182. public String getValue() throws ConfigurationException {
  183. String result = getValue0();
  184. if (result == null || "".equals(result)) {
  185. throw new ConfigurationException("No value in " + element.getNodeName());
  186. }
  187. return result;
  188. }
  189. @Override
  190. public String getValue(String defaultValue) {
  191. String result = getValue0();
  192. if (result == null || "".equals(result)) {
  193. result = defaultValue;
  194. }
  195. return result;
  196. }
  197. @Override
  198. public boolean getValueAsBoolean() throws ConfigurationException {
  199. return Boolean.parseBoolean(getValue0());
  200. }
  201. @Override
  202. public boolean getValueAsBoolean(boolean defaultValue) {
  203. String result = getValue0().trim();
  204. if ("".equals(result)) {
  205. return defaultValue;
  206. }
  207. return Boolean.parseBoolean(result);
  208. }
  209. @Override
  210. public int getValueAsInteger() throws ConfigurationException {
  211. try {
  212. return Integer.parseInt(getValue0());
  213. } catch (NumberFormatException e) {
  214. throw new ConfigurationException("Not an integer", e);
  215. }
  216. }
  217. @Override
  218. public int getValueAsInteger(int defaultValue) {
  219. String result = getValue0();
  220. if (result == null || "".equals(result)) {
  221. return defaultValue;
  222. }
  223. return Integer.parseInt(result);
  224. }
  225. @Override
  226. public float getValueAsFloat() throws ConfigurationException {
  227. try {
  228. return Float.parseFloat(getValue0());
  229. } catch (NumberFormatException e) {
  230. throw new ConfigurationException("Not a float", e);
  231. }
  232. }
  233. @Override
  234. public float getValueAsFloat(float defaultValue) {
  235. String result = getValue0();
  236. if (result == null || "".equals(result)) {
  237. return defaultValue;
  238. }
  239. return Float.parseFloat(getValue0());
  240. }
  241. @Override
  242. public String getLocation() {
  243. List<String> path = new ArrayList<String>();
  244. for (Node el = element; el != null; el = el.getParentNode()) {
  245. if (el instanceof Element) {
  246. path.add(((Element) el).getTagName());
  247. }
  248. }
  249. Collections.reverse(path);
  250. StringBuilder sb = new StringBuilder();
  251. for (String s : path) {
  252. if (sb.length() > 0) {
  253. sb.append("/");
  254. }
  255. sb.append(s);
  256. }
  257. return sb.toString();
  258. }
  259. }