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.*; public class Main { /** * escapes special XML characters so that they are valid XML data * * @param chars incoming character string value intended to be saved as XML data * @return escaped character string suitable as XML equivalent for the incoming parameter **/ public static final String escape(String chars) { /** * NOTE: READ BEFORE MAKING ANY UPDATES This code has been optimized for speed when * escape'ing very long character strings. Please do not modify it unless you perform timing * tests on such long strings, such as long XML text sequences. **/ boolean modified = false; //*Set true if have to change chars to escape() it properly if (chars == null) { return ""; } final int siz = chars.length(); // Curr size of chars final StringBuilder str = new StringBuilder(siz * 2); for (int j = 0; j < siz; j++) { final char currChar = chars.charAt(j); // One special char to look for in chars switch (currChar) { case 10: case 11: case 13: str.append("<br />"); modified = true; break; case '&': str.append("&"); modified = true; break; // Insert ampersand char sequence case '\"': str.append("""); modified = true; break; // Insert double quotation mark sequence case '<': str.append("<"); modified = true; break; // Insert "less than" sequence case '>': str.append(">"); modified = true; break; // Insert "greater than" sequence case '\'': str.append("'"); modified = true; break; // Insert apostrophe default: if (currChar < 32) { // Remove control characters modified = true; break; } if (currChar <= 127) { str.append(currChar); // Insert char as it is } else { str.append("&#"); str.append(Integer.toString(currChar)); str.append(";"); // Insert Unicode char code for ones we don't know modified = true; } } } if (modified || (str.length() != siz)) { chars = str.toString(); } return chars; // Return the escaped character string } /** * 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(); } 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); } }