📄 xmlprocessor.java
字号:
/* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is Rhino DOM-only E4X implementation. * * The Initial Developer of the Original Code is * David P. Caldwell. * Portions created by David P. Caldwell are Copyright (C) * 2007 David P. Caldwell. All Rights Reserved. * * * Contributor(s): * David P. Caldwell <inonit@inonit.com> * * Alternatively, the contents of this file may be used under the terms of * the GNU General Public License Version 2 or later (the "GPL"), in which * case the provisions of the GPL are applicable instead of those above. If * you wish to allow use of your version of this file only under the terms of * the GPL and not to allow others to use your version of this file under the * MPL, indicate your decision by deleting the provisions above and replacing * them with the notice and other provisions required by the GPL. If you do * not delete the provisions above, a recipient may use your version of this * file under either the MPL or the GPL. * * ***** END LICENSE BLOCK ***** */package org.mozilla.javascript.xmlimpl;import org.w3c.dom.*;import javax.xml.parsers.DocumentBuilder;import org.mozilla.javascript.*;// Disambiguate from org.mozilla.javascript.Nodeimport org.w3c.dom.Node;import org.xml.sax.ErrorHandler;import org.xml.sax.SAXParseException;class XmlProcessor { private boolean ignoreComments; private boolean ignoreProcessingInstructions; private boolean ignoreWhitespace; private boolean prettyPrint; private int prettyIndent; private javax.xml.parsers.DocumentBuilderFactory dom; private javax.xml.transform.TransformerFactory xform; private DocumentBuilder documentBuilder; private RhinoSAXErrorHandler errorHandler = new RhinoSAXErrorHandler(); private static class RhinoSAXErrorHandler implements ErrorHandler { private void throwError(SAXParseException e) { throw ScriptRuntime.constructError("TypeError", e.getMessage(), e.getLineNumber() - 1); } public void error(SAXParseException e) { throwError(e); } public void fatalError(SAXParseException e) { throwError(e); } public void warning(SAXParseException e) { Context.reportWarning(e.getMessage()); } } XmlProcessor() { setDefault(); this.dom = javax.xml.parsers.DocumentBuilderFactory.newInstance(); this.dom.setNamespaceAware(true); this.dom.setIgnoringComments(false); this.xform = javax.xml.transform.TransformerFactory.newInstance(); } final void setDefault() { this.setIgnoreComments(true); this.setIgnoreProcessingInstructions(true); this.setIgnoreWhitespace(true); this.setPrettyPrinting(true); this.setPrettyIndent(2); } final void setIgnoreComments(boolean b) { this.ignoreComments = b; } final void setIgnoreWhitespace(boolean b) { this.ignoreWhitespace = b; } final void setIgnoreProcessingInstructions(boolean b) { this.ignoreProcessingInstructions = b; } final void setPrettyPrinting(boolean b) { this.prettyPrint = b; } final void setPrettyIndent(int i) { this.prettyIndent = i; } final boolean isIgnoreComments() { return ignoreComments; } final boolean isIgnoreProcessingInstructions() { return ignoreProcessingInstructions; } final boolean isIgnoreWhitespace() { return ignoreWhitespace; } final boolean isPrettyPrinting() { return prettyPrint; } final int getPrettyIndent() { return prettyIndent; } private String toXmlNewlines(String rv) { StringBuffer nl = new StringBuffer(); for (int i=0; i<rv.length(); i++) { if (rv.charAt(i) == '\r') { if (rv.charAt(i+1) == '\n') { // DOS, do nothing and skip the \r } else { // Macintosh, substitute \n nl.append('\n'); } } else { nl.append(rv.charAt(i)); } } return nl.toString(); } private javax.xml.parsers.DocumentBuilderFactory getDomFactory() { return dom; } private synchronized DocumentBuilder getDocumentBuilderFromPool() throws javax.xml.parsers.ParserConfigurationException { DocumentBuilder result; if (documentBuilder == null) { javax.xml.parsers.DocumentBuilderFactory factory = getDomFactory(); result = factory.newDocumentBuilder(); } else { result = documentBuilder; documentBuilder = null; } result.setErrorHandler(errorHandler); return result; } private synchronized void returnDocumentBuilderToPool(DocumentBuilder db) { if (documentBuilder == null) { documentBuilder = db; documentBuilder.reset(); } } private void addProcessingInstructionsTo(java.util.Vector v, Node node) { if (node instanceof ProcessingInstruction) { v.add(node); } if (node.getChildNodes() != null) { for (int i=0; i<node.getChildNodes().getLength(); i++) { addProcessingInstructionsTo(v, node.getChildNodes().item(i)); } } } private void addCommentsTo(java.util.Vector v, Node node) { if (node instanceof Comment) { v.add(node); } if (node.getChildNodes() != null) { for (int i=0; i<node.getChildNodes().getLength(); i++) { addProcessingInstructionsTo(v, node.getChildNodes().item(i)); } } } private void addTextNodesToRemoveAndTrim(java.util.Vector toRemove, Node node) { if (node instanceof Text) { Text text = (Text)node; boolean BUG_369394_IS_VALID = false; if (!BUG_369394_IS_VALID) { text.setData(text.getData().trim()); } else { if (text.getData().trim().length() == 0) { text.setData(""); } } if (text.getData().length() == 0) { toRemove.add(node); } } if (node.getChildNodes() != null) { for (int i=0; i<node.getChildNodes().getLength(); i++) { addTextNodesToRemoveAndTrim(toRemove, node.getChildNodes().item(i)); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -