Search in sources :

Example 16 with StreamException

use of org.apache.axiom.core.stream.StreamException in project webservices-axiom by apache.

the class Serializer method startDocument.

@Override
public void startDocument(String inputEncoding, String xmlVersion, String xmlEncoding, Boolean standalone) throws StreamException {
    switchContext(TAG);
    try {
        writer.write("<?xml version=\"");
        writer.write(xmlVersion == null ? "1.0" : xmlVersion);
        writer.write('"');
        if (xmlEncoding != null) {
            writer.write(" encoding=\"");
            writer.write(xmlEncoding);
            writer.write('"');
        }
        if (standalone != null) {
            writer.write(" standalone=\"");
            writer.write(standalone ? "yes" : "no");
            writer.write('"');
        }
        writer.write("?>");
    } catch (IOException ex) {
        throw new StreamException(ex);
    }
    switchContext(MIXED_CONTENT);
}
Also used : IOException(java.io.IOException) StreamException(org.apache.axiom.core.stream.StreamException)

Example 17 with StreamException

use of org.apache.axiom.core.stream.StreamException in project webservices-axiom by apache.

the class Serializer method internalEntityDecl.

/**
     * Report an internal entity declaration.
     *
     * <p>Only the effective (first) declaration for each entity
     * will be reported.</p>
     *
     * @param name The name of the entity.  If it is a parameter
     *        entity, the name will begin with '%'.
     * @param value The replacement text of the entity.
     * @exception StreamException The application may raise an exception.
     * @see #externalEntityDecl
     * @see org.xml.sax.DTDHandler#unparsedEntityDecl
     */
public void internalEntityDecl(String name, String value) throws StreamException {
    try {
        DTDprolog();
        outputEntityDecl(name, value);
    } catch (IOException ex) {
        throw new StreamException(ex);
    }
}
Also used : IOException(java.io.IOException) StreamException(org.apache.axiom.core.stream.StreamException)

Example 18 with StreamException

use of org.apache.axiom.core.stream.StreamException 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 19 with StreamException

use of org.apache.axiom.core.stream.StreamException in project webservices-axiom by apache.

the class Serializer method endElement.

@Override
public void endElement() throws StreamException {
    depth--;
    try {
        if (startTagOpen) {
            if (spaceBeforeClose) {
                writer.write(" />");
            } else {
                writer.write("/>");
            }
        } else {
            switchContext(TAG);
            writer.write("</");
            String prefix = elementNameStack[2 * depth];
            if (!prefix.isEmpty()) {
                writer.write(prefix);
                writer.write(':');
            }
            writer.write(elementNameStack[2 * depth + 1]);
            writer.write('>');
            switchContext(MIXED_CONTENT);
        }
    } catch (IOException ex) {
        throw new StreamException(ex);
    }
    startTagOpen = false;
}
Also used : IOException(java.io.IOException) StreamException(org.apache.axiom.core.stream.StreamException)

Example 20 with StreamException

use of org.apache.axiom.core.stream.StreamException in project webservices-axiom by apache.

the class Serializer method writeAttribute.

private void writeAttribute(String prefix, String localName, String value) throws StreamException {
    try {
        writer.write(' ');
        if (!prefix.isEmpty()) {
            writer.write(prefix);
            writer.write(':');
        }
        writer.write(localName);
        writer.write("=\"");
        if (!value.isEmpty()) {
            switchContext(ATTRIBUTE_VALUE);
            characters(value);
            switchContext(TAG);
        }
        writer.write('\"');
    } catch (IOException ex) {
        throw new StreamException(ex);
    }
}
Also used : IOException(java.io.IOException) StreamException(org.apache.axiom.core.stream.StreamException)

Aggregations

StreamException (org.apache.axiom.core.stream.StreamException)38 IOException (java.io.IOException)23 SAXException (org.xml.sax.SAXException)9 XMLStreamException (javax.xml.stream.XMLStreamException)6 CoreModelException (org.apache.axiom.core.CoreModelException)3 XmlHandler (org.apache.axiom.core.stream.XmlHandler)3 CoreElement (org.apache.axiom.core.CoreElement)2 Serializer (org.apache.axiom.core.stream.serializer.Serializer)2 XmlDeclarationRewriterHandler (org.apache.axiom.om.impl.stream.XmlDeclarationRewriterHandler)2 OutputStream (java.io.OutputStream)1 StringWriter (java.io.StringWriter)1 DataHandler (javax.activation.DataHandler)1 NamespaceContext (javax.xml.namespace.NamespaceContext)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1 CoreAttribute (org.apache.axiom.core.CoreAttribute)1 CoreNamespaceDeclaration (org.apache.axiom.core.CoreNamespaceDeclaration)1 CoreParentNode (org.apache.axiom.core.CoreParentNode)1 CharacterData (org.apache.axiom.core.stream.CharacterData)1 DocumentElementExtractingFilterHandler (org.apache.axiom.core.stream.DocumentElementExtractingFilterHandler)1 NamespaceRepairingFilterHandler (org.apache.axiom.core.stream.NamespaceRepairingFilterHandler)1