Search in sources :

Example 6 with SaxException

use of nl.nn.adapterframework.xml.SaxException in project iaf by ibissource.

the class XmlUtils method parseXml.

public static void parseXml(InputSource inputSource, ContentHandler handler, ErrorHandler errorHandler) throws IOException, SAXException {
    XMLReader xmlReader;
    try {
        xmlReader = getXMLReader(null, handler);
        if (errorHandler != null) {
            xmlReader.setErrorHandler(errorHandler);
        }
    } catch (ParserConfigurationException e) {
        throw new SaxException("Cannot configure parser", e);
    }
    xmlReader.parse(inputSource);
}
Also used : SaxException(nl.nn.adapterframework.xml.SaxException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XMLReader(org.xml.sax.XMLReader)

Example 7 with SaxException

use of nl.nn.adapterframework.xml.SaxException in project iaf by ibissource.

the class XsltExceptionTest method testXsltException.

public void testXsltException(boolean expectChildThreads, int tailCount) throws Exception {
    String xpathExpression = "*/*";
    int xsltVersion = 1;
    TransformerPool tp = TransformerPool.configureTransformer0(null, null, null, xpathExpression, null, OutputType.XML, false, null, xsltVersion);
    XmlWriter writer = new XmlWriter();
    FullXmlFilter filter = new FullXmlFilter(writer) {

        @Override
        public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
            if (localName.equals("error")) {
                throw new SaxException("Found error");
            }
            super.startElement(uri, localName, qName, atts);
        }
    };
    try (ThreadConnector threadConnector = expectChildThreads ? new ThreadConnector(null, null, null, (PipeLineSession) null) : null) {
        TransformerFilter transformer = tp.getTransformerFilter(threadConnector, filter);
        try {
            try (SaxDocumentBuilder seb = new SaxDocumentBuilder("root", transformer)) {
                seb.addElement("elem");
                seb.addElement("error");
                for (int i = 0; i < tailCount; i++) {
                    seb.addElement("elem");
                }
            }
            fail("Expected exception to be caught while processing");
        } catch (Exception e) {
            System.out.println("Expected exception: " + e.getMessage());
        }
        System.out.println(writer);
    }
}
Also used : FullXmlFilter(nl.nn.adapterframework.xml.FullXmlFilter) TransformerFilter(nl.nn.adapterframework.xml.TransformerFilter) Attributes(org.xml.sax.Attributes) ThreadConnector(nl.nn.adapterframework.stream.ThreadConnector) SaxDocumentBuilder(nl.nn.adapterframework.xml.SaxDocumentBuilder) PipeLineSession(nl.nn.adapterframework.core.PipeLineSession) SaxException(nl.nn.adapterframework.xml.SaxException) TransformerPool(nl.nn.adapterframework.util.TransformerPool) XmlWriter(nl.nn.adapterframework.xml.XmlWriter) SaxException(nl.nn.adapterframework.xml.SaxException) SAXException(org.xml.sax.SAXException)

Example 8 with SaxException

use of nl.nn.adapterframework.xml.SaxException in project iaf by ibissource.

the class XmlFileElementIteratorPipe method iterateOverInput.

@Override
protected StopReason iterateOverInput(Message input, PipeLineSession session, Map<String, Object> threadContext, ItemCallback callback) throws SenderException, TimeoutException {
    InputStream xmlInput;
    try {
        xmlInput = new FileInputStream(input.asString());
    } catch (IOException e) {
        throw new SenderException("could not find file [" + input + "]", e);
    }
    ItemCallbackCallingHandler handler = new ItemCallbackCallingHandler(callback);
    log.debug("obtaining list of elements [" + getElementName() + "] using sax parser");
    try {
        SAXParserFactory parserFactory = XmlUtils.getSAXParserFactory();
        parserFactory.setNamespaceAware(true);
        SAXParser saxParser = parserFactory.newSAXParser();
        saxParser.parse(xmlInput, handler);
    } catch (Exception e) {
        if (handler.getTimeOutException() != null) {
            throw handler.getTimeOutException();
        }
        if (!handler.isStopRequested()) {
            throw new SenderException("Could not extract list of elements [" + getElementName() + "] using sax parser", e);
        }
        try {
            handler.endDocument();
        } catch (SAXException e1) {
            throw new SenderException("could not endDocument after stop was requested", e1);
        }
    }
    return handler.stopReason;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SAXParser(javax.xml.parsers.SAXParser) IOException(java.io.IOException) SenderException(nl.nn.adapterframework.core.SenderException) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) SaxException(nl.nn.adapterframework.xml.SaxException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) SAXException(org.xml.sax.SAXException) SenderException(nl.nn.adapterframework.core.SenderException) TimeoutException(nl.nn.adapterframework.core.TimeoutException) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 9 with SaxException

