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 2003/12/18 15:46:21 UTC

cvs commit: cocoon-2.2/src/java/org/apache/cocoon/components/modules/input XMLMetaModule.java

cziegeler    2003/12/18 06:46:21

  Modified:    src/java/org/apache/cocoon/xml/dom DOMUtil.java
               src/java/org/apache/cocoon/transformation
                        SourceWritingTransformer.java
               src/java/org/apache/cocoon/components/modules/input
                        XMLMetaModule.java
  Log:
  Remove dependencies to Xalan and remove deprecated code from DOMUtil
  
  Revision  Changes    Path
  1.7       +42 -95    cocoon-2.2/src/java/org/apache/cocoon/xml/dom/DOMUtil.java
  
  Index: DOMUtil.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.2/src/java/org/apache/cocoon/xml/dom/DOMUtil.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- DOMUtil.java	24 Sep 2003 21:41:12 -0000	1.6
  +++ DOMUtil.java	18 Dec 2003 14:46:21 -0000	1.7
  @@ -51,13 +51,11 @@
   package org.apache.cocoon.xml.dom;
   
   import org.apache.excalibur.source.SourceParameters;
  -import org.apache.xpath.XPathAPI;
   import org.apache.excalibur.xml.xpath.NodeListImpl;
   import org.apache.excalibur.xml.xpath.XPathProcessor;
   import org.apache.excalibur.xml.xpath.XPathUtil;
   import org.apache.cocoon.ProcessingException;
   import org.apache.cocoon.xml.IncludeXMLConsumer;
  -import org.apache.cocoon.xml.XMLUtils;
   
   import org.apache.excalibur.xml.sax.SAXParser;
   import org.apache.excalibur.xml.sax.XMLizable;
  @@ -69,12 +67,10 @@
   import java.util.Collection;
   import java.util.Iterator;
   import java.util.Map;
  -import java.util.Properties;
   
   import javax.xml.parsers.DocumentBuilder;
   import javax.xml.parsers.DocumentBuilderFactory;
   import javax.xml.parsers.ParserConfigurationException;
  -import javax.xml.transform.OutputKeys;
   import javax.xml.transform.TransformerException;
   
   /**
  @@ -185,12 +181,11 @@
        * @param defaultValue Default boolean value.
        * @return     The value of the node or <CODE>defaultValue</CODE>
        */
  -    public static boolean getValueOfNodeAsBoolean(
  -        XPathProcessor processor,
  -        Node root,
  -        String path,
  -        boolean defaultValue)
  -        throws ProcessingException {
  +    public static boolean getValueOfNodeAsBoolean(XPathProcessor processor,
  +                                                  Node root,
  +                                                  String path,
  +                                                  boolean defaultValue)
  +    throws ProcessingException {
           String value = getValueOfNode(processor, root, path);
           if (value == null) {
               return defaultValue;
  @@ -623,14 +618,17 @@
        *
        * @param contextNode The node to start searching from.
        * @param str A valid XPath string.
  +     * @param processor The XPath processor to use
        * @return The first node found that matches the XPath, or null.
        *
        * @throws TransformerException
        */
  -    public static Node getSingleNode(Node contextNode, String str) throws TransformerException {
  +    public static Node getSingleNode(Node contextNode, String str, 
  +                                     XPathProcessor processor) 
  +    throws TransformerException {
           String[] pathComponents = buildPathArray(str);
           if (pathComponents == null) {
  -            return XPathAPI.selectSingleNode(contextNode, str);
  +            return processor.selectSingleNode(contextNode, str); 
           } else {
               return getFirstNodeFromPath(contextNode, pathComponents, false);
           }
  @@ -654,12 +652,14 @@
        * a very simple XPath parser which is not able to parse all kinds of selectors
        * correctly.
        *
  -     * @param rootNode The node to start the search.
  -     * @param path     XPath expression for searching the node.
  -     * @return         The node specified by the path.
  +     * @param rootNode  The node to start the search.
  +     * @param path      XPath expression for searching the node.
  +     * @param processor The XPath processor to use
  +     * @return          The node specified by the path.
        * @throws ProcessingException If no path is specified or the XPath engine fails.
        */
  -    public static Node selectSingleNode(Node rootNode, String path) throws ProcessingException {
  +    public static Node selectSingleNode(Node rootNode, String path, XPathProcessor processor)
  +    throws ProcessingException {
           // Now we have to parse the string
           // First test:  path? rootNode?
           if (path == null) {
  @@ -674,7 +674,7 @@
           // now the first "quick" test is if the node exists using the
           // full XPathAPI
           try {
  -            Node testNode = getSingleNode(rootNode, path);
  +            Node testNode = getSingleNode(rootNode, path, processor);
               if (testNode != null)
                   return testNode;
           } catch (javax.xml.transform.TransformerException local) {
  @@ -720,7 +720,7 @@
   
               Node singleNode;
               try {
  -                singleNode = getSingleNode(parent, nodeName);
  +                singleNode = getSingleNode(parent, nodeName, processor);
               } catch (javax.xml.transform.TransformerException localException) {
                   throw new ProcessingException(
                       "XPathUtil.selectSingleNode: " + localException.getMessage(),
  @@ -812,9 +812,11 @@
        *
        * @param root The node to start the search.
        * @param path XPath search expression.
  +     * @param processor The XPath processor to use
        * @return     The value of the node or <CODE>null</CODE>
        */
  -    public static String getValueOf(Node root, String path) throws ProcessingException {
  +    public static String getValueOf(Node root, String path,
  +                                    XPathProcessor processor) throws ProcessingException {
           if (path == null) {
               throw new ProcessingException("Not a valid XPath: " + path);
           }
  @@ -827,7 +829,7 @@
           }
   
           try {
  -            Node node = getSingleNode(root, path);
  +            Node node = getSingleNode(root, path, processor);
               if (node != null) {
                   return getValueOfNode(node);
               }
  @@ -847,11 +849,13 @@
        * @param root The node to start the search.
        * @param path XPath search expression.
        * @param defaultValue The default value if the node does not exist.
  +     * @param processor The XPath Processor
        * @return     The value of the node or <CODE>defaultValue</CODE>
        */
  -    public static String getValueOf(Node root, String path, String defaultValue)
  -        throws ProcessingException {
  -        String value = getValueOf(root, path);
  +    public static String getValueOf(Node root, String path, String defaultValue,
  +                                    XPathProcessor processor)
  +    throws ProcessingException {
  +        String value = getValueOf(root, path, processor);
           if (value == null)
               value = defaultValue;
   
  @@ -866,11 +870,14 @@
        *
        * @param root The node to start the search.
        * @param path XPath search expression.
  +     * @param processor The XPath Processor
        * @return     The boolean value of the node.
        * @throws ProcessingException If the node is not found.
        */
  -    public static boolean getValueAsBooleanOf(Node root, String path) throws ProcessingException {
  -        String value = getValueOf(root, path);
  +    public static boolean getValueAsBooleanOf(Node root, String path,
  +                                              XPathProcessor processor) 
  +    throws ProcessingException {
  +        String value = getValueOf(root, path, processor);
           if (value == null) {
               throw new ProcessingException("No such node: " + path);
           }
  @@ -887,11 +894,13 @@
        * @param root The node to start the search.
        * @param path XPath search expression.
        * @param defaultValue Default boolean value.
  +     * @param processor The XPath Processor
        * @return     The value of the node or <CODE>defaultValue</CODE>
        */
  -    public static boolean getValueAsBooleanOf(Node root, String path, boolean defaultValue)
  -        throws ProcessingException {
  -        String value = getValueOf(root, path);
  +    public static boolean getValueAsBooleanOf(Node root, String path, boolean defaultValue,
  +                                              XPathProcessor processor)
  +    throws ProcessingException {
  +        String value = getValueOf(root, path, processor);
           if (value == null) {
               return defaultValue;
           }
  @@ -919,15 +928,16 @@
        *
        *  @param contextNode The node to start searching from.
        *  @param str A valid XPath string.
  +     *  @param processor The XPath Processor
        *  @return A NodeIterator, should never be null.
        *
        * @throws TransformerException
        */
  -    public static NodeList selectNodeList(Node contextNode, String str)
  -        throws TransformerException {
  +    public static NodeList selectNodeList(Node contextNode, String str, XPathProcessor processor)
  +    throws TransformerException {
           String[] pathComponents = buildPathArray(str);
           if (pathComponents == null) {
  -            return XPathAPI.selectNodeList(contextNode, str);
  +            return processor.selectNodeList(contextNode, str); 
           } else {
               return getNodeListFromPath(contextNode, pathComponents);
           }
  @@ -1167,69 +1177,6 @@
                   }
               }
           }
  -    }
  -
  -    /**
  -     * Converts a org.w3c.dom.Node to a String. Uses {@link javax.xml.transform.Transformer}
  -     * to convert from a Node to a String.
  -     * 
  -     * @param node a <code>org.w3c.dom.Node</code> value
  -     * @return String representation of the document
  -     * @deprecated Use {@link XMLUtils#serializeNodeToXML(Node)} instead.
  -     */
  -    public static String node2String(Node node) {
  -        try {
  -            return XMLUtils.serializeNodeToXML(node);
  -        } catch (ProcessingException e) {
  -        }
  -        return "";
  -    }
  -
  -    /**
  -     * Create a string representation of a org.w3c.dom.Node and any
  -     * (most) subtypes.
  -     * @param node a <code>org.w3c.dom.Node</code> value
  -     * @param pretty a <code>boolean</code> value whether to format the XML
  -     * @return a <code>String</code> value
  -     * @deprecated Please use {@link XMLUtils#serializeNode(Node, Properties)} instead.
  -     */
  -    public static String node2String(Node node, boolean pretty) {
  -        try {
  -            if (pretty) {
  -                Properties props = new Properties();
  -                props.setProperty(OutputKeys.INDENT, "yes");
  -                return XMLUtils.serializeNode(node, props);
  -            } else {
  -                return XMLUtils.serializeNodeToXML(node);
  -            }
  -        } catch (ProcessingException e) {
  -        }
  -        return "";
  -    }
  -
  -    /**
  -     * Create a string representation of a org.w3c.dom.Node and any
  -     * (most) subtypes.
  -     * @param node a <code>org.w3c.dom.Node</code> value
  -     * @return a <code>StringBuffer</code> value
  -     * @deprecated Please use {@link XMLUtils#serializeNodeToXML(Node)} instead.
  -     */
  -    public static StringBuffer node2StringBuffer(Node node) {
  -        return new StringBuffer(node2String(node));
  -    }
  -
  -    /**
  -     * Create a string representation of a org.w3c.dom.Node and any
  -     * (most) subtypes.
  -     * @param node a <code>org.w3c.dom.Node</code> value
  -     * @param pretty a <code>boolean</code> value whether to format the XML
  -     * @param indent a <code>String</code> value containing spaces as
  -     * initial indent, if null defaults to empty string.
  -     * @return a <code>StringBuffer</code> value
  -     * @deprecated Please use {@link XMLUtils#serializeNode(Node, Properties)} instead.
  -     */
  -    public static StringBuffer node2StringBuffer(Node node, boolean pretty, String indent) {
  -        return new StringBuffer(node2String(node, pretty));
       }
   
   }
  
  
  
  1.9       +34 -8     cocoon-2.2/src/java/org/apache/cocoon/transformation/SourceWritingTransformer.java
  
  Index: SourceWritingTransformer.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.2/src/java/org/apache/cocoon/transformation/SourceWritingTransformer.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- SourceWritingTransformer.java	21 Oct 2003 12:45:12 -0000	1.8
  +++ SourceWritingTransformer.java	18 Dec 2003 14:46:21 -0000	1.9
  @@ -54,10 +54,12 @@
   import java.io.OutputStream;
   import java.util.Map;
   
  +import org.apache.avalon.framework.activity.Disposable;
   import org.apache.avalon.framework.configuration.Configuration;
   import org.apache.avalon.framework.configuration.ConfigurationException;
   import org.apache.avalon.framework.parameters.Parameters;
   import org.apache.avalon.framework.service.ServiceException;
  +import org.apache.avalon.framework.service.ServiceManager;
   import org.apache.avalon.framework.service.ServiceSelector;
   import org.apache.cocoon.ProcessingException;
   import org.apache.cocoon.components.source.SourceUtil;
  @@ -70,6 +72,7 @@
   import org.apache.excalibur.source.Source;
   import org.apache.excalibur.source.SourceException;
   import org.apache.excalibur.xml.dom.DOMParser;
  +import org.apache.excalibur.xml.xpath.XPathProcessor;
   import org.w3c.dom.DOMException;
   import org.w3c.dom.Document;
   import org.w3c.dom.DocumentFragment;
  @@ -258,7 +261,8 @@
    * @version CVS $Id$
    */
   public class SourceWritingTransformer
  -    extends AbstractSAXTransformer {
  +    extends AbstractSAXTransformer
  +    implements Disposable {
   
       public static final String SWT_URI = "http://apache.org/cocoon/source/1.0";
       public static final String DEFAULT_SERIALIZER = "xml";
  @@ -306,7 +310,9 @@
       /** The configured serializer name */
       protected String configuredSerializerName;
   
  -
  +    /** The XPath processor */
  +    protected XPathProcessor xpathProcessor;
  +    
       /**
        * Constructor.
        * Sets the namespace.
  @@ -648,12 +654,12 @@
                   // import the fragment
                   Node importNode = resource.importNode(fragment, true);
                   // get the node
  -                Node parent = DOMUtil.selectSingleNode(resource, path);
  +                Node parent = DOMUtil.selectSingleNode(resource, path, this.xpathProcessor);
   
                   // replace?
                   if (replacePath != null) {
                       try {
  -                        Node replaceNode = DOMUtil.getSingleNode(parent, replacePath);
  +                        Node replaceNode = DOMUtil.getSingleNode(parent, replacePath, this.xpathProcessor);
                           // now get the parent of this node until it is the parent node for insertion
                           while (replaceNode != null && replaceNode.getParentNode().equals(parent) == false) {
                              replaceNode = replaceNode.getParentNode();
  @@ -666,7 +672,7 @@
                                       try {
                                           resource = parser.createDocument();
                                       } finally {
  -                                        this.manager.release(parser );
  +                                        this.manager.release( parser );
                                       }
   
                                       resource.appendChild(resource.importNode(importNode, true));
  @@ -677,7 +683,7 @@
                                   }
                                   message += ", replacing: " + replacePath;
                                   if (reinsertPath != null) {
  -                                    Node insertAt = DOMUtil.getSingleNode(parent, reinsertPath);
  +                                    Node insertAt = DOMUtil.getSingleNode(parent, reinsertPath, this.xpathProcessor);
                                       if (insertAt != null) {
                                           while (replaceNode.hasChildNodes()) {
                                               insertAt.appendChild(replaceNode.getFirstChild());
  @@ -715,7 +721,7 @@
   
                   } else {
                       // get the node
  -                    Node parent = DOMUtil.selectSingleNode(resource, path);
  +                    Node parent = DOMUtil.selectSingleNode(resource, path, this.xpathProcessor);
                       // add fragment
                       parent.appendChild(importNode);
                       message = "content appended to: " + path;
  @@ -829,4 +835,24 @@
               }
           sendEndElementEvent(RESULT_ELEMENT);
       }
  +    
  +    
  +    /* (non-Javadoc)
  +     * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
  +     */
  +    public void service(ServiceManager manager) throws ServiceException {
  +        super.service(manager);
  +        this.xpathProcessor = (XPathProcessor)this.manager.lookup(XPathProcessor.ROLE);
  +    }
  +
  +    /* (non-Javadoc)
  +     * @see org.apache.avalon.framework.activity.Disposable#dispose()
  +     */
  +    public void dispose() {
  +        if ( this.manager != null ) {
  +            this.manager.release(this.xpathProcessor);
  +            this.xpathProcessor = null;
  +        }
  +    }
  +
   }
  
  
  
  1.2       +31 -7     cocoon-2.2/src/java/org/apache/cocoon/components/modules/input/XMLMetaModule.java
  
  Index: XMLMetaModule.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.2/src/java/org/apache/cocoon/components/modules/input/XMLMetaModule.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- XMLMetaModule.java	9 Mar 2003 00:09:04 -0000	1.1
  +++ XMLMetaModule.java	18 Dec 2003 14:46:21 -0000	1.2
  @@ -57,6 +57,9 @@
   import java.util.TreeSet;
   import java.util.Vector;
   
  +import org.apache.avalon.framework.component.Component;
  +import org.apache.avalon.framework.component.ComponentException;
  +import org.apache.avalon.framework.component.ComponentManager;
   import org.apache.avalon.framework.configuration.Configuration;
   import org.apache.avalon.framework.configuration.ConfigurationException;
   import org.apache.avalon.framework.thread.ThreadSafe;
  @@ -65,6 +68,7 @@
   import org.apache.cocoon.environment.Request;
   import org.apache.cocoon.xml.dom.DOMUtil;
   import org.apache.cocoon.xml.dom.DocumentWrapper;
  +import org.apache.excalibur.xml.xpath.XPathProcessor;
   
   import org.w3c.dom.Document;
   import org.w3c.dom.Element;
  @@ -126,11 +130,12 @@
   public class XMLMetaModule extends AbstractMetaModule implements ThreadSafe {
   
       protected String rootName = "root";
  -    protected String ignore = null;
  -    protected String use = null;
  -    protected String strip = null;
  -    protected Object config = null;
  -
  +    protected String ignore;
  +    protected String use;
  +    protected String strip;
  +    protected Object config;
  +    protected XPathProcessor xpathProcessor;
  +    
       protected static final String CACHE_OBJECT_NAME = "org.apache.cocoon.component.modules.input.XMLMetaModule";
   
       final static Vector returnNames;
  @@ -259,7 +264,7 @@
                   if (value.length > 0) {
                       // add new node from xpath 
                       // (since the names are in a set, the node cannot exist already)
  -                    Node node = DOMUtil.selectSingleNode(doc.getDocumentElement(), aName);
  +                    Node node = DOMUtil.selectSingleNode(doc.getDocumentElement(), aName, this.xpathProcessor);
                       node.appendChild( node.getOwnerDocument().createTextNode(value[0].toString()));
   
                       if (value.length > 1) {
  @@ -347,6 +352,25 @@
           Object[] values = new Object[1];
           values[0] = this.getAttribute(name, modeConf, objectModel);
           return values;
  +    }
  +
  +    /* (non-Javadoc)
  +     * @see org.apache.avalon.framework.component.Composable#compose(org.apache.avalon.framework.component.ComponentManager)
  +     */
  +    public void compose(ComponentManager manager) throws ComponentException {
  +        super.compose(manager);
  +        this.xpathProcessor = (XPathProcessor)this.manager.lookup(XPathProcessor.ROLE);
  +    }
  +
  +    /* (non-Javadoc)
  +     * @see org.apache.avalon.framework.activity.Disposable#dispose()
  +     */
  +    public void dispose() {
  +        if ( this.manager != null ) {
  +            this.manager.release((Component)this.xpathProcessor);
  +            this.xpathProcessor = null;
  +        }
  +        super.dispose();
       }
   
   }