Author: julien(a)jboss.com
Date: 2007-08-28 05:26:34 -0400 (Tue, 28 Aug 2007)
New Revision: 8064
Added:
modules/common/trunk/common/src/main/org/jboss/portal/common/io/WriterCharWriter.java
modules/common/trunk/common/src/main/org/jboss/portal/common/text/AbstractCharEncoder.java
modules/common/trunk/common/src/main/org/jboss/portal/common/text/CharEncoder.java
modules/common/trunk/common/src/main/org/jboss/portal/common/text/CharToByteEncoder.java
modules/common/trunk/common/src/main/org/jboss/portal/common/text/CharWriter.java
modules/common/trunk/common/src/main/org/jboss/portal/common/text/EncodingException.java
modules/common/trunk/common/src/main/org/jboss/portal/common/text/FastURLDecoder.java
modules/common/trunk/common/src/main/org/jboss/portal/common/text/MalformedInputException.java
modules/common/trunk/common/src/main/org/jboss/portal/common/text/TextTools.java
modules/common/trunk/common/src/main/org/jboss/portal/common/text/UnsupportedCharsetException.java
modules/common/trunk/common/src/main/org/jboss/portal/test/common/io/WriterCharWriterTestCase.java
modules/common/trunk/common/src/main/org/jboss/portal/test/common/text/
modules/common/trunk/common/src/main/org/jboss/portal/test/common/text/CharBufferTestCase.java
modules/common/trunk/common/src/main/org/jboss/portal/test/common/text/CharToByteEncoderTestCase.java
modules/common/trunk/common/src/main/org/jboss/portal/test/common/text/FastURLDecoderTestCase.java
modules/common/trunk/common/src/main/org/jboss/portal/test/common/text/FastURLEncoderTestCase.java
modules/common/trunk/common/src/main/org/jboss/portal/test/common/text/TextToolsTestCase.java
Removed:
modules/common/trunk/common/src/main/org/jboss/portal/test/common/CharBufferTestCase.java
modules/common/trunk/common/src/main/org/jboss/portal/test/common/FastURLEncoderTestCase.java
Modified:
modules/common/trunk/common/build.xml
modules/common/trunk/common/src/main/org/jboss/portal/common/invocation/InterceptorStack.java
modules/common/trunk/common/src/main/org/jboss/portal/common/markup/MarkupAttribute.java
modules/common/trunk/common/src/main/org/jboss/portal/common/text/CharBuffer.java
modules/common/trunk/common/src/main/org/jboss/portal/common/text/FastURLEncoder.java
Log:
- refactored and improved the common.text package
- reimplemented FastURLEncoder
- implemented FastURLDecoder
Modified: modules/common/trunk/common/build.xml
===================================================================
--- modules/common/trunk/common/build.xml 2007-08-28 09:16:47 UTC (rev 8063)
+++ modules/common/trunk/common/build.xml 2007-08-28 09:26:34 UTC (rev 8064)
@@ -211,9 +211,15 @@
</x-sysproperty>
<x-test>
+ <test todir="${test.reports}"
name="org.jboss.portal.test.common.text.FastURLDecoderTestCase"/>
+ <test todir="${test.reports}"
name="org.jboss.portal.test.common.text.CharBufferTestCase"/>
+ <test todir="${test.reports}"
name="org.jboss.portal.test.common.text.TextToolsTestCase"/>
+ <test todir="${test.reports}"
name="org.jboss.portal.test.common.text.FastURLEncoderTestCase"/>
+ <test todir="${test.reports}"
name="org.jboss.portal.test.common.text.CharToByteEncoderTestCase"/>
<test todir="${test.reports}"
name="org.jboss.portal.test.common.net.URLNavigatorTestCase"/>
<test todir="${test.reports}"
name="org.jboss.portal.test.common.net.URLToolsTestCase"/>
<test todir="${test.reports}"
name="org.jboss.portal.test.common.io.IOToolsTestCase"/>
+ <test todir="${test.reports}"
name="org.jboss.portal.test.common.io.WriterCharWriterTestCase"/>
<test todir="${test.reports}"
name="org.jboss.portal.test.common.reflect.ReflectionTestCase"/>
<test todir="${test.reports}"
name="org.jboss.portal.test.common.reflect.ModifierTestCase"/>
<test todir="${test.reports}"
name="org.jboss.portal.test.common.adapter.AdapterTestCase"/>
@@ -223,7 +229,6 @@
<test todir="${test.reports}"
name="org.jboss.portal.test.common.ObjectLoaderTestCase"/>
<test todir="${test.reports}"
name="org.jboss.portal.test.common.MarkupTestCase"/>
<test todir="${test.reports}"
name="org.jboss.portal.test.common.TypedMapTestCase"/>
- <test todir="${test.reports}"
name="org.jboss.portal.test.common.FastURLEncoderTestCase"/>
<test todir="${test.reports}"
name="org.jboss.portal.test.common.AbstractInvocationContextTestCase"/>
<test todir="${test.reports}"
name="org.jboss.portal.test.common.LocaleFormatTestCase"/>
<test todir="${test.reports}"
name="org.jboss.portal.test.common.ParentChildResourceBundleTestCase"/>
@@ -240,7 +245,6 @@
<test todir="${test.reports}"
name="org.jboss.portal.test.common.LocalizedStringTestCase"/>
<test todir="${test.reports}"
name="org.jboss.portal.test.common.ImplodeTestCase"/>
<test todir="${test.reports}"
name="org.jboss.portal.test.common.BufferedStreamTestCase"/>
- <test todir="${test.reports}"
name="org.jboss.portal.test.common.CharBufferTestCase"/>
<test todir="${test.reports}"
name="org.jboss.portal.test.common.CopyOnWriteRegistryTestCase"/>
<test todir="${test.reports}"
name="org.jboss.portal.test.common.ToolsTestCase"/>
<test todir="${test.reports}"
name="org.jboss.portal.test.common.IteratorStatusTestCase"/>
Modified:
modules/common/trunk/common/src/main/org/jboss/portal/common/invocation/InterceptorStack.java
===================================================================
---
modules/common/trunk/common/src/main/org/jboss/portal/common/invocation/InterceptorStack.java 2007-08-28
09:16:47 UTC (rev 8063)
+++
modules/common/trunk/common/src/main/org/jboss/portal/common/invocation/InterceptorStack.java 2007-08-28
09:26:34 UTC (rev 8064)
@@ -31,7 +31,7 @@
public interface InterceptorStack
{
/**
- * Returns the stack length.
+ * Returns the stack length.
*
* @return the length
*/
Added:
modules/common/trunk/common/src/main/org/jboss/portal/common/io/WriterCharWriter.java
===================================================================
--- modules/common/trunk/common/src/main/org/jboss/portal/common/io/WriterCharWriter.java
(rev 0)
+++
modules/common/trunk/common/src/main/org/jboss/portal/common/io/WriterCharWriter.java 2007-08-28
09:26:34 UTC (rev 8064)
@@ -0,0 +1,164 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This 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.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software 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 software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.common.io;
+
+import org.jboss.portal.common.text.CharWriter;
+import org.jboss.portal.common.io.UndeclaredIOException;
+
+import java.io.Writer;
+import java.io.IOException;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class WriterCharWriter implements CharWriter
+{
+
+ /** . */
+ private final Writer writer;
+
+ /**
+ * @param writer the target writer
+ * @throws IllegalArgumentException if the writer is null
+ */
+ public WriterCharWriter(Writer writer) throws IllegalArgumentException
+ {
+ if (writer == null)
+ {
+ throw new IllegalArgumentException("No null writer accepted");
+ }
+ this.writer = writer;
+ }
+
+ public CharWriter append(char c) throws UndeclaredIOException
+ {
+ try
+ {
+ writer.write(c);
+ }
+ catch (IOException e)
+ {
+ throw new UndeclaredIOException(e);
+ }
+ return this;
+ }
+
+ public CharWriter append(char[] chars, int off, int len) throws UndeclaredIOException
+ {
+ if (chars == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (off < 0)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (len < 0)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (off + len > chars.length)
+ {
+ throw new IllegalArgumentException();
+ }
+ try
+ {
+ writer.write(chars, off, len);
+ }
+ catch (IOException e)
+ {
+ throw new UndeclaredIOException(e);
+ }
+ return this;
+ }
+
+ public CharWriter append(char[] chars) throws UndeclaredIOException
+ {
+ if (chars == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ try
+ {
+ writer.write(chars, 0, chars.length);
+ }
+ catch (IOException e)
+ {
+ throw new UndeclaredIOException(e);
+ }
+ return this;
+ }
+
+ public CharWriter append(CharSequence s) throws UndeclaredIOException
+ {
+ if (s == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ try
+ {
+ if (s instanceof String)
+ {
+ writer.write((String)s);
+ }
+ else
+ {
+ for (int i = 0;i < s.length();i++)
+ {
+ char c = s.charAt(i);
+ writer.write(c);
+ }
+ }
+ }
+ catch (IOException e)
+ {
+ throw new UndeclaredIOException(e);
+ }
+ return this;
+ }
+
+ public void flush() throws UndeclaredIOException
+ {
+ try
+ {
+ writer.flush();
+ }
+ catch (IOException e)
+ {
+ throw new UndeclaredIOException(e);
+ }
+ }
+
+ public void close() throws UndeclaredIOException
+ {
+ try
+ {
+ writer.flush();
+ }
+ catch (IOException e)
+ {
+ throw new UndeclaredIOException(e);
+ }
+ }
+}
Modified:
modules/common/trunk/common/src/main/org/jboss/portal/common/markup/MarkupAttribute.java
===================================================================
---
modules/common/trunk/common/src/main/org/jboss/portal/common/markup/MarkupAttribute.java 2007-08-28
09:16:47 UTC (rev 8063)
+++
modules/common/trunk/common/src/main/org/jboss/portal/common/markup/MarkupAttribute.java 2007-08-28
09:26:34 UTC (rev 8064)
@@ -23,11 +23,15 @@
package org.jboss.portal.common.markup;
import org.jboss.portal.common.io.UndeclaredIOException;
+import org.jboss.portal.common.io.WriterCharWriter;
import org.jboss.portal.common.text.FastURLEncoder;
+import org.jboss.portal.common.text.CharEncoder;
+import org.jboss.portal.common.text.AbstractCharEncoder;
+import org.jboss.portal.common.text.CharWriter;
+import org.jboss.portal.common.text.EncodingException;
import java.io.IOException;
import java.io.StringWriter;
-import java.io.UnsupportedEncodingException;
import java.io.Writer;
/**
@@ -143,32 +147,35 @@
/** %ContentType required : CDATA -- media type, as per [RFC2045]. */
public static final Type CONTENT_TYPE = new CDATAType();
+ private static final char[] SLASH_ARRAY = "/".toCharArray();
+
/** %URI : CDATA -- a Uniform Resource Identifier, see [URI]. */
public static final Type URI = new CDATAType()
{
{
- try
+ // Patches the encoder to let '/' not being encoded
+ encoder = new AbstractCharEncoder()
{
- // Patches the encoder to let '/' not being encoded
- encoder = new FastURLEncoder("UTF-8", 0, 512)
+ public void encode(char c, CharWriter writer) throws EncodingException
{
+ if (c == '/')
{
- table[(int)'/'] = "/".toCharArray();
+ writer.append(SLASH_ARRAY);
}
- };
- }
- catch (UnsupportedEncodingException e)
- {
- throw new Error(e);
- }
+ else
+ {
+ FastURLEncoder.getInstance().encode(c, writer);
+ }
+ }
+ };
}
/** Our encoder for URI. */
- final FastURLEncoder encoder;
+ final CharEncoder encoder;
public void encode(String string, Writer writer)
{
- encoder.encode(string, writer);
+ encoder.encode(string, new WriterCharWriter(writer));
}
};
Added:
modules/common/trunk/common/src/main/org/jboss/portal/common/text/AbstractCharEncoder.java
===================================================================
---
modules/common/trunk/common/src/main/org/jboss/portal/common/text/AbstractCharEncoder.java
(rev 0)
+++
modules/common/trunk/common/src/main/org/jboss/portal/common/text/AbstractCharEncoder.java 2007-08-28
09:26:34 UTC (rev 8064)
@@ -0,0 +1,52 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This 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.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software 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 software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.common.text;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public abstract class AbstractCharEncoder implements CharEncoder
+{
+
+ public void encode(char[] chars, int off, int len, CharWriter writer) throws
EncodingException
+ {
+ for (int i = off;i < len;i++)
+ {
+ encode(chars[i], writer);
+ }
+ }
+
+ public void encode(char[] chars, CharWriter writer) throws EncodingException
+ {
+ encode(chars, 0, chars.length, writer);
+ }
+
+ public void encode(CharSequence s, CharWriter writer) throws EncodingException
+ {
+ for (int i = 0;i < s.length();i++)
+ {
+ encode(s.charAt(i), writer);
+ }
+ }
+}
Modified:
modules/common/trunk/common/src/main/org/jboss/portal/common/text/CharBuffer.java
===================================================================
---
modules/common/trunk/common/src/main/org/jboss/portal/common/text/CharBuffer.java 2007-08-28
09:16:47 UTC (rev 8063)
+++
modules/common/trunk/common/src/main/org/jboss/portal/common/text/CharBuffer.java 2007-08-28
09:26:34 UTC (rev 8064)
@@ -22,14 +22,13 @@
******************************************************************************/
package org.jboss.portal.common.text;
-import java.io.UnsupportedEncodingException;
-import java.net.URLEncoder;
-
/**
+ * A simple char buffer.
+ *
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 7228 $
*/
-public class CharBuffer
+public class CharBuffer implements CharWriter
{
/** . */
@@ -38,7 +37,12 @@
/** . */
protected int length;
- public CharBuffer(int size)
+ /**
+ *
+ * @param size the initial size
+ * @throws IllegalArgumentException if the initial size is not a positive value.
+ */
+ public CharBuffer(int size) throws IllegalArgumentException
{
if (size < 0)
{
@@ -54,66 +58,78 @@
this.length = 0;
}
- public CharBuffer append(String s)
+ public CharBuffer append(String s, CharEncoder encoder)
{
if (s == null)
{
throw new IllegalArgumentException();
}
- appendNoCheck(s);
+ if (encoder == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ encoder.encode(s, this);
return this;
}
- public CharBuffer append(String s, FastURLEncoder encoder)
+ public CharWriter append(CharSequence s)
{
if (s == null)
{
throw new IllegalArgumentException();
}
- if (encoder == null)
- {
- throw new IllegalArgumentException();
- }
- for (int i = 0; i < s.length(); i++)
- {
- char c = s.charAt(i);
- if (c >= encoder.from && c < encoder.to)
- {
- appendNoCheck(encoder.table[c - encoder.from]);
- }
- else
- {
- try
- {
- appendNoCheck(URLEncoder.encode(Character.toString(c),
encoder.encoding));
- }
- catch (UnsupportedEncodingException e)
- {
- // That should never happen has the encoding validity is check at
construction time
- throw new Error(e);
- }
- }
- }
+ appendNoCheck(s);
return this;
}
- public CharBuffer append(char[] chars)
+ public CharWriter append(char[] chars, int off, int len)
{
if (chars == null)
{
throw new IllegalArgumentException();
}
- appendNoCheck(chars);
+ if (off < 0)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (off + len > chars.length)
+ {
+ throw new IllegalArgumentException();
+ }
+
+ //
+ appendNoCheck(chars, off, len);
+
+ //
return this;
}
- public CharBuffer append(char c)
+
+ public CharWriter append(char c)
{
ensureCapacity(length + 1);
+
+ //
buffer[length++] = c;
+
+ //
return this;
}
+ public CharWriter append(char[] chars)
+ {
+ if (chars == null)
+ {
+ throw new IllegalArgumentException();
+ }
+
+ //
+ appendNoCheck(chars, 0, chars.length);
+
+ //
+ return this;
+ }
+
public String asString()
{
return new String(buffer, 0, length);
@@ -153,32 +169,44 @@
this.length = 0;
}
- protected final void appendNoCheck(char[] chars)
+ protected final void appendNoCheck(char[] chars, int off, int len)
{
- ensureCapacity(length + chars.length);
- if (chars.length < 10)
+ ensureCapacity(length + len);
+ if (len < 10)
{
- for (int i = 0; i < chars.length; i++)
+ while (off < len)
{
- buffer[length++] = chars[i];
+ buffer[length++] = chars[off++];
}
}
else
{
- System.arraycopy(chars, 0, buffer, length, chars.length);
- length += chars.length;
+ System.arraycopy(chars, off, buffer, length, len);
+ length += len;
}
}
- protected final void appendNoCheck(String s)
+ protected final void appendNoCheck(CharSequence s)
{
ensureCapacity(length + s.length());
- s.getChars(0, s.length(), buffer, length);
- length += s.length();
+
+ //
+ if (s instanceof String)
+ {
+ ((String)s).getChars(0, s.length(), buffer, length);
+ length += s.length();
+ }
+ else
+ {
+ for (int i = 0;i < s.length();i++)
+ {
+ char c = s.charAt(i);
+ buffer[length++] = c;
+ }
+ }
}
- /** Can be subclassed to provide a customized implementation. */
- protected void ensureCapacity(int minimumCapacity)
+ protected final void ensureCapacity(int minimumCapacity)
{
int capacity = buffer.length;
if (capacity < minimumCapacity)
Added: modules/common/trunk/common/src/main/org/jboss/portal/common/text/CharEncoder.java
===================================================================
--- modules/common/trunk/common/src/main/org/jboss/portal/common/text/CharEncoder.java
(rev 0)
+++
modules/common/trunk/common/src/main/org/jboss/portal/common/text/CharEncoder.java 2007-08-28
09:26:34 UTC (rev 8064)
@@ -0,0 +1,35 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This 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.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software 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 software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.common.text;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public interface CharEncoder
+{
+ void encode(char c, CharWriter writer) throws EncodingException;
+ void encode(char[] chars, int off, int len, CharWriter writer) throws
EncodingException;
+ void encode(char[] chars, CharWriter writer) throws EncodingException;
+ void encode(CharSequence s, CharWriter writer) throws EncodingException;
+}
Added:
modules/common/trunk/common/src/main/org/jboss/portal/common/text/CharToByteEncoder.java
===================================================================
---
modules/common/trunk/common/src/main/org/jboss/portal/common/text/CharToByteEncoder.java
(rev 0)
+++
modules/common/trunk/common/src/main/org/jboss/portal/common/text/CharToByteEncoder.java 2007-08-28
09:26:34 UTC (rev 8064)
@@ -0,0 +1,113 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This 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.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software 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 software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.common.text;
+
+import java.nio.charset.CharsetEncoder;
+import java.nio.charset.Charset;
+import java.nio.CharBuffer;
+import java.nio.ByteBuffer;
+
+/**
+ * Defines an interface for encoding a char to a sequence of bytes.
+ *
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public interface CharToByteEncoder
+{
+
+ /**
+ * Encode the specified char. The returned byte array can be used safely until a new
invocation to the same
+ * object is done. If the returned array has a length of zero it means that the char
cannot be encoded.
+ *
+ * @param c the char to encode
+ * @return the encoded char as a byte array
+ * @throws EncodingException
+ */
+ byte[] encode(char c) throws EncodingException;
+
+ /** . */
+ final byte[] EMPTY = new byte[0];
+
+ /**
+ * Generic implementation that works for any charset, based on NIO.
+ */
+ public static class Generic implements CharToByteEncoder
+ {
+
+ /** . */
+ public static final Generic UTF8 = new Generic("UTF8");
+
+ /** . */
+ private final CharsetEncoder encoder;
+
+ /** . */
+ private final java.nio.CharBuffer in;
+
+ /** . */
+ private final ByteBuffer out;
+
+ /** . */
+ private final byte[][] arrays = {new byte[0],new byte[1],new byte[2],new
byte[3],new byte[4], new byte[5]};
+
+ public Generic(Charset charset)
+ {
+ encoder = charset.newEncoder();
+ in = CharBuffer.allocate(1);
+ out = ByteBuffer.allocate(100);
+ }
+
+ public Generic(String encoding)
+ {
+ this(Charset.forName(encoding));
+ }
+
+ public byte[] encode(char c) throws EncodingException
+ {
+ switch(Character.getType(c))
+ {
+ case Character.SURROGATE:
+ case Character.PRIVATE_USE:
+ return EMPTY;
+ default:
+ if (encoder.canEncode(c))
+ {
+ in.rewind();
+ out.rewind();
+ in.put(0, c);
+ encoder.reset();
+ encoder.encode(in, out, true);
+ encoder.flush(out);
+ int length = out.position();
+ byte[] bytes = arrays[length];
+ System.arraycopy(out.array(), 0, bytes, 0, length);
+ return bytes;
+ }
+ else
+ {
+ return EMPTY;
+ }
+ }
+ }
+ }
+}
Added: modules/common/trunk/common/src/main/org/jboss/portal/common/text/CharWriter.java
===================================================================
--- modules/common/trunk/common/src/main/org/jboss/portal/common/text/CharWriter.java
(rev 0)
+++
modules/common/trunk/common/src/main/org/jboss/portal/common/text/CharWriter.java 2007-08-28
09:26:34 UTC (rev 8064)
@@ -0,0 +1,35 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This 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.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software 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 software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.common.text;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public interface CharWriter
+{
+ CharWriter append(char c);
+ CharWriter append(char[] chars, int off, int len);
+ CharWriter append(char[] chars);
+ CharWriter append(CharSequence s);
+}
Added:
modules/common/trunk/common/src/main/org/jboss/portal/common/text/EncodingException.java
===================================================================
---
modules/common/trunk/common/src/main/org/jboss/portal/common/text/EncodingException.java
(rev 0)
+++
modules/common/trunk/common/src/main/org/jboss/portal/common/text/EncodingException.java 2007-08-28
09:26:34 UTC (rev 8064)
@@ -0,0 +1,49 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This 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.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software 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 software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.common.text;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class EncodingException extends RuntimeException
+{
+ public EncodingException()
+ {
+ }
+
+ public EncodingException(String string)
+ {
+ super(string);
+ }
+
+ public EncodingException(String string, Throwable throwable)
+ {
+ super(string, throwable);
+ }
+
+ public EncodingException(Throwable throwable)
+ {
+ super(throwable);
+ }
+}
Added:
modules/common/trunk/common/src/main/org/jboss/portal/common/text/FastURLDecoder.java
===================================================================
--- modules/common/trunk/common/src/main/org/jboss/portal/common/text/FastURLDecoder.java
(rev 0)
+++
modules/common/trunk/common/src/main/org/jboss/portal/common/text/FastURLDecoder.java 2007-08-28
09:26:34 UTC (rev 8064)
@@ -0,0 +1,335 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This 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.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software 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 software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.common.text;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class FastURLDecoder implements CharEncoder
+{
+
+ /** . */
+ private static final FastURLDecoder INSTANCE = new
FastURLDecoder(CharToByteEncoder.Generic.UTF8);
+
+ public static FastURLDecoder getInstance()
+ {
+ return INSTANCE;
+ }
+
+ /** . */
+ private static final int AS_IS = 0;
+
+ /** . */
+ private static final int PLUS = 1;
+
+ /** . */
+ private static final int ESCAPE = 2;
+
+ /** . */
+ private static final int ERROR = 3;
+
+ /** . */
+ private final LookupNonTerm root = new LookupNonTerm();
+
+ /** . */
+ private final int[] decisions;
+
+ /** . */
+ private final int[][] conversions;
+
+ public FastURLDecoder(CharToByteEncoder encoder)
+ {
+ this(encoder, (char)0, (char)0x10FFFD);
+ }
+
+ public FastURLDecoder(CharToByteEncoder encoder, char from, char to)
+ {
+ for (char c = from;c <= to;c++)
+ {
+ byte[] v = encoder.encode(c);
+ LookupNonTerm node = root;
+ int k = v.length;
+ for (int i = 0; i < k; i++)
+ {
+ int index = (int)v[i] & 0xFF;
+ if (i == k - 1)
+ {
+ node.children[index] = new LookupTerm(c);
+ }
+ else
+ {
+ if (node.children[index] == null)
+ {
+ node.children[index] = new LookupNonTerm();
+ }
+ node = (LookupNonTerm)node.children[index];
+ }
+ }
+ }
+
+ //
+ conversions = new int[256][256];
+ for (char i = 0;i < 256;i++)
+ {
+ int x = hexValue(i);
+ for (char j = 0;j < 256;j++)
+ {
+ int y = hexValue(j);
+ if (x != -1 && y != -1)
+ {
+ conversions[i][j] = (x << 4) + y;
+ }
+ else
+ {
+ conversions[i][j] = -1;
+ }
+ }
+ }
+
+
+ //
+ decisions = new int[256];
+ for (int i = 0;i < decisions.length;i++)
+ {
+ if (TextTools.isAlphaNumeric((char)i))
+ {
+ decisions[i] = AS_IS;
+ }
+ else
+ {
+ switch(i)
+ {
+ case '+':
+ decisions[i] = PLUS;
+ break;
+ case '.':
+ case '-':
+ case '*':
+ case '_':
+ decisions[i] = AS_IS;
+ break;
+ case '%':
+ decisions[i] = ESCAPE;
+ break;
+ default:
+ decisions[i] = ERROR;
+ }
+ }
+ }
+ }
+
+ public void encode(char c, CharWriter writer)
+ {
+ encode(new char[]{c}, writer);
+ }
+
+ public void encode(char[] chars, int off, int len, CharWriter writer)
+ {
+ if (chars == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (off < 0)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (len < 0)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (off + len > chars.length)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (writer == null)
+ {
+ throw new IllegalArgumentException();
+ }
+
+ //
+ encodeNoCheck(chars, off, len, writer);
+ }
+
+ public void encode(char[] chars, CharWriter writer)
+ {
+ if (chars == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (writer == null)
+ {
+ throw new IllegalArgumentException();
+ }
+
+ //
+ encodeNoCheck(chars, 0, chars.length, writer);
+ }
+
+ public void encode(CharSequence s, CharWriter writer)
+ {
+ if (s == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (writer == null)
+ {
+ throw new IllegalArgumentException();
+ }
+
+ //
+ if (s instanceof String)
+ {
+ char[] chars = ((String)s).toCharArray();
+ encodeNoCheck(chars, 0, chars.length, writer);
+ }
+ else
+ {
+ char[] chars = new char[s.length()];
+ for (int i = 0;i < s.length();i++)
+ {
+ char c = s.charAt(i);
+ chars[i] = c;
+ }
+ encodeNoCheck(chars, 0, chars.length, writer);
+ }
+ }
+
+ private void encodeNoCheck(char[] chars, int i, int length, CharWriter tmp)
+ {
+ while (i < length)
+ {
+ char c = chars[i++];
+ int decision = c < 256 ? decisions[c] : ERROR;
+ switch(decision)
+ {
+ case AS_IS:
+ tmp.append(c);
+ break;
+ case PLUS:
+ tmp.append(' ');
+ break;
+ case ESCAPE:
+ if (i + 2 > length)
+ {
+ throw new MalformedInputException("Not enough chars to decode an
escaped value length should have been" +
+ (i + 2) + " but is " + length);
+ }
+
+ //
+ int j = i;
+
+ //
+ LookupNonTerm current = root;
+ while (true)
+ {
+ char c1 = chars[j++];
+ char c2 = chars[j++];
+
+ //
+ if (c1 > 255)
+ {
+ throw new MalformedInputException("Input out of the lookup
range (" + c1 + "," + c2 + ")");
+ }
+ if (c2 > 255)
+ {
+ throw new MalformedInputException("Input out of the lookup
range (" + c1 + "," + c2 + ")");
+ }
+
+
+ int z = conversions[c1][c2];
+
+ //
+ if (z == -1)
+ {
+ throw new MalformedInputException("Input out of the lookup
range (" + c1 + "," + c2 + ")");
+ }
+
+ //
+ LookupNode child = current.children[z];
+ if (child instanceof LookupTerm)
+ {
+ LookupTerm term = (LookupTerm)child;
+ tmp.append(term.c);
+ i = j;
+ break;
+ }
+ else
+ {
+ current = (LookupNonTerm)child;
+ }
+
+ //
+ j++;
+ }
+ break;
+ case ERROR:
+ throw new MalformedInputException("Cannot decode char " + c);
+ }
+ }
+ }
+
+ private static class LookupNode
+ {
+ }
+
+ private static class LookupTerm extends LookupNode
+ {
+
+ /** . */
+ private final char c;
+
+ public LookupTerm(char c)
+ {
+ this.c = c;
+ }
+ }
+
+ private static class LookupNonTerm extends LookupNode
+ {
+ private final LookupNode[] children = new LookupNode[256];
+ }
+
+ /**
+ * Returns the hex value of the char c. If the char cannot be converted then -1 is
returned.
+ *
+ * @param c the char to convert
+ * @return the converted hex value
+ */
+ private static int hexValue(char c)
+ {
+ if (c >= 'A' && c <= 'F')
+ {
+ return c - 'A' + 10;
+ }
+ if (c >= 'a' && c <= 'f')
+ {
+ return c - 'a' + 10;
+ }
+ if (c >= '0' && c <= '9')
+ {
+ return c - '0';
+ }
+ return -1;
+ }
+}
Modified:
modules/common/trunk/common/src/main/org/jboss/portal/common/text/FastURLEncoder.java
===================================================================
---
modules/common/trunk/common/src/main/org/jboss/portal/common/text/FastURLEncoder.java 2007-08-28
09:16:47 UTC (rev 8063)
+++
modules/common/trunk/common/src/main/org/jboss/portal/common/text/FastURLEncoder.java 2007-08-28
09:26:34 UTC (rev 8064)
@@ -25,61 +25,183 @@
import org.jboss.portal.common.io.UndeclaredIOException;
import java.io.IOException;
-import java.io.UnsupportedEncodingException;
import java.io.Writer;
-import java.net.URLEncoder;
/**
+ * An implementation based on a table for lookups.
+ *
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 7228 $
*/
-public class FastURLEncoder
+public class FastURLEncoder implements CharEncoder
{
/** . */
- public static final FastURLEncoder DEFAULT_ENCODER = create("UTF-8", 0,
1024);
+ private static final FastURLEncoder DEFAULT_ENCODER = new
FastURLEncoder(CharToByteEncoder.Generic.UTF8);
- /** . */
- protected final String encoding;
+ public static FastURLEncoder getInstance()
+ {
+ return DEFAULT_ENCODER;
+ }
/** . */
- protected final char[][] table;
+ private final char[][] table;
/** . */
- protected final int from;
+ private static final char MAX = (char)0x10FFFD;
- /** . */
- protected final int to;
-
- public FastURLEncoder(String encoding, int from, int to) throws
UnsupportedEncodingException, IllegalArgumentException
+ public FastURLEncoder(CharToByteEncoder encoder) throws IllegalArgumentException
{
- if (encoding == null)
+ if (encoder == null)
{
throw new IllegalArgumentException("No encoding provided");
}
- if (to <= from)
+
+ //
+ this.table = new char[MAX + 1][];
+
+ //
+ for (char c = 0;c <= MAX;c++)
{
- throw new IllegalArgumentException("Bad range [" + from +
"," + to + "[");
+ char[] z;
+ if (TextTools.isAlphaNumeric(c))
+ {
+ z = new char[]{c};
+ }
+ else
+ {
+ switch(c)
+ {
+ case ' ':
+ z = new char[]{'+'};
+ break;
+ case '.':
+ case '-':
+ case '*':
+ case '_':
+ z = new char[]{c};
+ break;
+ default:
+ byte[] v = encoder.encode(c);
+ if (v.length > 0)
+ {
+ z = new char[v.length * 3];
+ int index = 0;
+ for (int i = 0;i < v.length;i++)
+ {
+ byte b = v[i];
+ z[index++] = '%';
+ z[index++] = TextTools.toHex((b & 0xF0) >> 4);
+ z[index++] = TextTools.toHex(b & 0x0F);
+ }
+ }
+ else
+ {
+ z = null;
+ }
+ break;
+ }
+ }
+ table[c] = z;
}
- if (from < 0)
+ }
+
+ public void encode(CharSequence s, CharWriter out) throws IllegalArgumentException
+ {
+ if (s == null)
{
- throw new IllegalArgumentException("Lower bound cannot be negative " +
to);
+ throw new IllegalArgumentException();
}
+ if (out == null)
+ {
+ throw new IllegalArgumentException();
+ }
//
- this.encoding = encoding;
- this.from = from;
- this.to = to;
- this.table = new char[to - from][];
+ for (int i = 0; i < s.length(); i++)
+ {
+ char c = s.charAt(i);
+ char[] z = table[c];
+ if (z == null)
+ {
+ throw new MalformedInputException("Wrong");
+ }
+ out.append(z);
+ }
+ }
+ public void encode(char c, CharWriter writer)
+ {
+ if (writer == null)
+ {
+ throw new IllegalArgumentException();
+ }
+
//
- for (int c = (to - 1); c >= from; c--)
+ char[] z = table[c];
+ if (z == null)
{
- String v = URLEncoder.encode(Character.toString((char)c), encoding);
- table[c - from] = v.toCharArray();
+ throw new MalformedInputException("Wrong");
}
+ writer.append(z);
}
+ public void encode(char[] chars, int off, int len, CharWriter writer)
+ {
+ if (chars == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (off < 0)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (len < 0)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (off + len > chars.length)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (writer == null)
+ {
+ throw new IllegalArgumentException();
+ }
+
+ //
+ encodeNoCheck(chars, off, len, writer);
+ }
+
+ public void encode(char[] chars, CharWriter writer)
+ {
+ if (chars == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (writer == null)
+ {
+ throw new IllegalArgumentException();
+ }
+
+ //
+ encodeNoCheck(chars, 0, chars.length, writer);
+ }
+
+ private void encodeNoCheck(char[] chars, int off, int len, CharWriter writer)
+ {
+ for (int i = off; i < len; i++)
+ {
+ char c = chars[i];
+ char[] z = table[c];
+ if (z == null)
+ {
+ throw new IllegalArgumentException("Wrong");
+ }
+ writer.append(z);
+ }
+ }
+
public String encode(String s) throws IllegalArgumentException
{
StringBuffer tmp = new StringBuffer();
@@ -100,22 +222,12 @@
for (int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);
- if (c >= from && c < to)
+ char[] z = table[c];
+ if (z == null)
{
- out.append(table[c - from]);
+ throw new MalformedInputException("Cannot encode char=" + (int)c);
}
- else
- {
- try
- {
- out.append(URLEncoder.encode(Character.toString(c), encoding));
- }
- catch (UnsupportedEncodingException e)
- {
- // That should never happen has the encoding validity is check at
construction time
- throw new Error(e);
- }
- }
+ out.append(z);
}
}
@@ -134,22 +246,12 @@
for (int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);
- if (c >= from && c < to)
+ char[] z = table[c];
+ if (z == null)
{
- out.write(table[c - from]);
+ throw new MalformedInputException("Wrong");
}
- else
- {
- try
- {
- out.write(URLEncoder.encode(Character.toString(c), encoding));
- }
- catch (UnsupportedEncodingException e)
- {
- // That should never happen has the encoding validity is check at
construction time
- throw new Error(e);
- }
- }
+ out.write(z);
}
}
catch (IOException e)
@@ -160,20 +262,6 @@
public String toString()
{
- return "FastURLEncoder[" + encoding + ",[" + from +
"," + to + "]]";
+ return "FastURLEncoder[" + "" + ",[" + 0 +
"," + MAX + "]]";
}
-
- public static FastURLEncoder create(String encoding, int from, int to) throws
IllegalArgumentException
- {
- try
- {
- return new FastURLEncoder(encoding, from, to);
- }
- catch (UnsupportedEncodingException e)
- {
- IllegalArgumentException iae = new IllegalArgumentException();
- iae.initCause(e);
- throw iae;
- }
- }
}
Added:
modules/common/trunk/common/src/main/org/jboss/portal/common/text/MalformedInputException.java
===================================================================
---
modules/common/trunk/common/src/main/org/jboss/portal/common/text/MalformedInputException.java
(rev 0)
+++
modules/common/trunk/common/src/main/org/jboss/portal/common/text/MalformedInputException.java 2007-08-28
09:26:34 UTC (rev 8064)
@@ -0,0 +1,49 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This 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.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software 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 software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.common.text;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class MalformedInputException extends EncodingException
+{
+ public MalformedInputException()
+ {
+ }
+
+ public MalformedInputException(String string)
+ {
+ super(string);
+ }
+
+ public MalformedInputException(String string, Throwable throwable)
+ {
+ super(string, throwable);
+ }
+
+ public MalformedInputException(Throwable throwable)
+ {
+ super(throwable);
+ }
+}
Added: modules/common/trunk/common/src/main/org/jboss/portal/common/text/TextTools.java
===================================================================
--- modules/common/trunk/common/src/main/org/jboss/portal/common/text/TextTools.java
(rev 0)
+++
modules/common/trunk/common/src/main/org/jboss/portal/common/text/TextTools.java 2007-08-28
09:26:34 UTC (rev 8064)
@@ -0,0 +1,64 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This 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.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software 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 software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.common.text;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class TextTools
+{
+
+ /**
+ * Returns true if the char c is alpha numeric i.e it belongs to one of the following
ranges [0,9], [A,Z] or
+ * [a,z]
+ * @param c the char to test
+ * @return true if c is alpha numeric
+ */
+ public static boolean isAlphaNumeric(char c)
+ {
+ return (c >= '0' && c <= '9') || (c >= 'A'
&& c <= 'Z') || (c >= 'a' && c <= 'z');
+ }
+
+ /**
+ * Returns the hexadecimal value of the provided numeric value.
+ * @param z the numeric value to convert
+ * @return the hexadecimal char
+ * @throws IllegalArgumentException if the value is not between 0 and 15
+ */
+ public static char toHex(int z) throws IllegalArgumentException
+ {
+ if (z >= 0 && z < 10)
+ {
+ return (char)('0' + z);
+ }
+ else if (z >= 10 && z < 16)
+ {
+ return (char)('A' - 10 + z);
+ }
+ else
+ {
+ throw new IllegalArgumentException("Wrong character");
+ }
+ }
+}
Added:
modules/common/trunk/common/src/main/org/jboss/portal/common/text/UnsupportedCharsetException.java
===================================================================
---
modules/common/trunk/common/src/main/org/jboss/portal/common/text/UnsupportedCharsetException.java
(rev 0)
+++
modules/common/trunk/common/src/main/org/jboss/portal/common/text/UnsupportedCharsetException.java 2007-08-28
09:26:34 UTC (rev 8064)
@@ -0,0 +1,49 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This 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.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software 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 software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.common.text;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class UnsupportedCharsetException extends EncodingException
+{
+ public UnsupportedCharsetException()
+ {
+ }
+
+ public UnsupportedCharsetException(String string)
+ {
+ super(string);
+ }
+
+ public UnsupportedCharsetException(String string, Throwable throwable)
+ {
+ super(string, throwable);
+ }
+
+ public UnsupportedCharsetException(Throwable throwable)
+ {
+ super(throwable);
+ }
+}
Deleted:
modules/common/trunk/common/src/main/org/jboss/portal/test/common/CharBufferTestCase.java
===================================================================
---
modules/common/trunk/common/src/main/org/jboss/portal/test/common/CharBufferTestCase.java 2007-08-28
09:16:47 UTC (rev 8063)
+++
modules/common/trunk/common/src/main/org/jboss/portal/test/common/CharBufferTestCase.java 2007-08-28
09:26:34 UTC (rev 8064)
@@ -1,118 +0,0 @@
-/******************************************************************************
- * JBoss, a division of Red Hat *
- * Copyright 2006, Red Hat Middleware, LLC, and individual *
- * contributors as indicated by the @authors tag. See the *
- * copyright.txt in the distribution for a full listing of *
- * individual contributors. *
- * *
- * This 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.1 of *
- * the License, or (at your option) any later version. *
- * *
- * This software 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 software; if not, write to the Free *
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
- ******************************************************************************/
-package org.jboss.portal.test.common;
-
-import junit.framework.TestCase;
-import org.jboss.portal.common.text.CharBuffer;
-import org.jboss.portal.common.text.FastURLEncoder;
-
-/**
- * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
- * @version $Revision: 1.1 $
- */
-public class CharBufferTestCase extends TestCase
-{
-
- private FastURLEncoder encoder = FastURLEncoder.create("utf-8", 0, 512);
-
- public void testUTF8EncodedStringAppend()
- {
- CharBuffer buffer = new CharBuffer(0);
- buffer.append("/ a$\u0400", encoder);
- assertEquals("%2F+a%24%D0%80", buffer.asString());
- }
-
- public void testStringAppend()
- {
- CharBuffer buffer = new CharBuffer(0);
- buffer.append("abc");
- assertEquals("abc", buffer.asString());
- }
-
- public void testCharArrayAppend()
- {
- CharBuffer buffer = new CharBuffer(0);
- buffer.append("abc".toCharArray());
- assertEquals("abc", buffer.asString());
- }
-
- public void testCharsAppend()
- {
- CharBuffer buffer = new CharBuffer(0);
- buffer.append('a').append('b').append('c');
- assertEquals("abc", buffer.asString());
- }
-
- public void testReset()
- {
- CharBuffer buffer = new CharBuffer(0);
- buffer.append("abc".toCharArray());
- assertEquals("abc", buffer.asString());
- buffer.reset();
- buffer.append("def".toCharArray());
- assertEquals("def", buffer.asString());
- }
-
- public void testCharArrayAppendThrowsIAE()
- {
- CharBuffer buffer = new CharBuffer(0);
- try
- {
- buffer.append((char[])null);
- }
- catch (IllegalArgumentException e)
- {
- }
- }
-
- public void testStringAppendThrowsIAE()
- {
- CharBuffer buffer = new CharBuffer(0);
- try
- {
- buffer.append((String)null);
- }
- catch (IllegalArgumentException e)
- {
- }
- }
-
- public void testUTF8EncodedAppendThrowsIAE()
- {
- CharBuffer buffer = new CharBuffer(0);
- try
- {
- buffer.append(null, encoder);
- }
- catch (IllegalArgumentException e)
- {
- }
- try
- {
- buffer.append("abc", null);
- }
- catch (IllegalArgumentException e)
- {
- }
- }
-}
Deleted:
modules/common/trunk/common/src/main/org/jboss/portal/test/common/FastURLEncoderTestCase.java
===================================================================
---
modules/common/trunk/common/src/main/org/jboss/portal/test/common/FastURLEncoderTestCase.java 2007-08-28
09:16:47 UTC (rev 8063)
+++
modules/common/trunk/common/src/main/org/jboss/portal/test/common/FastURLEncoderTestCase.java 2007-08-28
09:26:34 UTC (rev 8064)
@@ -1,177 +0,0 @@
-/******************************************************************************
- * JBoss, a division of Red Hat *
- * Copyright 2006, Red Hat Middleware, LLC, and individual *
- * contributors as indicated by the @authors tag. See the *
- * copyright.txt in the distribution for a full listing of *
- * individual contributors. *
- * *
- * This 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.1 of *
- * the License, or (at your option) any later version. *
- * *
- * This software 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 software; if not, write to the Free *
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
- ******************************************************************************/
-package org.jboss.portal.test.common;
-
-import org.jboss.portal.common.text.FastURLEncoder;
-
-import java.io.UnsupportedEncodingException;
-import java.net.URLEncoder;
-
-import junit.framework.TestCase;
-
-/**
- * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
- * @version $Revision: 1.1 $
- */
-public class FastURLEncoderTestCase extends TestCase
-{
-
- public void testConstructor() throws Exception
- {
- try
- {
- new FastURLEncoder(null, 0, 10);
- fail();
- }
- catch (IllegalArgumentException expected)
- {
- }
- try
- {
- new FastURLEncoder("UTF8", -1, 10);
- fail();
- }
- catch (IllegalArgumentException expected)
- {
- }
- try
- {
- new FastURLEncoder("UTF8", -2, -1);
- fail();
- }
- catch (IllegalArgumentException expected)
- {
- }
- try
- {
- new FastURLEncoder("UTF8", 1, 0);
- fail();
- }
- catch (IllegalArgumentException expected)
- {
- }
- try
- {
- new FastURLEncoder("UTF8", 0, 0);
- fail();
- }
- catch (IllegalArgumentException expected)
- {
- }
- try
- {
- new FastURLEncoder("ABCDEF", 0, 10);
- fail();
- }
- catch (UnsupportedEncodingException expected)
- {
- }
- }
-
- public void testFactory() throws Exception
- {
- try
- {
- FastURLEncoder.create(null, 0, 10);
- fail();
- }
- catch (IllegalArgumentException expected)
- {
- }
- try
- {
- FastURLEncoder.create("UTF8", -1, 10);
- fail();
- }
- catch (IllegalArgumentException expected)
- {
- }
- try
- {
- FastURLEncoder.create("UTF8", -2, -1);
- fail();
- }
- catch (IllegalArgumentException expected)
- {
- }
- try
- {
- FastURLEncoder.create("UTF8", 1, 0);
- fail();
- }
- catch (IllegalArgumentException expected)
- {
- }
- try
- {
- FastURLEncoder.create("UTF8", 0, 0);
- fail();
- }
- catch (IllegalArgumentException expected)
- {
- }
- try
- {
- FastURLEncoder.create("ABCDEF", 0, 10);
- fail();
- }
- catch (IllegalArgumentException expected)
- {
- }
- }
-
- public void testEncodeThrowsIAE() throws Exception
- {
- FastURLEncoder encoder = new FastURLEncoder("UTF8", 0, 256);
- try
- {
- encoder.encode(null, new StringBuffer());
- fail();
- }
- catch (IllegalArgumentException expected)
- {
- }
- try
- {
- encoder.encode("", (StringBuffer)null);
- fail();
- }
- catch (IllegalArgumentException expected)
- {
- }
- }
-
- public void testEncode() throws Exception
- {
- FastURLEncoder encoder = new FastURLEncoder("UTF8", 0, 512);
- StringBuffer tmp = new StringBuffer();
- for (int i = 0;i < 512;i++)
- {
- tmp.append((char)i);
- }
- String s = tmp.toString();
- String u1 = encoder.encode(s);
- String u2 = URLEncoder.encode(s, "UTF8");
- assertEquals(u2, u1);
- }
-}
Added:
modules/common/trunk/common/src/main/org/jboss/portal/test/common/io/WriterCharWriterTestCase.java
===================================================================
---
modules/common/trunk/common/src/main/org/jboss/portal/test/common/io/WriterCharWriterTestCase.java
(rev 0)
+++
modules/common/trunk/common/src/main/org/jboss/portal/test/common/io/WriterCharWriterTestCase.java 2007-08-28
09:26:34 UTC (rev 8064)
@@ -0,0 +1,203 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This 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.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software 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 software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.test.common.io;
+
+import org.jboss.portal.common.io.WriterCharWriter;
+import org.jboss.portal.common.io.UndeclaredIOException;
+
+import java.io.StringWriter;
+import java.io.IOException;
+import java.io.Writer;
+
+import junit.framework.TestCase;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class WriterCharWriterTestCase extends TestCase
+{
+
+ public void testAppend1() throws IOException
+ {
+ StringWriter s = new StringWriter();
+ WriterCharWriter writer = new WriterCharWriter(s);
+ writer.append('a');
+ s.close();
+ assertEquals("a", s.toString());
+ }
+
+ public void testAppend2() throws IOException
+ {
+ StringWriter s = new StringWriter();
+ WriterCharWriter writer = new WriterCharWriter(s);
+ writer.append("abc".toCharArray());
+ s.close();
+ assertEquals("abc", s.toString());
+ }
+
+ public void testAppend3() throws IOException
+ {
+ StringWriter s = new StringWriter();
+ WriterCharWriter writer = new WriterCharWriter(s);
+ writer.append("abcdef".toCharArray(), 1, 3);
+ s.close();
+ assertEquals("bcd", s.toString());
+ }
+
+ public void testAppend4() throws IOException
+ {
+ StringWriter s = new StringWriter();
+ WriterCharWriter writer = new WriterCharWriter(s);
+ writer.append("abc");
+ s.close();
+ assertEquals("abc", s.toString());
+ }
+
+ private Writer failingWriter = new Writer()
+ {
+ public void write(char[] chars, int i, int i1) throws IOException
+ {
+ throw new IOException();
+ }
+
+ public void flush() throws IOException
+ {
+ throw new IOException();
+ }
+
+ public void close() throws IOException
+ {
+ throw new IOException();
+ }
+ };
+
+ public void testWrappedIOException()
+ {
+ WriterCharWriter writer = new WriterCharWriter(failingWriter);
+ try
+ {
+ writer.append('c');
+ fail("Was expecting an UndeclaredIOException");
+ }
+ catch (UndeclaredIOException expected)
+ {
+ }
+ try
+ {
+ writer.append("abc".toCharArray());
+ fail("Was expecting an UndeclaredIOException");
+ }
+ catch (UndeclaredIOException expected)
+ {
+ }
+ try
+ {
+ writer.append("abcdef".toCharArray(), 1, 3);
+ fail("Was expecting an UndeclaredIOException");
+ }
+ catch (UndeclaredIOException expected)
+ {
+ }
+ try
+ {
+ writer.append("abc");
+ fail("Was expecting an UndeclaredIOException");
+ }
+ catch (UndeclaredIOException expected)
+ {
+ }
+ }
+
+ public void testAppendThrowsIAE()
+ {
+ try
+ {
+ new WriterCharWriter(null);
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+
+ //
+ WriterCharWriter writer = new WriterCharWriter(new StringWriter());
+ try
+ {
+ writer.append(new char[10], -1, 0);
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ try
+ {
+ writer.append(new char[10], 5, -1);
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ try
+ {
+ writer.append(new char[10], 15, 0);
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ try
+ {
+ writer.append(new char[10], 5, 6);
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ try
+ {
+ writer.append(null, 0, 5);
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ try
+ {
+ writer.append((char[])null);
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ try
+ {
+ writer.append((CharSequence)null);
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ }
+
+}
Copied:
modules/common/trunk/common/src/main/org/jboss/portal/test/common/text/CharBufferTestCase.java
(from rev 7954,
modules/common/trunk/common/src/main/org/jboss/portal/test/common/CharBufferTestCase.java)
===================================================================
---
modules/common/trunk/common/src/main/org/jboss/portal/test/common/text/CharBufferTestCase.java
(rev 0)
+++
modules/common/trunk/common/src/main/org/jboss/portal/test/common/text/CharBufferTestCase.java 2007-08-28
09:26:34 UTC (rev 8064)
@@ -0,0 +1,118 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This 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.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software 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 software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.test.common.text;
+
+import junit.framework.TestCase;
+import org.jboss.portal.common.text.CharBuffer;
+import org.jboss.portal.common.text.FastURLEncoder;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class CharBufferTestCase extends TestCase
+{
+
+ private FastURLEncoder encoder = FastURLEncoder.getInstance();
+
+ public void testUTF8EncodedStringAppend()
+ {
+ CharBuffer buffer = new CharBuffer(0);
+ buffer.append("/ a$\u0400", encoder);
+ assertEquals("%2F+a%24%D0%80", buffer.asString());
+ }
+
+ public void testStringAppend()
+ {
+ CharBuffer buffer = new CharBuffer(0);
+ buffer.append("abc");
+ assertEquals("abc", buffer.asString());
+ }
+
+ public void testCharArrayAppend()
+ {
+ CharBuffer buffer = new CharBuffer(0);
+ buffer.append("abc".toCharArray());
+ assertEquals("abc", buffer.asString());
+ }
+
+ public void testCharsAppend()
+ {
+ CharBuffer buffer = new CharBuffer(0);
+ buffer.append('a').append('b').append('c');
+ assertEquals("abc", buffer.asString());
+ }
+
+ public void testReset()
+ {
+ CharBuffer buffer = new CharBuffer(0);
+ buffer.append("abc".toCharArray());
+ assertEquals("abc", buffer.asString());
+ buffer.reset();
+ buffer.append("def".toCharArray());
+ assertEquals("def", buffer.asString());
+ }
+
+ public void testCharArrayAppendThrowsIAE()
+ {
+ CharBuffer buffer = new CharBuffer(0);
+ try
+ {
+ buffer.append((char[])null);
+ }
+ catch (IllegalArgumentException e)
+ {
+ }
+ }
+
+ public void testStringAppendThrowsIAE()
+ {
+ CharBuffer buffer = new CharBuffer(0);
+ try
+ {
+ buffer.append((String)null);
+ }
+ catch (IllegalArgumentException e)
+ {
+ }
+ }
+
+ public void testUTF8EncodedAppendThrowsIAE()
+ {
+ CharBuffer buffer = new CharBuffer(0);
+ try
+ {
+ buffer.append(null, encoder);
+ }
+ catch (IllegalArgumentException e)
+ {
+ }
+ try
+ {
+ buffer.append("abc", null);
+ }
+ catch (IllegalArgumentException e)
+ {
+ }
+ }
+}
Added:
modules/common/trunk/common/src/main/org/jboss/portal/test/common/text/CharToByteEncoderTestCase.java
===================================================================
---
modules/common/trunk/common/src/main/org/jboss/portal/test/common/text/CharToByteEncoderTestCase.java
(rev 0)
+++
modules/common/trunk/common/src/main/org/jboss/portal/test/common/text/CharToByteEncoderTestCase.java 2007-08-28
09:26:34 UTC (rev 8064)
@@ -0,0 +1,61 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This 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.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software 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 software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.test.common.text;
+
+import junit.framework.TestCase;
+import org.jboss.portal.common.text.CharToByteEncoder;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class CharToByteEncoderTestCase extends TestCase
+{
+
+ public void testCorrectness()
+ {
+ CharToByteEncoder encoder = new CharToByteEncoder.Generic("UTF8");
+
+ //
+ for (int i = 0;i <= 0x10FFFD;i++)
+ {
+ char c = (char)i;
+ int type = Character.getType(c);
+ byte[] r = encoder.encode(c);
+ if (type == Character.SURROGATE || type == Character.PRIVATE_USE)
+ {
+ if (r.length != 0)
+ {
+ fail("Char " + i + " has length " + r.length);
+ }
+ }
+ else
+ {
+ if (r.length < 1)
+ {
+ fail("Char " + i + " has length " + r.length);
+ }
+ }
+ }
+ }
+}
Added:
modules/common/trunk/common/src/main/org/jboss/portal/test/common/text/FastURLDecoderTestCase.java
===================================================================
---
modules/common/trunk/common/src/main/org/jboss/portal/test/common/text/FastURLDecoderTestCase.java
(rev 0)
+++
modules/common/trunk/common/src/main/org/jboss/portal/test/common/text/FastURLDecoderTestCase.java 2007-08-28
09:26:34 UTC (rev 8064)
@@ -0,0 +1,218 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This 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.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software 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 software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.test.common.text;
+
+import junit.framework.TestCase;
+import org.jboss.portal.common.text.FastURLDecoder;
+import org.jboss.portal.common.text.CharEncoder;
+import org.jboss.portal.common.text.CharBuffer;
+import org.jboss.portal.common.text.MalformedInputException;
+
+import java.net.URLEncoder;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class FastURLDecoderTestCase extends TestCase
+{
+
+ public void testEncodeThrowsIAE() throws Exception
+ {
+ CharEncoder encoder = FastURLDecoder.getInstance();
+ try
+ {
+ encoder.encode(new char[10], -1, 0, new CharBuffer());
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ try
+ {
+ encoder.encode(new char[10], 5, -1, new CharBuffer());
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ try
+ {
+ encoder.encode(new char[10], 15, 0, new CharBuffer());
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ try
+ {
+ encoder.encode(new char[10], 5, 6, new CharBuffer());
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ try
+ {
+ encoder.encode(null, 0, 5, new CharBuffer());
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ try
+ {
+ encoder.encode((char[])null, new CharBuffer());
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ try
+ {
+ encoder.encode((CharSequence)null, new CharBuffer());
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+
+ //
+ try
+ {
+ encoder.encode(new char[10], 0, 10, null);
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ try
+ {
+ encoder.encode(new char[10], null);
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ try
+ {
+ encoder.encode('A', null);
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ try
+ {
+ encoder.encode("abc", null);
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ }
+
+ public void testEncode() throws Exception
+ {
+ FastURLDecoder encoder = FastURLDecoder.getInstance();
+ StringBuffer tmp = new StringBuffer();
+ for (int i = 0;i < 512;i++)
+ {
+ tmp.append((char)i);
+ }
+ CharBuffer out = new CharBuffer();
+ String s = tmp.toString();
+ String t = URLEncoder.encode(s, "UTF8");
+ encoder.encode(t, out);
+ assertEquals(s, out.asString());
+ }
+
+ public void testEncodeMalformedInput()
+ {
+ FastURLDecoder encoder = FastURLDecoder.getInstance();
+ try
+ {
+ encoder.encode("%0T", new CharBuffer());
+ fail("Was expecting a malformed input exception");
+ }
+ catch (MalformedInputException expected)
+ {
+ }
+ try
+ {
+ encoder.encode("%T0", new CharBuffer());
+ fail("Was expecting a malformed input exception");
+ }
+ catch (MalformedInputException expected)
+ {
+ }
+ try
+ {
+ encoder.encode("%0\u0100", new CharBuffer());
+ fail("Was expecting a malformed input exception");
+ }
+ catch (MalformedInputException expected)
+ {
+ }
+ try
+ {
+ encoder.encode("%\u01000", new CharBuffer());
+ fail("Was expecting a malformed input exception");
+ }
+ catch (MalformedInputException expected)
+ {
+ }
+ try
+ {
+ encoder.encode("%0", new CharBuffer());
+ fail("Was expecting a malformed input exception");
+ }
+ catch (MalformedInputException expected)
+ {
+ }
+ try
+ {
+ encoder.encode("%", new CharBuffer());
+ fail("Was expecting a malformed input exception");
+ }
+ catch (MalformedInputException expected)
+ {
+ }
+ try
+ {
+ encoder.encode(";", new CharBuffer());
+ fail("Was expecting a malformed input exception");
+ }
+ catch (MalformedInputException expected)
+ {
+ }
+ try
+ {
+ encoder.encode("\u0100", new CharBuffer());
+ fail("Was expecting a malformed input exception");
+ }
+ catch (MalformedInputException expected)
+ {
+ }
+ }
+}
Copied:
modules/common/trunk/common/src/main/org/jboss/portal/test/common/text/FastURLEncoderTestCase.java
(from rev 7954,
modules/common/trunk/common/src/main/org/jboss/portal/test/common/FastURLEncoderTestCase.java)
===================================================================
---
modules/common/trunk/common/src/main/org/jboss/portal/test/common/text/FastURLEncoderTestCase.java
(rev 0)
+++
modules/common/trunk/common/src/main/org/jboss/portal/test/common/text/FastURLEncoderTestCase.java 2007-08-28
09:26:34 UTC (rev 8064)
@@ -0,0 +1,149 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This 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.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software 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 software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.test.common.text;
+
+import org.jboss.portal.common.text.FastURLEncoder;
+import org.jboss.portal.common.text.CharBuffer;
+import org.jboss.portal.common.text.CharEncoder;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+
+import junit.framework.TestCase;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class FastURLEncoderTestCase extends TestCase
+{
+
+ public void testEncodeThrowsIAE() throws Exception
+ {
+ CharEncoder encoder = FastURLEncoder.getInstance();
+ try
+ {
+ encoder.encode(new char[10], -1, 0, new CharBuffer());
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ try
+ {
+ encoder.encode(new char[10], 5, -1, new CharBuffer());
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ try
+ {
+ encoder.encode(new char[10], 15, 0, new CharBuffer());
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ try
+ {
+ encoder.encode(new char[10], 5, 6, new CharBuffer());
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ try
+ {
+ encoder.encode(null, 0, 5, new CharBuffer());
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ try
+ {
+ encoder.encode((char[])null, new CharBuffer());
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ try
+ {
+ encoder.encode((CharSequence)null, new CharBuffer());
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+
+ //
+ try
+ {
+ encoder.encode(new char[10], 0, 10, null);
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ try
+ {
+ encoder.encode(new char[10], null);
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ try
+ {
+ encoder.encode('A', null);
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ try
+ {
+ encoder.encode("abc", null);
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ }
+
+ public void testEncode() throws Exception
+ {
+ FastURLEncoder encoder = FastURLEncoder.getInstance();
+ StringBuffer tmp = new StringBuffer();
+ for (int i = 0;i < 512;i++)
+ {
+ tmp.append((char)i);
+ }
+ String s = tmp.toString();
+ String u1 = encoder.encode(s);
+ String u2 = URLEncoder.encode(s, "UTF8");
+ assertEquals(u2, u1);
+ }
+}
Added:
modules/common/trunk/common/src/main/org/jboss/portal/test/common/text/TextToolsTestCase.java
===================================================================
---
modules/common/trunk/common/src/main/org/jboss/portal/test/common/text/TextToolsTestCase.java
(rev 0)
+++
modules/common/trunk/common/src/main/org/jboss/portal/test/common/text/TextToolsTestCase.java 2007-08-28
09:26:34 UTC (rev 8064)
@@ -0,0 +1,96 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This 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.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software 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 software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.test.common.text;
+
+import junit.framework.TestCase;
+import org.jboss.portal.common.text.TextTools;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class TextToolsTestCase extends TestCase
+{
+
+ public void testIsAlphaNumeric()
+ {
+ assertForChar(false, 0, '0');
+ assertForChar(true, '0', '9' + 1);
+ assertForChar(false, '9' + 1, 'A');
+ assertForChar(true, 'A', 'Z' + 1);
+ assertForChar(false, 'Z' + 1, 'a');
+ assertForChar(true, 'a', 'z' + 1);
+ assertForChar(false, 'z' + 1, 512);
+ }
+
+ private void assertForChar(boolean result, int from, int to)
+ {
+ while (from < to)
+ {
+ assertEquals(result, TextTools.isAlphaNumeric((char)(from++)));
+ }
+ }
+
+ public void testToHex()
+ {
+ assertEquals('0', TextTools.toHex(0));
+ assertEquals('1', TextTools.toHex(1));
+ assertEquals('2', TextTools.toHex(2));
+ assertEquals('3', TextTools.toHex(3));
+ assertEquals('4', TextTools.toHex(4));
+ assertEquals('5', TextTools.toHex(5));
+ assertEquals('6', TextTools.toHex(6));
+ assertEquals('7', TextTools.toHex(7));
+ assertEquals('8', TextTools.toHex(8));
+ assertEquals('9', TextTools.toHex(9));
+ assertEquals('A', TextTools.toHex(10));
+ assertEquals('B', TextTools.toHex(11));
+ assertEquals('C', TextTools.toHex(12));
+ assertEquals('D', TextTools.toHex(13));
+ assertEquals('E', TextTools.toHex(14));
+ assertEquals('F', TextTools.toHex(15));
+ for (int i = 16;i < 512;i++)
+ {
+ try
+ {
+ TextTools.toHex(i);
+ fail("Was expecting an IAE at index=" + i);
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ }
+ for (int i = -512;i < 0;i++)
+ {
+ try
+ {
+ TextTools.toHex(i);
+ fail("Was expecting an IAE at index=" + i);
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ }
+ }
+
+}