You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by tr...@apache.org on 2006/01/20 14:48:55 UTC
svn commit: r370807 [9/22] - in /directory/sandbox/trustin/mina-spi: ./
core/src/main/java/org/apache/mina/common/
core/src/main/java/org/apache/mina/common/support/
core/src/main/java/org/apache/mina/common/support/discovery/
core/src/main/java/org/ap...
Added: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/SerializationUtils.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/SerializationUtils.java?rev=370807&view=auto
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/SerializationUtils.java (added)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/SerializationUtils.java Fri Jan 20 05:47:50 2006
@@ -0,0 +1,194 @@
+/*
+ * Copyright 2002-2005 The Apache Software Foundation.
+ *
+ * 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.
+ */
+package org.apache.mina.common.support.lang;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.OutputStream;
+import java.io.Serializable;
+
+/**
+ * <p>Assists with the serialization process and performs additional functionality based
+ * on serialization.</p>
+ * <p>
+ * <ul>
+ * <li>Deep clone using serialization
+ * <li>Serialize managing finally and IOException
+ * <li>Deserialize managing finally and IOException
+ * </ul>
+ *
+ * <p>This class throws exceptions for invalid <code>null</code> inputs.
+ * Each method documents its behaviour in more detail.</p>
+ *
+ * @author <a href="mailto:nissim@nksystems.com">Nissim Karpenstein</a>
+ * @author <a href="mailto:janekdb@yahoo.co.uk">Janek Bogucki</a>
+ * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
+ * @author Stephen Colebourne
+ * @author Jeff Varszegi
+ * @author Gary Gregory
+ * @since 1.0
+ * @version $Id$
+ */
+public class SerializationUtils {
+
+ /**
+ * <p>SerializationUtils instances should NOT be constructed in standard programming.
+ * Instead, the class should be used as <code>SerializationUtils.clone(object)</code>.</p>
+ *
+ * <p>This constructor is public to permit tools that require a JavaBean instance
+ * to operate.</p>
+ * @since 2.0
+ */
+ public SerializationUtils() {
+ super();
+ }
+
+ // Clone
+ //-----------------------------------------------------------------------
+ /**
+ * <p>Deep clone an <code>Object</code> using serialization.</p>
+ *
+ * <p>This is many times slower than writing clone methods by hand
+ * on all objects in your object graph. However, for complex object
+ * graphs, or for those that don't support deep cloning this can
+ * be a simple alternative implementation. Of course all the objects
+ * must be <code>Serializable</code>.</p>
+ *
+ * @param object the <code>Serializable</code> object to clone
+ * @return the cloned object
+ * @throws SerializationException (runtime) if the serialization fails
+ */
+ public static Object clone(Serializable object) {
+ return deserialize(serialize(object));
+ }
+
+ // Serialize
+ //-----------------------------------------------------------------------
+ /**
+ * <p>Serializes an <code>Object</code> to the specified stream.</p>
+ *
+ * <p>The stream will be closed once the object is written.
+ * This avoids the need for a finally clause, and maybe also exception
+ * handling, in the application code.</p>
+ *
+ * <p>The stream passed in is not buffered internally within this method.
+ * This is the responsibility of your application if desired.</p>
+ *
+ * @param obj the object to serialize to bytes, may be null
+ * @param outputStream the stream to write to, must not be null
+ * @throws IllegalArgumentException if <code>outputStream</code> is <code>null</code>
+ * @throws SerializationException (runtime) if the serialization fails
+ */
+ public static void serialize(Serializable obj, OutputStream outputStream) {
+ if (outputStream == null) {
+ throw new IllegalArgumentException("The OutputStream must not be null");
+ }
+ ObjectOutputStream out = null;
+ try {
+ // stream closed in the finally
+ out = new ObjectOutputStream(outputStream);
+ out.writeObject(obj);
+
+ } catch (IOException ex) {
+ throw new SerializationException(ex);
+ } finally {
+ try {
+ if (out != null) {
+ out.close();
+ }
+ } catch (IOException ex) {
+ // ignore;
+ }
+ }
+ }
+
+ /**
+ * <p>Serializes an <code>Object</code> to a byte array for
+ * storage/serialization.</p>
+ *
+ * @param obj the object to serialize to bytes
+ * @return a byte[] with the converted Serializable
+ * @throws SerializationException (runtime) if the serialization fails
+ */
+ public static byte[] serialize(Serializable obj) {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
+ serialize(obj, baos);
+ return baos.toByteArray();
+ }
+
+ // Deserialize
+ //-----------------------------------------------------------------------
+ /**
+ * <p>Deserializes an <code>Object</code> from the specified stream.</p>
+ *
+ * <p>The stream will be closed once the object is written. This
+ * avoids the need for a finally clause, and maybe also exception
+ * handling, in the application code.</p>
+ *
+ * <p>The stream passed in is not buffered internally within this method.
+ * This is the responsibility of your application if desired.</p>
+ *
+ * @param inputStream the serialized object input stream, must not be null
+ * @return the deserialized object
+ * @throws IllegalArgumentException if <code>inputStream</code> is <code>null</code>
+ * @throws SerializationException (runtime) if the serialization fails
+ */
+ public static Object deserialize(InputStream inputStream) {
+ if (inputStream == null) {
+ throw new IllegalArgumentException("The InputStream must not be null");
+ }
+ ObjectInputStream in = null;
+ try {
+ // stream closed in the finally
+ in = new ObjectInputStream(inputStream);
+ return in.readObject();
+
+ } catch (ClassNotFoundException ex) {
+ throw new SerializationException(ex);
+ } catch (IOException ex) {
+ throw new SerializationException(ex);
+ } finally {
+ try {
+ if (in != null) {
+ in.close();
+ }
+ } catch (IOException ex) {
+ // ignore
+ }
+ }
+ }
+
+ /**
+ * <p>Deserializes a single <code>Object</code> from an array of bytes.</p>
+ *
+ * @param objectData the serialized object, must not be null
+ * @return the deserialized object
+ * @throws IllegalArgumentException if <code>objectData</code> is <code>null</code>
+ * @throws SerializationException (runtime) if the serialization fails
+ */
+ public static Object deserialize(byte[] objectData) {
+ if (objectData == null) {
+ throw new IllegalArgumentException("The byte[] must not be null");
+ }
+ ByteArrayInputStream bais = new ByteArrayInputStream(objectData);
+ return deserialize(bais);
+ }
+
+}
Propchange: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/SerializationUtils.java
------------------------------------------------------------------------------
svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision
Added: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/StringEscapeUtils.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/StringEscapeUtils.java?rev=370807&view=auto
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/StringEscapeUtils.java (added)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/StringEscapeUtils.java Fri Jan 20 05:47:50 2006
@@ -0,0 +1,521 @@
+/*
+ * Copyright 2002-2005 The Apache Software Foundation.
+ *
+ * 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.
+ */
+package org.apache.mina.common.support.lang;
+
+import java.io.IOException;
+import java.io.Writer;
+
+import org.apache.mina.common.support.lang.exception.NestableRuntimeException;
+
+/**
+ * <p>Escapes and unescapes <code>String</code>s for
+ * Java, Java Script, HTML, XML, and SQL.</p>
+ *
+ * @author Apache Jakarta Turbine
+ * @author GenerationJavaCore library
+ * @author Purple Technology
+ * @author <a href="mailto:bayard@generationjava.com">Henri Yandell</a>
+ * @author <a href="mailto:alex@purpletech.com">Alexander Day Chaffee</a>
+ * @author Antony Riley
+ * @author Helge Tesgaard
+ * @author <a href="sean@boohai.com">Sean Brown</a>
+ * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
+ * @author Phil Steitz
+ * @author Pete Gieser
+ * @since 2.0
+ * @version $Id$
+ */
+public class StringEscapeUtils {
+
+ /**
+ * <p><code>StringEscapeUtils</code> instances should NOT be constructed in
+ * standard programming.</p>
+ *
+ * <p>Instead, the class should be used as:
+ * <pre>StringEscapeUtils.escapeJava("foo");</pre></p>
+ *
+ * <p>This constructor is public to permit tools that require a JavaBean
+ * instance to operate.</p>
+ */
+ public StringEscapeUtils() {
+ }
+
+ // Java and JavaScript
+ //--------------------------------------------------------------------------
+ /**
+ * <p>Escapes the characters in a <code>String</code> using Java String rules.</p>
+ *
+ * <p>Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.) </p>
+ *
+ * <p>So a tab becomes the characters <code>'\\'</code> and
+ * <code>'t'</code>.</p>
+ *
+ * <p>The only difference between Java strings and JavaScript strings
+ * is that in JavaScript, a single quote must be escaped.</p>
+ *
+ * <p>Example:
+ * <pre>
+ * input string: He didn't say, "Stop!"
+ * output string: He didn't say, \"Stop!\"
+ * </pre>
+ * </p>
+ *
+ * @param str String to escape values in, may be null
+ * @return String with escaped values, <code>null</code> if null string input
+ */
+ public static String escapeJava(String str) {
+ return escapeJavaStyleString(str, false);
+ }
+
+ /**
+ * <p>Escapes the characters in a <code>String</code> using Java String rules to
+ * a <code>Writer</code>.</p>
+ *
+ * <p>A <code>null</code> string input has no effect.</p>
+ *
+ * @see #escapeJava(java.lang.String)
+ * @param out Writer to write escaped string into
+ * @param str String to escape values in, may be null
+ * @throws IllegalArgumentException if the Writer is <code>null</code>
+ * @throws IOException if error occurs on underlying Writer
+ */
+ public static void escapeJava(Writer out, String str) throws IOException {
+ escapeJavaStyleString(out, str, false);
+ }
+
+ /**
+ * <p>Escapes the characters in a <code>String</code> using JavaScript String rules.</p>
+ * <p>Escapes any values it finds into their JavaScript String form.
+ * Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.) </p>
+ *
+ * <p>So a tab becomes the characters <code>'\\'</code> and
+ * <code>'t'</code>.</p>
+ *
+ * <p>The only difference between Java strings and JavaScript strings
+ * is that in JavaScript, a single quote must be escaped.</p>
+ *
+ * <p>Example:
+ * <pre>
+ * input string: He didn't say, "Stop!"
+ * output string: He didn\'t say, \"Stop!\"
+ * </pre>
+ * </p>
+ *
+ * @param str String to escape values in, may be null
+ * @return String with escaped values, <code>null</code> if null string input
+ */
+ public static String escapeJavaScript(String str) {
+ return escapeJavaStyleString(str, true);
+ }
+
+ /**
+ * <p>Escapes the characters in a <code>String</code> using JavaScript String rules
+ * to a <code>Writer</code>.</p>
+ *
+ * <p>A <code>null</code> string input has no effect.</p>
+ *
+ * @see #escapeJavaScript(java.lang.String)
+ * @param out Writer to write escaped string into
+ * @param str String to escape values in, may be null
+ * @throws IllegalArgumentException if the Writer is <code>null</code>
+ * @throws IOException if error occurs on underlying Writer
+ **/
+ public static void escapeJavaScript(Writer out, String str) throws IOException {
+ escapeJavaStyleString(out, str, true);
+ }
+
+ private static String escapeJavaStyleString(String str, boolean escapeSingleQuotes) {
+ if (str == null) {
+ return null;
+ }
+ try {
+ StringPrintWriter writer = new StringPrintWriter(str.length() * 2);
+ escapeJavaStyleString(writer, str, escapeSingleQuotes);
+ return writer.getString();
+ } catch (IOException ioe) {
+ // this should never ever happen while writing to a StringWriter
+ ioe.printStackTrace();
+ return null;
+ }
+ }
+
+ private static void escapeJavaStyleString(Writer out, String str, boolean escapeSingleQuote) throws IOException {
+ if (out == null) {
+ throw new IllegalArgumentException("The Writer must not be null");
+ }
+ if (str == null) {
+ return;
+ }
+ int sz;
+ sz = str.length();
+ for (int i = 0; i < sz; i++) {
+ char ch = str.charAt(i);
+
+ // handle unicode
+ if (ch > 0xfff) {
+ out.write("\\u" + hex(ch));
+ } else if (ch > 0xff) {
+ out.write("\\u0" + hex(ch));
+ } else if (ch > 0x7f) {
+ out.write("\\u00" + hex(ch));
+ } else if (ch < 32) {
+ switch (ch) {
+ case '\b':
+ out.write('\\');
+ out.write('b');
+ break;
+ case '\n':
+ out.write('\\');
+ out.write('n');
+ break;
+ case '\t':
+ out.write('\\');
+ out.write('t');
+ break;
+ case '\f':
+ out.write('\\');
+ out.write('f');
+ break;
+ case '\r':
+ out.write('\\');
+ out.write('r');
+ break;
+ default :
+ if (ch > 0xf) {
+ out.write("\\u00" + hex(ch));
+ } else {
+ out.write("\\u000" + hex(ch));
+ }
+ break;
+ }
+ } else {
+ switch (ch) {
+ case '\'':
+ if (escapeSingleQuote) {
+ out.write('\\');
+ }
+ out.write('\'');
+ break;
+ case '"':
+ out.write('\\');
+ out.write('"');
+ break;
+ case '\\':
+ out.write('\\');
+ out.write('\\');
+ break;
+ default :
+ out.write(ch);
+ break;
+ }
+ }
+ }
+ }
+
+ /**
+ * <p>Returns an upper case hexadecimal <code>String</code> for the given
+ * character.</p>
+ *
+ * @param ch The character to convert.
+ * @return An upper case hexadecimal <code>String</code>
+ */
+ private static String hex(char ch) {
+ return Integer.toHexString(ch).toUpperCase();
+ }
+
+ /**
+ * <p>Unescapes any Java literals found in the <code>String</code>.
+ * For example, it will turn a sequence of <code>'\'</code> and
+ * <code>'n'</code> into a newline character, unless the <code>'\'</code>
+ * is preceded by another <code>'\'</code>.</p>
+ *
+ * @param str the <code>String</code> to unescape, may be null
+ * @return a new unescaped <code>String</code>, <code>null</code> if null string input
+ */
+ public static String unescapeJava(String str) {
+ if (str == null) {
+ return null;
+ }
+ try {
+ StringPrintWriter writer = new StringPrintWriter(str.length());
+ unescapeJava(writer, str);
+ return writer.getString();
+ } catch (IOException ioe) {
+ // this should never ever happen while writing to a StringWriter
+ ioe.printStackTrace();
+ return null;
+ }
+ }
+
+ /**
+ * <p>Unescapes any Java literals found in the <code>String</code> to a
+ * <code>Writer</code>.</p>
+ *
+ * <p>For example, it will turn a sequence of <code>'\'</code> and
+ * <code>'n'</code> into a newline character, unless the <code>'\'</code>
+ * is preceded by another <code>'\'</code>.</p>
+ *
+ * <p>A <code>null</code> string input has no effect.</p>
+ *
+ * @param out the <code>Writer</code> used to output unescaped characters
+ * @param str the <code>String</code> to unescape, may be null
+ * @throws IllegalArgumentException if the Writer is <code>null</code>
+ * @throws IOException if error occurs on underlying Writer
+ */
+ public static void unescapeJava(Writer out, String str) throws IOException {
+ if (out == null) {
+ throw new IllegalArgumentException("The Writer must not be null");
+ }
+ if (str == null) {
+ return;
+ }
+ int sz = str.length();
+ StringBuffer unicode = new StringBuffer(4);
+ boolean hadSlash = false;
+ boolean inUnicode = false;
+ for (int i = 0; i < sz; i++) {
+ char ch = str.charAt(i);
+ if (inUnicode) {
+ // if in unicode, then we're reading unicode
+ // values in somehow
+ unicode.append(ch);
+ if (unicode.length() == 4) {
+ // unicode now contains the four hex digits
+ // which represents our unicode character
+ try {
+ int value = Integer.parseInt(unicode.toString(), 16);
+ out.write((char) value);
+ unicode.setLength(0);
+ inUnicode = false;
+ hadSlash = false;
+ } catch (NumberFormatException nfe) {
+ throw new NestableRuntimeException("Unable to parse unicode value: " + unicode, nfe);
+ }
+ }
+ continue;
+ }
+ if (hadSlash) {
+ // handle an escaped value
+ hadSlash = false;
+ switch (ch) {
+ case '\\':
+ out.write('\\');
+ break;
+ case '\'':
+ out.write('\'');
+ break;
+ case '\"':
+ out.write('"');
+ break;
+ case 'r':
+ out.write('\r');
+ break;
+ case 'f':
+ out.write('\f');
+ break;
+ case 't':
+ out.write('\t');
+ break;
+ case 'n':
+ out.write('\n');
+ break;
+ case 'b':
+ out.write('\b');
+ break;
+ case 'u':
+ {
+ // uh-oh, we're in unicode country....
+ inUnicode = true;
+ break;
+ }
+ default :
+ out.write(ch);
+ break;
+ }
+ continue;
+ } else if (ch == '\\') {
+ hadSlash = true;
+ continue;
+ }
+ out.write(ch);
+ }
+ if (hadSlash) {
+ // then we're in the weird case of a \ at the end of the
+ // string, let's output it anyway.
+ out.write('\\');
+ }
+ }
+
+ /**
+ * <p>Unescapes any JavaScript literals found in the <code>String</code>.</p>
+ *
+ * <p>For example, it will turn a sequence of <code>'\'</code> and <code>'n'</code>
+ * into a newline character, unless the <code>'\'</code> is preceded by another
+ * <code>'\'</code>.</p>
+ *
+ * @see #unescapeJava(String)
+ * @param str the <code>String</code> to unescape, may be null
+ * @return A new unescaped <code>String</code>, <code>null</code> if null string input
+ */
+ public static String unescapeJavaScript(String str) {
+ return unescapeJava(str);
+ }
+
+ /**
+ * <p>Unescapes any JavaScript literals found in the <code>String</code> to a
+ * <code>Writer</code>.</p>
+ *
+ * <p>For example, it will turn a sequence of <code>'\'</code> and <code>'n'</code>
+ * into a newline character, unless the <code>'\'</code> is preceded by another
+ * <code>'\'</code>.</p>
+ *
+ * <p>A <code>null</code> string input has no effect.</p>
+ *
+ * @see #unescapeJava(Writer,String)
+ * @param out the <code>Writer</code> used to output unescaped characters
+ * @param str the <code>String</code> to unescape, may be null
+ * @throws IllegalArgumentException if the Writer is <code>null</code>
+ * @throws IOException if error occurs on underlying Writer
+ */
+ public static void unescapeJavaScript(Writer out, String str) throws IOException {
+ unescapeJava(out, str);
+ }
+
+ // HTML and XML
+ //--------------------------------------------------------------------------
+ /**
+ * <p>Escapes the characters in a <code>String</code> using HTML entities.</p>
+ *
+ * <p>
+ * For example:
+ * </p>
+ * <p><code>"bread" & "butter"</code></p>
+ * becomes:
+ * <p>
+ * <code>&quot;bread&quot; &amp; &quot;butter&quot;</code>.
+ * </p>
+ *
+ * <p>Supports all known HTML 4.0 entities, including funky accents.</p>
+ *
+ * @param str the <code>String</code> to escape, may be null
+ * @return a new escaped <code>String</code>, <code>null</code> if null string input
+ *
+ * @see #unescapeHtml(String)
+ * @see </br><a href="http://hotwired.lycos.com/webmonkey/reference/special_characters/">ISO Entities</a>
+ * @see </br><a href="http://www.w3.org/TR/REC-html32#latin1">HTML 3.2 Character Entities for ISO Latin-1</a>
+ * @see </br><a href="http://www.w3.org/TR/REC-html40/sgml/entities.html">HTML 4.0 Character entity references</a>
+ * @see </br><a href="http://www.w3.org/TR/html401/charset.html#h-5.3">HTML 4.01 Character References</a>
+ * @see </br><a href="http://www.w3.org/TR/html401/charset.html#code-position">HTML 4.01 Code positions</a>
+ **/
+ public static String escapeHtml(String str) {
+ if (str == null) {
+ return null;
+ }
+ //todo: add a version that takes a Writer
+ //todo: rewrite underlying method to use a Writer instead of a StringBuffer
+ return Entities.HTML40.escape(str);
+ }
+
+ /**
+ * <p>Unescapes a string containing entity escapes to a string
+ * containing the actual Unicode characters corresponding to the
+ * escapes. Supports HTML 4.0 entities.</p>
+ *
+ * <p>For example, the string "&lt;Fran&ccedil;ais&gt;"
+ * will become "<Français>"</p>
+ *
+ * <p>If an entity is unrecognized, it is left alone, and inserted
+ * verbatim into the result string. e.g. "&gt;&zzzz;x" will
+ * become ">&zzzz;x".</p>
+ *
+ * @param str the <code>String</code> to unescape, may be null
+ * @return a new unescaped <code>String</code>, <code>null</code> if null string input
+ * @see #escapeHtml(String)
+ **/
+ public static String unescapeHtml(String str) {
+ if (str == null) {
+ return null;
+ }
+ return Entities.HTML40.unescape(str);
+ }
+
+ /**
+ * <p>Escapes the characters in a <code>String</code> using XML entities.</p>
+ *
+ * <p>For example: <tt>"bread" & "butter"</tt> =>
+ * <tt>&quot;bread&quot; &amp; &quot;butter&quot;</tt>.
+ * </p>
+ *
+ * <p>Supports only the five basic XML entities (gt, lt, quot, amp, apos).
+ * Does not support DTDs or external entities.</p>
+ *
+ * @param str the <code>String</code> to escape, may be null
+ * @return a new escaped <code>String</code>, <code>null</code> if null string input
+ * @see #unescapeXml(java.lang.String)
+ **/
+ public static String escapeXml(String str) {
+ if (str == null) {
+ return null;
+ }
+ return Entities.XML.escape(str);
+ }
+
+ /**
+ * <p>Unescapes a string containing XML entity escapes to a string
+ * containing the actual Unicode characters corresponding to the
+ * escapes.</p>
+ *
+ * <p>Supports only the five basic XML entities (gt, lt, quot, amp, apos).
+ * Does not support DTDs or external entities.</p>
+ *
+ * @param str the <code>String</code> to unescape, may be null
+ * @return a new unescaped <code>String</code>, <code>null</code> if null string input
+ * @see #escapeXml(String)
+ **/
+ public static String unescapeXml(String str) {
+ if (str == null) {
+ return null;
+ }
+ return Entities.XML.unescape(str);
+ }
+
+ /**
+ * <p>Escapes the characters in a <code>String</code> to be suitable to pass to
+ * an SQL query.</p>
+ *
+ * <p>For example,
+ * <pre>statement.executeQuery("SELECT * FROM MOVIES WHERE TITLE='" +
+ * StringEscapeUtils.escapeSql("McHale's Navy") +
+ * "'");</pre>
+ * </p>
+ *
+ * <p>At present, this method only turns single-quotes into doubled single-quotes
+ * (<code>"McHale's Navy"</code> => <code>"McHale''s Navy"</code>). It does not
+ * handle the cases of percent (%) or underscore (_) for use in LIKE clauses.</p>
+ *
+ * see http://www.jguru.com/faq/view.jsp?EID=8881
+ * @param str the string to escape, may be null
+ * @return a new String, escaped for SQL, <code>null</code> if null string input
+ */
+ public static String escapeSql(String str) {
+ if (str == null) {
+ return null;
+ }
+ return StringUtils.replace(str, "'", "''");
+ }
+
+}
+
Propchange: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/StringEscapeUtils.java
------------------------------------------------------------------------------
svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision
Added: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/StringPrintWriter.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/StringPrintWriter.java?rev=370807&view=auto
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/StringPrintWriter.java (added)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/StringPrintWriter.java Fri Jan 20 05:47:50 2006
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2002-2005 The Apache Software Foundation.
+ *
+ * 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.
+ */
+package org.apache.mina.common.support.lang;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+/**
+ * <p>A PrintWriter that maintains a String as its backing store.</p>
+ *
+ * <p>Usage:
+ * <pre>
+ * StringPrintWriter out = new StringPrintWriter();
+ * printTo(out);
+ * System.out.println( out.getString() );
+ * </pre>
+ * </p>
+ *
+ * @author Alex Chaffee
+ * @author Scott Stanchfield
+ * @author Gary D. Gregory
+ * @since 2.0
+ */
+class StringPrintWriter extends PrintWriter {
+
+ /**
+ * Constructs a new instance.
+ */
+ public StringPrintWriter() {
+ super(new StringWriter());
+ }
+
+ /**
+ * Constructs a new instance using the specified initial string-buffer
+ * size.
+ *
+ * @param initialSize an int specifying the initial size of the buffer.
+ */
+ public StringPrintWriter(int initialSize) {
+ super(new StringWriter(initialSize));
+ }
+
+ /**
+ * <p>Since toString() returns information *about* this object, we
+ * want a separate method to extract just the contents of the
+ * internal buffer as a String.</p>
+ *
+ * @return the contents of the internal string buffer
+ */
+ public String getString() {
+ flush();
+ return ((StringWriter) this.out).toString();
+ }
+
+}
+
Propchange: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/StringPrintWriter.java
------------------------------------------------------------------------------
svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision