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);
}
}
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);
}
}
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);
}
}
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);
}
}
}
Aggregations