Search in sources :

Example 11 with SaxElementBuilder

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

the class MailFileSystemTestBase method testExtractNormalMessageSimple.

// @Test
// public void testForward() throws Exception {
// M emailMessage = getFirstFileFromFolder(null);
// fileSystem.forwardMail(emailMessage, "xxx&yyy.nl");
// }
@Test
public void testExtractNormalMessageSimple() throws Exception {
    M emailMessage = getFirstFileFromFolder(null);
    SaxElementBuilder xml = new SaxElementBuilder("email");
    MailFileSystemUtils.addEmailInfoSimple(fileSystem, emailMessage, xml);
    xml.close();
    String expected = TestFileUtils.getTestFile("/ExchangeMailNormalSimple.xml");
    MatchUtils.assertXmlEquals(expected, xml.toString());
}
Also used : SaxElementBuilder(nl.nn.adapterframework.xml.SaxElementBuilder) Test(org.junit.Test)

Example 12 with SaxElementBuilder

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

the class MailFileSystemTestBase method testExtractMessageWithProblematicAddress.

@Test
public void testExtractMessageWithProblematicAddress() throws Exception {
    M emailMessage = getFirstFileFromFolder("FromAddressProblem");
    SaxElementBuilder xml = new SaxElementBuilder("email");
    fileSystem.extractEmail(emailMessage, xml);
    xml.close();
    String expected = TestFileUtils.getTestFile("/ExchangeMailFromAddressProblem.xml");
    MatchUtils.assertXmlEquals(expected, xml.toString());
}
Also used : SaxElementBuilder(nl.nn.adapterframework.xml.SaxElementBuilder) Test(org.junit.Test)

Example 13 with SaxElementBuilder

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

use of nl.nn.adapterframework.xml.SaxElementBuilder 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)

Example 15 with SaxElementBuilder

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

the class JdbcUtilTest method testWarningsToXml.

@Test
public void testWarningsToXml() throws SAXException {
    String expected = getExpectedWarningXml();
    XmlWriter writer = new XmlWriter();
    PrettyPrintFilter ppf = new PrettyPrintFilter(writer);
    try (SaxElementBuilder seb = new SaxElementBuilder(ppf)) {
        JdbcUtil.warningsToXml(getWarnings(), seb);
        MatchUtils.assertXmlEquals(expected, writer.toString());
    }
}
Also used : SaxElementBuilder(nl.nn.adapterframework.xml.SaxElementBuilder) XmlWriter(nl.nn.adapterframework.xml.XmlWriter) PrettyPrintFilter(nl.nn.adapterframework.xml.PrettyPrintFilter) Test(org.junit.Test)

Aggregations

SaxElementBuilder (nl.nn.adapterframework.xml.SaxElementBuilder)15 Test (org.junit.Test)7 SAXException (org.xml.sax.SAXException)5 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 SenderException (nl.nn.adapterframework.core.SenderException)2 SaxDocumentBuilder (nl.nn.adapterframework.xml.SaxDocumentBuilder)2 XmlWriter (nl.nn.adapterframework.xml.XmlWriter)2 Reader (java.io.Reader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 Statement (java.sql.Statement)1 Map (java.util.Map)1 AddressException (javax.mail.internet.AddressException)1 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)1 ListenerException (nl.nn.adapterframework.core.ListenerException)1 PipeRunException (nl.nn.adapterframework.core.PipeRunException)1 Message (nl.nn.adapterframework.stream.Message)1 MessageOutputStream (nl.nn.adapterframework.stream.MessageOutputStream)1 PrettyPrintFilter (nl.nn.adapterframework.xml.PrettyPrintFilter)1