Search in sources :

Example 1 with XmlWriter

use of org.apache.axiom.core.stream.serializer.writer.XmlWriter in project webservices-axiom by apache.

the class Serializer method characters.

/**
     * Receive notification of character data.
     *
     * <p>The Parser will call this method to report each chunk of
     * character data.  SAX parsers may return all contiguous character
     * data in a single chunk, or they may split it into several
     * chunks; however, all of the characters in any single event
     * must come from the same external entity, so that the Locator
     * provides useful information.</p>
     *
     * <p>The application must not attempt to read from the array
     * outside of the specified range.</p>
     *
     * <p>Note that some parsers will report whitespace using the
     * ignorableWhitespace() method rather than this one (validating
     * parsers must do so).</p>
     *
     * @param chars The characters from the XML document.
     * @param start The start position in the array.
     * @param length The number of characters to read from the array.
     * @throws StreamException Any SAX exception, possibly
     *            wrapping another exception.
     * @see #ignorableWhitespace
     * @see org.xml.sax.Locator
     *
     * @throws StreamException
     */
public void characters(char[] chars, int start, int length) throws StreamException {
    // is created if string is empty.	
    if (length == 0)
        return;
    final XmlWriter writer = this.writer;
    final int context = this.context;
    final String illegalCharacterSequence = illegalCharacterSequences[context];
    try {
        int i;
        final int end = start + length;
        // last non-clean character that was processed
        int lastDirtyCharProcessed = start - 1;
        // that was processed
        int matchedIllegalCharacters = this.matchedIllegalCharacters;
        int squareBrackets = this.squareBrackets;
        for (i = start; i < end; i++) {
            char ch = chars[i];
            if (illegalCharacterSequence != null) {
                while (true) {
                    if (ch == illegalCharacterSequence.charAt(matchedIllegalCharacters)) {
                        if (++matchedIllegalCharacters == illegalCharacterSequence.length()) {
                            throw new IllegalCharacterSequenceException("Illegal character sequence \"" + illegalCharacterSequence + "\"");
                        }
                        break;
                    } else if (matchedIllegalCharacters > 0) {
                        int offset = 1;
                        loop: while (offset < matchedIllegalCharacters) {
                            for (int j = 0; j < matchedIllegalCharacters - offset; j++) {
                                if (illegalCharacterSequence.charAt(j) != illegalCharacterSequence.charAt(j + offset)) {
                                    offset++;
                                    continue loop;
                                }
                            }
                            break;
                        }
                        matchedIllegalCharacters -= offset;
                    } else {
                        break;
                    }
                }
            }
            String replacement = null;
            boolean generateCharacterReference = false;
            if (context == MIXED_CONTENT || context == ATTRIBUTE_VALUE) {
                if (ch <= 0x1F) {
                    // common TAB, NEW-LINE, CARRIAGE-RETURN
                    switch(ch) {
                        case 0x09:
                            if (context == ATTRIBUTE_VALUE) {
                                replacement = "&#9;";
                            }
                            break;
                        case 0x0A:
                            if (context == ATTRIBUTE_VALUE) {
                                replacement = "&#10;";
                            }
                            break;
                        case 0x0D:
                            replacement = "&#13;";
                            // Leave whitespace carriage return as a real character
                            break;
                        default:
                            generateCharacterReference = true;
                            break;
                    }
                } else if (ch < 0x7F) {
                    switch(ch) {
                        case '<':
                            replacement = "&lt;";
                            break;
                        case '>':
                            if (context == MIXED_CONTENT && squareBrackets >= 2) {
                                replacement = "&gt;";
                            }
                            break;
                        case '&':
                            replacement = "&amp;";
                            break;
                        case '"':
                            if (context == ATTRIBUTE_VALUE) {
                                replacement = "&quot;";
                            }
                    }
                } else if (ch <= 0x9F) {
                    // Range 0x7F through 0x9F inclusive
                    // More control characters, including NEL (0x85)
                    generateCharacterReference = true;
                } else if (ch == 0x2028) {
                    // LINE SEPARATOR
                    replacement = "&#8232;";
                }
                if (ch == ']') {
                    squareBrackets++;
                } else {
                    squareBrackets = 0;
                }
            }
            int startClean = lastDirtyCharProcessed + 1;
            int lengthClean = i - startClean;
            if (replacement != null || generateCharacterReference || lengthClean == CHUNK_SIZE) {
                if (startClean < i) {
                    writer.write(chars, startClean, lengthClean);
                }
                if (replacement != null) {
                    writer.write(replacement);
                } else if (generateCharacterReference) {
                    writer.writeCharacterReference(ch);
                }
                lastDirtyCharProcessed = i;
            }
        }
        // we've reached the end. Any clean characters at the
        // end of the array than need to be written out?
        int startClean = lastDirtyCharProcessed + 1;
        if (i > startClean) {
            int lengthClean = i - startClean;
            writer.write(chars, startClean, lengthClean);
        }
        this.matchedIllegalCharacters = matchedIllegalCharacters;
        this.squareBrackets = squareBrackets;
    } catch (IOException ex) {
        throw new StreamException(ex);
    }
}
Also used : IOException(java.io.IOException) XmlWriter(org.apache.axiom.core.stream.serializer.writer.XmlWriter) WriterXmlWriter(org.apache.axiom.core.stream.serializer.writer.WriterXmlWriter) StreamException(org.apache.axiom.core.stream.StreamException)

Example 2 with XmlWriter

use of org.apache.axiom.core.stream.serializer.writer.XmlWriter in project webservices-axiom by apache.

the class OutputStreamXmlWriterTest method testUnmappableCharacterToCharacterReference.

@Test
public void testUnmappableCharacterToCharacterReference() throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XmlWriter writer = new OutputStreamXmlWriter(baos, Charset.forName("iso-8859-1"));
    writer.setUnmappableCharacterHandler(UnmappableCharacterHandler.CONVERT_TO_CHARACTER_REFERENCE);
    writer.write("abc€def");
    writer.flushBuffer();
    assertThat(baos.toString("iso-8859-1")).isEqualTo("abc&#8364;def");
}
Also used : OutputStreamXmlWriter(org.apache.axiom.core.stream.serializer.writer.OutputStreamXmlWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) XmlWriter(org.apache.axiom.core.stream.serializer.writer.XmlWriter) OutputStreamXmlWriter(org.apache.axiom.core.stream.serializer.writer.OutputStreamXmlWriter) Test(org.junit.Test)

Aggregations

XmlWriter (org.apache.axiom.core.stream.serializer.writer.XmlWriter)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 StreamException (org.apache.axiom.core.stream.StreamException)1 OutputStreamXmlWriter (org.apache.axiom.core.stream.serializer.writer.OutputStreamXmlWriter)1 WriterXmlWriter (org.apache.axiom.core.stream.serializer.writer.WriterXmlWriter)1 Test (org.junit.Test)1