You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by dm...@apache.org on 2004/01/18 02:42:58 UTC

cvs commit: jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/xml XMLParser2.java JDOMParser.java DOMParser.java DocumentContainer.java

dmitri      2004/01/17 17:42:58

  Modified:    jxpath/src/java/org/apache/commons/jxpath/xml
                        JDOMParser.java DOMParser.java
                        DocumentContainer.java
  Added:       jxpath/src/java/org/apache/commons/jxpath/xml
                        XMLParser2.java
  Log:
  Added support for parser features
  
  Revision  Changes    Path
  1.5       +25 -6     jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/xml/JDOMParser.java
  
  Index: JDOMParser.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/xml/JDOMParser.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- JDOMParser.java	9 Oct 2003 21:31:42 -0000	1.4
  +++ JDOMParser.java	18 Jan 2004 01:42:58 -0000	1.5
  @@ -72,14 +72,33 @@
    * @author Dmitri Plotnikov
    * @version $Revision$ $Date$
    */
  -public class JDOMParser implements XMLParser {
  -
  +public class JDOMParser extends XMLParser2 {
  +    
  +    /**
  +     * Temporary fix for JDOM problem - JDOM B9 does not properly handle
  +     * isNamespaceAware = false
  +     */
  +    public boolean isNamespaceAware() {
  +        return true;
  +    }
  +    
       public Object parseXML(InputStream stream) {
           try {
               SAXBuilder builder = new SAXBuilder();
  +            builder.setExpandEntities(isExpandEntityReferences());
  +            builder.setIgnoringElementContentWhitespace(
  +                    isIgnoringElementContentWhitespace());
  +            builder.setValidation(isValidating());
  +            builder.setFeature(
  +                    "http://xml.org/sax/features/namespaces",
  +                    isNamespaceAware());
  +            builder.setFeature(
  +                    "http://xml.org/sax/features/namespace-prefixes",
  +                    isNamespaceAware());
               return builder.build(stream);
           }
           catch (Exception ex) {
  +            ex.printStackTrace();
               throw new JXPathException("JDOM parser error", ex);
           }
       }
  
  
  
  1.5       +13 -5     jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/xml/DOMParser.java
  
  Index: DOMParser.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/xml/DOMParser.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- DOMParser.java	9 Oct 2003 21:31:42 -0000	1.4
  +++ DOMParser.java	18 Jan 2004 01:42:58 -0000	1.5
  @@ -73,12 +73,20 @@
    * @author Dmitri Plotnikov
    * @version $Revision$ $Date$
    */
  -public class DOMParser implements XMLParser {
  +public class DOMParser extends XMLParser2 {
   
       public Object parseXML(InputStream stream) {
           try {
               DocumentBuilderFactory factory =
                       DocumentBuilderFactory.newInstance();
  +            
  +            factory.setValidating(isValidating());
  +            factory.setNamespaceAware(isNamespaceAware());
  +            factory.setIgnoringElementContentWhitespace(
  +                    isIgnoringElementContentWhitespace());
  +            factory.setExpandEntityReferences(isExpandEntityReferences());
  +            factory.setIgnoringComments(isIgnoringComments());
  +            factory.setCoalescing(isCoalescing());
               return factory.newDocumentBuilder().parse(stream);
           }
           catch (Exception ex) {
  
  
  
  1.8       +33 -7     jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/xml/DocumentContainer.java
  
  Index: DocumentContainer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/xml/DocumentContainer.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- DocumentContainer.java	9 Oct 2003 21:31:42 -0000	1.7
  +++ DocumentContainer.java	18 Jan 2004 01:42:58 -0000	1.8
  @@ -81,7 +81,7 @@
    * @author Dmitri Plotnikov
    * @version $Revision$ $Date$
    */
  -public class DocumentContainer implements Container {
  +public class DocumentContainer extends XMLParser2 implements Container {
   
       public static final String MODEL_DOM = "DOM";
       public static final String MODEL_JDOM = "JDOM";
  @@ -109,6 +109,14 @@
       }
   
       /**
  +     * Add a class of a custom XML parser. 
  +     * Parsers for the models "DOM" and "JDOM" are pre-registered.
  +     */    
  +    public static void registerXMLParser(String model, String parserClassName) {
  +        parserClasses.put(model, parserClassName);
  +    }
  +
  +    /**
        * Use this constructor if the desired model is DOM.
        *
        * @param URL is a URL for an XML file.
  @@ -145,7 +153,7 @@
                       if (xmlURL != null) {
                           stream = xmlURL.openStream();
                       }
  -                    document = getParser(model).parseXML(stream);
  +                    document = parseXML(stream);
                   }
                   finally {
                       if (stream != null) {
  @@ -163,6 +171,24 @@
       }
   
       /**
  +     * Parses XML using the parser for the specified model.
  +     */
  +    public Object parseXML(InputStream stream) {
  +        XMLParser parser = getParser(model);
  +        if (parser instanceof XMLParser2) {
  +            XMLParser2 parser2 = (XMLParser2) parser;
  +            parser2.setValidating(isValidating());
  +            parser2.setNamespaceAware(isNamespaceAware());
  +            parser2.setIgnoringElementContentWhitespace(
  +                    isIgnoringElementContentWhitespace());
  +            parser2.setExpandEntityReferences(isExpandEntityReferences());
  +            parser2.setIgnoringComments(isIgnoringComments());
  +            parser2.setCoalescing(isCoalescing());
  +        }
  +        return parser.parseXML(stream);
  +    }
  +
  +    /**
        * Throws an UnsupportedOperationException
        */
       public void setValue(Object value) {
  @@ -181,7 +207,7 @@
               }
               try {
                   Class clazz = Class.forName(className);
  -                parser = (XMLParser) clazz.newInstance();
  +                parser = (XMLParser) clazz.newInstance();                
               }
               catch (Exception ex) {
                   throw new JXPathException(
  
  
  
  1.1                  jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/xml/XMLParser2.java
  
  Index: XMLParser2.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/xml/XMLParser2.java,v 1.1 2004/01/18 01:42:58 dmitri Exp $
   * $Revision: 1.1 $
   * $Date: 2004/01/18 01:42:58 $
   *
   * ====================================================================
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 1999-2003 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 acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements 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 Software Foundation.
   *
   * 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 and was
   * originally based on software copyright (c) 2001, Plotnix, Inc,
   * <http://www.plotnix.com/>.
   * For more information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.commons.jxpath.xml;
  
  import java.io.InputStream;
  
  /**
   * The abstract superclass of XML parsers that produce DOM Documents.
   * The features have the same defaults as DocumentBuilderFactory.
   *
   * @author Dmitri Plotnikov
   * @version $Revision: 1.1 $ $Date: 2004/01/18 01:42:58 $
   */
  public abstract class XMLParser2 implements XMLParser 
  {
      private boolean validating = false;
      private boolean namespaceAware = false;
      private boolean whitespace = false;
      private boolean expandEntityRef = true;
      private boolean ignoreComments = false;
      private boolean coalescing = false;
      
      /**
       * @see DocumentBuilderFactory#setValidating(boolean)
       */
      public void setValidating(boolean validating) {
          this.validating = validating;
      }
      
      /**
       * @see DocumentBuilderFactory#isValidating()
       */
      public boolean isValidating() {
          return validating;
      }
      
      /**
       * @see DocumentBuilderFactory#isNamespaceAware()
       */
      public boolean isNamespaceAware() {
          return namespaceAware;
      }
      
      /**
       * @see DocumentBuilderFactory#setNamespaceAware(boolean)
       */
      public void setNamespaceAware(boolean namespaceAware) {
          this.namespaceAware = namespaceAware;
      }
      
      /**
       * @see DocumentBuilderFactory#setIgnoringElementContentWhitespace(boolean)
       */
      public void setIgnoringElementContentWhitespace(boolean whitespace) {
          this.whitespace = whitespace;
      }
      
      /**
       * @see DocumentBuilderFactory#isIgnoringElementContentWhitespace()
       */
      public boolean isIgnoringElementContentWhitespace() {
          return whitespace;
      }
      
      /**
       * @see DocumentBuilderFactory#isExpandEntityReferences()
       */
      public boolean isExpandEntityReferences() {
          return expandEntityRef;
      }
      
      /**
       * @see DocumentBuilderFactory#setExpandEntityReferences(boolean)
       */
      public void setExpandEntityReferences(boolean expandEntityRef) {
          this.expandEntityRef = expandEntityRef;
      }
      
      /**
       * @see DocumentBuilderFactory#isIgnoringComments()
       */
      public boolean isIgnoringComments() {
          return ignoreComments;
      }
      
      /**
       * @see DocumentBuilderFactory#setIgnoringComments(boolean)
       */
      public void setIgnoringComments(boolean ignoreComments) {
          this.ignoreComments = ignoreComments;
      }
      
      /**
       * @see DocumentBuilderFactory#isCoalescing()
       */
      public boolean isCoalescing() {
          return coalescing;
      }
      
      /**
       * @see DocumentBuilderFactory#setCoalescing(boolean)
       */
      public void setCoalescing(boolean coalescing) {
          this.coalescing = coalescing;
      }
      
      public abstract Object parseXML(InputStream stream);
  }
  
  

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