You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xindice-dev@xml.apache.org by jb...@apache.org on 2002/10/23 17:58:56 UTC

cvs commit: xml-xindice/java/src/org/apache/xindice/xml/sax SetContentHandler.java SAXEventGenerator.java

jbates      2002/10/23 08:58:56

  Modified:    java/src/org/apache/xindice/xml/dom NodeImpl.java
               java/src/org/apache/xindice/xml/sax SAXEventGenerator.java
  Added:       java/src/org/apache/xindice/xml/sax SetContentHandler.java
  Log:
  Moved SetContentHandler to Xindice and starting namespace W3C conformance
  fixes in set/getContentAsDOM methods of XMLResourceImpl
  
  Revision  Changes    Path
  1.3       +12 -3     xml-xindice/java/src/org/apache/xindice/xml/dom/NodeImpl.java
  
  Index: NodeImpl.java
  ===================================================================
  RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/xml/dom/NodeImpl.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- NodeImpl.java	22 Feb 2002 22:38:55 -0000	1.2
  +++ NodeImpl.java	23 Oct 2002 15:58:56 -0000	1.3
  @@ -82,7 +82,9 @@
      public static final String TYPE_REPLACE = "replace";
      public static final String TYPE_INSERT = "insert";
      public static final String TYPE_APPEND = "append";
  -   
  +
  +
  +    public static final String XMLNS_URI = "http://www.w3.org/2000/xmlns/";
      // Static Exception Instances
      public static final DOMException EX_NO_MODIFICATION_ALLOWED =
         new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, "This Node Is Read-Only");
  @@ -655,9 +657,16 @@
        * @since DOM Level 2
        */
      public final String getNamespaceURI() {
  +
  +      short nodeType = getNodeType();
  +      if ((nodeType == Node.ATTRIBUTE_NODE)
  +        && (nodeName.equals(XMLNS_PREFIX) || nodeName.startsWith(XMLNS_PREFIX + ":"))) {
  +
  +            return XMLNS_URI;
  +      }
         if ( nsURI != null )
            return nsURI;
  -      short nodeType = getNodeType();
  +
         if ( !(nodeType == Node.ELEMENT_NODE || nodeType == Node.ATTRIBUTE_NODE) )
            return null;
   
  
  
  
  1.8       +3 -9      xml-xindice/java/src/org/apache/xindice/xml/sax/SAXEventGenerator.java
  
  Index: SAXEventGenerator.java
  ===================================================================
  RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/xml/sax/SAXEventGenerator.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- SAXEventGenerator.java	23 Oct 2002 12:35:30 -0000	1.7
  +++ SAXEventGenerator.java	23 Oct 2002 15:58:56 -0000	1.8
  @@ -320,15 +320,9 @@
                  if (hasSaxNamespacesPrefixes) {
   
                      /*
  -                    * According to SAX, the local name should be EMPTY, but this
  -                    * breaks a whole lot of other software at the moment, so
  -                    * we leave some acceptable local name for the moment
  -                    *
  -                    * ATTENTION!: default attribute is reported with an EMPTY
  -                    * local name. This is consistent with current (22/10/2002) Xalan
  -                    * behaviour, and is accepted by the XML:DB SDK SetContentHandler.
  +                    * According to SAX, the local name should be EMPTY
                       */
  -                   attrs.addAttribute("", prefix, attrName, "CDATA", attrValue);
  +                   attrs.addAttribute("", "", attrName, "CDATA", attrValue);
                  }
               } else {
   
  
  
  
  1.1                  xml-xindice/java/src/org/apache/xindice/xml/sax/SetContentHandler.java
  
  Index: SetContentHandler.java
  ===================================================================
  package org.apache.xindice.xml.sax;
  
  import org.xml.sax.helpers.DefaultHandler;
  import org.xml.sax.SAXException;
  import org.xml.sax.Attributes;
  import org.xmldb.api.modules.XMLResource;
  
  import java.util.Hashtable;
  import java.util.Enumeration;
  
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * This software is based upon work by:
   * The XML:DB Initiative
   *
   * Copyright (c) 2000-2001 The XML:DB Initiative.  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 acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Xindice" 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 name, 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) 1999-2001, The dbXML
   * Group, L.L.C., http://www.dbxmlgroup.com.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * $Id
   */
  
  /**
   * Simple ContentHandler that just converts the SAX event stream into a text
   * representation of the document and stores it in the associated resource.
   */
  public class SetContentHandler extends DefaultHandler {
  
     protected XMLResource resource = null;
     protected StringBuffer newContent = null;
     protected Hashtable namespaces = null;
  
     public SetContentHandler(XMLResource resource) {
        this.resource = resource;
        namespaces = new Hashtable();
     }
  
     /**
      * Receive notification of the beginning of the document.
      *
      * @exception SAXException Description of Exception
      * @see org.xml.sax.ContentHandler#startDocument
      */
     public void startDocument()
            throws SAXException {
        newContent = new StringBuffer();
        // TODO: what is the proper way to set this?
        newContent.append("<?xml version=\"1.0\"?>");
     }
  
  
     /**
      * Receive notification of the end of the document.
      *
      * @exception SAXException Description of Exception
      * @see org.xml.sax.ContentHandler#endDocument
      */
     public void endDocument()
            throws SAXException {
        try {
           resource.setContent(newContent.toString());
        }
        catch (Exception e) {
           e.printStackTrace();
        }
     }
  
  
     /**
      * Receive notification of the start of a Namespace mapping.
      *
      * @param prefix The Namespace prefix being declared.
      * @param uri The Namespace URI mapped to the prefix.
      * @exception SAXException Description of Exception
      * @see org.xml.sax.ContentHandler#startPrefixMapping
      */
     public void startPrefixMapping(String prefix, String uri)
            throws SAXException {
        namespaces.put(prefix, uri);
     }
  
  
     /**
      * Receive notification of the end of a Namespace mapping.
      *
      * @param prefix The Namespace prefix being declared.
      * @exception SAXException Description of Exception
      * @see org.xml.sax.ContentHandler#endPrefixMapping
      */
     public void endPrefixMapping(String prefix)
            throws SAXException {
        namespaces.remove(prefix);
     }
  
     private String getLocalName(String qn) {
  
         if (qn.indexOf(':') != -1) {
  
             return qn.substring(qn.indexOf(':') + 1);
         } else {
  
             return qn;
         }
     }
  
  
      private String getQNameAtt(String uri, String localName) throws SAXException  {
  
           String prefix = null;
  
           if ("".equals(uri)) {
  
               return localName;
           }
  
           /* Look for prefix */
           Enumeration prefixes = namespaces.keys();
           while (prefixes.hasMoreElements()) {
  
               String key = (String) prefixes.nextElement();
               if ((!("".equals(key))) && namespaces.get(key).equals(uri)) {
  
                   prefix = key;
                   break;
               }
           }
  
           if (prefix == null) {
  
               throw new SAXException("No declared prefix for namespace '"
                     + uri + "'.");
           }
  
           return (prefix + ":" + localName);
      }
  
  
     private String getQNameElement(String uri, String localName) throws SAXException  {
  
          String prefix = null;
  
          if ("".equals(uri)) {
  
              if (namespaces.get("") != null) {
  
                  throw new SAXException("default namespace is declared here!");
              } else {
  
                  return localName;
              }
          }
  
          /* Look for prefix */
          Enumeration prefixes = namespaces.keys();
          while (prefixes.hasMoreElements()) {
  
              String key = (String) prefixes.nextElement();
              if (namespaces.get(key).equals(uri)) {
  
                  prefix = key;
                  break;
              }
          }
  
          if (prefix == null) {
  
              throw new SAXException("No declared prefix for namespace '"
                    + uri + "'.");
          }
  
          return ("".equals(prefix) ? localName : prefix + ":" + localName);
     }
  
  
     /**
      * Receive notification of the start of an element.
      *
      * @param attributes The specified or defaulted attributes.
      * @param uri Description of Parameter
      * @param localName Description of Parameter
      * @param qName Description of Parameter
      * @exception SAXException Description of Exception
      * @see org.xml.sax.ContentHandler#startElement
      */
     public void startElement(String uri, String localName,
           String qName, Attributes attributes)
            throws SAXException {
  
        newContent.append("<");
  
        /* Make up a correct qName if necessary */
        if ("".equals(qName)) {
  
            newContent.append(getQNameElement(uri, localName));
        } else {
  
            newContent.append(qName);
        }
  
  
        for (int i = 0; i < attributes.getLength(); i++) {
           String qn = attributes.getQName(i);
  
           /* Make up a correct qName if necessary */
           if ("".equals(qn)) {
  
               qn = getQNameAtt(attributes.getURI(i), attributes.getLocalName(i));
           }
           newContent.append(" ");
           newContent.append(qn);
           newContent.append("=");
           newContent.append("\"");
           newContent.append(attributes.getValue(i));
           newContent.append("\"");
  
           //Avoid duplicate namespace declarations
           if (qn.equals("xmlns")) {
  
               namespaces.remove("");
           }
  
           if (qn.startsWith("xmlns:")) {
  
              String ln = getLocalName(qn);
              namespaces.remove(ln);
           }
        }
  
        Enumeration enum = namespaces.keys();
        while ( enum.hasMoreElements() ) {
           String key = (String) enum.nextElement();
           newContent.append(" xmlns");
           if (key.length() > 0) {
              newContent.append(":");
              newContent.append(key);
           }
           newContent.append("=");
           newContent.append("\"");
           newContent.append(namespaces.get(key));
           newContent.append("\"");
           namespaces.remove(key);
        }
  
        newContent.append(">");
     }
  
  
     /**
      * Receive notification of the end of an element.
      *
      * @param uri Description of Parameter
      * @param localName Description of Parameter
      * @param qName Description of Parameter
      * @exception SAXException Description of Exception
      * @see org.xml.sax.ContentHandler#endElement
      */
     public void endElement(String uri, String localName, String qName)
            throws SAXException {
        newContent.append("</");
  
         if ("".equals(qName)) {
  
             qName = getQNameElement(uri, localName);
         }
        newContent.append(qName);
        newContent.append(">");
     }
  
  
     /**
      * Receive notification of character data inside an element.
      *
      * @param ch The characters.
      * @param start The start position in the character array.
      * @param length The number of characters to use from the
      * character array.
      * @exception SAXException Description of Exception
      * @see org.xml.sax.ContentHandler#characters
      */
     public void characters(char ch[], int start, int length)
            throws SAXException {
        int i = 0;
        while ( i < length ) {
           char c = ch[start + i];
           switch (c) {
              case '&':
                 newContent.append("&amp;");
                 break;
              case '<':
                 newContent.append("&lt;");
                 break;
              case '>':
                 newContent.append("&gt;");
                 break;
              case '"':
                 newContent.append("&quot;");
                 break;
              case '\'':
                 newContent.append("&apos;");
                 break;
              default:
                 // If we're outside 7 bit ascii encode as a character ref.
                 // Not sure what the proper behavior here should be.
                 if ((int) c > 127) {
                    newContent.append("&#" + (int) c + ";");
                 }
                 else {
                    newContent.append(c);
                 }
           }
  
           i++;
        }
     }
  
  
     /**
      * Receive notification of ignorable whitespace in element content.
      *
      * @param ch The whitespace characters.
      * @param start The start position in the character array.
      * @param length The number of characters to use from the
      * character array.
      * @exception SAXException Description of Exception
      * @see org.xml.sax.ContentHandler#ignorableWhitespace
      */
     public void ignorableWhitespace(char ch[], int start, int length)
            throws SAXException {
        int i = 0;
        while ( i < length ) {
           newContent.append(ch[start + i]);
           i++;
        }
     }
  
  
     /**
      * Receive notification of a processing instruction.
      *
      * @param target The processing instruction target.
      * @param data The processing instruction data, or null if
      * none is supplied.
      * @exception SAXException Description of Exception
      * @see org.xml.sax.ContentHandler#processingInstruction
      */
     public void processingInstruction(String target, String data)
            throws SAXException {
        newContent.append("<?");
        newContent.append(target);
        newContent.append(" ");
  
        if (data != null) {
           newContent.append(data);
        }
  
        newContent.append("?>");
     }
  
  
     /**
      * Receive notification of a skipped entity.
      *
      * @param name The name of the skipped entity.
      * @exception SAXException Description of Exception
      * @see org.xml.sax.ContentHandler#processingInstruction
      */
     public void skippedEntity(String name)
            throws SAXException {
        // no op
     }
  
  }