commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rdon...@apache.org
Subject cvs commit: jakarta-commons/digester/src/test/org/apache/commons/digester NodeCreateRuleTestCase.java
Date Wed, 02 Oct 2002 20:27:18 GMT
rdonkin     2002/10/02 13:27:18

  Modified:    digester build.xml
               digester/src/java/org/apache/commons/digester package.html
  Added:       digester/src/java/org/apache/commons/digester
                        NodeCreateRule.java
               digester/src/test/org/apache/commons/digester
                        NodeCreateRuleTestCase.java
  Log:
  New rule which creates a DOM node from part of the XML tree and pushes it onto the stack.
Submitted by Christopher Lenz.
  
  Revision  Changes    Path
  1.29      +13 -3     jakarta-commons/digester/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/digester/build.xml,v
  retrieving revision 1.28
  retrieving revision 1.29
  diff -u -r1.28 -r1.29
  --- build.xml	30 Sep 2002 19:48:50 -0000	1.28
  +++ build.xml	2 Oct 2002 20:27:17 -0000	1.29
  @@ -251,7 +251,8 @@
                                  test.rule,
                                  test.callmethod,
                                  test.bpsr,
  -                               test.xmlrules
  +                               test.xmlrules,
  +                               test.node
                                 "
      description="Run all unit test cases">
     </target>
  @@ -343,5 +344,14 @@
     </target>
   
   
  -
  +  <target name="test.node" depends="compile.tests"
  +          description="Run NodeCreateRule tests ...">
  +    <echo message="Running NodeCreateRule tests ..."/>
  +    <java classname="${test.runner}" fork="yes"
  +        failonerror="${test.failonerror}">
  +      <arg value="org.apache.commons.digester.NodeCreateRuleTestCase"/>
  +      <classpath refid="test.classpath"/>
  +    </java>
  +  </target>
  + 
   </project>
  
  
  
  1.16      +3 -0      jakarta-commons/digester/src/java/org/apache/commons/digester/package.html
  
  Index: package.html
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/package.html,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- package.html	28 Sep 2002 07:36:33 -0000	1.15
  +++ package.html	2 Oct 2002 20:27:17 -0000	1.16
  @@ -430,6 +430,9 @@
       CallMethodRule within which we are nested.  You can specify that the
       parameter value be taken from a particular named attribute, or from the
       nested body content of this element.</li>
  +<li><a href="NodeCreateRule.html">NodeCreateRule</a> - A specialized
rule
  +    that converts part of the tree into a <code>DOM Node</code> and then
  +    pushes it onto the stack.</li>
   </ul>
   
   <p>You can create instances of the standard <code>Rule</code> classes