use of nl.nn.adapterframework.xml.SaxException in project iaf by ibissource.

the class ForEachChildElementPipe method createHandler.

protected void createHandler(HandlerRecord result, ThreadConnector threadConnector, PipeLineSession session, ItemCallback callback) throws TransformerConfigurationException {
    result.itemHandler = new ItemCallbackCallingHandler(callback);
    result.inputHandler = result.itemHandler;
    if (getXmlDebugger() != null && (StringUtils.isNotEmpty(getContainerElement()) || StringUtils.isNotEmpty(getTargetElement()) || getExtractElementsTp() != null)) {
        String containerElementString = StringUtils.isNotEmpty(getContainerElement()) ? "filter to containerElement '" + getContainerElement() + "'" : null;
        String targetElementString = StringUtils.isNotEmpty(getTargetElement()) ? "filter to targetElement '" + getTargetElement() + "'" : null;
        String xpathString = getExtractElementsTp() != null ? "filter XPath '" + getElementXPathExpression() + "'" : null;
        String label = "XML after preprocessing: " + Misc.concat(", ", containerElementString, targetElementString, xpathString);
        result.inputHandler = getXmlDebugger().inspectXml(session, label, result.inputHandler);
    }
    if (isRemoveNamespaces()) {
        result.inputHandler = new NamespaceRemovingFilter(result.inputHandler);
    }
    if (getExtractElementsTp() != null) {
        if (log.isDebugEnabled())
            log.debug("transforming input to obtain list of elements using xpath [" + getElementXPathExpression() + "]");
        TransformerFilter transformerFilter = getExtractElementsTp().getTransformerFilter(threadConnector, result.inputHandler);
        result.inputHandler = transformerFilter;
        result.transformerErrorListener = (TransformerErrorListener) transformerFilter.getErrorListener();
        result.errorMessage = "Could not process list of elements using xpath [" + getElementXPathExpression() + "]";
    }
    if (StringUtils.isNotEmpty(getTargetElement())) {
        result.inputHandler = new NodeSetFilter(XmlUtils.getNamespaceMap(getNamespaceDefs()), getTargetElement(), true, true, result.inputHandler);
    }
    if (StringUtils.isNotEmpty(getContainerElement())) {
        result.inputHandler = new NodeSetFilter(XmlUtils.getNamespaceMap(getNamespaceDefs()), getContainerElement(), false, true, result.inputHandler);
    }
    result.inputHandler = new StopSensor(result.itemHandler, result.inputHandler);
    result.inputHandler = new ExceptionCatchingFilter(result.inputHandler) {

        @Override
        protected void handleException(Exception e) throws SAXException {
            if (e instanceof SaxTimeoutException) {
                throw (SaxTimeoutException) e;
            }
            if (result.itemHandler.isStopRequested()) {
                if (e instanceof SaxAbortException) {
                    throw (SaxAbortException) e;
                }
                throw new SaxAbortException(e);
            }
            // For improved diagnosability of error situations, rethrow the original exception, where applicable.
            if (result.transformerErrorListener != null) {
                TransformerException tex = result.transformerErrorListener.getFatalTransformerException();
                if (tex != null) {
                    throw new SaxException(result.errorMessage, tex);
                }
                IOException iox = result.transformerErrorListener.getFatalIOException();
                if (iox != null) {
                    throw new SaxException(result.errorMessage, iox);
                }
            }
            throw new SaxException(result.errorMessage, e);
        }
    };
}
Also used : TransformerFilter(nl.nn.adapterframework.xml.TransformerFilter) SaxException(nl.nn.adapterframework.xml.SaxException) IOException(java.io.IOException) SaxTimeoutException(nl.nn.adapterframework.stream.SaxTimeoutException) ExceptionCatchingFilter(nl.nn.adapterframework.xml.ExceptionCatchingFilter) TransformerException(javax.xml.transform.TransformerException) SaxException(nl.nn.adapterframework.xml.SaxException) SaxAbortException(nl.nn.adapterframework.stream.SaxAbortException) TimeoutException(nl.nn.adapterframework.core.TimeoutException) SaxTimeoutException(nl.nn.adapterframework.stream.SaxTimeoutException) PipeStartException(nl.nn.adapterframework.core.PipeStartException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) StreamingException(nl.nn.adapterframework.stream.StreamingException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) SenderException(nl.nn.adapterframework.core.SenderException) NamespaceRemovingFilter(nl.nn.adapterframework.xml.NamespaceRemovingFilter) SAXException(org.xml.sax.SAXException) NodeSetFilter(nl.nn.adapterframework.xml.NodeSetFilter) SaxAbortException(nl.nn.adapterframework.stream.SaxAbortException) TransformerException(javax.xml.transform.TransformerException)

