--- /home/cpdev/src/classpath/gnu/xml/transform/StreamSerializer.java	2005-07-10 05:32:35.000000000 +0000
+++ gnu/xml/transform/StreamSerializer.java	2005-06-30 05:34:21.000000000 +0000
@@ -41,14 +41,9 @@
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
-import java.nio.ByteBuffer;
-import java.nio.CharBuffer;
-import java.nio.charset.Charset;
-import java.nio.charset.CharsetEncoder;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.Map;
 import javax.xml.XMLConstants;
 import org.w3c.dom.Attr;
@@ -73,85 +68,8 @@
   static final int KET = 0x3e; // >
   static final int EQ = 0x3d; // =
 
-  /**
-   * HTML 4.01 boolean attributes
-   */
-  static final Map HTML_BOOLEAN_ATTRIBUTES = new HashMap();
-  static
-  {
-    HashSet set;
-    
-    set = new HashSet();
-    set.add("nohref");
-    HTML_BOOLEAN_ATTRIBUTES.put("area", set);
-
-    set = new HashSet();
-    set.add("ismap");
-    HTML_BOOLEAN_ATTRIBUTES.put("img", set);
-
-    set = new HashSet();
-    set.add("declare");
-    HTML_BOOLEAN_ATTRIBUTES.put("object", set);
-    
-    set = new HashSet();
-    set.add("noshade");
-    HTML_BOOLEAN_ATTRIBUTES.put("hr", set);
-    
-    set = new HashSet();
-    set.add("compact");
-    HTML_BOOLEAN_ATTRIBUTES.put("dl", set);
-    HTML_BOOLEAN_ATTRIBUTES.put("ol", set);
-    HTML_BOOLEAN_ATTRIBUTES.put("ul", set);
-    HTML_BOOLEAN_ATTRIBUTES.put("dir", set);
-    HTML_BOOLEAN_ATTRIBUTES.put("menu", set);
-    
-    set = new HashSet();
-    set.add("checked");
-    set.add("disabled");
-    set.add("readonly");
-    set.add("ismap");
-    HTML_BOOLEAN_ATTRIBUTES.put("input", set);
-    
-    set = new HashSet();
-    set.add("multiple");
-    set.add("disabled");
-    HTML_BOOLEAN_ATTRIBUTES.put("select", set);
-    
-    set = new HashSet();
-    set.add("disabled");
-    HTML_BOOLEAN_ATTRIBUTES.put("optgroup", set);
-    
-    set = new HashSet();
-    set.add("selected");
-    set.add("disabled");
-    HTML_BOOLEAN_ATTRIBUTES.put("option", set);
-    
-    set = new HashSet();
-    set.add("disabled");
-    set.add("readonly");
-    HTML_BOOLEAN_ATTRIBUTES.put("textarea", set);
-    
-    set = new HashSet();
-    set.add("disabled");
-    HTML_BOOLEAN_ATTRIBUTES.put("button", set);
-    
-    set = new HashSet();
-    set.add("nowrap");
-    HTML_BOOLEAN_ATTRIBUTES.put("th", set);
-    HTML_BOOLEAN_ATTRIBUTES.put("td", set);
-    
-    set = new HashSet();
-    set.add("noresize");
-    HTML_BOOLEAN_ATTRIBUTES.put("frame", set);
-    
-    set = new HashSet();
-    set.add("defer");
-    HTML_BOOLEAN_ATTRIBUTES.put("script", set);
-  }
-
-  protected final String encoding;
-  final Charset charset;
-  final CharsetEncoder encoder;
+  protected String encoding;
+  boolean compatibilityMode;
   final int mode;
   final Map namespaces;
   protected String eol;
@@ -178,8 +96,16 @@
         encoding = "UTF-8";
       }
     this.encoding = encoding.intern();