and
  
  
  
  1.1                  jakarta-commons/digester/src/java/org/apache/commons/digester/NodeCreateRule.java
  
  Index: NodeCreateRule.java
  ===================================================================
  /*
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:  
   *       "This product includes software developed by the 
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written 
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  
  package org.apache.commons.digester;
  
  
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import javax.xml.parsers.ParserConfigurationException;
  
  import org.w3c.dom.Attr;
  import org.w3c.dom.DOMException;
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  import org.w3c.dom.Node;
  import org.xml.sax.Attributes;
  import org.xml.sax.ContentHandler;
  import org.xml.sax.SAXException;
  import org.xml.sax.XMLReader;
  import org.xml.sax.helpers.DefaultHandler;
  
  
  /**
   * A rule implementation that creates a DOM
   * {@link org.w3c.dom.Node Node} containing the XML at the element that matched
   * the rule. Two concrete types of nodes can be created by this rule:
   * <ul>
   *   <li>the default is to create an {@link org.w3c.dom.Element Element} node.
   *   The created element will correspond to the element that matched the rule,
   *   containing all XML content underneath that element.</li>
   *   <li>alternatively, this rule can create nodes of type
   *   {@link org.w3c.dom.DocumentFragment DocumentFragment}, which will contain
   *   only the XML content under the element the rule was trigged on.</li>
   * </ul>
   * The created node will be normalized, meaning it will not contain text nodes 
   * that only contain white space characters.
   * 
  
   * 
   * <p>The created <code>Node</code> will be pushed on Digester's object
stack
   * when done. To use it in the context of another DOM
   * {@link org.w3c.dom.Document Document}, it must be imported first, using the
   * Document method
   * {@link org.w3c.dom.Document#importNode(org.w3c.dom.Node, boolean) importNode()}.
   * </p>
   *
   * <p><strong>Important Note:</strong> This is implemented by replacing
the SAX
   * {@link org.xml.sax.ContentHandler ContentHandler} in the parser used by 
   * Digester, and resetting it when the matched element is closed. As a side 
   * effect, rules that would match XML nodes under the element that matches 
   * a <code>NodeCreateRule</code> will never be triggered by Digester, which

   * usually is the behavior one would expect.</p>
   * 
   * <p><strong>Note</strong> that the current implementation does not set
the namespace prefixes
   * in the exported nodes. The (usually more important) namespace URIs are set,
   * of course.</p>
   * 
   * @author Christopher Lenz
   * @version $Revision: 1.1 $ $Date: 2002/10/02 20:27:17 $
   * @since Digester 1.4
   */
  
  public class NodeCreateRule extends Rule {
  
  
      // ---------------------------------------------------------- Inner Classes
  
  
      /**
       * The SAX content handler that does all the actual work of assembling the 
       * DOM node tree from the SAX events.
       * 
       * @author <a href="mailto:cmlenz@apache.org">Christopher Lenz</a>
       */
      private class NodeBuilder
          extends DefaultHandler {
  
  
          // ------------------------------------------------------- Constructors
  
  
          /**
           * Constructor.
           * 
           * <p>Stores the content handler currently used by Digester so it can 
           * be reset when done, and initializes the DOM objects needed to 
           * build the node.</p>
           * 
           * @param doc the document to use to create nodes
           * @param root the root node
           * @throws ParserConfigurationException if the DocumentBuilderFactory 
           *   could not be instantiated
           * @throws SAXException if the XMLReader could not be instantiated by 
           *   Digester (should not happen)
           */
          public NodeBuilder(Document doc, Node root)
              throws ParserConfigurationException, SAXException {
  
              this.doc = doc;
              this.root = root;
              this.top = root;
              
              oldContentHandler = digester.getXMLReader().getContentHandler();
  
          }
  
  
          // ------------------------------------------------- Instance Variables
  
  
          /**
           * The content handler used by Digester before it was set to this 
           * content handler.
           */
          protected ContentHandler oldContentHandler = null;
  
  
          /**
           * Depth of the current node, relative to the element where the content
           * handler was put into action.
           */
          protected int depth = 0;
  
  
          /**
           * A DOM Document used to create the various Node instances.
           */
          protected Document doc = null;
  
  
          /**
           * The DOM node that will be pushed on Digester's stack.
           */
          protected Node root = null;
  
  
          /**
           * The current top DOM mode.
           */
          protected Node top = null;
  
  
          // --------------------------------------------- ContentHandler Methods
  
  
          /**
           * Appends a {@link org.w3c.dom.Text Text} node to the current node.
           * 
           * @param ch the characters from the XML document
           * @param start the start position in the array
           * @param length the number of characters to read from the array
           * @throws SAXException if the DOM implementation throws an exception
           */
          public void characters(char[] ch, int start, int length)
              throws SAXException {
  
              try {
                  String str = new String(ch, start, length);
                  if (str.trim().length() > 0) { 
                      top.appendChild(doc.createTextNode(str));
                  }
              } catch (DOMException e) {
                  throw new SAXException(e.getMessage());
              }
  
          }
  
  
          /**
           * Checks whether control needs to be returned to Digester.
           * 
           * @param namespaceURI the namespace URI
           * @param localName the local name
           * @param qName the qualified (prefixed) name
           * @throws SAXException if the DOM implementation throws an exception
           */
          public void endElement(String namespaceURI, String localName,
                                 String qName)
              throws SAXException {
              
              try {
                  if (depth == 0) {
                      getDigester().getXMLReader().setContentHandler(
                          oldContentHandler);
                      getDigester().push(root);
                      getDigester().endElement(namespaceURI, localName, qName);
                  }
      
                  top = top.getParentNode();
                  depth--;
              } catch (DOMException e) {
                  throw new SAXException(e.getMessage());
              }
  
          }
  
  
          /**
           * Adds a new
           * {@link org.w3c.dom.ProcessingInstruction ProcessingInstruction} to 
           * the current node.
           * 
           * @param target the processing instruction target
           * @param data the processing instruction data, or null if none was 
           *   supplied
           * @throws SAXException if the DOM implementation throws an exception
           */
          public void processingInstruction(String target, String data)
              throws SAXException {
              
              try {
                  top.appendChild(doc.createProcessingInstruction(target, data));
              } catch (DOMException e) {
                  throw new SAXException(e.getMessage());
              }
  
          }
  
  
          /**
           * Adds a new child {@link org.w3c.dom.Element Element} to the current
           * node.
           * 
           * @param namespaceURI the namespace URI
           * @param localName the local name
           * @param qName the qualified (prefixed) name
           * @param atts the list of attributes
           * @throws SAXException if the DOM implementation throws an exception
           */
          public void startElement(String namespaceURI, String localName,
                                   String qName, Attributes atts)
              throws SAXException {
  
              try {
                  Node previousTop = top;
                  if ((localName == null) || (localName.length() == 0)) { 
                      top = doc.createElement(qName);
                  } else {
                      top = doc.createElementNS(namespaceURI, localName);
                  }
                  for (int i = 0; i < atts.getLength(); i++) {
                      Attr attr = null;
                      if ((atts.getLocalName(i) == null) ||
                          (atts.getLocalName(i).length() == 0)) {
                          attr = doc.createAttribute(atts.getQName(i));
                          attr.setNodeValue(atts.getValue(i));
                          ((Element)top).setAttributeNode(attr);
                      } else {
                          attr = doc.createAttributeNS(atts.getURI(i),
                                                       atts.getLocalName(i));
                          attr.setNodeValue(atts.getValue(i));
                          ((Element)top).setAttributeNodeNS(attr);
                      }
                  }
                  previousTop.appendChild(top);
                  depth++;
              } catch (DOMException e) {
                  throw new SAXException(e.getMessage());
              }
  
          }
  
      }
  
  
      // ----------------------------------------------------------- Constructors
  
  
      /**
       * Default constructor. Creates an instance of this rule that will create a
       * DOM {@link org.w3c.dom.Element Element}.
       */
      public NodeCreateRule() throws ParserConfigurationException {
  
          this(Node.ELEMENT_NODE);
  
      }
  
  
      /**
       * Constructor. Creates an instance of this rule that will create a DOM
       * {@link org.w3c.dom.Element Element}, but lets you specify the JAXP 
       * <code>DocumentBuilder</code> that should be used when constructing the
       * node tree.
       * 
       * @param documentBuilder the JAXP <code>DocumentBuilder</code> to use
       */
      public NodeCreateRule(DocumentBuilder documentBuilder) {
  
          this(Node.ELEMENT_NODE, documentBuilder);
  
      }
  
  
      /**
       * Constructor. Creates an instance of this rule that will create either a 
       * DOM {@link org.w3c.dom.Element Element} or a DOM 
       * {@link org.w3c.dom.DocumentFragment DocumentFragment}, depending on the
       * value of the <code>nodeType</code> parameter.
       * 
       * @param nodeType the type of node to create, which can be either
       *   {@link org.w3c.dom.Node#ELEMENT_NODE Node.ELEMENT_NODE} or 
       *   {@link org.w3c.dom.Node#DOCUMENT_FRAGMENT_NODE Node.DOCUMENT_FRAGMENT_NODE}
       * @throws IllegalArgumentException if the node type is not supported
       */
      public NodeCreateRule(int nodeType) throws ParserConfigurationException {
  
          this(nodeType,
               DocumentBuilderFactory.newInstance().newDocumentBuilder());
  
      }
  
  
      /**
       * Constructor. Creates an instance of this rule that will create either a 
       * DOM {@link org.w3c.dom.Element Element} or a DOM 
       * {@link org.w3c.dom.DocumentFragment DocumentFragment}, depending on the
       * value of the <code>nodeType</code> parameter. This constructor lets you
       * specify the JAXP <code>DocumentBuilder</code> that should be used when
       * constructing the node tree.
       * 
       * @param nodeType the type of node to create, which can be either
       *   {@link org.w3c.dom.Node#ELEMENT_NODE Node.ELEMENT_NODE} or 
       *   {@link org.w3c.dom.Node#DOCUMENT_FRAGMENT_NODE Node.DOCUMENT_FRAGMENT_NODE}
       * @param documentBuilder the JAXP <code>DocumentBuilder</code> to use
       * @throws IllegalArgumentException if the node type is not supported
       */
      public NodeCreateRule(int nodeType, DocumentBuilder documentBuilder) {
  
          if (!((nodeType == Node.DOCUMENT_FRAGMENT_NODE) ||
                (nodeType == Node.ELEMENT_NODE))) {
              throw new IllegalArgumentException(
                  "Can only create nodes of type DocumentFragment and Element");
          }
          this.nodeType = nodeType;
          this.documentBuilder = documentBuilder;
  
      }
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      /**
       * The JAXP <code>DocumentBuilder</code> to use.
       */
      private DocumentBuilder documentBuilder = null;
  
  
      /**
       * The type of the node that should be created. Must be one of the
       * constants defined in {@link org.w3c.dom.Node Node}, but currently only
       * {@link org.w3c.dom.Node#ELEMENT_NODE Node.ELEMENT_NODE} and 
       * {@link org.w3c.dom.Node#DOCUMENT_FRAGMENT_NODE Node.DOCUMENT_FRAGMENT_NODE}
       * are allowed values.
       */
      private int nodeType = Node.ELEMENT_NODE;
  
  
      // ----------------------------------------------------------- Rule Methods
  
  
      /**
       * Implemented to replace the content handler currently in use by a 
       * {@link NodeBuilder NodeCreateRule.NodeBuilder}.
       * 
       * @param namespaceURI the namespace URI of the matching element, or an 
       *   empty string if the parser is not namespace aware or the element has
       *   no namespace
       * @param name the local name if the parser is namespace aware, or just 
       *   the element name otherwise
       * @param attributes The attribute list of this element
       * @throws Exception indicates a JAXP configuration problem
       */
      public void begin(String namespaceURI, String name, Attributes attributes)
          throws Exception {
  
          XMLReader xmlReader = getDigester().getXMLReader();
          Document doc = documentBuilder.newDocument();
          NodeBuilder builder = null;
          if (nodeType == Node.ELEMENT_NODE) {
              Element element = null;
              if (getDigester().getNamespaceAware()) {
                  element =
                      doc.createElementNS(namespaceURI, name);
                  for (int i = 0; i < attributes.getLength(); i++) {
                      element.setAttributeNS(attributes.getURI(i),
                                             attributes.getLocalName(i),
                                             attributes.getValue(i));
                  }
              } else {
                  element = doc.createElement(name);
                  for (int i = 0; i < attributes.getLength(); i++) {
                      element.setAttribute(attributes.getQName(i),
                                           attributes.getValue(i));
                  }
              }
              builder = new NodeBuilder(doc, element);
          } else {
              builder = new NodeBuilder(doc, doc.createDocumentFragment());
          }
          xmlReader.setContentHandler(builder);
  
      }
  
  
  }
  
  
  
  1.1                  jakarta-commons/digester/src/test/org/apache/commons/digester/NodeCreateRuleTestCase.java
  
  Index: NodeCreateRuleTestCase.java
  ===================================================================
  /*
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  
  package org.apache.commons.digester;
  
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.StringReader;
  import java.util.ArrayList;
  import java.util.List;
  
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import javax.xml.parsers.ParserConfigurationException;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  import org.w3c.dom.DOMImplementation;
  import org.w3c.dom.Document;
  import org.w3c.dom.DocumentFragment;
  import org.w3c.dom.Element;
  import org.w3c.dom.Node;
  import org.xml.sax.EntityResolver;
  import org.xml.sax.ErrorHandler;
  import org.xml.sax.InputSource;
  import org.xml.sax.SAXException;
  
  
  /**
   * <p>Test case for the <code>NodeCreateRule</code>.
   * 
   * @author Christopher Lenz
   * @version $Revision: 1.1 $ $Date: 2002/10/02 20:27:18 $
   */
  
  public class NodeCreateRuleTestCase extends TestCase {
  
  
      // ----------------------------------------------------- Instance Variables
  
      /**
       * Simple test xml document used in the tests.
       */
      protected final static String TEST_XML =
          "<?xml version='1.0'?><root>ROOT BODY<alpha>ALPHA BODY</alpha>"
+
          "<beta>BETA BODY</beta><gamma>GAMMA BODY</gamma></root>";
  
  
      /**
       * The digester instance we will be processing.
       */
      protected Digester digester = null;
  
  
      // ----------------------------------------------------------- Constructors
  
  
      /**
       * Construct a new instance of this test case.
       *
       * @param name Name of the test case
       */
      public NodeCreateRuleTestCase(String name) {
  
          super(name);
  
      }
  
  
      // --------------------------------------------------- Overall Test Methods
  
  
      /**
       * Set up instance variables required by this test case.
       */
      public void setUp() {
  
          digester = new Digester();
  
      }
  
  
      /**
       * Return the tests included in this test suite.
       */
      public static Test suite() {
  
          return (new TestSuite(NodeCreateRuleTestCase.class));
  
      }
  
  
      /**
       * Tear down instance variables required by this test case.
       */
      public void tearDown() {
  
          digester = null;
  
      }
  
  
  
      // ------------------------------------------------ Individual Test Methods
  
  
      /**
       * Tests simple element construction, using the {@link TEST_XML TEST_XML}
       * XML input data.
       */
      public void testInvalidNodeTypes()
          throws SAXException, ParserConfigurationException, IOException {
  
          try {
              Rule rule = new NodeCreateRule(Node.ATTRIBUTE_NODE);
              fail("IllegalArgumentException expected for type ATTRIBUTE_NODE");
          } catch (IllegalArgumentException iae) {
              // expected
          }
          try {
              Rule rule = new NodeCreateRule(Node.CDATA_SECTION_NODE);
              fail("IllegalArgumentException expected for type " +
                   "CDATA_SECTION_NODE");
          } catch (IllegalArgumentException iae) {
              // expected
          }
          try {
              Rule rule = new NodeCreateRule(Node.COMMENT_NODE);
              fail("IllegalArgumentException expected for type COMMENT_NODE");
          } catch (IllegalArgumentException iae) {
              // expected
          }
          try {
              Rule rule = new NodeCreateRule(Node.DOCUMENT_NODE);
              fail("IllegalArgumentException expected for type DOCUMENT_NODE");
          } catch (IllegalArgumentException iae) {
              // expected
          }
          try {
              Rule rule = new NodeCreateRule(Node.DOCUMENT_TYPE_NODE);
              fail("IllegalArgumentException expected for type " +
                   "DOCUMENT_TYPE_NODE");
          } catch (IllegalArgumentException iae) {
              // expected
          }
          try {
              Rule rule = new NodeCreateRule(Node.ENTITY_NODE);
              fail("IllegalArgumentException expected for type ENTITY_NODE");
          } catch (IllegalArgumentException iae) {
              // expected
          }
          try {
              Rule rule = new NodeCreateRule(Node.ENTITY_REFERENCE_NODE);
              fail("IllegalArgumentException expected for type " +
                   "ENTITY_REFERENCE_NODE");
          } catch (IllegalArgumentException iae) {
              // expected
          }
          try {
              Rule rule = new NodeCreateRule(Node.NOTATION_NODE);
              fail("IllegalArgumentException expected for type NOTATION_NODE");
          } catch (IllegalArgumentException iae) {
              // expected
          }
          try {
              Rule rule = new NodeCreateRule(Node.PROCESSING_INSTRUCTION_NODE);
              fail("IllegalArgumentException expected for type " +
                   "PROCESSING_INSTRUCTION_NODE");
          } catch (IllegalArgumentException iae) {
              // expected
          }
          try {
              Rule rule = new NodeCreateRule(Node.TEXT_NODE);
              fail("IllegalArgumentException expected for type TEXT_NODE");
          } catch (IllegalArgumentException iae) {
              // expected
          }
  
      }
  
      /**
       * Tests simple element construction, using the {@link TEST_XML TEST_XML}
       * XML input data.
       */
      public void testElement()
          throws SAXException, ParserConfigurationException, IOException {
  
          digester.addRule("root/alpha", new NodeCreateRule());
          Object result = digester.parse(new StringReader(TEST_XML));
  
          assertNotNull(result);
          assertTrue(result instanceof Element);
          Element element = (Element)result;
          assertEquals("alpha", element.getNodeName());
          assertNull(((Element)element).getLocalName());
          assertNull(((Element)element).getNamespaceURI());
          assertEquals(1, element.getChildNodes().getLength());
          assertEquals("ALPHA BODY", element.getFirstChild().getNodeValue());
  
      }
  
  
      /**
       * Tests simple fragment construction, using the {@link TEST_XML TEST_XML}
       * XML input data.
       */
      public void testDocumentFragment()
          throws SAXException, ParserConfigurationException, IOException {
  
          digester.addRule("root",
                           new NodeCreateRule(Node.DOCUMENT_FRAGMENT_NODE));
          Object result = digester.parse(new StringReader(TEST_XML));
  
          assertNotNull(result);
          assertTrue(result instanceof DocumentFragment);
          DocumentFragment fragment = (DocumentFragment)result;
          assertEquals(4, fragment.getChildNodes().getLength());
  
          Node rootBody = fragment.getFirstChild();
          assertEquals(Node.TEXT_NODE, rootBody.getNodeType());
          assertEquals("ROOT BODY", rootBody.getNodeValue());
  
          Node alpha = fragment.getChildNodes().item(1);
          assertEquals(Node.ELEMENT_NODE, alpha.getNodeType());
          assertEquals("alpha", alpha.getNodeName());
          assertNull(((Element)alpha).getLocalName());
          assertNull(((Element)alpha).getNamespaceURI());
          assertEquals(1, alpha.getChildNodes().getLength());
          assertEquals("ALPHA BODY", alpha.getFirstChild().getNodeValue());
  
          Node beta = fragment.getChildNodes().item(2);
          assertEquals(Node.ELEMENT_NODE, beta.getNodeType());
          assertEquals("beta", beta.getNodeName());
          assertNull(((Element)beta).getLocalName());
          assertNull(((Element)beta).getNamespaceURI());
          assertEquals(1, beta.getChildNodes().getLength());
          assertEquals("BETA BODY", beta.getFirstChild().getNodeValue());
  
          Node gamma = fragment.getChildNodes().item(3);
          assertEquals(Node.ELEMENT_NODE, gamma.getNodeType());
          assertEquals("gamma", gamma.getNodeName());
          assertNull(((Element)gamma).getLocalName());
          assertNull(((Element)gamma).getNamespaceURI());
          assertEquals(1, gamma.getChildNodes().getLength());
          assertEquals("GAMMA BODY", gamma.getFirstChild().getNodeValue());
  
      }
  
  
      /**
       * Tests whether control is returned to digester after fragment
       * construction.
       */
      public void testNested()
          throws SAXException, ParserConfigurationException, IOException {
  
          digester.addObjectCreate("root", ArrayList.class);
          digester.addRule("root/a/b",
                           new NodeCreateRule(Node.DOCUMENT_FRAGMENT_NODE));
          digester.addSetRoot("root/a/b", "add");
          digester.addObjectCreate("root/b", String.class);
          digester.addSetRoot("root/b", "add");
          Object result = digester.parse(getInputStream("Test4.xml"));
  
          assertNotNull(result);
          assertTrue(result instanceof List);
          List list = (List)result;
          assertEquals(2, list.size());
  
          assertTrue(list.get(0) instanceof DocumentFragment);
          DocumentFragment fragment = (DocumentFragment)list.get(0);
  
          assertEquals(Node.ELEMENT_NODE,
                       fragment.getFirstChild().getNodeType());
          Element a = (Element)fragment.getFirstChild();
          assertEquals("a", a.getNodeName());
          assertEquals(1, a.getAttributes().getLength());
          assertEquals("THREE", a.getAttribute("name"));
  
          assertTrue(list.get(1) instanceof String);
  
      }
  
  
      /**
       * Tests whether attributes are correctly imported into the fragment, using
       * the example in the Test1 XML file.
       */
      public void testAttributes()
          throws SAXException, ParserConfigurationException, IOException {
  
          digester.addRule("employee",
                           new NodeCreateRule(Node.DOCUMENT_FRAGMENT_NODE));
          Object result = digester.parse(getInputStream("Test1.xml"));
  
          assertNotNull(result);
          assertTrue(result instanceof DocumentFragment);
          DocumentFragment fragment = (DocumentFragment)result;
          assertEquals(2, fragment.getChildNodes().getLength());
  
          assertEquals(Node.ELEMENT_NODE, fragment.getFirstChild().getNodeType());
          Element address1 = (Element)fragment.getFirstChild();
          assertEquals("address", address1.getNodeName());
          assertEquals(5, address1.getAttributes().getLength());
          assertEquals("home", address1.getAttribute("type"));
          assertEquals("Home Street", address1.getAttribute("street"));
          assertEquals("Home City", address1.getAttribute("city"));
          assertEquals("HS", address1.getAttribute("state"));
          assertEquals("HmZip", address1.getAttribute("zipCode"));
  
          assertEquals(Node.ELEMENT_NODE, fragment.getLastChild().getNodeType());
          Element address2 = (Element)fragment.getLastChild();
          assertEquals("address", address2.getNodeName());
          assertEquals(5, address2.getAttributes().getLength());
          assertEquals("office", address2.getAttribute("type"));
          assertEquals("Office Street", address2.getAttribute("street"));
          assertEquals("Office City", address2.getAttribute("city"));
          assertEquals("OS", address2.getAttribute("state"));
          assertEquals("OfZip", address2.getAttribute("zipCode"));
  
      }
  
  
      /**
       * Tests whether namespaces are handled correctly, using the example from 
       * the file Test3 XML file.
       */
      public void testNamespaces()
          throws SAXException, ParserConfigurationException, IOException {
  
          digester.setNamespaceAware(true);
          digester.setRuleNamespaceURI(null);
          digester.addRule("employee",
                           new NodeCreateRule(Node.DOCUMENT_FRAGMENT_NODE));
          Object result = digester.parse(getInputStream("Test3.xml"));
  
          assertNotNull(result);
          assertTrue(result instanceof DocumentFragment);
          DocumentFragment fragment = (DocumentFragment)result;
          assertEquals(2, fragment.getChildNodes().getLength());
  
          assertEquals(Node.ELEMENT_NODE, fragment.getFirstChild().getNodeType());
          Element address1 = (Element)fragment.getFirstChild();
          assertEquals("address", address1.getNodeName());
          assertEquals("http://jakarta.apache.org/digester/Bar",
                       address1.getNamespaceURI());
          assertEquals("address", address1.getLocalName());
          assertEquals(5, address1.getAttributes().getLength());
          assertEquals("home", address1.getAttribute("type"));
          assertEquals("Home Street", address1.getAttribute("street"));
          assertEquals("Home City", address1.getAttribute("city"));
          assertEquals("HS", address1.getAttribute("state"));
          assertEquals("HmZip", address1.getAttribute("zipCode"));
  
          assertEquals(Node.ELEMENT_NODE, fragment.getLastChild().getNodeType());
          Element address2 = (Element)fragment.getLastChild();
          assertEquals("address", address2.getNodeName());
          assertEquals("http://jakarta.apache.org/digester/Bar",
                       address2.getNamespaceURI());
          assertEquals("address", address2.getLocalName());
          assertEquals(5, address2.getAttributes().getLength());
          assertEquals("office", address2.getAttribute("type"));
          assertEquals("Office Street", address2.getAttribute("street"));
          assertEquals("Office City", address2.getAttribute("city"));
          assertEquals("OS", address2.getAttribute("state"));
          assertEquals("OfZip", address2.getAttribute("zipCode"));
  
      }
  
  
      /**
       * Tests whether the created fragment can be imported into an existing 
       * document.
       */
      public void testImport()
          throws SAXException, ParserConfigurationException, IOException {
  
          digester.addRule("root",
                           new NodeCreateRule(Node.DOCUMENT_FRAGMENT_NODE));
          Object result = digester.parse(new StringReader(TEST_XML));
          DocumentFragment fragment = (DocumentFragment)result;
  
          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
          DocumentBuilder builder = factory.newDocumentBuilder();
          Document doc = builder.newDocument();
          Node importedFragment = doc.importNode(fragment, true);
          doc.appendChild(doc.createElement("root"));
          doc.getFirstChild().appendChild(importedFragment);
  
      }
  
  
      // ------------------------------------------------ Utility Support Methods
  
  
      /**
       * Return an appropriate InputStream for the specified test file (which
       * must be inside our current package.
       *
       * @param name Name of the test file we want
       *
       * @exception IOException if an input/output error occurs
       */
      protected InputStream getInputStream(String name) throws IOException {
  
          return (this.getClass().getResourceAsStream
                  ("/org/apache/commons/digester/" + name));
  
      }
  
  
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message