Java tutorial
//package com.java2s; /** * The contents of this file are subject to the Regenstrief Public License * Version 1.0 (the "License"); you may not use this file except in compliance with the License. * Please contact Regenstrief Institute if you would like to obtain a copy of the license. * * 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. * * Copyright (C) Regenstrief Institute. All Rights Reserved. */ import org.w3c.dom.*; import java.util.*; public class Main { private static Map<String, String> XML_ENTITY_MAP = null; /** * takes an escape'd XML string and converts it back to orginal text contents * * @param chars XML contents * @return String translation of XML contents **/ public static final String unescape(final String chars) { if (chars == null) { return ""; } final StringBuilder res = new StringBuilder(chars); int i = res.indexOf("&"); while (i != -1) { final int j = res.indexOf(";", i + 1); if (j == -1) { break; } final String entity = res.substring(i + 1, j); if (entity.startsWith("#")) { if ((entity.length() == 3) && (entity.charAt(1) == 'x')) { // http://www.w3.org/TR/1999/WD-xml-c14n-19991109.html // 5.2 Character Escaping switch (entity.charAt(2)) { case '9': res.replace(i, j + 1, "\t"); break; case 'D': res.replace(i, j + 1, "\r"); break; case 'A': res.replace(i, j + 1, "\n"); break; } } else { try { final int charCode = Integer.parseInt(entity.substring(1)); final char c = (char) charCode; res.replace(i, j + 1, Character.toString(c)); i = i + 1; } catch (final NumberFormatException e) { res.delete(i, j + 1); } } } else { final String textEntity = XML_ENTITY_MAP.get(entity); if (textEntity != null) { res.replace(i, j + 1, textEntity); i = i + textEntity.length(); } else { res.delete(i, j + 1); } } i = res.indexOf("&", i); } return res.toString(); } /** * Retrieves the String of a Node * * @param n the Node * @return the String **/ public final static String toString(final Node n) { if (n == null) { return null; } return n instanceof Text ? n.getNodeValue() : n.getNodeName(); } public final static String toString(final short nodeType) { return toClass(nodeType).getSimpleName(); } /** * Retrieves the desired Node of the given NodeList * * @param nodeList the NodeList * @param i the desired index * @return the desired Node **/ public final static Node get(final NodeList nodeList, final int i) { return size(nodeList) <= i ? null : nodeList.item(i); } public final static Class<? extends Node> toClass(final short nodeType) { switch (nodeType) { case Node.ATTRIBUTE_NODE: return Attr.class; case Node.CDATA_SECTION_NODE: return CDATASection.class; case Node.COMMENT_NODE: return Comment.class; case Node.DOCUMENT_FRAGMENT_NODE: return DocumentFragment.class; case Node.DOCUMENT_NODE: return Document.class; case Node.DOCUMENT_TYPE_NODE: return DocumentType.class; case Node.ELEMENT_NODE: return Element.class; case Node.ENTITY_NODE: return Entity.class; case Node.ENTITY_REFERENCE_NODE: return EntityReference.class; case Node.NOTATION_NODE: return Notation.class; case Node.PROCESSING_INSTRUCTION_NODE: return ProcessingInstruction.class; case Node.TEXT_NODE: return Text.class; } throw new RuntimeException("Unrecognized node type " + nodeType); } /** * Retrieves the number of Nodes in the NodeList * * @param nodeList the NodeList * @return the number of Nodes **/ public final static int size(final NodeList nodeList) { return nodeList == null ? 0 : nodeList.getLength(); } /** * Retrieves the number of Nodes in the NamedNodeMap * * @param nodeMap the NamedNodeMap * @return the number of Nodes **/ public final static int size(final NamedNodeMap nodeMap) { return nodeMap == null ? 0 : nodeMap.getLength(); } /** * Retrieves the number of Nodes in the tree rooted at the given Node * * @param node the root Node * @return the number of Nodes **/ public final static int size(final Node node) { if (node == null) { return 0; } final NodeList children = node.getChildNodes(); int size = 1; for (int i = 0, n = size(children); i < n; i++) { size += size(children.item(i)); } return size; } }