You are viewing a plain text version of this content. The canonical link for it is here.
Posted to crimson-cvs@xml.apache.org by ed...@apache.org on 2001/04/09 21:47:48 UTC

cvs commit: xml-crimson/src/org/apache/crimson/parser Parser2.java

edwingo     01/04/09 12:47:47

  Modified:    src/org/apache/crimson/parser Parser2.java
  Log:
  Fix ns attribute not recognized if it comes before ns decl
  
  Revision  Changes    Path
  1.8       +66 -24    xml-crimson/src/org/apache/crimson/parser/Parser2.java
  
  Index: Parser2.java
  ===================================================================
  RCS file: /home/cvs/xml-crimson/src/org/apache/crimson/parser/Parser2.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- Parser2.java	2001/03/15 03:40:06	1.7
  +++ Parser2.java	2001/04/09 19:47:46	1.8
  @@ -1,5 +1,5 @@
   /*
  - * $Id: Parser2.java,v 1.7 2001/03/15 03:40:06 edwingo Exp $
  + * $Id: Parser2.java,v 1.8 2001/04/09 19:47:46 edwingo Exp $
    *
    * The Apache Software License, Version 1.1
    *
  @@ -119,7 +119,7 @@
    * @author David Brownell
    * @author Rajiv Mordani
    * @author Edwin Goei
  - * @version $Revision: 1.7 $
  + * @version $Revision: 1.8 $
    */
   public class Parser2
   {
  @@ -1330,6 +1330,11 @@
           // in some cases it's required, though superfluous
           boolean         sawWhite = in.maybeWhitespace ();
   
  +        // These are exceptions from the first pass; they should be ignored
  +        // if there's a second pass, but reported otherwise.  A second pass
  +        // occurs when a namespace declaration is found in the first pass.
  +	Vector exceptions = null;
  +
           // SAX2 Namespace processing
           if (namespaces) {
               nsSupport.pushContext();
  @@ -1408,8 +1413,9 @@
               String defaultValue = (info == null) ? null : info.defaultValue;
   
               if (namespaces) {
  -                processAttributeNS(attQName, type, value, defaultValue,
  -                                   true, false);
  +                exceptions = processAttributeNS(attQName, type, value,
  +                                                defaultValue, true, false,
  +                                                exceptions);
               } else {
                   // No namespaces case
                   attTmp.addAttribute("", "", attQName, type, value,
  @@ -1436,17 +1442,29 @@
               int length = attTmp.getLength();
               for (int i = 0; i < length; i++) {
                   String attQName = attTmp.getQName(i);
  -                if (!attQName.startsWith("xmlns")) {
  -                    String attName[] = processName(attQName, true);
  -                    attTmp.setURI(i, attName[0]);
  -                    attTmp.setLocalName(i, attName[1]);
  +                if (attQName.startsWith("xmlns")) {
  +                    // Could be a namespace declaration
  +
  +                    if (attQName.length() == 5 || attQName.charAt(5) == ':') {
  +                        // Default or non-default NS declaration
  +                        continue;
  +                    }
                   }
  +
  +                // assert(not a namespace declaration)
  +                String attName[] = processName(attQName, true, false);
  +                attTmp.setURI(i, attName[0]);
  +                attTmp.setLocalName(i, attName[1]);
  +            }
  +	} else if (exceptions != null && errHandler != null) {
  +	    for (int i = 0; i < exceptions.size(); i++) {
  +		errHandler.error((SAXParseException)(exceptions.get(i)));
               }
           }
   
           // OK, finally report the event.
           if (namespaces) {
  -            String[] parts = processName(name.name, false);
  +            String[] parts = processName(name.name, false, false);
               contentHandler.startElement(parts[0], parts[1], parts[2], attTmp);
           } else {
               contentHandler.startElement("", "", name.name, attTmp);
  @@ -1484,7 +1502,7 @@
               // Split the name.  Unfortunately, we can't always reuse the
               // info from the startElement event above b/c this element may
               // have subelements and a global temporary is used.
  -            String[] parts = processName(name.name, false);
  +            String[] parts = processName(name.name, false, false);
   
               // Report appropriate events...
               contentHandler.endElement(parts[0], parts[1], parts[2]);
  @@ -1510,27 +1528,30 @@
        * @param isDefaulting true iff we are processing this attribute from
        *                     the <code>defaultAttributes(...)</code> method
        *
  +     * The namespace processing code is derived from the SAX2 ParserAdapter
  +     * code.  This code should be kept in sync with ParserAdapter bug
  +     * fixes.
  +     *
        * Note: Modifies <code>seenNSDecl</code> iff a xmlns attribute, ie a
        * namespace decl, was found.  Modifies <code>attTmp</code> and
        * <code>nsAttTmp</code>.
        */
  -    private void processAttributeNS(String attQName, String type,
  -                                    String value, String defaultValue,
  -                                    boolean isSpecified, boolean isDefaulting)
  +    private Vector processAttributeNS(String attQName, String type,
  +                                      String value, String defaultValue,
  +                                      boolean isSpecified, boolean isDefaulting,
  +                                      Vector exceptions)
           throws SAXException
       {
           // assert(namespaces == true)
   
  +      nonNamespace:
           if (attQName.startsWith("xmlns")) {
               // Could be a namespace declaration
   
               boolean defaultNSDecl = attQName.length() == 5;
               if (!defaultNSDecl && attQName.charAt(5) != ':') {
  -                // This isn't a namespace declaration.
  -                String attName[] = processName(attQName, true);
  -                attTmp.addAttribute(attName[0], attName[1], attName[2], type,
  -                                    value, defaultValue, isSpecified);
  -                return;
  +                // Not a namespace declaration
  +                break nonNamespace;
               }
   
               // Must be some kind of namespace declaration
  @@ -1562,12 +1583,23 @@
                   nsAttTmp.addElement(attQName);
               }
               seenNSDecl = true;
  -        } else {
  -            // This isn't a namespace declaration.
  -            String attName[] = processName(attQName, true);
  +            return exceptions;
  +        }
  +
  +        // This isn't a namespace declaration.
  +        try {
  +            String attName[] = processName(attQName, true, true);
               attTmp.addAttribute(attName[0], attName[1], attName[2], type,
                                   value, defaultValue, isSpecified);
  +        } catch (SAXException e) {
  +            if (exceptions == null) {
  +                exceptions = new Vector();
  +            }
  +            exceptions.add(e);
  +            attTmp.addAttribute("", attQName, attQName, type, value,
  +                                defaultValue, isSpecified);
           }
  +        return exceptions;
       }
   
       /**
  @@ -1583,7 +1615,8 @@
        * @exception org.xml.sax.SAXException The client may throw
        *            an exception if there is an error callback.
        */
  -    private String[] processName(String qName, boolean isAttribute)
  +    private String[] processName(String qName, boolean isAttribute,
  +                                 boolean useException)
           throws SAXException
       {
           // assert(namespaces == true)
  @@ -1591,11 +1624,20 @@
                                                  isAttribute);
           if (parts == null) {
               parts = new String[3];
  +            // SAX should use "" instead of null for parts 0 and 1 ???
               parts[0] = "";
               String localName = XmlNames.getLocalPart(qName);
               parts[1] = localName != null ? localName.intern() : "";
               parts[2] = qName.intern();
  -            error("P-084", new Object[] { qName });
  +
  +            String messageId = "P-084";
  +            Object[] parameters = new Object[] { qName };
  +	    if (useException) {
  +                throw new SAXParseException(
  +                    messages.getMessage(locale, messageId, parameters),
  +                    locator);
  +            }
  +            error(messageId, parameters);
           }
           return parts;
       }
  @@ -1688,7 +1730,7 @@
   
                   if (namespaces) {
                       processAttributeNS(declAttName, info.type, defaultValue,
  -                                       defaultValue, false, true);
  +                                       defaultValue, false, true, null);
                   } else {
                       attTmp.addAttribute("", "", declAttName, info.type,
                                           defaultValue, defaultValue, false);
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: crimson-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: crimson-cvs-help@xml.apache.org