summaryrefslogtreecommitdiff
path: root/gnu/xml/transform/StreamSerializer.java
diff options
context:
space:
mode:
authorChris Burdess <dog@bluezoo.org>2005-05-05 20:09:01 +0000
committerChris Burdess <dog@bluezoo.org>2005-05-05 20:09:01 +0000
commit498e2e3ac1592116d99fe992da12aae7b9bcf654 (patch)
tree1b2163353204b439e583bbd34a28dca0ad279b3e /gnu/xml/transform/StreamSerializer.java
parent361bf9a3ea36e66ad36d27b3da1773faf829d6bb (diff)
downloadclasspath-498e2e3ac1592116d99fe992da12aae7b9bcf654.tar.gz
2005-05-05 Chris Burdess <dog@gnu.org>
* gnu/xml/transform/StreamSerializer.java: Produce compact, human-readable XML for non-UTF/ASCII encodings using NIO.
Diffstat (limited to 'gnu/xml/transform/StreamSerializer.java')
-rw-r--r--gnu/xml/transform/StreamSerializer.java66
1 files changed, 34 insertions, 32 deletions
diff --git a/gnu/xml/transform/StreamSerializer.java b/gnu/xml/transform/StreamSerializer.java
index 136105a4e..bc018b796 100644
--- a/gnu/xml/transform/StreamSerializer.java
+++ b/gnu/xml/transform/StreamSerializer.java
@@ -41,6 +41,10 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetEncoder;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
@@ -68,8 +72,9 @@ public class StreamSerializer
static final int KET = 0x3e; // >
static final int EQ = 0x3d; // =
- protected String encoding;
- boolean compatibilityMode;
+ protected final String encoding;
+ final Charset charset;
+ final CharsetEncoder encoder;
final int mode;
final Map namespaces;
protected String eol;
@@ -96,16 +101,8 @@ public class StreamSerializer
encoding = "UTF-8";
}
this.encoding = encoding.intern();
- compatibilityMode = true;
- if (encoding.length() > 3)
- {
- String p = encoding.substring(0, 3);
- if (p.equalsIgnoreCase("UTF") ||
- p.equalsIgnoreCase("UCS"))
- {
- compatibilityMode = false;
- }
- }
+ charset = Charset.forName(this.encoding);
+ encoder = charset.newEncoder();
this.eol = (eol != null) ? eol : System.getProperty("line.separator");
namespaces = new HashMap();
}
@@ -498,37 +495,42 @@ public class StreamSerializer
}
final byte[] encodeText(String text)
- throws UnsupportedEncodingException
+ throws IOException
{
- if (compatibilityMode)
+ encoder.reset();
+ if (!encoder.canEncode(text))
{
+ // Check each character
+ StringBuffer buf = new StringBuffer();
int len = text.length();
- StringBuffer buf = null;
for (int i = 0; i < len; i++)
{
char c = text.charAt(i);
- if (c >= 127)
+ if (encoder.canEncode(c))
{
- if (buf == null)
- {
- buf = new StringBuffer(text.substring(0, i));
- }
- buf.append('&');
- buf.append('#');
- buf.append((int) c);
- buf.append(';');
+ buf.append(c);
}
- else if (buf != null)
+ else
{
- buf.append(c);
+ // Replace with character entity reference
+ String hex = Integer.toHexString((int) c);
+ buf.append("&#x");
+ buf.append(hex);
+ buf.append(';');
}
}
- if (buf != null)
- {
- text = buf.toString();
- }
+ text = buf.toString();
+ }
+ ByteBuffer encoded = encoder.encode(CharBuffer.wrap(text));
+ if (encoded.hasArray())
+ {
+ return encoded.array();
}
- return text.getBytes(encoding);
+ encoded.flip();
+ int len = encoded.limit() - encoded.position();
+ byte[] ret = new byte[len];
+ encoded.get(ret, 0, len);
+ return ret;
}
String encode(String text, boolean encodeCtl, boolean inAttr)
@@ -628,5 +630,5 @@ public class StreamSerializer
throw new RuntimeException(e.getMessage());
}
}
-
+
}