`

两个工具类

 
阅读更多
package com.retrieve.utils;

import java.io.PrintStream;
import java.io.PrintWriter;

/**
 * @Description An exception for the lucene search.
 *              <p>
 *              It can handle nested exceptions. Nested exceptions will be
 *              printed with the stacktrace.
 *              <p>
 * @Author zhangzuoqiang
 * @Date 2012-3-17 | 下午8:11:04
 */
public class RetrieveException extends Exception {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** The nested exception. May be null. */
	private Throwable mCause;

	/**
	 * Creates a new instance of SearchException.
	 * 
	 * @param message
	 *            The error message
	 */
	public RetrieveException(String message) {
		this(message, null);
	}

	/**
	 * Creates a new instance of SearchException.
	 * 
	 * @param message
	 *            The error message
	 * @param cause
	 *            The nested exception.
	 */
	public RetrieveException(String message, Throwable cause) {
		super(message);
		mCause = cause;
	}

	/**
	 * Gets the cause of this exception. (May be null)
	 * 
	 * @return The cause of this exception.
	 */
	public Throwable getCause() {
		return mCause;
	}

	/**
	 * Prints the stack trace of this exception an of the nested exception, if
	 * present.
	 * 
	 * @param stream
	 *            The stream to print to.
	 */
	public void printStackTrace(PrintStream stream) {
		super.printStackTrace(stream);
		if ((mCause != null) && (!superClassPrintsCause())) {
			stream.println("Caused by: " + mCause.getMessage() + ":");
			mCause.printStackTrace(stream);
		}
	}

	/**
	 * Prints the stack trace of this exception an of the nested exception, if
	 * present.
	 * 
	 * @param writer
	 *            The writer to print to.
	 */
	public void printStackTrace(PrintWriter writer) {
		super.printStackTrace(writer);

		if ((mCause != null) && (!superClassPrintsCause())) {
			writer.println("Caused by: " + mCause.getMessage() + ":");
			mCause.printStackTrace(writer);
		}
	}

	/**
	 * Gets whether the superclass is able to print the cause of the exception.
	 * 
	 * @return Whether the superclass is able to print the cause of the
	 *         exception.
	 */
	private boolean superClassPrintsCause() {
		// Check whether there is a getCause method in the super class
		try {
			getClass().getSuperclass().getMethod("getCause");
			// The superclass has a getCause method
			return true;
		} catch (Exception exc) {
			// The superclass has no getCause method
			return false;
		}
	}

}

 

 

package com.retrieve.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Attr;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * @Description
 * @Author zhangzuoqiang
 * @Date 2012-3-17 | 下午8:29:10
 */
public class XmlToolkit {

	/**
	 * Loads an XML file and returns its content as Document.
	 * 
	 * @param xmlFile
	 *            The XML file to load.
	 * @return The XML document of the file.
	 * @throws RetrieveException
	 *             If loading the XML file failed.
	 */
	public static Document loadXmlDocument(File xmlFile)
			throws RetrieveException {
		DocumentBuilder builder;
		try {
			builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		} catch (Exception exc) {
			throw new RetrieveException(
					"Creating XML document builder failed!", exc);
		}

		Document doc = null;
		FileInputStream stream = null;
		try {
			stream = new FileInputStream(xmlFile);
			doc = builder.parse(stream);
		} catch (Exception exc) {
			throw new RetrieveException("Parsing XML failed: "
					+ xmlFile.getAbsolutePath(), exc);
		} finally {
			if (stream != null) {
				try {
					stream.close();
				} catch (Exception exc) {
				}
			}
		}
		return doc;
	}

	/**
	 * Saves an XML Document to a file.
	 * 
	 * @param xmlFile
	 *            The XML file to save to.
	 * @param doc
	 *            The XML document to save.
	 * @throws RetrieveException
	 *             If saving the XML file failed.
	 */
	public static void saveXmlDocument(File xmlFile, Document doc)
			throws RetrieveException {
		FileOutputStream stream = null;
		try {
			stream = new FileOutputStream(xmlFile);
			String encoding = "UTF-8";
			PrintStream out = new PrintStream(stream, true, encoding);

			out.println("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>");
			out.println();
			out.println("<!DOCTYPE entities [");
			out.println("  <!ENTITY minus \"&#45;\">");
			out.println("  <!ENTITY lt \"&#60;\">");
			out.println("  <!ENTITY gt \"&#62;\">");
			out.println("]>");
			out.println();

			Element root = doc.getDocumentElement();

			printNode(out, "", root);

			out.close();
		} catch (Exception exc) {
			throw new RetrieveException("Saving XML file failed: "
					+ xmlFile.getAbsolutePath(), exc);
		} finally {
			if (stream != null) {
				try {
					stream.close();
				} catch (Exception exc) {
				}
			}
		}
	}

	/**
	 * Prints a XML node.
	 * 
	 * @param out
	 *            The PrintStream where to print the node.
	 * @param prefix
	 *            The prefix to put before every line.
	 * @param node
	 *            The node to print.
	 * @throws IOException
	 *             If printing failed.
	 */
	private static void printNode(PrintStream out, String prefix, Node node)
			throws IOException {
		prefix = "";

		String name = node.getNodeName();

		boolean isText = name.equals("#text");
		boolean isComment = name.equals("#comment");
		boolean isCDATA = name.equals("#cdata-section");
		if (isText) {
			// This is a text tag
			String text = node.getNodeValue();
			text = replace(text, "<", "&lt;");
			text = replace(text, ">", "&gt;");
			text = replace(text, "--", "&minus;&minus;");
			out.print(text);
		} else if (isComment) {
			// This is a comment tag
			String comment = node.getNodeValue();
			out.print("<!--" + comment + "-->");
		} else if (isCDATA) {
			String text = node.getNodeValue();
			out.print("<![CDATA[" + text + "]]>");
		} else {
			// This is a normal tag
			out.print(prefix + "<" + name);
			if (node.hasAttributes()) {
				NamedNodeMap attributes = node.getAttributes();
				for (int i = 0; i < attributes.getLength(); i++) {
					Node attrib = attributes.item(i);
					out.print(" " + attrib.getNodeName() + "=\""
							+ attrib.getNodeValue() + "\"");
				}
			}

			if (!node.hasChildNodes()) {
				out.print("/>");
			} else {
				out.print(">");
				NodeList childList = node.getChildNodes();
				String childPrefix = prefix + "  ";
				for (int i = 0; i < childList.getLength(); i++) {
					printNode(out, childPrefix, childList.item(i));
				}
				out.print(prefix + "</" + name + ">");
			}
		}
	}

	public static String replace(String source, String pattern,
			String replacement) {
		// Check whether the pattern occurs in the source at all
		int pos = source.indexOf(pattern);
		if (pos == -1) {
			// The pattern does not occur in the source -> return the source
			return source;
		}

		// Build a new String where pattern is replaced by the replacement
		StringBuilder target = new StringBuilder(source.length());
		int start = 0; // The start of a part without the pattern
		do {
			target.append(source.substring(start, pos));
			target.append(replacement);
			start = pos + pattern.length();
		} while ((pos = source.indexOf(pattern, start)) != -1);
		target.append(source.substring(start, source.length()));

		// return the String
		return target.toString();
	}

	/**
	 * 
	 * @param node
	 * @return
	 * @throws RetrieveException
	 */
	public static boolean getTextAsBoolean(Node node) throws RetrieveException {
		String asString = getText(node, true, true);
		if (asString.equalsIgnoreCase("true")) {
			return true;
		} else if (asString.equalsIgnoreCase("false")) {
			return false;
		} else {
			throw new RetrieveException("Value of node '" + node.getNodeName()
					+ "' must be either 'true' or 'false'!");
		}
	}

	/**
	 * 
	 * @param node
	 * @return
	 * @throws RetrieveException
	 */
	public static int getTextAsInt(Node node) throws RetrieveException {
		String asString = getText(node, true, true);
		try {
			return Integer.parseInt(asString);
		} catch (NumberFormatException exc) {
			throw new RetrieveException("Value of node '" + node.getNodeName()
					+ "' must be an integer: '" + asString + "'", exc);
		}
	}

	/**
	 * 
	 * @param node
	 * @return
	 * @throws RetrieveException
	 */
	public static double getTextAsDouble(Node node) throws RetrieveException {
		String asString = getText(node, true, true);
		try {
			return Double.parseDouble(asString);
		} catch (NumberFormatException exc) {
			throw new RetrieveException("Value of node '" + node.getNodeName()
					+ "' must be a floating-point number (double): '"
					+ asString + "'", exc);
		}
	}

	/**
	 * 
	 * @param node
	 * @param mandatory
	 * @return
	 * @throws RetrieveException
	 */
	public static String[] getTextAsWordList(Node node, boolean mandatory)
			throws RetrieveException {
		String asString = getText(node, mandatory);

		if (asString == null) {
			return null;
		} else {
			StringTokenizer tokenizer = new StringTokenizer(asString);
			String[] wordList = new String[tokenizer.countTokens()];
			for (int i = 0; i < wordList.length; i++) {
				wordList[i] = tokenizer.nextToken();
			}
			return wordList;
		}
	}

	/**
	 * 
	 * @param node
	 * @return
	 * @throws RetrieveException
	 */
	public static String getTextOrCDataAsUrl(Node node)
			throws RetrieveException {
		String asString = getTextOrCData(node, true);

		// Check whether the text contains a back slash
		if (asString.indexOf('\\') != -1) {
			throw new RetrieveException(
					"Text of node '"
							+ node.getNodeName()
							+ "' is not a valid URL. Use normal slashes instead of backslashes: '"
							+ asString + "'");
		}

		return asString;
	}

	/**
	 * 
	 * @param node
	 * @return
	 */
	public static String getText(Node node) {
		Node textNode = getChild(node, "#text");
		if (textNode == null) {
			return null;
		}

		return textNode.getNodeValue();
	}

	/**
	 * Returns the CDATA value from an node.
	 * 
	 * @param node
	 *            from which the cdata will be read
	 * @param mandatory
	 *            should an exception be thrown in case of an empty node
	 * @return value of cdata node
	 * @throws RetrieveException
	 */
	public static String getCData(Node node, boolean mandatory)
			throws RetrieveException {
		return getCData(node, mandatory, false);
	}

	/**
	 * Returns the CDATA value from an node (optional trimmed).
	 * 
	 * @param node
	 *            from which the cdata will be read
	 * @param mandatory
	 *            should an exception be thrown in case of an empty node
	 * @param trimmed
	 *            should the value from the node be trimmed
	 * @return value of cdata node
	 * @throws RetrieveException
	 */
	public static String getCData(Node node, boolean mandatory, boolean trimmed)
			throws RetrieveException {
		String text = getCData(node);

		if (trimmed && (text != null)) {
			text = text.trim();
		}

		if (mandatory && ((text == null) || (text.length() == 0))) {
			throw new RetrieveException("Node '" + node.getNodeName()
					+ "' has no CDATA element.");
		} else {
			return text;
		}
	}

	/**
	 * Gets the CDATA value of a node.
	 * 
	 * @param node
	 *            The node to get the CDATA value from.
	 * @return The text of the node or <code>null</code> if the node has no
	 *         CDATA value.
	 */
	public static String getCData(Node node) {
		Node cDataNode = getChild(node, "#cdata-section");
		if (cDataNode == null) {
			return null;
		}

		return cDataNode.getNodeValue();
	}

	/**
	 * Returns text from a text or CDATA node.
	 * 
	 * @param node
	 *            The node from which to read.
	 * @param mandatory
	 *            Sets whether the result ( a text with minimal 1 char) has to
	 *            be present or not.
	 * @return the text value from the node.
	 * @throws RetrieveException
	 */
	public static String getTextOrCData(Node node, boolean mandatory)
			throws RetrieveException {
		String asString = getText(node, false);
		// Check whether we've found a text node with at least 1 char
		if (asString == null || asString.length() == 0) {
			// Try to read CDATA content from the node
			asString = getCData(node, false);
		}
		// Nothing found but this is a mandatory value
		if ((asString == null || asString.length() == 0) && mandatory) {
			throw new RetrieveException("Node '" + node.getNodeName()
					+ "' contains no text/CDATA. This is a mandatory value.");
		}
		return asString;
	}

	/**
	 * 
	 * @param node
	 * @param mandatory
	 * @return
	 * @throws RetrieveException
	 */
	public static String getText(Node node, boolean mandatory)
			throws RetrieveException {
		return getText(node, mandatory, false);
	}

	/**
	 * 
	 * @param node
	 * @param mandatory
	 * @param trimmed
	 * @return
	 * @throws RetrieveException
	 */
	public static String getText(Node node, boolean mandatory, boolean trimmed)
			throws RetrieveException {
		String text = getText(node);

		if (trimmed && (text != null)) {
			text = text.trim();
		}

		if (mandatory && ((text == null) || (text.length() == 0))) {
			throw new RetrieveException("Node '" + node.getNodeName()
					+ "' has no text");
		} else {
			return text;
		}
	}

	/**
	 * Gets a child node with a certain name.
	 * <p>
	 * If the node has more than one such children, then the first child is
	 * returned.
	 * 
	 * @param node
	 *            The node whichs child should be returned.
	 * @param childNodeName
	 *            The name of the child node.
	 * @return The child node or <code>null</code> if there is no such child.
	 */
	public static Node getChild(Node node, String childNodeName) {
		NodeList list = node.getChildNodes();
		for (int i = 0; i < list.getLength(); i++) {
			Node child = list.item(i);
			if (child.getNodeName().equals(childNodeName)) {
				return child;
			}
		}

		// No such child found
		return null;
	}

	/**
	 * 
	 * @param node
	 * @param childNodeName
	 * @param mandatory
	 * @return
	 * @throws RetrieveException
	 */
	public static Node getChild(Node node, String childNodeName,
			boolean mandatory) throws RetrieveException {
		Node childNode = getChild(node, childNodeName);

		if (mandatory && (childNode == null)) {
			throw new RetrieveException("Node '" + node.getNodeName()
					+ "' must have a child named '" + childNodeName + "'!");
		} else {
			return childNode;
		}
	}

	/**
	 * 
	 * @param node
	 * @param childNodeName
	 * @return
	 */
	public static Node[] getChildArr(Node node, String childNodeName) {
		ArrayList<Node> list = new ArrayList<Node>();

		NodeList nodeList = node.getChildNodes();
		for (int i = 0; i < nodeList.getLength(); i++) {
			Node child = nodeList.item(i);
			if (child.getNodeName().equals(childNodeName)) {
				list.add(child);
			}
		}

		Node[] nodeArr = new Node[list.size()];
		list.toArray(nodeArr);

		return nodeArr;
	}

	/**
	 * Gets a child node from a parent node. If the parent node has no such
	 * child the child of the <code>defaultNode</code> is used.
	 * 
	 * @param node
	 *            The node to get the child from.
	 * @param defaultNode
	 *            The node to get the child from if <code>node</code> has no
	 *            such child.
	 * @param childNodeName
	 *            The name of the child.
	 * @return The child with the given name or <code>null</code> if both the
	 *         <code>node</code> and the <code>defaultNode</code> have no child
	 *         with the given name.
	 */
	public static Node getCascadedChild(Node node, Node defaultNode,
			String childNodeName) {
		Node child = XmlToolkit.getChild(node, childNodeName);
		if (child == null) {
			// Try to get the cascaded child
			child = XmlToolkit.getChild(defaultNode, childNodeName);
		}

		return child;
	}

	/**
	 * Gets a child node from a parent node. If the parent node has no such
	 * child the child of the <code>defaultNode</code> is used.
	 * 
	 * @param node
	 *            The node to get the child from.
	 * @param defaultNode
	 *            The node to get the child from if <code>node</code> has no
	 *            such child.
	 * @param childNodeName
	 *            The name of the child.
	 * @param mandatory
	 *            Specifies whether to throw an exception if none of the nodes
	 *            have such a child.
	 * @return The child with the given name.
	 * @throws RetrieveException
	 *             If both the <code>node</code> and the
	 *             <code>defaultNode</code> have no child with the given name
	 *             and <code>mandatory</code> is <code>true</code>.
	 */
	public static Node getCascadedChild(Node node, Node defaultNode,
			String childNodeName, boolean mandatory) throws RetrieveException {
		Node child = getCascadedChild(node, defaultNode, childNodeName);
		if (mandatory && (child == null)) {
			throw new RetrieveException("Node '" + node.getNodeName()
					+ "' or node '" + defaultNode.getNodeName()
					+ "' must have a child named '" + childNodeName + "'!");
		}

		return child;
	}

	/**
	 * Gets the text of a child node.
	 * 
	 * @param node
	 *            The (parent) node that has the child to get the text from.
	 * @param childNodeName
	 *            The name of the child node.
	 * @param mandatory
	 *            Specifies whether an exception should be thrown if the child
	 *            has no text.
	 * @return The text of the child node or <code>null</code> if the child has
	 *         no text and <code>mandatory</code> is <code>false</code>.
	 * @throws RetrieveException
	 *             If the given node has no child with the given name of if the
	 *             child node has no text and <code>mandatory</code> is
	 *             <code>true</code>.
	 */
	public static String getChildText(Node node, String childNodeName,
			boolean mandatory) throws RetrieveException {
		Node child = getChild(node, childNodeName, mandatory);
		if (child == null) {
			return null;
		} else {
			return getText(child, mandatory);
		}
	}

	/**
	 * Gets an attribute value from a node and converts it to a boolean.
	 * 
	 * @param node
	 *            The node to get the attribute value from.
	 * @param attributeName
	 *            The name of the attribute to get.
	 * @return The value of the attribute or <code>defaultValue</code> if there
	 *         is no such attribute.
	 * @throws RetrieveException
	 *             If there is no such attribute or if the attribute value is no
	 *             boolean.
	 */
	public static boolean getAttributeAsBoolean(Node node, String attributeName)
			throws RetrieveException {
		String asString = getAttribute(node, attributeName, true);

		if (asString.equalsIgnoreCase("true")) {
			return true;
		} else if (asString.equalsIgnoreCase("false")) {
			return false;
		} else {
			throw new RetrieveException("Attribute '" + attributeName
					+ "' of node '" + node.getNodeName()
					+ "' must be either 'true' or 'false': '" + asString + "'");
		}
	}

	/**
	 * Gets an attribute value from a node and converts it to a boolean.
	 * 
	 * @param node
	 *            The node to get the attribute value from.
	 * @param attributeName
	 *            The name of the attribute to get.
	 * @param defaultValue
	 *            The default value to return if there is no such attribute.
	 * @return The value of the attribute or <code>defaultValue</code> if there
	 *         is no such attribute.
	 * @throws RetrieveException
	 *             If the attribute value is no boolean.
	 */
	public static boolean getAttributeAsBoolean(Node node,
			String attributeName, boolean defaultValue)
			throws RetrieveException {
		String asString = getAttribute(node, attributeName);

		if (asString == null) {
			return defaultValue;
		} else if (asString.equalsIgnoreCase("true")) {
			return true;
		} else if (asString.equalsIgnoreCase("false")) {
			return false;
		} else {
			throw new RetrieveException("Attribute '" + attributeName
					+ "' of node '" + node.getNodeName()
					+ "' must be either 'true' or 'false': '" + asString + "'");
		}
	}

	/**
	 * Gets an attribute value from a node and converts it to an int.
	 * 
	 * @param node
	 *            The node to get the attribute value from.
	 * @param attributeName
	 *            The name of the attribute to get.
	 * @return The value of the attribute or <code>defaultValue</code> if there
	 *         is no such attribute.
	 * @throws RetrieveException
	 *             If there is no such attribute or if the attribute value is no
	 *             int.
	 */
	public static int getAttributeAsInt(Node node, String attributeName)
			throws RetrieveException {
		String asString = getAttribute(node, attributeName, true);

		try {
			return Integer.parseInt(asString);
		} catch (NumberFormatException exc) {
			throw new RetrieveException("Attribute '" + attributeName
					+ "' of node '" + node.getNodeName()
					+ "' must be a number: '" + asString + "'");
		}
	}

	/**
	 * Gets an attribute value from a node and converts it to an int.
	 * 
	 * @param node
	 *            The node to get the attribute value from.
	 * @param attributeName
	 *            The name of the attribute to get.
	 * @param defaultValue
	 *            The default value to return if there is no such attribute.
	 * @return The value of the attribute or <code>defaultValue</code> if there
	 *         is no such attribute.
	 * @throws RetrieveException
	 *             If the attribute value is no int.
	 */
	public static int getAttributeAsInt(Node node, String attributeName,
			int defaultValue) throws RetrieveException {
		String asString = getAttribute(node, attributeName);

		if (asString == null) {
			return defaultValue;
		} else {
			try {
				return Integer.parseInt(asString);
			} catch (NumberFormatException exc) {
				throw new RetrieveException("Attribute '" + attributeName
						+ "' of node '" + node.getNodeName()
						+ "' must be a number: '" + asString + "'");
			}
		}
	}

	/**
	 * Gets an attribute value from a node.
	 * 
	 * @param node
	 *            The node to get the attribute value from.
	 * @param attributeName
	 *            The name of the wanted attribute.
	 * @return The attribute value or <code>null</code> if there is no such
	 *         attribute.
	 */
	public static String getAttribute(Node node, String attributeName) {
		Node attributeNode = node.getAttributes().getNamedItem(attributeName);
		if (attributeNode == null) {
			return null;
		} else {
			return attributeNode.getNodeValue();
		}
	}

	/**
	 * 
	 * @param node
	 * @param attributeName
	 * @param mandatory
	 * @return
	 * @throws RetrieveException
	 */
	public static String getAttribute(Node node, String attributeName,
			boolean mandatory) throws RetrieveException {
		String value = getAttribute(node, attributeName);
		if (value == null) {
			if (mandatory) {
				throw new RetrieveException("Node '" + node.getNodeName()
						+ "' has no attribute '" + attributeName + "'");
			} else {
				return null;
			}
		} else {
			return value;
		}
	}

	/**
	 * Sets the text of a node.
	 * 
	 * @param doc
	 *            The document the node comes from.
	 * @param node
	 *            The node whichs text should be changed.
	 * @param text
	 *            The text to set.
	 */
	public static void setText(Document doc, Node node, String text) {
		Node textNode = getChild(node, "#text");

		if (textNode == null) {
			textNode = doc.createTextNode(text);
			node.appendChild(textNode);
		} else {
			textNode.setNodeValue(text);
		}
	}

	/**
	 * Set the text as a CDATA section of a node.
	 * 
	 * @param doc
	 *            The document the node comes from.
	 * @param node
	 *            The node whichs CDATA section should be changed.
	 * @param text
	 *            The text to set.
	 */
	public static void setCData(Document doc, Node node, String text) {
		Node cDataNode = getChild(node, "#cdata-section");

		if (cDataNode == null) {
			CDATASection cDataSection = doc.createCDATASection(text);
			node.appendChild(cDataSection);

		} else {
			cDataNode.setNodeValue(text);
		}
	}

	/**
	 * Removes all child nodes from a node.
	 * 
	 * @param node
	 *            The node to remove the children from.
	 */
	public static void removeAllChildren(Node node) {
		NodeList nodeList = node.getChildNodes();
		for (int i = nodeList.getLength() - 1; i >= 0; i--) {
			node.removeChild(nodeList.item(i));
		}
	}

	/**
	 * Removes all child nodes with a certain name.
	 * 
	 * @param node
	 *            The node to remove the children from.
	 * @param childNodeName
	 *            The name of the children to remove.
	 */
	public static void removeAllChildren(Node node, String childNodeName) {
		Node[] childArr = getChildArr(node, childNodeName);
		for (int i = 0; i < childArr.length; i++) {
			node.removeChild(childArr[i]);
		}
	}

	/**
	 * Adds a child node to a node.
	 * 
	 * @param doc
	 *            The document the node comes from.
	 * @param node
	 *            The node were to add the child.
	 * @param childNodeName
	 *            The name of the child node to add.
	 * @return The added child node.
	 */
	public static Node addChild(Document doc, Node node, String childNodeName) {
		Node childNode = doc.createElement(childNodeName);
		node.appendChild(childNode);
		return childNode;
	}

	/**
	 * Gets a child node or creates it if no such node exists.
	 * 
	 * @param doc
	 *            The document the node comes from.
	 * @param node
	 *            The node were to get the child from or where to add the child.
	 * @param childNodeName
	 *            The name of the child node to get or add.
	 * @return The child node.
	 */
	public static Node getOrAddChild(Document doc, Node node,
			String childNodeName) {
		Node child = getChild(node, childNodeName);
		if (child == null) {
			child = addChild(doc, node, childNodeName);
		}
		return child;
	}

	/**
	 * Adds a child node to a node and gives it a text.
	 * 
	 * @param doc
	 *            The document the node comes from.
	 * @param node
	 *            The node where to add the child.
	 * @param childNodeName
	 *            The name of the child node to add.
	 * @param text
	 *            The text to set to the child.
	 * @return The added child node.
	 */
	public static Node addChildWithText(Document doc, Node node,
			String childNodeName, String text) {
		Node childNode = addChild(doc, node, childNodeName);
		setText(doc, childNode, text);
		return childNode;
	}

	/**
	 * Adds a child node to a node and gives it a CDATA section.
	 * 
	 * @param doc
	 *            The document the node comes from.
	 * @param node
	 *            The node where to add the child.
	 * @param childNodeName
	 *            The name of the child node to add.
	 * @param text
	 *            The text (as CDATA section) to set to the child.
	 * @return The added child node.
	 */
	public static Node addChildWithCData(Document doc, Node node,
			String childNodeName, String text) {
		Node childNode = addChild(doc, node, childNodeName);
		setCData(doc, childNode, text);
		return childNode;
	}

	/**
	 * Sets an attribute of a node.
	 * 
	 * @param doc
	 *            The document the node comes from.
	 * @param node
	 *            The node where to set the attribute.
	 * @param attribName
	 *            The name of the attribute to set.
	 * @param attribValue
	 *            The value of the attribute to set.
	 */
	public static void setAttribute(Document doc, Node node, String attribName,
			String attribValue) {
		Attr attr = doc.createAttribute(attribName);
		attr.setNodeValue(attribValue);
		node.getAttributes().setNamedItem(attr);
	}

	/**
	 * Pretty prints a node.
	 * 
	 * @param doc
	 *            The document the node comes from.
	 * @param node
	 *            The node that should be pretty printed.
	 */
	public static void prettyPrint(Document doc, Node node) {
		// Get the text before the node and extract the indenting
		Node parent = node.getParentNode();

		String indenting = "";
		NodeList siblingList = parent.getChildNodes();
		for (int i = 1; i < siblingList.getLength(); i++) {
			Node sibling = siblingList.item(i);
			if (sibling == node) {
				Node nodeBefore = siblingList.item(i - 1);
				// Check whether this is a text node
				if (nodeBefore.getNodeName().equals("#text")) {
					// There is text before the node -> Extract the indenting
					String text = nodeBefore.getNodeValue();
					int newlinePos = text.lastIndexOf('\n');
					if (newlinePos != -1) {
						indenting = text.substring(newlinePos);
						if (indenting.trim().length() != 0) {
							// The indenting is no whitespace -> Forget it
							indenting = "";
						}
					}
				}
				break;
			}
		}

		// Now pretty print the node
		prettyPrint(doc, node, indenting);
	}

	/**
	 * Pretty prints a node.
	 * 
	 * @param doc
	 *            The document the node comes from.
	 * @param node
	 *            The node that should be pretty printed.
	 * @param prefix
	 *            The prefix the node should get.
	 */
	private static void prettyPrint(Document doc, Node node, String prefix) {
		String childPrefix = prefix + "  ";

		// Add the indenting to the children
		NodeList childList = node.getChildNodes();
		boolean hasChildren = false;
		for (int i = childList.getLength() - 1; i >= 0; i--) {
			Node child = childList.item(i);
			boolean isNormalNode = (!child.getNodeName().startsWith("#"));
			if (isNormalNode) {
				// Add the indenting to this node
				Node textNode = doc.createTextNode(childPrefix);
				node.insertBefore(textNode, child);

				// pretty print the child's children
				prettyPrint(doc, child, childPrefix);

				hasChildren = true;
			}
		}

		// Add the indenting to the end tag
		if (hasChildren) {
			Node textNode = doc.createTextNode(prefix);
			node.appendChild(textNode);
		}
	}

}
 
分享到:
评论

相关推荐

    Java实现的两个工具类,解决从键盘输入后,获得各种数据的问题的工具包及把socket字节流中包含的数据转换成字符串

    这两个工具类正是为了简化这些操作而设计的。首先,我们来深入理解标题和描述中的主要知识点。 1. **键盘输入处理**: Java提供多种方式从键盘接收用户输入,如Scanner类、BufferedReader类等。在描述中提到的工具...

    两个网页工具类,用于处理页面

    在使用这两个工具类时,开发者应了解其API接口,理解每个方法的作用和参数,以便有效地集成到自己的项目中。同时,遵循良好的编程习惯,如错误处理和日志记录,可以使工具类更加健壮和易维护。 总结起来,这两个...

    AS程序员实用工具类

    除此之外,GS还包含了TimelineLite和TimelineMax,这两个工具类提供了时间线管理和控制,允许开发者安排多个动画按照特定顺序执行,甚至可以响应用户交互或外部事件来改变动画的播放状态。这使得开发者可以创建复杂...

    apache工具类

    让我们详细探讨一下这两个工具类以及其他可能的Util类。 **1. Apache Commons Lang - StringUtil** `StringUtil` 是Apache Commons Lang库的一部分,它包含了大量处理字符串的方法,使得开发者可以更加高效地进行...

    JAVA利用poi完成word转pdf,内容包括两个现成工具类和使用到的所有jar包

    在实际项目中,为了方便重复使用,可以封装这些操作到两个工具类中:一个用于读取Word并转换为Docx4j可以理解的格式,另一个用于将Docx4j的输出转换为最终的PDF。此外,工具类中还可以包含字节流处理,以便于在网络...

    28个java常用的工具类源码

    4. **Collections 和 Arrays**: 这两个工具类提供了对集合和数组的各种操作,如排序、查找、填充等。 5. **Date 和 Calendar**: Date类表示特定的时间点,而Calendar是日期和时间的日历系统抽象类,提供了更复杂的...

    httpUtils等工具类

    下面我们将深入探讨这两个工具类以及它们在实际应用中的作用。 首先,我们来看`httpUtils`。在Java或Android开发中,`httpUtils`通常是一个用来简化HTTP通信的工具类。它封装了常见的HTTP请求方法,如GET、POST、...

    Map和Bean灵活转换工具类

    本文将详细探讨这两个工具类的功能以及它们在实际开发中的应用。 `MapUtils`类通常用于将Map对象转换为Java Bean对象,这在我们需要将存储在Map中的键值对数据转化为具有明确属性的对象时非常有用。例如,假设我们...

    安卓6.0后动态申请权限的工具类

    通常有两个工具类供开发者选择: 1. **PermissionApplyUtil**: 这个工具类可能包含了检查权限状态和请求权限的功能,通过方法如`applyPermission()`或`checkAndRequestPermissions()`来使用。开发者只需要传入需要...

    读取配置文件工具类.rar

    本压缩包包含两个工具类,分别用于读取`.yml`和`.properties`格式的配置文件,这两种格式都是Java开发中常见的配置文件类型。 首先,我们来看`.properties`配置文件。这种格式的文件历史较为悠久,它的语法简单,每...

    java 两种加密工具类

    这两个工具类可以帮助开发者实现数据的加密和解密,确保信息在传输过程中的安全性。 首先,我们来看EncryptUtil类。这个类通常会包含常见的加密算法,如MD5(Message-Digest Algorithm 5)或SHA(Secure Hash ...

    工作中经常常会用到的java工具类

    在处理日期和时间相关的业务逻辑时,这两个工具类可以极大地提升开发效率。 3. **Time.java**:可能是一个自定义的时间工具类,用来处理与时间相关的操作,补充了Java内置`java.time`包中的功能,或者提供了一些...

    安卓工具类

    本篇文章将详细探讨标题中提到的两个工具类:HousedAndroidClient 和 HouseServer,并分析它们在安卓项目中的作用。 首先,我们来讨论`HousedAndroidClient`类。这个类很可能是一个客户端工具,主要用于处理与...

    韩顺平SqlHelper,DBUtil工具类

    这两个工具类是非静态的,这意味着它们可以被实例化并复用,从而避免了静态类可能带来的线程安全问题。 首先,SqlHelper类通常用于执行SQL语句,包括查询、插入、更新和删除等操作。它封装了ADO.NET组件,如...

    python操作mysql工具类之压缩包1、封装一个不用写SQL语句,只需要填参数的工具类2、封装一个需要自己写SQL语句的

    以上代码展示了如何使用这两个工具类的基本用法。在实际项目中,可能还需要考虑异常处理、事务管理、连接池等高级特性,以确保代码的健壮性和性能优化。 总之,这两个Python工具类为开发者提供了便利,既满足了简单...

    Java将2个List集合合并到一个List里面并排序工具类

    Java将2个List集合合并到一个List里面并排序工具类 1、Java编程资源,定义了一个名为`ListMerger`的工具类,主要包含一个名为`mergeAndSortLists`的静态方法。此方法用于将两个已经根据时间顺序排列的List合并成一...

    java常用工具类的使用

    对于数组和列表的排序、搜索等功能,Java标准库提供了`java.util.Arrays`和`java.util.Collections`两个工具类。 ##### `Arrays`中的方法 - `void sort(T[])`: 对数组中的元素按照升序进行排序。 - `List asList...

    韩顺平老师PHP视频工具类

    这两个工具类体现了良好的代码组织原则,即把重复的、通用的功能抽象成独立的类。这不仅降低了代码的复杂性,也提高了代码的可读性和可扩展性。学习和掌握这些工具类的使用,对于PHP开发者来说,意味着能更好地应对...

    30个java工具类

    [工具类] Java中计算任意两个日期之间的工作天数 .java.txt [工具类] java抓取网页 .java.txt [工具类] MD5 .java.txt [工具类] MD5强化版 .java.txt [工具类] MD5超强化版 .java.txt [工具类] XML 实用工具类 .java...

Global site tag (gtag.js) - Google Analytics