Java tutorial
//package com.java2s; /* * eXist Open Source Native XML Database * Copyright (C) 2001-06 The eXist Project * http://exist-db.org * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * $Id$ */ import java.io.CharArrayWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; public class Main { /** * Escapes characters that are not allowed in various places * in XML by replacing all invalid characters with * <code>getEscape(c)</code>. * * @param buffer The <code>StringBuffer</code> containing * the text to escape in place. */ public static void XMLEscape(StringBuffer buffer) { if (buffer == null) { return; } char c; String escape; for (int i = 0; i < buffer.length();) { c = buffer.charAt(i); escape = getEscape(c); if (escape == null) { i++; } else { buffer.replace(i, i + 1, escape); i += escape.length(); } } } /** * Escapes characters that are not allowed in various places * in XML by replacing all invalid characters with * <code>getEscape(c)</code>. * * @param in The <code>String</code> containing * the text to escape in place. */ public static String XMLEscape(String in) { if (in == null) { return null; } final StringBuffer temp = new StringBuffer(in); XMLEscape(temp); return temp.toString(); } /** * Escapes characters that are not allowed in various * places in XML. Characters are replaced by the * corresponding entity. The characters &, <, * >, ", and ' are escaped. * * @param c The character to escape. * @return A <code>String</code> representing the * escaped character or null if the character does * not need to be escaped. */ public static String getEscape(char c) { switch (c) { case '&': return "&"; case '<': return "<"; case '>': return ">"; case '\"': return """; case '\'': return "'"; default: return null; } } /** Reads an <code>InputStream</code> into a string. * @param in The stream to read into a string. * @return The stream as a string * @throws IOException */ public static String toString(InputStream in) throws IOException { if (in == null) { return null; } final Reader reader = new InputStreamReader(in); final char[] buffer = new char[100]; final CharArrayWriter writer = new CharArrayWriter(1000); int read; while ((read = reader.read(buffer)) > -1) writer.write(buffer, 0, read); return writer.toString(); } }