Example 10 with SaxException

use of nl.nn.adapterframework.xml.SaxException in project iaf by ibissource.

the class XmlAligner method startElement.

@Override
public void startElement(String namespaceUri, String localName, String qName, Attributes attributes) throws SAXException {
    if (log.isTraceEnabled())
        log.trace("startElement() uri [" + namespaceUri + "] localName [" + localName + "] qName [" + qName + "]");
    // call getChildElementDeclarations with in startElement, to obtain all child elements of the current node
    typeDefinition = getTypeDefinition(psviProvider);
    if (typeDefinition == null) {
        throw new SaxException("No typeDefinition found for element [" + localName + "] in namespace [" + namespaceUri + "] qName [" + qName + "]");
    }
    multipleOccurringElements.push(multipleOccurringChildElements);
    parentOfSingleMultipleOccurringChildElements.push(parentOfSingleMultipleOccurringChildElement);
    // call findMultipleOccurringChildElements, to obtain all child elements that could be part of an array
    if (typeDefinition instanceof XSComplexTypeDefinition) {
        XSComplexTypeDefinition complexTypeDefinition = (XSComplexTypeDefinition) typeDefinition;
        multipleOccurringChildElements = findMultipleOccurringChildElements(complexTypeDefinition.getParticle());
        parentOfSingleMultipleOccurringChildElement = (ChildOccurrence.ONE_MULTIPLE_OCCURRING_ELEMENT == determineIsParentOfSingleMultipleOccurringChildElement(complexTypeDefinition.getParticle()));
        if (log.isTraceEnabled())
            log.trace("element [" + localName + "] is parentOfSingleMultipleOccurringChildElement [" + parentOfSingleMultipleOccurringChildElement + "]");
    } else {
        multipleOccurringChildElements = null;
        parentOfSingleMultipleOccurringChildElement = false;
        if (log.isTraceEnabled())
            log.trace("element [" + localName + "] is a SimpleType, and therefor not multiple");
    }
    super.startElement(namespaceUri, localName, qName, attributes);
    indentLevel++;
    context = new AlignmentContext(context, namespaceUri, localName, qName, attributes, typeDefinition, indentLevel, multipleOccurringChildElements, parentOfSingleMultipleOccurringChildElement);
}
Also used : SaxException(nl.nn.adapterframework.xml.SaxException) XSComplexTypeDefinition(org.apache.xerces.xs.XSComplexTypeDefinition)

Aggregations

SaxException (nl.nn.adapterframework.xml.SaxException)12 IOException (java.io.IOException)8 SAXException (org.xml.sax.SAXException)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3 XMLReader (org.xml.sax.XMLReader)3 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)2 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)2 SenderException (nl.nn.adapterframework.core.SenderException)2 TimeoutException (nl.nn.adapterframework.core.TimeoutException)2 TransformerFilter (nl.nn.adapterframework.xml.TransformerFilter)2 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 SAXParser (javax.xml.parsers.SAXParser)1 SAXParserFactory (javax.xml.parsers.SAXParserFactory)1 TransformerException (javax.xml.transform.TransformerException)1 PipeLineSession (nl.nn.adapterframework.core.PipeLineSession)1 PipeStartException (nl.nn.adapterframework.core.PipeStartException)1 Resource (nl.nn.adapterframework.core.Resource)1