You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by ve...@apache.org on 2016/05/14 13:31:52 UTC

svn commit: r1743828 - in /webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/stream/serializer: AbstractXmlWriter.java Latin1XmlWriter.java ToStream.java UTF8XmlWriter.java WriterToASCI.java WriterToUTF8Buffered.java

Author: veithen
Date: Sat May 14 13:31:52 2016
New Revision: 1743828

URL: http://svn.apache.org/viewvc?rev=1743828&view=rev
Log:
Rewrite the code that encodes to ASCII and UTF-8.

Added:
    webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/stream/serializer/AbstractXmlWriter.java   (with props)
    webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/stream/serializer/Latin1XmlWriter.java   (with props)
    webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/stream/serializer/UTF8XmlWriter.java   (with props)
Removed:
    webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/stream/serializer/WriterToASCI.java
    webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/stream/serializer/WriterToUTF8Buffered.java
Modified:
    webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/stream/serializer/ToStream.java

Added: webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/stream/serializer/AbstractXmlWriter.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/stream/serializer/AbstractXmlWriter.java?rev=1743828&view=auto
==============================================================================
--- webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/stream/serializer/AbstractXmlWriter.java (added)
+++ webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/stream/serializer/AbstractXmlWriter.java Sat May 14 13:31:52 2016
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.axiom.core.stream.serializer;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+abstract class AbstractXmlWriter extends XmlWriter {
+    private final OutputStream out;
+    private final byte[] buffer = new byte[4096];
+    private int len;
+    private char highSurrogate;
+    
+    AbstractXmlWriter(OutputStream out) {
+        this.out = out;
+    }
+
+    protected abstract void writeCharacter(int codePoint) throws IOException;
+
+    protected final void writeByte(byte b) throws IOException {
+        if (len == buffer.length) {
+            flushBuffer();
+        }
+        buffer[len++] = b;
+    }
+
+    @Override
+    final void write(char c) throws IOException {
+        if (highSurrogate != 0) {
+            if (Character.isLowSurrogate(c)) {
+                writeCharacter(Character.toCodePoint(highSurrogate, c));
+                highSurrogate = 0;
+            } else {
+                throw new IOException("Invalid surrogate pair");
+            }
+        } else if (Character.isLowSurrogate(c)) {
+            throw new IOException("Invalid surrogate pair");
+        } else {
+            writeCharacter(c);
+        }
+    }
+
+    @Override
+    final void write(String s) throws IOException {
+        for (int i=0, len=s.length(); i<len; i++) {
+            write(s.charAt(i));
+        }
+    }
+
+    @Override
+    final void write(char[] chars, int start, int length) throws IOException {
+        for (int i=0; i<length; i++) {
+            write(chars[i]);
+        }
+    }
+
+    @Override
+    final void flushBuffer() throws IOException {
+        out.write(buffer, 0, len);
+        len = 0;
+    }
+}

Propchange: webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/stream/serializer/AbstractXmlWriter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/stream/serializer/Latin1XmlWriter.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/stream/serializer/Latin1XmlWriter.java?rev=1743828&view=auto
==============================================================================
--- webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/stream/serializer/Latin1XmlWriter.java (added)
+++ webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/stream/serializer/Latin1XmlWriter.java Sat May 14 13:31:52 2016
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.axiom.core.stream.serializer;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+final class Latin1XmlWriter extends AbstractXmlWriter {
+    private final int maxChar;
+    private UnmappableCharacterHandler unmappableCharacterHandler = UnmappableCharacterHandler.THROW_EXCEPTION;
+
+    Latin1XmlWriter(OutputStream out, int maxChar) {
+        super(out);
+        this.maxChar = maxChar;
+    }
+
+    @Override
+    void setUnmappableCharacterHandler(UnmappableCharacterHandler unmappableCharacterHandler) throws IOException {
+        this.unmappableCharacterHandler = unmappableCharacterHandler;
+    }
+
+    @Override
+    protected void writeCharacter(int codePoint) throws IOException {
+        if (codePoint > maxChar) {
+            unmappableCharacterHandler.processUnmappableCharacter(codePoint, this);
+        } else {
+            writeByte((byte)codePoint);
+        }
+    }
+}

Propchange: webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/stream/serializer/Latin1XmlWriter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/stream/serializer/ToStream.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/stream/serializer/ToStream.java?rev=1743828&r1=1743827&r2=1743828&view=diff
==============================================================================
--- webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/stream/serializer/ToStream.java (original)
+++ webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/stream/serializer/ToStream.java Sat May 14 13:31:52 2016
@@ -529,13 +529,13 @@ abstract public class ToStream extends S
         {
             // We wrap the OutputStream with a writer, but
             // not one set by the user
-            setWriterInternal(new WriterToUTF8Buffered(output), false);
+            setWriterInternal(new UTF8XmlWriter(output), false);
         } else if (
                 "WINDOWS-1250".equals(encoding)
                 || "US-ASCII".equals(encoding)
                 || "ASCII".equals(encoding))
         {
-            setWriterInternal(new WriterToASCI(output), false);
+            setWriterInternal(new Latin1XmlWriter(output, 127), false);
         } else if (encoding != null) {
             Writer osw = null;
                 try

Added: webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/stream/serializer/UTF8XmlWriter.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/stream/serializer/UTF8XmlWriter.java?rev=1743828&view=auto
==============================================================================
--- webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/stream/serializer/UTF8XmlWriter.java (added)
+++ webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/stream/serializer/UTF8XmlWriter.java Sat May 14 13:31:52 2016
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.axiom.core.stream.serializer;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+final class UTF8XmlWriter extends AbstractXmlWriter {
+    UTF8XmlWriter(OutputStream out) {
+        super(out);
+    }
+
+    @Override
+    void setUnmappableCharacterHandler(UnmappableCharacterHandler unmappableCharacterHandler) {
+        // There are no unmappable characters in UTF-8
+    }
+
+    @Override
+    protected void writeCharacter(int codePoint) throws IOException {
+        if (codePoint < 0x80) {
+            writeByte((byte)codePoint);
+        } else if (codePoint < 0x800) {
+            writeByte((byte)(0xc0 + (codePoint >> 6)));
+            writeByte((byte)(0x80 + (codePoint & 0x3f)));
+        } else if (codePoint < 0x10000) {
+            writeByte((byte)(0xe0 + (codePoint >> 12)));
+            writeByte((byte)(0x80 + ((codePoint >> 6) & 0x3f)));
+            writeByte((byte)(0x80 + (codePoint & 0x3f)));
+        } else {
+            writeByte((byte)(0xf0 + (codePoint >> 18)));
+            writeByte((byte)(0x80 + ((codePoint >> 12) & 0x3f)));
+            writeByte((byte)(0x80 + ((codePoint >> 6) & 0x3f)));
+            writeByte((byte)(0x80 + (codePoint & 0x3f)));
+        }
+    }
+}

Propchange: webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/stream/serializer/UTF8XmlWriter.java
------------------------------------------------------------------------------
    svn:eol-style = native