123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- /*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
- /* $Id: Accessibility.java 1343632 2012-05-29 09:48:03Z vhennebert $ */
- package org.apache.fop.configuration;
-
- import java.io.StringWriter;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
-
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.ParserConfigurationException;
- import javax.xml.transform.OutputKeys;
- import javax.xml.transform.Transformer;
- import javax.xml.transform.TransformerException;
- import javax.xml.transform.TransformerFactory;
- import javax.xml.transform.dom.DOMSource;
- import javax.xml.transform.stream.StreamResult;
-
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.NamedNodeMap;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
-
- public class DefaultConfiguration implements Configuration {
-
- static final DocumentBuilderFactory DBF = DocumentBuilderFactory.newInstance();
-
- static {
- DBF.setNamespaceAware(false);
- DBF.setValidating(false);
- DBF.setIgnoringComments(true);
- DBF.setIgnoringElementContentWhitespace(true);
- DBF.setExpandEntityReferences(true);
- }
-
- /**
- * @deprecated For debug only.
- */
- public static String toString(Document document) {
- try {
- Transformer transformer = TransformerFactory.newInstance().newTransformer();
- transformer.setOutputProperty(OutputKeys.INDENT, "yes");
- transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
- //initialize StreamResult with File object to save to file
- StreamResult result = new StreamResult(new StringWriter());
- DOMSource source = new DOMSource(document);
- transformer.transform(source, result);
- return result.getWriter().toString();
- } catch (TransformerException e) {
- throw new IllegalStateException(e);
- }
- }
-
- private Element element;
-
- public DefaultConfiguration(String key) {
- DocumentBuilder builder = null;
- try {
- builder = DBF.newDocumentBuilder();
- } catch (ParserConfigurationException e) {
- e.printStackTrace();
- throw new IllegalStateException(e);
- }
- Document doc = builder.newDocument();
- // create the root element node
- element = doc.createElement(key);
- doc.appendChild(element);
- }
-
- DefaultConfiguration(Element element) {
- this.element = element;
- }
-
- Element getElement() {
- return element;
- }
-
- public void addChild(DefaultConfiguration configuration) {
- Element node = (Element) element.getOwnerDocument().importNode(configuration.getElement(), true);
- element.appendChild(node);
- }
-
- String getValue0() {
- String result = element.getTextContent();
- if (result == null) {
- result = "";
- }
- return result;
- }
-
- @Override
- public Configuration getChild(String key) {
- NodeList nl = element.getElementsByTagName(key);
- for (int i = 0; i < nl.getLength(); ++i) {
- Node n = nl.item(i);
- if (n.getNodeName().equals(key)) {
- return new DefaultConfiguration((Element) n);
- }
- }
- return NullConfiguration.INSTANCE;
- }
-
- @Override
- public Configuration getChild(String key, boolean required) {
- Configuration result = getChild(key);
- if (!required && result == NullConfiguration.INSTANCE) {
- return null;
- }
- if (required && (result == null || result == NullConfiguration.INSTANCE)) {
- // throw new IllegalStateException("No child '" + key + "'");
- return NullConfiguration.INSTANCE;
- }
- return result;
- }
-
- @Override
- public Configuration[] getChildren(String key) {
- NodeList nl = element.getElementsByTagName(key);
- Configuration[] result = new Configuration[nl.getLength()];
- for (int i = 0; i < nl.getLength(); ++i) {
- Node n = nl.item(i);
- result[i] = new DefaultConfiguration((Element) n);
- }
- return result;
- }
-
- @Override
- public String[] getAttributeNames() {
- NamedNodeMap nnm = element.getAttributes();
- String[] result = new String[nnm.getLength()];
- for (int i = 0; i < nnm.getLength(); ++i) {
- Node n = nnm.item(i);
- result[i] = n.getNodeName();
- }
- return result;
- }
-
- @Override
- public String getAttribute(String key) {
- String result = element.getAttribute(key);
- if ("".equals(result)) {
- result = null;
- }
- return result;
- }
-
- @Override
- public String getAttribute(String key, String defaultValue) {
- String result = getAttribute(key);
- if (result == null || "".equals(result)) {
- result = defaultValue;
- }
- return result;
- }
-
- @Override
- public boolean getAttributeAsBoolean(String key, boolean defaultValue) {
- String result = getAttribute(key);
- if (result == null || "".equals(result)) {
- return defaultValue;
- }
- return "true".equalsIgnoreCase(result) || "yes".equalsIgnoreCase(result);
- }
-
- @Override
- public float getAttributeAsFloat(String key) throws ConfigurationException {
- return Float.parseFloat(getAttribute(key));
- }
-
- @Override
- public float getAttributeAsFloat(String key, float defaultValue) {
- String result = getAttribute(key);
- if (result == null || "".equals(result)) {
- return defaultValue;
- }
- return Float.parseFloat(result);
- }
-
- @Override
- public int getAttributeAsInteger(String key, int defaultValue) {
- String result = getAttribute(key);
- if (result == null || "".equals(result)) {
- return defaultValue;
- }
- return Integer.parseInt(result);
- }
-
- @Override
- public String getValue() throws ConfigurationException {
- String result = getValue0();
- if (result == null || "".equals(result)) {
- throw new ConfigurationException("No value in " + element.getNodeName());
- }
- return result;
- }
-
- @Override
- public String getValue(String defaultValue) {
- String result = getValue0();
- if (result == null || "".equals(result)) {
- result = defaultValue;
- }
- return result;
- }
-
- @Override
- public boolean getValueAsBoolean() throws ConfigurationException {
- return Boolean.parseBoolean(getValue0());
- }
-
- @Override
- public boolean getValueAsBoolean(boolean defaultValue) {
- String result = getValue0().trim();
- if ("".equals(result)) {
- return defaultValue;
- }
- return Boolean.parseBoolean(result);
- }
-
- @Override
- public int getValueAsInteger() throws ConfigurationException {
- try {
- return Integer.parseInt(getValue0());
- } catch (NumberFormatException e) {
- throw new ConfigurationException("Not an integer", e);
- }
- }
-
- @Override
- public int getValueAsInteger(int defaultValue) {
- String result = getValue0();
- if (result == null || "".equals(result)) {
- return defaultValue;
- }
- return Integer.parseInt(result);
- }
-
- @Override
- public float getValueAsFloat() throws ConfigurationException {
- try {
- return Float.parseFloat(getValue0());
- } catch (NumberFormatException e) {
- throw new ConfigurationException("Not a float", e);
- }
- }
-
- @Override
- public float getValueAsFloat(float defaultValue) {
- String result = getValue0();
- if (result == null || "".equals(result)) {
- return defaultValue;
- }
- return Float.parseFloat(getValue0());
- }
-
- @Override
- public String getLocation() {
- List<String> path = new ArrayList<String>();
- for (Node el = element; el != null; el = el.getParentNode()) {
- if (el instanceof Element) {
- path.add(((Element) el).getTagName());
- }
- }
- Collections.reverse(path);
-
- StringBuilder sb = new StringBuilder();
- for (String s : path) {
- if (sb.length() > 0) {
- sb.append("/");
- }
- sb.append(s);
- }
- return sb.toString();
- }
- }
|