Index: DOMBuilder.java
===================================================================
RCS file: /home/cvspublic/xml-xalan/java/src/org/apache/xml/utils/DOMBuilder.java,v
retrieving revision 1.10
diff -u -r1.10 DOMBuilder.java
--- DOMBuilder.java	3 Jan 2002 20:12:19 -0000	1.10
+++ DOMBuilder.java	4 Jan 2002 07:39:01 -0000
@@ -2,7 +2,7 @@
  * The Apache Software License, Version 1.1
  *
  *
- * Copyright (c) 1999 The Apache Software Foundation.  All rights 
+ * Copyright (c) 1999 The Apache Software Foundation.  All rights
  * reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -10,7 +10,7 @@
  * are met:
  *
  * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer. 
+ *    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
@@ -18,7 +18,7 @@
  *    distribution.
  *
  * 3. The end-user documentation included with the redistribution,
- *    if any, must include the following acknowledgment:  
+ *    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,
@@ -26,7 +26,7 @@
  *
  * 4. The names "Xalan" and "Apache Software Foundation" must
  *    not be used to endorse or promote products derived from this
- *    software without prior written permission. For written 
+ *    software without prior written permission. For written
  *    permission, please contact apache@apache.org.
  *
  * 5. Products derived from this software may not be called "Apache",
@@ -60,6 +60,7 @@
 import org.apache.xpath.res.XPATHErrorResources;
 import org.apache.xml.utils.NodeVector;
 import java.util.Stack;
+import java.util.Vector;
 
 import org.xml.sax.ext.LexicalHandler;
 import org.xml.sax.ContentHandler;
@@ -90,6 +91,9 @@
   /** Vector of element nodes          */
   protected Stack m_elemStack = new Stack();
 
+  /** The vector of namespaces declarations to include in the next element */
+  private Vector m_undecl = new Vector(5);
+
   /**
    * DOMBuilder instance constructor... it will add the DOM nodes
    * to the document fragment.
@@ -287,7 +291,7 @@
    *
    *
    * @param ns The namespace of the node
-   * @param localName The local part of the qualified name 
+   * @param localName The local part of the qualified name
    * @param name The element name.
    * @param atts The attributes attached to the element, if any.
    * @see #endElement
@@ -300,8 +304,8 @@
 
     Element elem;
 
-	// Note that the namespace-aware call must be used to correctly
-	// construct a Level 2 DOM, even for non-namespaced nodes.
+    // Note that the namespace-aware call must be used to correctly
+    // construct a Level 2 DOM, even for non-namespaced nodes.
     if ((null == ns) || (ns.length() == 0))
       elem = m_doc.createElementNS(null,name);
     else
@@ -312,43 +316,37 @@
     try
     {
       int nAtts = atts.getLength();
-  
+
       if (0 != nAtts)
       {
-        for (int i = 0; i < nAtts; i++)
-        {
-  
-          //System.out.println("type " + atts.getType(i) + " name " + atts.getLocalName(i) );
-          // First handle a possible ID attribute
-          if (atts.getType(i).equalsIgnoreCase("ID"))
-            setIDAttribute(atts.getValue(i), elem);
-  
-          String attrNS = atts.getURI(i);
-          
-          if(attrNS == null)
-            attrNS = ""; // defensive, shouldn't have to do this.
-  
-          // System.out.println("attrNS: "+attrNS+", localName: "+atts.getQName(i)
-          //                   +", qname: "+atts.getQName(i)+", value: "+atts.getValue(i));
-          // Crimson won't let us set an xmlns: attribute on the DOM.
-          String attrQName = atts.getQName(i);
-          if ((attrNS.length() == 0) /* || attrQName.startsWith("xmlns:") || attrQName.equals("xmlns") */)
-            elem.setAttribute(attrQName, atts.getValue(i));
-          else
-          {
-  
-            // elem.setAttributeNS(atts.getURI(i), atts.getLocalName(i), atts.getValue(i));
-            elem.setAttributeNS(attrNS, attrQName, atts.getValue(i));
-          }
+      // Process all attributes, leave out namespace attributes
+      for ( int x = 0; x < atts.getLength(); x++ ) {
+        String auri = atts.getURI( x );
+        String araw = atts.getQName( x );
+        String aval = atts.getValue( x );
+        if ( araw.startsWith( "xmlns:" ) == false && araw.equals( "xmlns" ) == false )
+
+          if (auri != null && auri.length() == 0) auri = null;
+          elem.setAttributeNS(auri, araw, aval);
         }
       }
