📄 transformerimpl.java
字号:
{ if (errorListener != null) errorListener.error(new TransformerException(e)); else e.printStackTrace(System.err); } } } /** * Strip whitespace from the source tree. */ static boolean strip(Stylesheet stylesheet, Node node) throws TransformerConfigurationException { short nt = node.getNodeType(); if (nt == Node.ENTITY_REFERENCE_NODE) { // Replace entity reference with its content Node parent = node.getParentNode(); Node nextSibling = node.getNextSibling(); Node child = node.getFirstChild(); while (child != null) { Node next = child.getNextSibling(); node.removeChild(child); if (nextSibling != null) parent.insertBefore(child, nextSibling); else parent.appendChild(child); child = next; } return true; } if (nt == Node.TEXT_NODE || nt == Node.CDATA_SECTION_NODE) { // Denormalize text into whitespace and non-whitespace nodes String text = node.getNodeValue(); String[] tokens = tokenizeWhitespace(text); if (tokens.length > 1) { node.setNodeValue(tokens[0]); Node parent = node.getParentNode(); Node nextSibling = node.getNextSibling(); Document doc = node.getOwnerDocument(); for (int i = 1; i < tokens.length; i++) { Node newChild = (nt == Node.CDATA_SECTION_NODE) ? doc.createCDATASection(tokens[i]) : doc.createTextNode(tokens[i]); if (nextSibling != null) parent.insertBefore(newChild, nextSibling); else parent.appendChild(newChild); } } return !stylesheet.isPreserved((Text) node, true); } else { Node child = node.getFirstChild(); while (child != null) { boolean remove = strip(stylesheet, child); Node next = child.getNextSibling(); if (remove) node.removeChild(child); child = next; } } return false; } /** * Tokenize the specified text into contiguous whitespace-only and * non-whitespace chunks. */ private static String[] tokenizeWhitespace(String text) { int len = text.length(); int start = 0, end = len - 1; // Find index of text start for (int i = 0; i < len; i++) { char c = text.charAt(i); boolean whitespace = (c == ' ' || c == '\n' || c == '\t' || c == '\r'); if (whitespace) start++; else break; } if (start == end) // all whitespace return new String[] { text }; // Find index of text end for (int i = end; i > start; i--) { char c = text.charAt(i); boolean whitespace = (c == ' ' || c == '\n' || c == '\t' || c == '\r'); if (whitespace) end--; else break; } if (start == 0 && end == len - 1) // all non-whitespace return new String[] { text }; // whitespace, then text, then whitespace String[] ret = (start > 0 && end < len - 1) ? new String[3] : new String[2]; int i = 0; if (start > 0) ret[i++] = text.substring(0, start); ret[i++] = text.substring(start, end + 1); if (end < len - 1) ret[i++] = text.substring(end + 1); return ret; } /** * Obtain a suitable output stream for writing the result to, * and use the StreamSerializer to write the result tree to the stream. */ void writeStreamResult(Node node, StreamResult sr, int outputMethod, String encoding) throws IOException { OutputStream out = null; boolean created = false; try { out = sr.getOutputStream(); if (out == null) { Writer writer = sr.getWriter(); if (writer != null) out = new WriterOutputStream(writer); } if (out == null) { String systemId = sr.getSystemId(); try { URL url = new URL(systemId); URLConnection connection = url.openConnection(); // We need to call setDoInput(false), because our // implementation of the file protocol allows writing // (unlike Sun), but it will fail with a FileNotFoundException // if we also open the connection for input and the output // file doesn't yet exist. connection.setDoInput(false); connection.setDoOutput(true); out = connection.getOutputStream(); } catch (MalformedURLException e) { out = new FileOutputStream(systemId); } catch (UnknownServiceException e) { URL url = new URL(systemId); out = new FileOutputStream(url.getPath()); } created = true; } out = new BufferedOutputStream(out); StreamSerializer serializer = new StreamSerializer(outputMethod, encoding, null); if (stylesheet != null) { Collection celem = stylesheet.outputCdataSectionElements; serializer.setCdataSectionElements(celem); } serializer.serialize(node, out); out.flush(); } finally { try { if (out != null && created) out.close(); } catch (IOException e) { } } } void copyChildren(Document dstDoc, Node src, Node dst) { Node srcChild = src.getFirstChild(); while (srcChild != null) { Node dstChild = dstDoc.adoptNode(srcChild); dst.appendChild(dstChild); srcChild = srcChild.getNextSibling(); } } public void setParameter(String name, Object value) { if (stylesheet != null) stylesheet.bindings.set(new QName(null, name), value, Bindings.PARAM); } public Object getParameter(String name) { if (stylesheet != null) return stylesheet.bindings.get(new QName(null, name), null, 1, 1); return null; } public void clearParameters() { if (stylesheet != null) { stylesheet.bindings.pop(Bindings.PARAM); stylesheet.bindings.push(Bindings.PARAM); } } public void setURIResolver(URIResolver resolver) { uriResolver = resolver; } public URIResolver getURIResolver() { return uriResolver; } public void setOutputProperties(Properties oformat) throws IllegalArgumentException { if (oformat == null) outputProperties.clear(); else outputProperties.putAll(oformat); } public Properties getOutputProperties() { return (Properties) outputProperties.clone(); } public void setOutputProperty(String name, String value) throws IllegalArgumentException { outputProperties.put(name, value); } public String getOutputProperty(String name) throws IllegalArgumentException { return outputProperties.getProperty(name); } public void setErrorListener(ErrorListener listener) { errorListener = listener; } public ErrorListener getErrorListener() { return errorListener; } static final String INDENT_WHITESPACE = " "; /* * Apply indent formatting to the given tree. */ void reindent(Document doc, Node node, int offset) { if (node.hasChildNodes()) { boolean markupContent = false; boolean textContent = false; List children = new LinkedList(); Node ctx = node.getFirstChild(); while (ctx != null) { switch (ctx.getNodeType()) { case Node.ELEMENT_NODE: case Node.PROCESSING_INSTRUCTION_NODE: case Node.DOCUMENT_TYPE_NODE: markupContent = true; break; case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: case Node.ENTITY_REFERENCE_NODE: case Node.COMMENT_NODE: textContent = true; break; } children.add(ctx); ctx = ctx.getNextSibling(); } if (markupContent) { if (textContent) { // XXX handle mixed content differently? } int nodeType = node.getNodeType(); if (nodeType == Node.DOCUMENT_NODE) { for (Iterator i = children.iterator(); i.hasNext(); ) { ctx = (Node) i.next(); reindent(doc, ctx, offset); } } else { StringBuffer buf = new StringBuffer(); buf.append('\n'); for (int i = 0; i < offset + 1; i++) buf.append(INDENT_WHITESPACE); String ws = buf.toString(); for (Iterator i = children.iterator(); i.hasNext(); ) { ctx = (Node) i.next(); node.insertBefore(doc.createTextNode(ws), ctx); reindent(doc, ctx, offset + 1); } buf = new StringBuffer(); buf.append('\n'); for (int i = 0; i < offset; i++) buf.append(INDENT_WHITESPACE); ws = buf.toString(); node.appendChild(doc.createTextNode(ws)); } } } } /** * Converts the text node children of any cdata-section-elements in the * tree to CDATA section nodes. */ void convertCdataSectionElements(Document doc, Node node, List list) { if (node.getNodeType() == Node.ELEMENT_NODE) { boolean match = false; for (Iterator i = list.iterator(); i.hasNext(); ) { QName qname = (QName) i.next(); if (match(qname, node)) { match = true; break; } } if (match) { Node ctx = node.getFirstChild(); while (ctx != null) { if (ctx.getNodeType() == Node.TEXT_NODE) { Node cdata = doc.createCDATASection(ctx.getNodeValue()); node.replaceChild(cdata, ctx); ctx = cdata; } ctx = ctx.getNextSibling(); } } } Node ctx = node.getFirstChild(); while (ctx != null) { if (ctx.hasChildNodes()) convertCdataSectionElements(doc, ctx, list); ctx = ctx.getNextSibling(); } } boolean match(QName qname, Node node) { String ln1 = qname.getLocalPart(); String ln2 = node.getLocalName(); if (ln2 == null) return ln1.equals(node.getNodeName()); else { String uri1 = qname.getNamespaceURI(); String uri2 = node.getNamespaceURI(); return (uri1.equals(uri2) && ln1.equals(ln2)); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -