Author: ilgrosso
Date: Mon Oct 10 07:28:43 2016
New Revision: 1764023
URL: http://svn.apache.org/viewvc?rev=1764023&view=rev
Log:
[COCOON-2352] Support for Unicode surrogate pairs - This closes #1
Added:
cocoon/branches/BRANCH_2_1_X/src/blocks/serializers/test/
cocoon/branches/BRANCH_2_1_X/src/blocks/serializers/test/org/
cocoon/branches/BRANCH_2_1_X/src/blocks/serializers/test/org/apache/
cocoon/branches/BRANCH_2_1_X/src/blocks/serializers/test/org/apache/cocoon/
cocoon/branches/BRANCH_2_1_X/src/blocks/serializers/test/org/apache/cocoon/components/
cocoon/branches/BRANCH_2_1_X/src/blocks/serializers/test/org/apache/cocoon/components/serializers/
cocoon/branches/BRANCH_2_1_X/src/blocks/serializers/test/org/apache/cocoon/components/serializers/encoding/
cocoon/branches/BRANCH_2_1_X/src/blocks/serializers/test/org/apache/cocoon/components/serializers/encoding/XMLEncoderTestCase.java (with props)
Modified:
cocoon/branches/BRANCH_2_1_X/src/blocks/serializers/java/org/apache/cocoon/components/serializers/EncodingSerializer.java
cocoon/branches/BRANCH_2_1_X/src/blocks/serializers/java/org/apache/cocoon/components/serializers/encoding/XMLEncoder.java
Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/serializers/java/org/apache/cocoon/components/serializers/EncodingSerializer.java
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/src/blocks/serializers/java/org/apache/cocoon/components/serializers/EncodingSerializer.java?rev=1764023&r1=1764022&r2=1764023&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/serializers/java/org/apache/cocoon/components/serializers/EncodingSerializer.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/serializers/java/org/apache/cocoon/components/serializers/EncodingSerializer.java Mon Oct 10 07:28:43 2016
@@ -26,6 +26,7 @@ import org.apache.avalon.excalibur.pool.
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.cocoon.components.serializers.encoding.XMLEncoder;
import org.apache.cocoon.components.serializers.encoding.Charset;
import org.apache.cocoon.components.serializers.encoding.CharsetFactory;
import org.apache.cocoon.components.serializers.encoding.Encoder;
@@ -151,6 +152,7 @@ public abstract class EncodingSerializer
this.locator = null;
this.out = null;
this.prolog = true;
+ if (this.encoder instanceof XMLEncoder) ((XMLEncoder) this.encoder).reset();
}
/**
Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/serializers/java/org/apache/cocoon/components/serializers/encoding/XMLEncoder.java
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/src/blocks/serializers/java/org/apache/cocoon/components/serializers/encoding/XMLEncoder.java?rev=1764023&r1=1764022&r2=1764023&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/serializers/java/org/apache/cocoon/components/serializers/encoding/XMLEncoder.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/serializers/java/org/apache/cocoon/components/serializers/encoding/XMLEncoder.java Mon Oct 10 07:28:43 2016
@@ -31,6 +31,8 @@ public class XMLEncoder extends Compiled
private static final char ENCODE_LT[] = "<".toCharArray();
private static final char ENCODE_GT[] = ">".toCharArray();
+ private Character highSurrogate = null;
+
/**
* Create a new instance of this <code>XMLEncoder</code>.
*/
@@ -48,6 +50,10 @@ public class XMLEncoder extends Compiled
super(name);
}
+ public void reset() {
+ this.highSurrogate = null;
+ }
+
/**
* Return true or false wether this encoding can encode the specified
* character or not.
@@ -86,6 +92,18 @@ public class XMLEncoder extends Compiled
* specified character.
*/
public char[] encode(char c) {
+ if (highSurrogate != null) {
+ if (!Character.isLowSurrogate(c)) {
+ throw new IllegalArgumentException("Expected low surrogate char");
+ }
+ int codePoint = Character.toCodePoint(highSurrogate.charValue(), c);
+ highSurrogate = null;
+ return new char[] {(char) codePoint};
+ } else if (Character.isHighSurrogate(c)) {
+ highSurrogate = Character.valueOf(c);
+ return new char[0];
+ }
+
switch (c) {
case 0x22: return(ENCODE_QUOT); // (") ["]
case 0x26: return(ENCODE_AMP); // (&) [&]
Added: cocoon/branches/BRANCH_2_1_X/src/blocks/serializers/test/org/apache/cocoon/components/serializers/encoding/XMLEncoderTestCase.java
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/src/blocks/serializers/test/org/apache/cocoon/components/serializers/encoding/XMLEncoderTestCase.java?rev=1764023&view=auto
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/serializers/test/org/apache/cocoon/components/serializers/encoding/XMLEncoderTestCase.java (added)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/serializers/test/org/apache/cocoon/components/serializers/encoding/XMLEncoderTestCase.java Mon Oct 10 07:28:43 2016
@@ -0,0 +1,44 @@
+/*
+ * 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.cocoon.components.serializers.encoding;
+
+import junit.framework.TestCase;
+
+import java.util.Arrays;
+
+/**
+ * Test case for the {@link XMLEncoder} class.
+ *
+ * @version $Id$
+ */
+public class XMLEncoderTestCase extends TestCase {
+
+ private XMLEncoder encoder = new XMLEncoder();
+
+ public XMLEncoderTestCase(String name) {
+ super(name);
+ }
+
+ /**
+ * Test COCOON-2352: XMLEncoder doesn't support Unicode surrogate pairs.
+ */
+ public void testEncodingSurrogatePairs() {
+ assertTrue(encoder.encode('\uD83C').length == 0);
+
+ assertTrue(Arrays.equals(new char[] {(char) 127808}, encoder.encode('\uDF40')));
+ }
+}
Propchange: cocoon/branches/BRANCH_2_1_X/src/blocks/serializers/test/org/apache/cocoon/components/serializers/encoding/XMLEncoderTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cocoon/branches/BRANCH_2_1_X/src/blocks/serializers/test/org/apache/cocoon/components/serializers/encoding/XMLEncoderTestCase.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: cocoon/branches/BRANCH_2_1_X/src/blocks/serializers/test/org/apache/cocoon/components/serializers/encoding/XMLEncoderTestCase.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.