📄 xmldocumentutils.java
字号:
/* * Copyright 2002 Sun Microsystems, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * Neither the name of Sun Microsystems, Inc. or the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * This software is provided "AS IS," without a warranty of any * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that Software is not designed, licensed or intended * for use in the design, construction, operation or maintenance of * any nuclear facility. */package com.sun.j2ee.blueprints.xmldocuments;import java.io.*;import java.net.URL;import java.util.Properties;import java.util.Locale;import javax.xml.parsers.*;import org.w3c.dom.*;import org.xml.sax.*;import org.xml.sax.helpers.*;import javax.xml.transform.*;import javax.xml.transform.dom.*;import javax.xml.transform.sax.*;import javax.xml.transform.stream.*;public final class XMLDocumentUtils { public static final String DEFAULT_ENCODING = "UTF-8"; public static final String SCHEMAS_DIRECTORY_PATH = "/com/sun/j2ee/blueprints/xmldocuments/rsrc/schemas/"; public static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; public static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource"; public static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema"; public static final String W3C_XML_SCHEMA_LOCATION_QNAME = "xsi:schemaLocation"; public static final String W3C_XML_SCHEMA_INSTANCE_NS = "http://www.w3.org/2001/XMLSchema-instance"; public static final String SAX_NS_PREFIXES = "http://xml.org/sax/features/namespace-prefixes"; private XMLDocumentUtils() {} public static String getAttribute(Element element, String name, boolean optional) throws XMLDocumentException { String value = element.getAttribute(name); if (value == null && !optional) { throw new XMLDocumentException("Attribute " + name + " of " + element.getTagName() + " expected."); } return value; } public static String getAttributeAsString(Element element, String name, boolean optional) throws XMLDocumentException { return getAttribute(element, name, optional); } public static int getAttributeAsInt(Element element, String name, boolean optional) throws XMLDocumentException { try { return Integer.parseInt(getAttribute(element, name, optional)); } catch (NumberFormatException exception) { throw new XMLDocumentException(element.getTagName() + "/@" + name + " attribute: value format error.", exception); } } public static Element getFirstChild(Element element, String name, boolean optional) throws XMLDocumentException { for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) { if (child.getNodeType() == Node.ELEMENT_NODE) { if (((Element) child).getTagName().equals(name)) { return (Element) child; } break; } } if (!optional) { throw new XMLDocumentException(name + " element expected as first child of " + element.getTagName() + "."); } return null; } public static Element getChild(Element element, String name, boolean optional) throws XMLDocumentException { for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) { if (child.getNodeType() == Node.ELEMENT_NODE) { if (((Element) child).getTagName().equals(name)) { return (Element) child; } } } if (!optional) { throw new XMLDocumentException(name + " element expected as child of " + element.getTagName() + "."); } return null; } public static Element getSibling(Element element, boolean optional) throws XMLDocumentException { return getSibling(element, element.getTagName(), optional); } public static Element getSibling(Element element, String name, boolean optional) throws XMLDocumentException { for (Node sibling = element.getNextSibling(); sibling != null; sibling = sibling.getNextSibling()) { if (sibling.getNodeType() == Node.ELEMENT_NODE) { if (((Element) sibling).getTagName().equals(name)) { return (Element) sibling; } } } if (!optional) { throw new XMLDocumentException(name + " element expected after " + element.getTagName() + "."); } return null; } public Element getNextSibling(Element element, boolean optional) throws XMLDocumentException { return getNextSibling(element, element.getTagName(), optional); } public static Element getNextSibling(Element element, String name, boolean optional) throws XMLDocumentException { for (Node sibling = element.getNextSibling(); sibling != null; sibling = sibling.getNextSibling()) { if (sibling.getNodeType() == Node.ELEMENT_NODE) { if (((Element) sibling).getTagName().equals(name)) { return (Element) sibling; } break; } } if (!optional) { throw new XMLDocumentException(name + " element expected after " + element.getTagName() + "."); } return null; } public static String getContent(Element element, boolean optional) throws XMLDocumentException { StringBuffer buffer = new StringBuffer(); for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) { if (child.getNodeType() == Node.TEXT_NODE || child.getNodeType() == Node.CDATA_SECTION_NODE) { try { buffer.append(((Text) child).getData()); } catch (DOMException e) {} } } if (!optional && buffer.length() == 0) { throw new XMLDocumentException(element.getTagName() + " element: content expected."); } return buffer.toString(); } public static String getContentAsString(Element element, boolean optional) throws XMLDocumentException { return getContent(element, optional); } public static int getContentAsInt(Element element, boolean optional) throws XMLDocumentException { try { return Integer.parseInt(getContent(element, optional)); } catch (NumberFormatException exception) { throw new XMLDocumentException(element.getTagName() + " element: content format error.", exception); } } public static float getContentAsFloat(Element element, boolean optional) throws XMLDocumentException { try { return Float.parseFloat(getContent(element, optional)); } catch (NumberFormatException exception) { throw new XMLDocumentException(element.getTagName() + " element: content format error.", exception); } } public static String getAttributeNS(Element element, String nsURI, String name, boolean optional) throws XMLDocumentException { String value = element.getAttributeNS(nsURI, name); if (value == null && !optional) { throw new XMLDocumentException("Attribute " + name + " of " + element.getTagName() + " expected."); } return value; } public static String getAttributeAsStringNS(Element element, String nsURI, String name, boolean optional) throws XMLDocumentException { return getAttributeNS(element, nsURI, name, optional); } public static int getAttributeAsIntNS(Element element, String nsURI, String name, boolean optional) throws XMLDocumentException { try { return Integer.parseInt(getAttributeNS(element, nsURI, name, optional)); } catch (NumberFormatException exception) { throw new XMLDocumentException(element.getTagName() + "/@" + name + " attribute: value format error.", exception); } } public static Element getFirstChildNS(Element element, String nsURI, String name, boolean optional) throws XMLDocumentException { for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) { if (child.getNodeType() == Node.ELEMENT_NODE) { if (((Element) child).getLocalName().equals(name) && ((Element) child).getNamespaceURI().equals(nsURI)) { return (Element) child; } break; } } if (!optional) { throw new XMLDocumentException(name + " element expected as first child of " + element.getTagName() + "."); } return null; } public static Element getChildNS(Element element, String nsURI, String name, boolean optional) throws XMLDocumentException { for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) { if (child.getNodeType() == Node.ELEMENT_NODE) { if (((Element) child).getLocalName().equals(name) && ((Element) child).getNamespaceURI().equals(nsURI)) { return (Element) child; } } } if (!optional) { throw new XMLDocumentException(name + " element expected as child of " + element.getTagName() + "."); } return null; } public static Element getSiblingNS(Element element, boolean optional) throws XMLDocumentException { return getSiblingNS(element, element.getNamespaceURI(), element.getLocalName(), optional); } public static Element getSiblingNS(Element element, String nsURI, String name, boolean optional) throws XMLDocumentException { for (Node sibling = element.getNextSibling(); sibling != null; sibling = sibling.getNextSibling()) { if (sibling.getNodeType() == Node.ELEMENT_NODE) { if (((Element) sibling).getLocalName().equals(name) && ((Element) sibling).getNamespaceURI().equals(nsURI)) { return (Element) sibling; } } } if (!optional) { throw new XMLDocumentException(name + " element expected after " + element.getTagName() + "."); } return null; } public Element getNextSiblingNS(Element element, boolean optional) throws XMLDocumentException { return getNextSiblingNS(element, element.getNamespaceURI(), element.getLocalName(), optional); } public static Element getNextSiblingNS(Element element, String nsURI, String name, boolean optional) throws XMLDocumentException { for (Node sibling = element.getNextSibling(); sibling != null; sibling = sibling.getNextSibling()) { if (sibling.getNodeType() == Node.ELEMENT_NODE) { if (((Element) sibling).getLocalName().equals(name) && ((Element) sibling).getNamespaceURI().equals(nsURI)) { return (Element) sibling; } break; } } if (!optional) { throw new XMLDocumentException(name + " element expected after " + element.getTagName() + "."); } return null; } public static Element createElement(Document document, String name, String value) { if (value != null) { Element element = (Element) document.createElement(name); element.appendChild(document.createTextNode(value)); return element; } throw new IllegalArgumentException("XMLDocumentUtils.createElement: value of " + name + " element can't be null."); } public static Element createElement(Document document, String name, long value) { return createElement(document, name, Long.toString(value)); } public static Element createElement(Document document, String name, float value) { return createElement(document, name, Float.toString(value)); } public static Element createElement(Document document, String name, Element child) { Element element = (Element) document.createElement(name); element.appendChild(child); return element; } public static void appendChild(Document document, Node root, String name, String value) { Node node = document.createElement(name); node.appendChild(document.createTextNode(value != null ? value : "")); root.appendChild(node); return; } public static void appendChild(Document document, Node root, String name, long value) { appendChild(document, root, name, Long.toString(value)); return; } public static void appendChild(Document document, Node root, String name, float value) { appendChild(document, root, name, Float.toString(value)); return; } public static void appendChild(Node root, String name, String value) { appendChild(root.getOwnerDocument(), root, name, value); return; } public static Element createElement(Document document, String nsURI, String name, String value) { if (value != null) { Element element = (Element) document.createElementNS(nsURI, name); element.appendChild(document.createTextNode(value != null ? value : "")); return element; } throw new IllegalArgumentException("XMLDocumentUtils.createElement: value of " + name + " element can't be null."); } public static Element createElement(Document document, String nsURI, String name, long value) { return createElement(document, nsURI, name, Long.toString(value)); } public static Element createElement(Document document, String nsURI, String name, float value) { return createElement(document, nsURI, name, Float.toString(value)); } public static Element createElement(Document document, String nsURI, String name, Element child) { Element element = (Element) document.createElement(name); element.appendChild(child); return element; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -