streamserializer.java

来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 633 行 · 第 1/2 页

JAVA
633
字号
                    break;                  }              }            if (html == null)              {                html = doc.createElement("html");                node.appendChild(html);              }            Node head = null;            for (Node ctx = html.getFirstChild(); ctx != null;                 ctx = ctx.getNextSibling())              {                if (ctx.getNodeType() == Node.ELEMENT_NODE &&                    "head".equalsIgnoreCase(ctx.getLocalName()))                  {                    head = ctx;                    break;                  }              }            if (head == null)              {                head = doc.createElement("head");                Node c1 = null;                for (Node ctx = html.getFirstChild(); ctx != null;                     ctx = ctx.getNextSibling())                  {                    if (ctx.getNodeType() == Node.ELEMENT_NODE)                      {                        c1 = ctx;                        break;                      }                  }                if (c1 != null)                  {                    html.insertBefore(head, c1);                  }                else                  {                    html.appendChild(head);                  }              }            Node meta = null;            Node metaContent = null;            for (Node ctx = head.getFirstChild(); ctx != null;                 ctx = ctx.getNextSibling())              {                if (ctx.getNodeType() == Node.ELEMENT_NODE &&                    "meta".equalsIgnoreCase(ctx.getLocalName()))                  {                    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))                          {                            httpEquiv = attr.getNodeValue();                          }                        else if ("content".equalsIgnoreCase(attrName))                          {                            content = attr;                          }                      }                    if ("Content-Type".equalsIgnoreCase(httpEquiv))                      {                        meta = ctx;                        metaContent = content;                        break;                      }                  }              }            if (meta == null)              {                meta = doc.createElement("meta");                // Insert first                Node first = head.getFirstChild();                if (first == null)                  {                    head.appendChild(meta);                  }                else                  {                    head.insertBefore(meta, first);                  }                Node metaHttpEquiv = doc.createAttribute("http-equiv");                meta.getAttributes().setNamedItem(metaHttpEquiv);                metaHttpEquiv.setNodeValue("Content-Type");              }            if (metaContent == null)              {                metaContent = doc.createAttribute("content");                meta.getAttributes().setNamedItem(metaContent);              }            metaContent.setNodeValue(contentType);            // phew          }        children = node.getFirstChild();        if (children != null)          {            serialize(children, out, convertToCdata);          }        break;      case Node.DOCUMENT_TYPE_NODE:        DocumentType doctype = (DocumentType) node;        out.write(BRA);        out.write(BANG);        value = doctype.getNodeName();        out.write(encodeText(value));        String publicId = doctype.getPublicId();        if (publicId != null)          {            out.write(encodeText(" PUBLIC "));            out.write(APOS);            out.write(encodeText(publicId));            out.write(APOS);          }        String systemId = doctype.getSystemId();        if (systemId != null)          {            out.write(encodeText(" SYSTEM "));            out.write(APOS);            out.write(encodeText(systemId));            out.write(APOS);          }        String internalSubset = doctype.getInternalSubset();        if (internalSubset != null)          {            out.write(encodeText(internalSubset));          }        out.write(KET);        out.write(eol.getBytes(encoding));        break;      case Node.ENTITY_REFERENCE_NODE:        value = "&" + node.getNodeValue() + ";";        out.write(encodeText(value));        break;      case Node.PROCESSING_INSTRUCTION_NODE:        value = "<?" + node.getNodeName() + " " + node.getNodeValue() + "?>";        out.write(encodeText(value));        Node pp = node.getParentNode();        if (pp != null && pp.getNodeType() == Node.DOCUMENT_NODE)          {            out.write(encodeText(eol));          }        break;      }    if (defined)      {        undefine(uri);      }    if (next != null)      {        serialize(next, out, convertToCdata);      }  }  boolean isDefined(String uri)  {    return XMLConstants.XML_NS_URI.equals(uri) ||      XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(uri) ||      namespaces.containsKey(uri);  }  String define(String uri, String prefix)  {    while (namespaces.containsValue(prefix))      {        // Fabricate new prefix        prefix = prefix + "_";      }    namespaces.put(uri, prefix);    return prefix;  }  void undefine(String uri)  {    namespaces.remove(uri);  }  final byte[] encodeText(String text)    throws UnsupportedEncodingException  {    if (compatibilityMode)      {        int len = text.length();        StringBuffer buf = null;        for (int i = 0; i < len; i++)          {            char c = text.charAt(i);            if (c >= 127)              {                if (buf == null)                  {                    buf = new StringBuffer(text.substring(0, i));                  }                buf.append('&');                buf.append('#');                buf.append((int) c);                buf.append(';');              }            else if (buf != null)              {                buf.append(c);              }          }        if (buf != null)          {            text = buf.toString();          }      }    return text.getBytes(encoding);  }  String encode(String text, boolean encodeCtl, boolean inAttr)  {    int len = text.length();    StringBuffer buf = null;    for (int i = 0; i < len; i++)      {        char c = text.charAt(i);        if (c == '<')          {            if (buf == null)              {                buf = new StringBuffer(text.substring(0, i));              }            buf.append("&lt;");          }        else if (c == '>')          {            if (buf == null)              {                buf = new StringBuffer(text.substring(0, i));              }            buf.append("&gt;");          }        else if (c == '&')          {            if (mode == Stylesheet.OUTPUT_HTML && (i + 1) < len &&                text.charAt(i + 1) == '{')              {                if (buf != null)                  {                    buf.append(c);                  }              }            else              {                if (buf == null)                  {                    buf = new StringBuffer(text.substring(0, i));                  }                buf.append("&amp;");              }          }        else if (c == '\'' && inAttr)          {            if (buf == null)              {                buf = new StringBuffer(text.substring(0, i));              }            buf.append("&apos;");          }        else if (c == '"' && inAttr)          {            if (buf == null)              {                buf = new StringBuffer(text.substring(0, i));              }            buf.append("&quot;");          }        else if (encodeCtl)          {            if (c < 0x20)              {                if (buf == null)                  {                    buf = new StringBuffer(text.substring(0, i));                  }                buf.append('&');                buf.append('#');                buf.append((int) c);                buf.append(';');              }            else if (buf != null)              {                buf.append(c);              }          }        else if (buf != null)          {            buf.append(c);          }      }    return (buf == null) ? text : buf.toString();  }  String toString(Node node)  {    ByteArrayOutputStream out = new ByteArrayOutputStream();    try      {        serialize(node, out);        return new String(out.toByteArray(), encoding);      }    catch (IOException e)      {        throw new RuntimeException(e.getMessage());      }  }  }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?