-    charset = Charset.forName(this.encoding);
-    encoder = charset.newEncoder();
+    compatibilityMode = true;
+    if (encoding.length() > 3)
+      {
+        String p = encoding.substring(0, 3);
+        if (p.equalsIgnoreCase("UTF") ||
+            p.equalsIgnoreCase("UCS"))
+          {
+            compatibilityMode = false;
+          }
+      }
     this.eol = (eol != null) ? eol : System.getProperty("line.separator");
     namespaces = new HashMap();
   }
@@ -195,28 +121,17 @@
     serialize(node, out, false);
   }
   
-  void serialize(Node node, final OutputStream out,
+  void serialize(final Node node, final OutputStream out,
                  boolean convertToCdata)
     throws IOException
   {
-    while (node != null)
-      {
-        Node next = node.getNextSibling();
-        doSerialize(node, out, convertToCdata);
-        node = next;
-      }
-  }
-
-  private void doSerialize(final Node node, final OutputStream out,
-                           boolean convertToCdata)
-    throws IOException
-  {
     if (out == null)
       {
         throw new NullPointerException("no output stream");
       }
     String value, prefix;
     Node children;
+    Node next = node.getNextSibling();
     String uri = node.getNamespaceURI();
     boolean defined = false;
     short nt = node.getNodeType();
@@ -237,12 +152,7 @@
               {
                 break;
               }
-            String name = node.getLocalName();
-            if (name == null)
-              {
-                name = node.getNodeName();
-              }
-            define(nsuri, name);
+            define(nsuri, node.getLocalName());
           }
         else if (uri != null && !isDefined(uri))
           {
@@ -260,8 +170,7 @@
         out.write(encodeText(a_nodeName));
         String a_nodeValue = node.getNodeValue();
         if (mode == Stylesheet.OUTPUT_HTML &&
-            a_nodeName.equals(a_nodeValue) &&
-            isHTMLBoolean((Attr) node, a_nodeName))
+            a_nodeName.equals(a_nodeValue))
           {
             break;
           }
@@ -418,18 +327,11 @@
             for (Node ctx = html.getFirstChild(); ctx != null;
                  ctx = ctx.getNextSibling())
               {
-                if (ctx.getNodeType() == Node.ELEMENT_NODE)
+                if (ctx.getNodeType() == Node.ELEMENT_NODE &&
+                    "head".equalsIgnoreCase(ctx.getLocalName()))
                   {
-                    String name = ctx.getLocalName();
-                    if (name == null)
-                      {
-                        name = ctx.getNodeName();
-                      }
-                    if ("head".equalsIgnoreCase(name))
-                      {
-                        head = ctx;
-                        break;
-                      }
+                    head = ctx;
+                    break;
                   }
               }
             if (head == null)
@@ -459,39 +361,32 @@
             for (Node ctx = head.getFirstChild(); ctx != null;
                  ctx = ctx.getNextSibling())
               {
-                if (ctx.getNodeType() == Node.ELEMENT_NODE)
+                if (ctx.getNodeType() == Node.ELEMENT_NODE &&
+                    "meta".equalsIgnoreCase(ctx.getLocalName()))
                   {
-                    String name = ctx.getLocalName();
-                    if (name == null)
-                      {
-                        name = ctx.getNodeName();
-                      }
-                    if ("meta".equalsIgnoreCase(name))
+                    NamedNodeMap metaAttrs = ctx.getAttributes();
+                    int len = metaAttrs.getLength();
+                    String httpEquiv = null;
+                    Node content = null;
+                    for (int i = 0; i < len; i++)
                       {
-                        NamedNodeMap metaAttrs = ctx.getAttributes();
-                        int len = metaAttrs.getLength();
-                        String httpEquiv = null;
-                        Node content = null;
-                        for (int i = 0; i < len; i++)
+                        Node attr = metaAttrs.item(i);
+                        String attrName = attr.getNodeName();
+                        if ("http-equiv".equalsIgnoreCase(attrName))
                           {
-                            Node attr = metaAttrs.item(i);
-                            String attrName = attr.getNodeName();
-                            if ("http-equiv".equalsIgnoreCase(attrName))
-                              {
-                                httpEquiv = attr.getNodeValue();
-                              }
-                            else if ("content".equalsIgnoreCase(attrName))
-                              {
-                                content = attr;
-                              }
+                            httpEquiv = attr.getNodeValue();
                           }
-                        if ("Content-Type".equalsIgnoreCase(httpEquiv))
+                        else if ("content".equalsIgnoreCase(attrName))
                           {
-                            meta = ctx;
-                            metaContent = content;
-                            break;
+                            content = attr;
                           }
                       }
+                    if ("Content-Type".equalsIgnoreCase(httpEquiv))
+                      {
+                        meta = ctx;
+                        metaContent = content;
+                        break;
+                      }
                   }
               }
             if (meta == null)
@@ -529,7 +424,6 @@
         DocumentType doctype = (DocumentType) node;
         out.write(BRA);
         out.write(BANG);
-	out.write(encodeText("DOCTYPE "));
         value = doctype.getNodeName();
         out.write(encodeText(value));
         String publicId = doctype.getPublicId();
@@ -574,6 +468,10 @@
       {
         undefine(uri);
       }
+    if (next != null)
+      {
+        serialize(next, out, convertToCdata);
+      }
   }
 
   boolean isDefined(String uri)
