Java tutorial
//package com.java2s; /** * $RCSfile: StringUtils.java,v $ * $Revision: 1.1.2.1 $ * $Date: 2014/02/20 01:11:36 $ * * Copyright 2003-2007 Jive Software. * * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ public class Main { /** * Escapes the node portion of a JID according to "JID Escaping" (JEP-0106). * Escaping replaces characters prohibited by node-prep with escape sequences, * as follows:<p> * * <table border="1"> * <tr><td><b>Unescaped Character</b></td><td><b>Encoded Sequence</b></td></tr> * <tr><td><space></td><td>\20</td></tr> * <tr><td>"</td><td>\22</td></tr> * <tr><td>&</td><td>\26</td></tr> * <tr><td>'</td><td>\27</td></tr> * <tr><td>/</td><td>\2f</td></tr> * <tr><td>:</td><td>\3a</td></tr> * <tr><td><</td><td>\3c</td></tr> * <tr><td>></td><td>\3e</td></tr> * <tr><td>@</td><td>\40</td></tr> * <tr><td>\</td><td>\5c</td></tr> * </table><p> * * This process is useful when the node comes from an external source that doesn't * conform to nodeprep. For example, a username in LDAP may be "Joe Smith". Because * the <space> character isn't a valid part of a node, the username should * be escaped to "Joe\20Smith" before being made into a JID (e.g. "joe\20smith@example.com" * after case-folding, etc. has been applied).<p> * * All node escaping and un-escaping must be performed manually at the appropriate * time; the JID class will not escape or un-escape automatically. * * @param node the node. * @return the escaped version of the node. */ public static String escapeNode(String node) { if (node == null) { return null; } StringBuilder buf = new StringBuilder(node.length() + 8); for (int i = 0, n = node.length(); i < n; i++) { char c = node.charAt(i); switch (c) { case '"': buf.append("\\22"); break; case '&': buf.append("\\26"); break; case '\'': buf.append("\\27"); break; case '/': buf.append("\\2f"); break; case ':': buf.append("\\3a"); break; case '<': buf.append("\\3c"); break; case '>': buf.append("\\3e"); break; case '@': buf.append("\\40"); break; case '\\': buf.append("\\5c"); break; default: { if (Character.isWhitespace(c)) { buf.append("\\20"); } else { buf.append(c); } } } } return buf.toString(); } }