cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r425746 - /cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/util/ConfigurationUtil.java
Date Wed, 26 Jul 2006 14:37:23 GMT
Author: cziegeler
Date: Wed Jul 26 07:37:22 2006
New Revision: 425746

URL: http://svn.apache.org/viewvc?rev=425746&view=rev
Log:
Excalibur ConfigurationUtil does not support namespaces :(, so let's use our own for now

Added:
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/util/ConfigurationUtil.java
  (with props)

Added: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/util/ConfigurationUtil.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/util/ConfigurationUtil.java?rev=425746&view=auto
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/util/ConfigurationUtil.java
(added)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/util/ConfigurationUtil.java
Wed Jul 26 07:37:22 2006
@@ -0,0 +1,143 @@
+/*
+ * Copyright 1997-2006 The Apache Software Foundation
+ *
+ * Licensed 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.
+ */
+package org.apache.cocoon.util;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.configuration.DefaultConfiguration;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Text;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.CharacterData;
+
+/**
+ * This class is an improved version of the Excalibur ConfigurationUtil class
+ * to support namespaces in DOMs.
+ * @since 2.1.10
+ *
+ * @version $Id$
+ */
+public class ConfigurationUtil {
+    /**
+     * Private constructor to block instantiation.
+     */
+    private ConfigurationUtil() {
+    }
+
+    /**
+     * Convert a DOM Element tree into a configuration tree.
+     *
+     * @param element the DOM Element
+     * @return the configuration object
+     */
+    public static Configuration toConfiguration( final Element element ) {
+        final DefaultConfiguration configuration =
+            new DefaultConfiguration( element.getLocalName(), element.getPrefix(), element.getNamespaceURI(),
element.getPrefix() );
+        final NamedNodeMap attributes = element.getAttributes();
+        final int length = attributes.getLength();
+        for( int i = 0; i < length; i++ ) {
+            final Node node = attributes.item( i );
+            final String name = node.getNodeName();
+            final String value = node.getNodeValue();
+            configuration.setAttribute( name, value );
+        }
+
+        boolean flag = false;
+        String content = "";
+        final NodeList nodes = element.getChildNodes();
+        final int count = nodes.getLength();
+        for( int i = 0; i < count; i++ ) {
+            final Node node = nodes.item( i );
+            if( node instanceof Element ) {
+                final Configuration child = toConfiguration( (Element)node );
+                configuration.addChild( child );
+            } else if( node instanceof CharacterData ) {
+                final CharacterData data = (CharacterData)node;
+                content += data.getData();
+                flag = true;
+            }
+        }
+
+        if( flag ) {
+            configuration.setValue( content );
+        }
+
+        return configuration;
+    }
+
+    /**
+     * Convert a configuration tree into a DOM Element tree.
+     *
+     * @param configuration the configuration object
+     * @return the DOM Element
+     */
+    public static Element toElement( final Configuration configuration )
+    throws ConfigurationException {
+        try {
+            final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+            factory.setNamespaceAware(true);
+            final DocumentBuilder builder = factory.newDocumentBuilder();
+            final Document document = builder.newDocument();
+
+            return createElement( document, configuration );
+        } catch( final ParserConfigurationException pce ) {
+            throw new IllegalStateException( pce.toString() );
+        }
+    }
+
+    /**
+     * Create an DOM {@link Element} from a {@link Configuration}
+     * object.
+     *
+     * @param document the DOM document
+     * @param configuration the configuration to convert
+     * @return the DOM Element
+     */
+    private static Element createElement( final Document document,
+                                          final Configuration configuration )
+    throws ConfigurationException {
+        final Element element = document.createElementNS( configuration.getNamespace(), configuration.getName()
);
+        element.setPrefix( configuration.getLocation() );
+        final String content = configuration.getValue( null );
+        if( null != content )
+        {
+            final Text child = document.createTextNode( content );
+            element.appendChild( child );
+        }
+
+        final String[] names = configuration.getAttributeNames();
+        for( int i = 0; i < names.length; i++ )
+        {
+            final String name = names[ i ];
+            final String value = configuration.getAttribute( name, null );
+            element.setAttribute( name, value );
+        }
+        final Configuration[] children = configuration.getChildren();
+        for( int i = 0; i < children.length; i++ )
+        {
+            final Element child = createElement( document, children[ i ] );
+            element.appendChild( child );
+        }
+        return element;
+    }
+}

Propchange: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/util/ConfigurationUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/util/ConfigurationUtil.java
------------------------------------------------------------------------------
    svn:keywords = Id



Mime
View raw message