@@ -600,50 +498,37 @@
   }
 
   final byte[] encodeText(String text)
-    throws IOException
+    throws UnsupportedEncodingException
   {
-    encoder.reset();
-    if (!encoder.canEncode(text))
+    if (compatibilityMode)
       {
-        // Check each character
-        StringBuffer buf = new StringBuffer();
         int len = text.length();
+        StringBuffer buf = null;
         for (int i = 0; i < len; i++)
           {
             char c = text.charAt(i);
-            if (encoder.canEncode(c))
+            if (c >= 127)
               {
-                buf.append(c);
+                if (buf == null)
+                  {
+                    buf = new StringBuffer(text.substring(0, i));
+                  }
+                buf.append('&');
+                buf.append('#');
+                buf.append((int) c);
+                buf.append(';');
               }
-            else
+            else if (buf != null)
               {
-                // Replace with character entity reference
-                String hex = Integer.toHexString((int) c);
-                buf.append("&#x");
-                buf.append(hex);
-                buf.append(';');
+                buf.append(c);
               }
           }
-        text = buf.toString();
+        if (buf != null)
+          {
+            text = buf.toString();
+          }
       }
-    ByteBuffer encoded = encoder.encode(CharBuffer.wrap(text));
-    int len = encoded.limit() - encoded.position();
-    if (encoded.hasArray())
-      {
-        byte[] ret = encoded.array();
-        if (ret.length > len)
-          {
-            // Why?
-            byte[] ret2 = new byte[len];
-            System.arraycopy(ret, 0, ret2, 0, len);
-            ret = ret2;
-          }
-        return ret;
-      }
-    encoded.flip();
-    byte[] ret = new byte[len];
-    encoded.get(ret, 0, len);
-    return ret;
+    return text.getBytes(encoding);
   }
 
   String encode(String text, boolean encodeCtl, boolean inAttr)
@@ -743,20 +628,5 @@
         throw new RuntimeException(e.getMessage());
       }
   }
-
-  boolean isHTMLBoolean(Attr attr, String attrName)
-  {
-    attrName = attrName.toLowerCase();
-    Node element = attr.getOwnerElement();
-    String elementName = element.getLocalName();
-    if (elementName == null)
-      {
-        elementName = element.getNodeName();
-      }
-    elementName = elementName.toLowerCase();
-    Collection attributes =
-      (Collection) HTML_BOOLEAN_ATTRIBUTES.get(elementName);
-    return (attributes != null && attributes.contains(attrName));
-  }
-
+  
 }
