Java tutorial
//package com.java2s; /* // This software is subject to the terms of the Eclipse Public License v1.0 // Agreement, available at the following URL: // http://www.eclipse.org/legal/epl-v10.html. // You must accept the terms of that agreement to use this software. // // Copyright (C) 2003-2005 Julian Hyde // Copyright (C) 2005-2011 Pentaho // Copyright (c) 2008-2014 Open Link Financial, Inc. All Rights Reserved. */ import java.util.*; public class Main { /** * Invalid characters for XML element name. * * <p>XML element name: * * Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] * | [#xE000-#xFFFD] | [#x10000-#x10FFFF] * S ::= (#x20 | #x9 | #xD | #xA)+ * NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar * | Extender * Name ::= (Letter | '_' | ':') (NameChar)* * Names ::= Name (#x20 Name)* * Nmtoken ::= (NameChar)+ * Nmtokens ::= Nmtoken (#x20 Nmtoken)* * */ private static final String[] CHAR_TABLE = new String[256]; /** * Encodes an XML element name. * * <p>This function is mainly for encode element names in result of Drill * Through execute, because its element names come from database, we cannot * make sure they are valid XML contents. * * <p>Quoth the <a href="http://xmla.org">XML/A specification</a>, version * 1.1: * <blockquote> * XML does not allow certain characters as element and attribute names. * XML for Analysis supports encoding as defined by SQL Server 2000 to * address this XML constraint. For column names that contain invalid XML * name characters (according to the XML 1.0 specification), the nonvalid * Unicode characters are encoded using the corresponding hexadecimal * values. These are escaped as _x<i>HHHH_</i> where <i>HHHH</i> stands for * the four-digit hexadecimal UCS-2 code for the character in * most-significant bit first order. For example, the name "Order Details" * is encoded as Order_<i>x0020</i>_Details, where the space character is * replaced by the corresponding hexadecimal code. * </blockquote> * * @param name Name of XML element * @return encoded name */ private static String encodeElementName(String name) { StringBuilder buf = new StringBuilder(); char[] nameChars = name.toCharArray(); for (char ch : nameChars) { String encodedStr = (ch >= CHAR_TABLE.length ? null : CHAR_TABLE[ch]); if (encodedStr == null) { buf.append(ch); } else { buf.append(encodedStr); } } return buf.toString(); } private static <T> String toString(List<T> list) { StringBuilder buf = new StringBuilder(); int k = -1; for (T t : list) { if (++k > 0) { buf.append(", "); } buf.append(t); } return buf.toString(); } }