You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by cz...@apache.org on 2006/07/26 16:19:45 UTC

svn commit: r425739 - in /cocoon: branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/ trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/event/impl/ trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/...

Author: cziegeler
Date: Wed Jul 26 07:19:44 2006
New Revision: 425739

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

Added:
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/ConfigurationUtil.java   (with props)
Modified:
    cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/event/impl/JavaClassWidgetListenerBuilder.java
    cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/validation/impl/AbstractJavaValidator.java
    cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/validation/impl/JavaClassValidatorBuilder.java

Added: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/ConfigurationUtil.java
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/ConfigurationUtil.java?rev=425739&view=auto
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/ConfigurationUtil.java (added)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/ConfigurationUtil.java Wed Jul 26 07:19:44 2006
@@ -0,0 +1,146 @@
+/*
+ * 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 java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+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/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/ConfigurationUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/ConfigurationUtil.java
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/event/impl/JavaClassWidgetListenerBuilder.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/event/impl/JavaClassWidgetListenerBuilder.java?rev=425739&r1=425738&r2=425739&view=diff
==============================================================================
--- cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/event/impl/JavaClassWidgetListenerBuilder.java (original)
+++ cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/event/impl/JavaClassWidgetListenerBuilder.java Wed Jul 26 07:19:44 2006
@@ -20,7 +20,7 @@
 import org.apache.cocoon.forms.event.WidgetListenerBuilder;
 import org.apache.cocoon.forms.util.DomHelper;
 import org.apache.cocoon.util.ClassUtils;
-import org.apache.avalon.framework.configuration.ConfigurationUtil;
+import org.apache.cocoon.util.ConfigurationUtil;
 import org.apache.avalon.framework.context.Context;
 import org.apache.avalon.framework.context.ContextException;
 import org.apache.avalon.framework.context.Contextualizable;

Modified: cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/validation/impl/AbstractJavaValidator.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/validation/impl/AbstractJavaValidator.java?rev=425739&r1=425738&r2=425739&view=diff
==============================================================================
--- cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/validation/impl/AbstractJavaValidator.java (original)
+++ cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/validation/impl/AbstractJavaValidator.java Wed Jul 26 07:19:44 2006
@@ -18,10 +18,10 @@
 import org.apache.avalon.framework.configuration.Configurable;
 import org.apache.avalon.framework.configuration.Configuration;
 import org.apache.avalon.framework.configuration.ConfigurationException;
-import org.apache.avalon.framework.configuration.ConfigurationUtil;
 import org.apache.cocoon.forms.FormsConstants;
 import org.apache.cocoon.forms.util.DomHelper;
 import org.apache.cocoon.forms.validation.ValidationError;
+import org.apache.cocoon.util.ConfigurationUtil;
 import org.apache.excalibur.xml.sax.XMLizable;
 import org.w3c.dom.Element;
 

Modified: cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/validation/impl/JavaClassValidatorBuilder.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/validation/impl/JavaClassValidatorBuilder.java?rev=425739&r1=425738&r2=425739&view=diff
==============================================================================
--- cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/validation/impl/JavaClassValidatorBuilder.java (original)
+++ cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/validation/impl/JavaClassValidatorBuilder.java Wed Jul 26 07:19:44 2006
@@ -15,7 +15,6 @@
  */
 package org.apache.cocoon.forms.validation.impl;
 
-import org.apache.avalon.framework.configuration.ConfigurationUtil;
 import org.apache.avalon.framework.context.Context;
 import org.apache.avalon.framework.context.ContextException;
 import org.apache.avalon.framework.context.Contextualizable;
@@ -31,6 +30,7 @@
 import org.apache.cocoon.forms.validation.WidgetValidator;
 import org.apache.cocoon.forms.validation.WidgetValidatorBuilder;
 import org.apache.cocoon.util.ClassUtils;
+import org.apache.cocoon.util.ConfigurationUtil;
 import org.w3c.dom.Element;
 
 /**