xslwriter.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,511 行 · 第 1/3 页
JAVA
1,511 行
String prefix = name.substring(0, p); String local = name.substring(p + 1); startElement(url, prefix, local, name); } /** * Adds a namespace-aware element to the current node, making the * new element the current node. * * <p>Each pushElement should be matched by a popElement. * * @param prefix the prefix of the element name, e.g. xsl * @param local the local part of the element name, e.g. template * @param url the namespace url, e.g. http://www.xml.org/... */ public void pushElement(String url, String prefix, String local, String name) throws IOException, SAXException { popText(); /* if (url != null && url.startsWith("quote:")) url = url.substring(6); */ startElement(url, prefix, local, name); } /** * Adds a new attribute with the given name to the current node, making * the attribute the current node. */ public XMLWriter pushAttribute(String name) throws IOException, SAXException { popText(); XMLWriter oldWriter = _xmlWriter; _xmlWriter = ATTR_WRITER; _attributeURL = null; _attributePrefix = null; _attributeLocalName = null; _attributeName = name; return oldWriter; } /** * Adds a new attribute with the given name to the current node, making * the attribute the current node. */ public XMLWriter pushAttribute(String name, NamespaceContext namespace) throws IOException, SAXException { popText(); XMLWriter oldWriter = _xmlWriter; _xmlWriter = ATTR_WRITER; // Look up the proper namespace for the element. int p = name.indexOf(':'); String prefix = null; if (p > 0) prefix = name.substring(0, p); String url = namespace.find(namespace, prefix); Attr attr; if (url != null) { _attributeURL = url; _attributePrefix = prefix; _attributeLocalName = name.substring(p + 1); _attributeName = name; } else { _attributeURL = null; _attributePrefix = null; _attributeLocalName = null; _attributeName = name; } return oldWriter; } /** * Adds a new attribute to the current node, making the new attribute the * current node. * * <p>Each pushAttributeNs should be matched by a popAttribute. * * @param name name of the element * @param url namespace url */ public XMLWriter pushAttributeNs(String name, String url) throws IOException, SAXException { popText(); XMLWriter oldWriter = _xmlWriter; _xmlWriter = ATTR_WRITER; Attr attr; // Look up the proper namespace for the element. int p = name.indexOf(':'); String prefix = null; String local = name; if (p > 0) { prefix = name.substring(0, p); local = name.substring(p + 1); } _attributeURL = url; _attributePrefix = prefix; _attributeLocalName = local; _attributeName = name; return oldWriter; } /** * Adds a namespace-aware attribute to the current node, making the * new attribute the current node. * * <p>Each pushAttribute should be matched by a popAttribute. * * @param prefix the prefix of the element name, e.g. xsl * @param local the local part of the element name, e.g. template * @param url the namespace url, e.g. http://www.xml.org/... */ public XMLWriter pushAttribute(String prefix, String local, String url) throws IOException, SAXException { popText(); XMLWriter oldWriter = _xmlWriter; _xmlWriter = ATTR_WRITER; /* if (url != null && url.startsWith("quote:")) url = url.substring(6); */ _attributeURL = url; _attributePrefix = prefix; _attributeLocalName = local; if (prefix != null && ! prefix.equals("")) _attributeName = prefix + ":" + local; else _attributeName = local; return oldWriter; } /** * Adds a namespace-aware attribute to the current node, making the * new attribute the current node. * * <p>Each pushAttribute should be matched by a popAttribute. * * @param prefix the prefix of the element name, e.g. xsl * @param local the local part of the element name, e.g. template * @param url the namespace url, e.g. http://www.xml.org/... */ public void setAttribute(String prefix, String local, String url, String value) throws IOException, SAXException { popText(); /* if (url != null && url.startsWith("quote:")) url = url.substring(6); */ String attributeName; if (prefix != null && ! prefix.equals("")) attributeName = prefix + ":" + local; else attributeName = local; attribute(url, prefix, local, attributeName, value); } /** * Adds a new attribute with the given name to the current node, making * the attribute the current node. */ public void setAttribute(String name, NamespaceContext namespace, String value) throws IOException, SAXException { popText(); // Look up the proper namespace for the element. int p = name.indexOf(':'); String prefix = null; if (p > 0) prefix = name.substring(0, p); String url = namespace.find(namespace, prefix); Attr attr; if (url != null) { attribute(url, prefix, name.substring(p + 1), name, value); } else { attribute(null, null, null, name, value); } } /** * Sets the attribute value to the current text, and sets the current node * to the parent. */ public void popAttribute(XMLWriter writer) throws IOException, SAXException { _xmlWriter = writer; attribute(_attributeURL, _attributePrefix, _attributeLocalName, _attributeName, _text.toString()); _text.clear(); _attributeName = null; } /** * Directly sets an attribute with a value. */ public void setAttribute(String name, String value) throws IOException, SAXException { attribute(null, null, name, name, value); } /** * Copies the node without attributes or children. */ public void pushCopy(Node copyNode) throws IOException, SAXException { popText(); switch (copyNode.getNodeType()) { case Node.ATTRIBUTE_NODE: Node oldNode = copyNode; attribute(oldNode.getNamespaceURI(), oldNode.getPrefix(), oldNode.getLocalName(), oldNode.getNodeName(), oldNode.getNodeValue()); break; case Node.DOCUMENT_NODE: return; case Node.ELEMENT_NODE: Element oldElt = (Element) copyNode; /* String oldSystemId = _systemId; String oldFilename = _filename; int oldLine = _line; _systemId = oldElt.getBaseURI(); _filename = oldElt.getFilename(); _line = oldElt.getLine(); if (generateLocation) _xmlWriter.setLocation(.getFilename(), oldElt.getLine(), 0); */ startElement(oldElt.getNamespaceURI(), oldElt.getPrefix(), oldElt.getLocalName(), oldElt.getNodeName()); /* _systemId = oldSystemId; _filename = oldFilename; _line = oldLine; */ break; case Node.COMMENT_NODE: _xmlWriter.comment(((Comment) copyNode).getData()); break; case Node.TEXT_NODE: /* if (generateLocation) _xmlWriter.setLocation(((QAbstractNode) copyNode).getFilename(), ((QAbstractNode) copyNode).getLine(), 0); */ _text.append(((Text) copyNode).getData()); break; case Node.PROCESSING_INSTRUCTION_NODE: ProcessingInstruction oldPi = (ProcessingInstruction) copyNode; _xmlWriter.processingInstruction(oldPi.getNodeName(), oldPi.getNodeValue()); break; } } /** * Pops the copy. */ public void popCopy(Node copyNode) throws IOException, SAXException { if (copyNode.getNodeType() == Node.ELEMENT_NODE) { popText(); popElement(); } } public void pushPi() throws IOException, SAXException { popText(); } /** * Sets the PI data to the current text, and sets the current node * to the parent. */ public void popPi(String name) throws IOException, SAXException { _xmlWriter.processingInstruction(name, _text.toString()); _text.clear(); } /** * Adds an empty comment to the current node, making * the attribute the current node. */ public void pushComment() throws IOException, SAXException { popText(); } /** * Sets the comment data to the current text, and sets the current * to the the parent. */ public void popComment() throws IOException, SAXException { _xmlWriter.comment(_text.toString()); _text.clear(); } /** * Starts a fragment. The fragment becomes the current node. */ public XMLWriter pushFragment() throws IOException, SAXException { popText(); DOMBuilder domBuilder = new DOMBuilder(); if (_document == null) _document = Xml.createDocument(); domBuilder.init(_document.createDocumentFragment()); domBuilder.setDocumentLocator(_locator); XMLWriter oldWriter = _xmlWriter; _xmlWriter = domBuilder; return oldWriter; } /** * Returns the generated fragment. The current node does not contain * the new fragment. * * @return the generated fragment. */ public Node popFragment(XMLWriter oldWriter) throws IOException, SAXException { popText(); DOMBuilder domBuilder = (DOMBuilder) _xmlWriter; _xmlWriter = oldWriter; domBuilder.endDocument(); Node node = domBuilder.getNode(); return node; } /** * Adds a the contents of the node to the current node. * * @param node node to print */ public void valueOf(Object node) throws IOException, SAXException { if (node == null) return; else if (node instanceof Element || node instanceof DocumentFragment) { Node elt = (Node) node; for (Node child = elt.getFirstChild(); child != null; child = child.getNextSibling()) { elementValueOf(child); } } else if (node instanceof Text) { String data = ((Text) node).getNodeValue(); for (int i = 0; i < data.length(); i++) { if (! XmlChar.isWhitespace(data.charAt(i))) { print(data); return; } } /* if (! _stylesheet.stripSpaces(((Node) node).getParentNode())) */ print(data); } else if (node instanceof Node) { print(((QAbstractNode) node).getNodeValue()); } else if (node instanceof NodeList) { NodeList list = (NodeList) node; Node value = list.item(0); if (value != null) valueOf(value); } else if (node instanceof ArrayList) { ArrayList list = (ArrayList) node; if (list.size() > 0) valueOf(list.get(0)); } else if (node instanceof Iterator) { Iterator list = (Iterator) node; valueOf(list.next()); } else if (node instanceof Double) { Double d = (Double) node; double dValue = d.doubleValue(); if ((int) dValue == dValue) print((int) dValue); else print(dValue); } else print(node); } /** * Adds a the contents of the node to the current node. * * @param node node to print */ private void elementValueOf(Node node) throws IOException, SAXException { if (node == null) return; else if (node instanceof Element) { Element elt = (Element) node; for (Node child = elt.getFirstChild(); child != null; child = child.getNextSibling()) { elementValueOf(child); } } else if (node instanceof Text) { String data = ((Text) node).getNodeValue(); for (int i = 0; i < data.length(); i++) { if (! XmlChar.isWhitespace(data.charAt(i))) { print(data); return; } } /* if (! _stylesheet.stripSpaces(node.getParentNode())) */ print(data); } } /** * Adds a deep copy of the node to the current node. * * @param XPath node to be copied to the destination. */ public void copyOf(Object value) throws IOException, SAXException, XPathException {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?