Search in sources :

Example 1 with SaxDocumentBuilder

use of nl.nn.adapterframework.xml.SaxDocumentBuilder 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 2 with SaxDocumentBuilder

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

the class TextSplitterPipe method doPipe.

@Override
public PipeRunResult doPipe(Message message, PipeLineSession session) throws PipeRunException {
    try {
        String[] result = new String[100];
        int p, s, o = 0;
        String inputString = message.asString();
        if (softSplit) {
            for (p = 0; p < inputString.length() - maxBlockLength; ) {
                // find last space in msg part
                for (s = p + maxBlockLength >= inputString.length() ? inputString.length() - 1 : p + maxBlockLength; s >= p && !Character.isWhitespace(inputString.charAt(s)) && inputString.charAt(s) != '-'; s--) ;
                // now skip spaces
                for (; s >= p && Character.isWhitespace(inputString.charAt(s)); s--) ;
                // spaces found, soft break possible
                if (s >= p) {
                    result[o++] = inputString.substring(p, s + 1);
                    for (p = s + 1; p < inputString.length() && Character.isWhitespace(inputString.charAt(p)); p++) ;
                } else // no space found, soft-break not possible
                {
                    result[o++] = inputString.substring(p, p + maxBlockLength < inputString.length() ? p + maxBlockLength : inputString.length());
                    p += maxBlockLength;
                }
            }
            result[o++] = inputString.substring(p);
        } else {
            for (p = 0; p < inputString.length(); p += maxBlockLength) {
                if (p + maxBlockLength <= inputString.length()) {
                    result[o++] = inputString.substring(p, p + maxBlockLength);
                } else {
                    result[o++] = inputString.substring(p);
                }
            }
        }
        try (MessageOutputStream target = getTargetStream(session)) {
            try (SaxDocumentBuilder saxBuilder = new SaxDocumentBuilder("text", target.asContentHandler())) {
                for (int counter = 0; result[counter] != null; counter++) {
                    saxBuilder.addElement("block", result[counter]);
                }
            }
            return target.getPipeRunResult();
        }
    } catch (Exception e) {
        throw new PipeRunException(this, "Cannot create text blocks", e);
    }
}
Also used : MessageOutputStream(nl.nn.adapterframework.stream.MessageOutputStream) SaxDocumentBuilder(nl.nn.adapterframework.xml.SaxDocumentBuilder) PipeRunException(nl.nn.adapterframework.core.PipeRunException) PipeRunException(nl.nn.adapterframework.core.PipeRunException)

Example 3 with SaxDocumentBuilder

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

the class CsvParserPipe method doPipe.

@Override
public PipeRunResult doPipe(Message message, PipeLineSession session) throws PipeRunException {
    try (MessageOutputStream target = getTargetStream(session)) {
        try (Reader reader = message.asReader()) {
            try (SaxDocumentBuilder document = new SaxDocumentBuilder("csv", target.asContentHandler())) {
                CSVParser csvParser = format.parse(reader);
                for (CSVRecord record : csvParser) {
                    try (SaxElementBuilder element = document.startElement("record")) {
                        for (Entry<String, String> entry : record.toMap().entrySet()) {
                            String key = entry.getKey();
                            if (getHeaderCase() != null) {
                                key = getHeaderCase() == HeaderCase.LOWERCASE ? key.toLowerCase() : key.toUpperCase();
                            }
                            element.addElement(key, entry.getValue());
                        }
                    } catch (SAXException e) {
                        throw new PipeRunException(this, "Exception caught at line [" + record.getRecordNumber() + "] pos [" + record.getCharacterPosition() + "]", e);
                    }
                }
            }
        }
        return target.getPipeRunResult();
    } catch (Exception e) {
        if (e instanceof PipeRunException) {
            throw (PipeRunException) e;
        }
        throw new PipeRunException(this, "Cannot parse CSV", e);
    }
}
Also used : MessageOutputStream(nl.nn.adapterframework.stream.MessageOutputStream) SaxElementBuilder(nl.nn.adapterframework.xml.SaxElementBuilder) CSVParser(org.apache.commons.csv.CSVParser) SaxDocumentBuilder(nl.nn.adapterframework.xml.SaxDocumentBuilder) PipeRunException(nl.nn.adapterframework.core.PipeRunException) Reader(java.io.Reader) CSVRecord(org.apache.commons.csv.CSVRecord) PipeRunException(nl.nn.adapterframework.core.PipeRunException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException)

Example 4 with SaxDocumentBuilder

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

the class DB2XMLWriter method getXML.

