📄 streamserializer.java
字号:
// TODO warn if not outputting the declaration would be a // problem } else if (mode == Stylesheet.OUTPUT_HTML) { // Ensure that encoding is accessible String mediaType = (String) node.getUserData("media-type"); if (mediaType == null) { mediaType = "text/html"; } String contentType = mediaType + "; charset=" + ((encoding.indexOf(' ') != -1) ? "\"" + encoding + "\"" : encoding); Document doc = (node instanceof Document) ? (Document) node : node.getOwnerDocument(); Node html = null; for (Node ctx = node.getFirstChild(); ctx != null; ctx = ctx.getNextSibling()) { if (ctx.getNodeType() == Node.ELEMENT_NODE) { html = ctx; 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) { String name = ctx.getLocalName(); if (name == null) { name = ctx.getNodeName(); } if ("head".equalsIgnoreCase(name)) { 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) { 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++) { 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); out.write(encodeText("DOCTYPE ")); 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); } } 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 IOException { encoder.reset(); if (!encoder.canEncode(text)) { // Check each character StringBuffer buf = new StringBuffer(); int len = text.length(); for (int i = 0; i < len; i++) { char c = text.charAt(i); if (encoder.canEncode(c)) { buf.append(c); } else { // Replace with character entity reference String hex = Integer.toHexString((int) c); buf.append("&#x"); buf.append(hex); buf.append(';'); } } 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; } 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("<"); } else if (c == '>') { if (buf == null) { buf = new StringBuffer(text.substring(0, i)); } buf.append(">"); } 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("&"); } } else if (c == '\'' && inAttr) { if (buf == null) { buf = new StringBuffer(text.substring(0, i)); } if (mode == Stylesheet.OUTPUT_HTML) // HTML does not define ', use character entity ref buf.append("'"); else buf.append("'"); } else if (c == '"' && inAttr) { if (buf == null) { buf = new StringBuffer(text.substring(0, i)); } buf.append("""); } 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()); } } 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)); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -