You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by ju...@apache.org on 2002/05/15 08:40:17 UTC

cvs commit: jakarta-slide/src/webdav/server/org/apache/slide/webdav/util XMLValue.java

juergen     02/05/14 23:40:17

  Modified:    src/webdav/server/org/apache/slide/webdav/util XMLValue.java
  Log:
  Added methods XMLValue(String, Namespace) and add(String, Namespace) which allow to specify a default Namespace which will be used for Elements that does have any Namespace.
  (ralf)
  
  Revision  Changes    Path
  1.6       +133 -16   jakarta-slide/src/webdav/server/org/apache/slide/webdav/util/XMLValue.java
  
  Index: XMLValue.java
  ===================================================================
  RCS file: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/util/XMLValue.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- XMLValue.java	25 Apr 2002 21:15:15 -0000	1.5
  +++ XMLValue.java	15 May 2002 06:40:17 -0000	1.6
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/util/XMLValue.java,v 1.5 2002/04/25 21:15:15 jericho Exp $
  - * $Revision: 1.5 $
  - * $Date: 2002/04/25 21:15:15 $
  + * $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/util/XMLValue.java,v 1.6 2002/05/15 06:40:17 juergen Exp $
  + * $Revision: 1.6 $
  + * $Date: 2002/05/15 06:40:17 $
    *
    * ====================================================================
    *
  @@ -74,17 +74,25 @@
   import org.jdom.Document;
   import org.jdom.Element;
   import org.jdom.JDOMException;
  +import org.jdom.Namespace;
   
   import org.jdom.input.SAXBuilder;
  +import org.jdom.JDOMException;
   
   import org.jdom.output.XMLOutputter;
   
  +import org.xml.sax.ContentHandler;
  +import org.xml.sax.Attributes;
  +import org.xml.sax.SAXException;
  +
  +import org.xml.sax.helpers.XMLFilterImpl;
  +
   /**
    * This class is a container for a list of JDOM Elements.
    * The {@link #toString toString()} method provides a XML document fragment
    * describing the Elements of this XMLValue.
    *
  - * @version $Revision: 1.5 $
  + * @version $Revision: 1.6 $
    *
    * @author <a href="mailto:ralf.stuckert@softwareag.com">Ralf Stuckert</a>
    **/
  @@ -163,15 +171,28 @@
        *
        * @param      xmlString  a String representation of a list of XML Elements.
        *
  +     * @throws     JDOMException if parsing the <code>xmlString</code> fails.
        */
  -    public XMLValue(String xmlString) throws JDOMException, IllegalArgumentException{
  +    public XMLValue(String xmlString) throws JDOMException {
  +        this(xmlString, null);
  +    }
  +    
  +    /**
  +     * Creates a XMLValue from the given String representation of a
  +     * list of XML Elements. If the given <code>defaultNamespace</code> is not
  +     * <code>null</code>, all nodes that does not have any namespace will be
  +     * created with that Namespace.
  +     *
  +     * @param      xmlString  a String representation of a list of XML Elements.
  +     * @param      defaultNamespace  the Namespace to use to create nodes that
  +     *                               does not have any namespace.
  +     *                               May be <code>null</code>.
  +     *
  +     * @throws     JDOMException if parsing the <code>xmlString</code> fails.
  +     */
  +    public XMLValue(String xmlString, Namespace defaultNamespace) throws JDOMException {
           this((List)null);
  -        try {
  -            add(xmlString);
  -        }
  -        catch( IOException x ) {
  -            x.printStackTrace();
  -        }
  +        add(xmlString, defaultNamespace);
       }
       
       
  @@ -219,11 +240,25 @@
        *
        * @param      xmlString  a String representation of a list of XML Elements.
        *
  -     * @throws     IllegalArgumentException if one of the list items
  -     *                                      is not a <code>Element</code>.
        * @throws     JDOMException if parsing the <code>xmlString</code> fails.
        */
  -    public void add(String xmlString) throws IllegalArgumentException, JDOMException, IOException {
  +    public void add(String xmlString) throws JDOMException {
  +        add(xmlString, null);
  +    }
  +    
  +    /**
  +     * Adds the Elements given by an XML string representation. If the given
  +     * <code>namespace</code> is not <code>null</code>, all nodes that does not
  +     * have any namespace will be created with that Namespace.
  +     *
  +     * @param      xmlString         a String representation of a list of XML Elements.
  +     * @param      defaultNamespace  the Namespace to use to create nodes that
  +     *                               does not have any namespace.
  +     *                               May be <code>null</code>.
  +     *
  +     * @throws     JDOMException if parsing the <code>xmlString</code> fails.
  +     */
  +    public void add(String xmlString, Namespace defaultNamespace) throws JDOMException {
           
           if (xmlString != null) {
               StringBuffer buffer = new StringBuffer(START_TAG.length() +
  @@ -233,8 +268,20 @@
               buffer.append(xmlString);
               buffer.append(END_TAG);
               SAXBuilder builder = new SAXBuilder();
  -            Document document = builder.build(new StringReader(buffer.toString()));
  -            add(document.getRootElement().getChildren());
  +            if (defaultNamespace != null) {
  +                builder.setXMLFilter(new DefaultNamespaceXMLFilter(defaultNamespace));
  +            }
  +            try {
  +                Document document = builder.build(new StringReader(buffer.toString()));
  +                List children = document.getRootElement().getChildren();
  +                document.getRootElement().removeChildren();
  +                add(children);
  +            }
  +            catch (IOException e) {
  +                // should not happen since the StringReader does not
  +                // perform any "real" I/O
  +                throw new JDOMException(e.getMessage());
  +            }
           }
       }
       
  @@ -320,6 +367,76 @@
               }
           }
           return stringWriter.toString();
  +    }
  +    
  +    /**
  +     * This XMLFilter uses the given namespace as the default, means if no
  +     * namespace is provided.
  +     */
  +    protected static class DefaultNamespaceXMLFilter extends XMLFilterImpl {
  +        
  +        /**
  +         * The namespace to use as default.
  +         */
  +        Namespace defaultNamespace = null;
  +        
  +        /**
  +         * Creates a DefaultNamespaceXMLFilter which uses the given
  +         * <code>defaultNamespace</code> as default namespace, means if no
  +         * namespace is provided.
  +         *
  +         * @param      defaultNamespace  the Namespace to use as default.
  +         *                               Must NOT be <code>null</code>.
  +         */
  +        public DefaultNamespaceXMLFilter(Namespace defaultNamespace) {
  +            this.defaultNamespace = defaultNamespace;
  +        }
  +        
  +        /**
  +         * Overwrite <code>startElement()</code> in order to provide the
  +         * <code>defaultNamespace</code> if the current Element does not have
  +         * any namespace.
  +         *
  +         * @param      namespaceURI  the URI of the Namespace.
  +         * @param      localName     the name of the Element without any namspace
  +         *                           prefix.
  +         * @param      qName         the full name of the Element (with the namespace
  +         *                           prefix if there is one).
  +         * @param      atts          the Attributes of the element.
  +         *
  +         * @throws     SaxException
  +         */
  +        public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
  +            
  +            if ( (namespaceURI == null) || (namespaceURI.length() == 0) ) {
  +                namespaceURI = defaultNamespace.getURI();
  +                qName = defaultNamespace.getPrefix() + ":" + qName;
  +            }
  +            super.startElement(namespaceURI, localName, qName, atts);
  +        }
  +        
  +        /**
  +         * Overwrite <code>endElement()</code> in order to provide the
  +         * <code>defaultNamespace</code> if the current Element does not have
  +         * any namespace.
  +         *
  +         * @param      namespaceURI  the URI of the Namespace.
  +         * @param      localName     the name of the Element without any namspace
  +         *                           prefix.
  +         * @param      qName         the full name of the Element (with the namespace
  +         *                           prefix if there is one).
  +         *
  +         * @throws     SaxException
  +         */
  +        public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
  +            
  +            if ( (namespaceURI == null) || (namespaceURI.length() == 0) ) {
  +                namespaceURI = defaultNamespace.getURI();
  +                qName = defaultNamespace.getPrefix() + ":" + qName;
  +            }
  +            super.endElement(namespaceURI, localName, qName);
  +        }
  +        
       }
       
   }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>