Search in sources :

Example 6 with StreamException

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

the class NamespaceContextPreservationFilterHandler method attributesCompleted.

@Override
public void attributesCompleted() throws StreamException {
    if (!done) {
        try {
            CoreElement current = contextElement;
            while (true) {
                CoreAttribute attr = current.coreGetFirstAttribute();
                while (attr != null) {
                    if (attr instanceof CoreNamespaceDeclaration) {
                        CoreNamespaceDeclaration decl = (CoreNamespaceDeclaration) attr;
                        String prefix = decl.coreGetDeclaredPrefix();
                        if (prefixesAlreadyBound.add(prefix)) {
                            super.processNamespaceDeclaration(prefix, decl.coreGetCharacterData().toString());
                        }
                    }
                    attr = attr.coreGetNextAttribute();
                }
                CoreParentNode parent = current.coreGetParent();
                if (!(parent instanceof CoreElement)) {
                    break;
                }
                current = (CoreElement) parent;
            }
            prefixesAlreadyBound = null;
            done = true;
        } catch (CoreModelException ex) {
            throw new StreamException(ex);
        }
    }
    super.attributesCompleted();
}
Also used : CoreParentNode(org.apache.axiom.core.CoreParentNode) CoreAttribute(org.apache.axiom.core.CoreAttribute) CoreElement(org.apache.axiom.core.CoreElement) CoreModelException(org.apache.axiom.core.CoreModelException) CoreNamespaceDeclaration(org.apache.axiom.core.CoreNamespaceDeclaration) StreamException(org.apache.axiom.core.stream.StreamException)

Example 7 with StreamException

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

the class XMLStreamWriterNamespaceContextProvider method isBound.

/**
     * @param prefix 
     * @param namespace
     * @return true if the prefix is associated with the namespace in the current context
     */
public boolean isBound(String prefix, String namespace) throws StreamException {
    try {
        // of this issue.
        if ("xml".equals(prefix)) {
            return true;
        }
        // NOTE: Calling getNamespaceContext() on many XMLStreamWriter implementations is expensive.
        // Please use other writer methods first.
        // For consistency, convert null arguments.
        // This helps get around the parser implementation differences.
        // In addition, the getPrefix/getNamespace methods cannot be called with null parameters.
        prefix = (prefix == null) ? "" : prefix;
        namespace = (namespace == null) ? "" : namespace;
        if (namespace.length() > 0) {
            // QUALIFIED NAMESPACE
            // Get the namespace associated with the prefix
            String writerPrefix = writer.getPrefix(namespace);
            if (prefix.equals(writerPrefix)) {
                return true;
            }
            // So try getting the namespace as a second step.
            if (writerPrefix != null) {
                NamespaceContext nsContext = writer.getNamespaceContext();
                if (nsContext != null) {
                    String writerNS = nsContext.getNamespaceURI(prefix);
                    return namespace.equals(writerNS);
                }
            }
            return false;
        } else {
            // Neither XML 1.0 nor XML 1.1 allow to associate a prefix with an unqualified name (see also AXIOM-372).
            if (prefix.length() > 0) {
                throw new StreamException("Invalid namespace declaration: Prefixed namespace bindings may not be empty.");
            }
            // protected
            try {
                String writerPrefix = writer.getPrefix("");
                if (writerPrefix != null && writerPrefix.length() == 0) {
                    return true;
                }
            } catch (Throwable t) {
                if (log.isDebugEnabled()) {
                    log.debug("Caught exception from getPrefix(\"\"). Processing continues: " + t);
                }
            }
            // Fallback to using the namespace context
            NamespaceContext nsContext = writer.getNamespaceContext();
            if (nsContext != null) {
                String writerNS = nsContext.getNamespaceURI("");
                if (writerNS != null && writerNS.length() > 0) {
                    return false;
                }
            }
            return true;
        }
    } catch (XMLStreamException ex) {
        throw new StreamException(ex);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) NamespaceContext(javax.xml.namespace.NamespaceContext) StreamException(org.apache.axiom.core.stream.StreamException) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 8 with StreamException

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

the class ContentHandlerXmlHandler method processNamespaceDeclaration.

public void processNamespaceDeclaration(String prefix, String namespaceURI) throws StreamException {
    if (bindings == prefixStack.length) {
        String[] newPrefixStack = new String[prefixStack.length * 2];
        System.arraycopy(prefixStack, 0, newPrefixStack, 0, prefixStack.length);
        prefixStack = newPrefixStack;
    }
    prefixStack[bindings++] = prefix;
    try {
        contentHandler.startPrefixMapping(prefix, namespaceURI);
    } catch (SAXException ex) {
        throw new StreamException(ex);
    }
// TODO: depending on the http://xml.org/sax/features/xmlns-uris feature, we also need to add an attribute
}
Also used : SAXException(org.xml.sax.SAXException) StreamException(org.apache.axiom.core.stream.StreamException)

Example 9 with StreamException

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

the class ContentHandlerXmlHandler method processCharacterData.

public void processCharacterData(Object data, boolean ignorable) throws StreamException {
    try {
        switch(characterDataMode) {
            case PASS_THROUGH:
                if (ignorable) {
                    writeToBuffer(data.toString());
                    contentHandler.ignorableWhitespace(buffer, 0, bufferPos);
                    bufferPos = 0;
                } else if (data instanceof CharacterData) {
                    try {
                        ((CharacterData) data).writeTo(this);
                    } catch (IOException ex) {
                        Throwable cause = ex.getCause();
                        SAXException saxException;
                        if (cause instanceof SAXException) {
                            saxException = (SAXException) cause;
                        } else {
                            saxException = new SAXException(ex);
                        }
                        throw new StreamException(saxException);
                    }
                } else {
                    writeToBuffer(data.toString());
                    contentHandler.characters(buffer, 0, bufferPos);
                    bufferPos = 0;
                }
                break;
            case BUFFER:
                writeToBuffer(data.toString());
                break;
            case ACCUMULATE:
                accumulator.append(data);
                break;
            case SKIP:
        }
    } catch (SAXException ex) {
        throw new StreamException(ex);
    }
}
Also used : CharacterData(org.apache.axiom.core.stream.CharacterData) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) StreamException(org.apache.axiom.core.stream.StreamException)

Example 10 with StreamException

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

the class ContentHandlerXmlHandler method attributesCompleted.

public void attributesCompleted() throws StreamException {
    try {
        contentHandler.startElement(elementURI, elementLocalName, elementQName, attributes);
        elementNameStack.push(elementURI);
        elementNameStack.push(elementLocalName);
        elementNameStack.push(elementQName);
        elementURI = null;
        elementLocalName = null;
        elementQName = null;
        attributes.clear();
    } catch (SAXException ex) {
        throw new StreamException(ex);
    }
}
Also used : SAXException(org.xml.sax.SAXException) 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