001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.util;
018:
019: import javax.xml.parsers.DocumentBuilder;
020: import javax.xml.parsers.DocumentBuilderFactory;
021: import javax.xml.parsers.ParserConfigurationException;
022:
023: import org.apache.avalon.framework.configuration.Configuration;
024: import org.apache.avalon.framework.configuration.ConfigurationException;
025: import org.apache.avalon.framework.configuration.DefaultConfiguration;
026: import org.w3c.dom.Document;
027: import org.w3c.dom.Element;
028: import org.w3c.dom.Text;
029: import org.w3c.dom.NamedNodeMap;
030: import org.w3c.dom.Node;
031: import org.w3c.dom.NodeList;
032: import org.w3c.dom.CharacterData;
033:
034: /**
035: * This class is an improved version of the Excalibur ConfigurationUtil class
036: * to support namespaces in DOMs.
037: * @since 2.1.10
038: *
039: * @version $Id: ConfigurationUtil.java 433543 2006-08-22 06:22:54Z crossley $
040: */
041: public class ConfigurationUtil {
042: /**
043: * Private constructor to block instantiation.
044: */
045: private ConfigurationUtil() {
046: }
047:
048: /**
049: * Convert a DOM Element tree into a configuration tree.
050: *
051: * @param element the DOM Element
052: * @return the configuration object
053: */
054: public static Configuration toConfiguration(final Element element) {
055: final DefaultConfiguration configuration = new DefaultConfiguration(
056: element.getLocalName(), element.getPrefix(), element
057: .getNamespaceURI(), element.getPrefix());
058: final NamedNodeMap attributes = element.getAttributes();
059: final int length = attributes.getLength();
060: for (int i = 0; i < length; i++) {
061: final Node node = attributes.item(i);
062: final String name = node.getNodeName();
063: final String value = node.getNodeValue();
064: configuration.setAttribute(name, value);
065: }
066:
067: boolean flag = false;
068: String content = "";
069: final NodeList nodes = element.getChildNodes();
070: final int count = nodes.getLength();
071: for (int i = 0; i < count; i++) {
072: final Node node = nodes.item(i);
073: if (node instanceof Element) {
074: final Configuration child = toConfiguration((Element) node);
075: configuration.addChild(child);
076: } else if (node instanceof CharacterData) {
077: final CharacterData data = (CharacterData) node;
078: content += data.getData();
079: flag = true;
080: }
081: }
082:
083: if (flag) {
084: configuration.setValue(content);
085: }
086:
087: return configuration;
088: }
089:
090: /**
091: * Convert a configuration tree into a DOM Element tree.
092: *
093: * @param configuration the configuration object
094: * @return the DOM Element
095: */
096: public static Element toElement(final Configuration configuration)
097: throws ConfigurationException {
098: try {
099: final DocumentBuilderFactory factory = DocumentBuilderFactory
100: .newInstance();
101: factory.setNamespaceAware(true);
102: final DocumentBuilder builder = factory
103: .newDocumentBuilder();
104: final Document document = builder.newDocument();
105:
106: return createElement(document, configuration);
107: } catch (final ParserConfigurationException pce) {
108: throw new IllegalStateException(pce.toString());
109: }
110: }
111:
112: /**
113: * Create an DOM {@link Element} from a {@link Configuration}
114: * object.
115: *
116: * @param document the DOM document
117: * @param configuration the configuration to convert
118: * @return the DOM Element
119: */
120: private static Element createElement(final Document document,
121: final Configuration configuration)
122: throws ConfigurationException {
123: final Element element = document.createElementNS(configuration
124: .getNamespace(), configuration.getName());
125: element.setPrefix(configuration.getLocation());
126: final String content = configuration.getValue(null);
127: if (null != content) {
128: final Text child = document.createTextNode(content);
129: element.appendChild(child);
130: }
131:
132: final String[] names = configuration.getAttributeNames();
133: for (int i = 0; i < names.length; i++) {
134: final String name = names[i];
135: final String value = configuration.getAttribute(name, null);
136: element.setAttribute(name, value);
137: }
138: final Configuration[] children = configuration.getChildren();
139: for (int i = 0; i < children.length; i++) {
140: final Element child = createElement(document, children[i]);
141: element.appendChild(child);
142: }
143: return element;
144: }
145: }
|