-      
+      // Append the xmlns... attributes
+      if ( m_undecl.size() > 0 ) {
+        for ( int x = 0; x < m_undecl.size(); x++ ) {
+          String[] value = (String[])m_undecl.get(x);
+          String aname = "xmlns";
+          if ( value[0].length() > 0 ) aname = "xmlns:" + value[0];
+          elem.setAttributeNS( "http://www.w3.org/2000/xmlns/", aname, value[1] );
+        }
+        m_undecl.clear();
+      }
+
       // append(elem);
-  
+
       m_elemStack.push(elem);
-  
+
       m_currentNode = elem;
-      
+
       // append(elem);
     }
     catch(java.lang.Exception de)
@@ -356,7 +354,7 @@
       // de.printStackTrace();
       throw new org.xml.sax.SAXException(de);
     }
-    
+
   }
 
   /**
@@ -444,12 +442,12 @@
    *
    * @param ch Array containing the characters
    * @param start Index to start of characters in the array
-   * @param length Number of characters in the array 
+   * @param length Number of characters in the array
    */
   public void charactersRaw(char ch[], int start, int length)
           throws org.xml.sax.SAXException
   {
-    if(isOutsideDocElem()  
+    if(isOutsideDocElem()
        && org.apache.xml.utils.XMLCharacterRecognizer.isWhiteSpace(ch, start, length))
       return;  // avoid DOM006 Hierarchy request error
 
@@ -532,10 +530,10 @@
 
     append(m_doc.createTextNode(s));
   }
-  
+
   /**
    * Tell if the current node is outside the document element.
-   * 
+   *
    * @return true if the current node is outside the document element.
    */
    private boolean isOutsideDocElem()
@@ -628,7 +626,7 @@
    */
   public void cdata(char ch[], int start, int length) throws org.xml.sax.SAXException
   {
-    if(isOutsideDocElem()  
+    if(isOutsideDocElem()
        && org.apache.xml.utils.XMLCharacterRecognizer.isWhiteSpace(ch, start, length))
       return;  // avoid DOM006 Hierarchy request error
 
@@ -700,29 +698,8 @@
   public void startPrefixMapping(String prefix, String uri)
           throws org.xml.sax.SAXException
   {
-
-    /*
-    // Not sure if this is needed or wanted
-    // Also, it fails in the stree.
-    if((null != m_currentNode)
-       && (m_currentNode.getNodeType() == Node.ELEMENT_NODE))
-    {
-      String qname;
-      if(((null != prefix) && (prefix.length() == 0))
-         || (null == prefix))
-        qname = "xmlns";
-      else
-        qname = "xmlns:"+prefix;
-
-      Element elem = (Element)m_currentNode;
-      String val = elem.getAttribute(qname);
-      if(val == null)
-      {
-        elem.setAttributeNS("http://www.w3.org/XML/1998/namespace",
-                            qname, uri);
-      }
-    }
-    */
+    // Insert this namespace in tables avoiding duplicates
+    m_undecl.addElement( new String[] {prefix, uri } );
   }
 
   /**
@@ -737,7 +714,22 @@
    * @see #startPrefixMapping
    * @see #endElement
    */
-  public void endPrefixMapping(String prefix) throws org.xml.sax.SAXException{}
+  public void endPrefixMapping(String prefix)
+  throws org.xml.sax.SAXException
+  {
+    int i = m_undecl.size();
+    boolean found = false;
+    while (i > 0 && found == false)
+    {
+      i--;
+      String[] value = (String[])m_undecl.get(i);
+      if (value[0].equals(prefix) == true)
+      {
+        m_undecl.removeElementAt(i);
+        found = true;
+      }
+    }
+  }
 
   /**
    * Receive notification of a skipped entity.
