Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Java string Unicode supplementary plane codepoints #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions build/src/org/jibx/runtime/impl/ISO88591StreamWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,12 @@ protected void defineNamespace(int index, String prefix)
* @throws IOException if error writing to document
*/
protected void writeAttributeText(String text) throws IOException {
int length = text.length();
final int length = text.length();
makeSpace(length * 6);
int fill = m_fillOffset;
for (int i = 0; i < length; i++) {
char chr = text.charAt(i);
int chr;
for (int i = 0; i < length; i += Character.charCount(chr)) {
chr = text.codePointAt(i);
if (chr == '"') {
fill = writeEntity(m_quotEntityBytes, fill);
} else if (chr == '&') {
Expand Down Expand Up @@ -232,11 +233,12 @@ protected void writeAttributeText(String text) throws IOException {
*/
public void writeTextContent(String text) throws IOException {
flagTextContent();
int length = text.length();
final int length = text.length();
makeSpace(length * 5);
int fill = m_fillOffset;
for (int i = 0; i < length; i++) {
char chr = text.charAt(i);
int chr;
for (int i = 0; i < length; i += Character.charCount(chr)) {
chr = text.codePointAt(i);
if (chr == '&') {
fill = writeEntity(m_ampEntityBytes, fill);
} else if (chr == '<') {
Expand Down Expand Up @@ -291,12 +293,13 @@ public void writeTextContent(String text) throws IOException {
*/
public void writeCData(String text) throws IOException {
flagTextContent();
int length = text.length();
final int length = text.length();
makeSpace(length + 12);
int fill = m_fillOffset;
fill = writeEntity(m_cdataStartBytes, fill);
for (int i = 0; i < length; i++) {
char chr = text.charAt(i);
int chr;
for (int i = 0; i < length; i += Character.charCount(chr)) {
chr = text.codePointAt(i);
if (chr == '>' && i > 2 && text.charAt(i-1) == ']' &&
text.charAt(i-2) == ']') {
throw new IOException("Sequence \"]]>\" is not allowed " +
Expand Down
42 changes: 33 additions & 9 deletions build/src/org/jibx/runtime/impl/UTF8StreamWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,12 @@ protected void defineNamespace(int index, String prefix) {
* @throws IOException if error writing to document
*/
protected void writeAttributeText(String text) throws IOException {
int length = text.length();
final int length = text.length();
makeSpace(length * 6);
int fill = m_fillOffset;
for (int i = 0; i < length; i++) {
char chr = text.charAt(i);
int chr;
for (int i = 0; i < length; i += Character.charCount(chr)) {
chr = text.codePointAt(i);
if (chr == '"') {
fill = writeEntity(m_quotEntityBytes, fill);
} else if (chr == '&') {
Expand All @@ -218,6 +219,13 @@ protected void writeAttributeText(String text) throws IOException {
throw new IOException("Illegal character code 0x" +
Integer.toHexString(chr) +
" in attribute value text");
} else if (Character.isSupplementaryCodePoint(chr)) {
m_buffer[fill++] = (byte)(0xF0 + ((chr >> 18)));
m_buffer[fill++] =
(byte)(0x80 + ((chr >> 12) & 0x3F));
m_buffer[fill++] =
(byte)(0x80 + ((chr >> 6) & 0x3F));
m_buffer[fill++] = (byte)(0x80 + (chr & 0x3F));
} else {
m_buffer[fill++] = (byte)(0xE0 + (chr >> 12));
m_buffer[fill++] =
Expand All @@ -244,11 +252,12 @@ protected void writeAttributeText(String text) throws IOException {
*/
public void writeTextContent(String text) throws IOException {
flagTextContent();
int length = text.length();
final int length = text.length();
makeSpace(length * 5);
int fill = m_fillOffset;
for (int i = 0; i < length; i++) {
char chr = text.charAt(i);
int chr;
for (int i = 0; i < length; i += Character.charCount(chr)) {
chr = text.codePointAt(i);
if (chr == '&') {
fill = writeEntity(m_ampEntityBytes, fill);
} else if (chr == '<') {
Expand All @@ -270,6 +279,13 @@ public void writeTextContent(String text) throws IOException {
chr == 0xFFFF || chr > 0x10FFFF)) {
throw new IOException("Illegal character code 0x" +
Integer.toHexString(chr) + " in content text");
} else if (Character.isSupplementaryCodePoint(chr)) {
m_buffer[fill++] = (byte)(0xF0 + ((chr >> 18)));
m_buffer[fill++] =
(byte)(0x80 + ((chr >> 12) & 0x3F));
m_buffer[fill++] =
(byte)(0x80 + ((chr >> 6) & 0x3F));
m_buffer[fill++] = (byte)(0x80 + (chr & 0x3F));
} else {
m_buffer[fill++] = (byte)(0xE0 + (chr >> 12));
m_buffer[fill++] =
Expand All @@ -296,12 +312,13 @@ public void writeTextContent(String text) throws IOException {
*/
public void writeCData(String text) throws IOException {
flagTextContent();
int length = text.length();
final int length = text.length();
makeSpace(length * 3 + 12);
int fill = m_fillOffset;
fill = writeEntity(m_cdataStartBytes, fill);
for (int i = 0; i < length; i++) {
char chr = text.charAt(i);
int chr;
for (int i = 0; i < length; i += Character.charCount(chr)) {
chr = text.codePointAt(i);
if (chr == '>' && i > 2 && text.charAt(i-1) == ']' &&
text.charAt(i-2) == ']') {
throw new IOException("Sequence \"]]>\" is not allowed " +
Expand All @@ -321,6 +338,13 @@ public void writeCData(String text) throws IOException {
throw new IOException("Illegal character code 0x" +
Integer.toHexString(chr) +
" in CDATA section text");
} else if (Character.isSupplementaryCodePoint(chr)) {
m_buffer[fill++] = (byte)(0xF0 + ((chr >> 18)));
m_buffer[fill++] =
(byte)(0x80 + ((chr >> 12) & 0x3F));
m_buffer[fill++] =
(byte)(0x80 + ((chr >> 6) & 0x3F));
m_buffer[fill++] = (byte)(0x80 + (chr & 0x3F));
} else {
m_buffer[fill++] = (byte)(0xE0 + (chr >> 12));
m_buffer[fill++] =
Expand Down