public void getXML(IDbmsSupport dbmsSupport, ResultSet rs, int maxlength, boolean includeFieldDefinition, ContentHandler handler) throws SAXException {
    try (SaxDocumentBuilder root = new SaxDocumentBuilder(docname, handler)) {
        if (null == rs) {
            return;
        }
        if (maxlength < 0) {
            maxlength = Integer.MAX_VALUE;
        }
        Statement stmt = null;
        try {
            stmt = rs.getStatement();
            if (stmt != null) {
                JdbcUtil.warningsToXml(stmt.getWarnings(), root);
            }
        } catch (SQLException e1) {
            log.warn("exception obtaining statement warnings", e1);
        }
        int rowCounter = 0;
        try {
            ResultSetMetaData rsmeta = rs.getMetaData();
            if (includeFieldDefinition) {
                int nfields = rsmeta.getColumnCount();
                try (SaxElementBuilder fields = root.startElement("fielddefinition")) {
                    for (int j = 1; j <= nfields; j++) {
                        try (SaxElementBuilder field = fields.startElement("field")) {
                            String columnName = "" + rsmeta.getColumnName(j);
                            if (convertFieldnamesToUppercase)
                                columnName = columnName.toUpperCase();
                            field.addAttribute("name", columnName);
                            // Not every JDBC implementation implements these attributes!
                            try {
                                field.addAttribute("type", "" + getFieldType(rsmeta.getColumnType(j)));
                            } catch (SQLException e) {
                                log.debug("Could not determine columnType", e);
                            }
                            try {
                                field.addAttribute("columnDisplaySize", "" + rsmeta.getColumnDisplaySize(j));
                            } catch (SQLException e) {
                                log.debug("Could not determine columnDisplaySize", e);
                            }
                            try {
                                field.addAttribute("precision", "" + rsmeta.getPrecision(j));
                            } catch (SQLException e) {
                                log.warn("Could not determine precision", e);
                            } catch (NumberFormatException e2) {
                                if (log.isDebugEnabled())
                                    log.debug("Could not determine precision: " + e2.getMessage());
                            }
                            try {
                                field.addAttribute("scale", "" + rsmeta.getScale(j));
                            } catch (SQLException e) {
                                log.debug("Could not determine scale", e);
                            }
                            try {
                                field.addAttribute("isCurrency", "" + rsmeta.isCurrency(j));
                            } catch (SQLException e) {
                                log.debug("Could not determine isCurrency", e);
                            }
                            try {
                                String columnTypeName = "" + rsmeta.getColumnTypeName(j);
                                if (convertFieldnamesToUppercase)
                                    columnTypeName = columnTypeName.toUpperCase();
                                field.addAttribute("columnTypeName", columnTypeName);
                            } catch (SQLException e) {
                                log.debug("Could not determine columnTypeName", e);
                            }
                            try {
                                field.addAttribute("columnClassName", "" + rsmeta.getColumnClassName(j));
                            } catch (SQLException e) {
                                log.debug("Could not determine columnClassName", e);
                            }
                        }
                    }
                }
            }
            try (SaxElementBuilder queryresult = root.startElement(recordname)) {
                while (rs.next() && rowCounter < maxlength) {
                    getRowXml(queryresult, dbmsSupport, rs, rowCounter, rsmeta, getBlobCharset(), decompressBlobs, nullValue, trimSpaces, getBlobSmart);
                    rowCounter++;
                }
            }
        } catch (Exception e) {
            log.error("Error occured at row [" + rowCounter + "]", e);
        }
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException) SaxElementBuilder(nl.nn.adapterframework.xml.SaxElementBuilder) Statement(java.sql.Statement) SaxDocumentBuilder(nl.nn.adapterframework.xml.SaxDocumentBuilder) SQLException(java.sql.SQLException) SAXException(org.xml.sax.SAXException) SenderException(nl.nn.adapterframework.core.SenderException)

Aggregations

SaxDocumentBuilder (nl.nn.adapterframework.xml.SaxDocumentBuilder)4 SAXException (org.xml.sax.SAXException)3 PipeRunException (nl.nn.adapterframework.core.PipeRunException)2 MessageOutputStream (nl.nn.adapterframework.stream.MessageOutputStream)2 SaxElementBuilder (nl.nn.adapterframework.xml.SaxElementBuilder)2 Reader (java.io.Reader)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)1 PipeLineSession (nl.nn.adapterframework.core.PipeLineSession)1 SenderException (nl.nn.adapterframework.core.SenderException)1 ThreadConnector (nl.nn.adapterframework.stream.ThreadConnector)1 TransformerPool (nl.nn.adapterframework.util.TransformerPool)1 FullXmlFilter (nl.nn.adapterframework.xml.FullXmlFilter)1 SaxException (nl.nn.adapterframework.xml.SaxException)1 TransformerFilter (nl.nn.adapterframework.xml.TransformerFilter)1 XmlWriter (nl.nn.adapterframework.xml.XmlWriter)1 CSVParser (org.apache.commons.csv.CSVParser)1 CSVRecord (org.apache.commons.csv.CSVRecord)1