List of usage examples for org.xml.sax ContentHandler endDocument
public void endDocument() throws SAXException;
From source file:com.amalto.core.load.context.BufferStateContextWriter.java
public void writeEndDocument() throws XMLStreamException { processedElements.add(new PayloadProcessedElement() { public void flush(ContentHandler contentHandler) throws SAXException { contentHandler.endDocument(); }//from w ww . j ava 2s .c om }); }
From source file:com.aurel.track.util.HTMLDiff.java
public static String makeDiff(String newValue, String oldValue, Locale locale) throws URISyntaxException { boolean htmlOut = true; if (newValue == null) { newValue = ""; } else {/*from w w w . j a v a 2 s.com*/ newValue = newValue.replaceAll(" ", " "); } if (oldValue == null || (oldValue != null && oldValue.length() == 0)) { return newValue; } else { oldValue = oldValue.replaceAll(" ", " "); } try { SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory.newInstance(); TransformerHandler result = tf.newTransformerHandler(); StringWriter stringWriter = new StringWriter(); result.setResult(new StreamResult(stringWriter)); XslFilter filter = new XslFilter(); ContentHandler postProcess = htmlOut ? filter.xsl(result, "com/aurel/track/util/htmlheader.xsl") : result; String prefix = "diff"; HtmlCleaner cleaner = new HtmlCleaner(); InputSource oldSource = new InputSource(new StringReader(oldValue)); InputSource newSource = new InputSource(new StringReader(newValue)); DomTreeBuilder oldHandler = new DomTreeBuilder(); cleaner.cleanAndParse(oldSource, oldHandler); TextNodeComparator leftComparator = new TextNodeComparator(oldHandler, locale); DomTreeBuilder newHandler = new DomTreeBuilder(); cleaner.cleanAndParse(newSource, newHandler); TextNodeComparator rightComparator = new TextNodeComparator(newHandler, locale); postProcess.startDocument(); postProcess.startElement("", "diffreport", "diffreport", new AttributesImpl()); postProcess.startElement("", "diff", "diff", new AttributesImpl()); HtmlSaxDiffOutput output = new HtmlSaxDiffOutput(postProcess, prefix); HTMLDiffer differ = new HTMLDiffer(output); differ.diff(leftComparator, rightComparator); postProcess.endElement("", "diff", "diff"); postProcess.endElement("", "diffreport", "diffreport"); postProcess.endDocument(); return stringWriter.toString(); } catch (Exception e) { LOGGER.error(ExceptionUtils.getStackTrace(e)); if (e.getCause() != null) { LOGGER.error(ExceptionUtils.getStackTrace(e.getCause())); } if (e instanceof SAXException) { LOGGER.error(ExceptionUtils.getStackTrace(((SAXException) e).getException())); } } return null; }
From source file:org.apache.uima.ruta.descriptor.RutaDescriptorBuilder.java
private void toFile(XMLizable desc, File destination) throws SAXException, FileNotFoundException { destination.getParentFile().mkdirs(); OutputStream out = new FileOutputStream(destination); XMLSerializer sax = new XMLSerializer(out); ContentHandler ch = sax.getContentHandler(); ch.startDocument();/*from w ww . j a va 2s .c o m*/ desc.toXML(ch); ch.endDocument(); }
From source file:csns.web.controller.WikiController.java
private String diff(Revision oldRevision, Revision newRevision) throws Exception { StringWriter result = new StringWriter(); TransformerHandler transformerHandler = ((SAXTransformerFactory) TransformerFactory.newInstance()) .newTransformerHandler();//ww w. ja v a2s . c o m transformerHandler.setResult(new StreamResult(result)); XslFilter xslFilter = new XslFilter(); ContentHandler contentHandler = xslFilter.xsl(transformerHandler, "xsl/diff2html.xsl"); contentHandler.startDocument(); HtmlSaxDiffOutput diffOutput = new HtmlSaxDiffOutput(contentHandler, "diff"); HTMLDiffer differ = new HTMLDiffer(diffOutput); TextNodeComparator leftComparator = createComparator(oldRevision); TextNodeComparator rightComparator = createComparator(newRevision); differ.diff(leftComparator, rightComparator); contentHandler.endDocument(); return result.toString(); }
From source file:CSVXMLReader.java
/** * Parse a CSV file. SAX events are delivered to the ContentHandler * that was registered via <code>setContentHandler</code>. * * @param input the comma separated values file to parse. *//*from www . ja va2s . c o m*/ public void parse(InputSource input) throws IOException, SAXException { // if no handler is registered to receive events, don't bother // to parse the CSV file ContentHandler ch = getContentHandler(); if (ch == null) { return; } // convert the InputSource into a BufferedReader BufferedReader br = null; if (input.getCharacterStream() != null) { br = new BufferedReader(input.getCharacterStream()); } else if (input.getByteStream() != null) { br = new BufferedReader(new InputStreamReader(input.getByteStream())); } else if (input.getSystemId() != null) { java.net.URL url = new URL(input.getSystemId()); br = new BufferedReader(new InputStreamReader(url.openStream())); } else { throw new SAXException("Invalid InputSource object"); } ch.startDocument(); // emit <csvFile> ch.startElement("", "", "csvFile", EMPTY_ATTR); // read each line of the file until EOF is reached String curLine = null; while ((curLine = br.readLine()) != null) { curLine = curLine.trim(); if (curLine.length() > 0) { // create the <line> element ch.startElement("", "", "line", EMPTY_ATTR); // output data from this line parseLine(curLine, ch); // close the </line> element ch.endElement("", "", "line"); } } // emit </csvFile> ch.endElement("", "", "csvFile"); ch.endDocument(); }
From source file:net.ontopia.topicmaps.db2tm.RelationMapping.java
protected void write(ContentHandler dh) throws SAXException { // initialize attributes AttributesImpl atts = new AttributesImpl(); // <db2tm name="..."> if (name != null) addAttribute(atts, "name", "CDATA", name); dh.startDocument();/*from w w w .j a v a 2 s.c o m*/ dh.startElement("", "", "db2tm", atts); atts.clear(); // prefixes for (Prefix prefix : iprefixes.values()) { addAttribute(atts, "prefix", "CDATA", prefix.getId()); switch (prefix.getType()) { case Prefix.TYPE_SUBJECT_IDENTIFIER: addAttribute(atts, "subject-identifier", "CDATA", prefix.getLocator()); break; case Prefix.TYPE_ITEM_IDENTIFIER: addAttribute(atts, "item-identifier", "CDATA", prefix.getLocator()); break; case Prefix.TYPE_SUBJECT_LOCATOR: addAttribute(atts, "subject-locator", "CDATA", prefix.getLocator()); break; } dh.startElement("", "", "using", atts); atts.clear(); dh.endElement("", "", "using"); } // relations for (Relation rel : getRelations()) { // <relation> addAttribute(atts, "name", "CDATA", rel.getName()); addAttribute(atts, "columns", "CDATA", rel.getColumns()); dh.startElement("", "", "relation", atts); atts.clear(); outputEntities(rel, dh); // </relation> dh.endElement("", "", "relation"); } // </db2tm> dh.endElement("", "", "db2tm"); dh.endDocument(); }
From source file:com.mirth.connect.plugins.datatypes.delimited.DelimitedReader.java
public void parse(InputSource input) throws SAXException, IOException { // Parsing overview ///*w ww. ja va 2 s. c o m*/ // The incoming stream is a single message which is a collection of one // or more records. // Each record is a collection of columns. Columns can be either fixed // width, or delimited. // Records are delimited. // Each record is assumed to contain the same number and type of // columns. // // The following user configurable options affect the behavior of the // parser: // o columnWidths The array of fixed column widths. // o columnDelimiter The characters that delimit (separate) the // columns. // o recordDelimiter The characters that delimit (separate) each // record. // o quoteToken The characters that are used to quote a column value. // o escapeWithDoubleQuote Iff true, embedded quotes are escaped with // two consecutive quotes. // Otherwise, the quote escape characters escape embedded quote // characters. // o quoteEscapeToken The characters used to escape a quote (or itself). // o ignoreCR Iff true, all incoming \r characters are ignored and not // processed. // // The following user configurable options affect the behavior of the // output: // o columnNames A list of column names (taken from either file header, // or supplied by user). BufferedReader in = new BufferedReader(input.getCharacterStream()); // Start the document String documentHead = "delimited"; ContentHandler contentHandler = getContentHandler(); contentHandler.startDocument(); // Output <delimited> contentHandler.startElement("", documentHead, "", null); // While the parser gets records from the message ArrayList<String> record; int recordNo = 1; while ((record = getRecord(in, null)) != null) { // Output <rowN> if (serializationProperties.isNumberedRows()) { contentHandler.startElement("", "row" + recordNo, "", null); } else { contentHandler.startElement("", "row", "", null); } // For each column for (int i = 0; i < record.size(); i++) { String columnName; if (serializationProperties.getColumnNames() != null && i < serializationProperties.getColumnNames().length) { // User specified column name columnName = serializationProperties.getColumnNames()[i]; } else { // Default column name columnName = "column" + (i + 1); } // Output <columnN> contentHandler.startElement("", columnName, "", null); // Output column value contentHandler.characters(record.get(i).toCharArray(), 0, record.get(i).length()); // Output </columnN> contentHandler.endElement("", columnName, ""); } // Output </rowN> if (serializationProperties.isNumberedRows()) { contentHandler.endElement("", "row" + recordNo, ""); } else { contentHandler.endElement("", "row", ""); } recordNo++; } // Output </delimited> contentHandler.endElement("", documentHead, ""); // End the document contentHandler.endDocument(); }
From source file:com.mirth.connect.plugins.datatypes.hl7v2.ER7Reader.java
public void parse(InputSource source) throws SAXException, IOException { String message = getMessageFromSource(source); ContentHandler contentHandler = getContentHandler(); contentHandler.startDocument();// w w w.j a v a2s .c o m // first tokenize the segments if ((message == null) || (message.length() < 6)) { throw new SAXException("Unable to parse message. It is NULL or too short. " + message); } // usually | String fieldSeparator = DEFAULT_FIELD_SEPARATOR; String componentSeparator = DEFAULT_COMPONENT_SEPARATOR; String repetitionSeparator = DEFAULT_REPETITION_SEPARATOR; String escapeCharacter = DEFAULT_ESCAPE_CHARACTER; String subcomponentSeparator = DEFAULT_SUBCOMPONENT_TERMINATOR; // if we have a header, grab the actual separators from the message String firstSegment = message.substring(0, 3); if (firstSegment.equalsIgnoreCase("MSH") || firstSegment.equalsIgnoreCase("FHS") || firstSegment.equalsIgnoreCase("BHS")) { fieldSeparator = new String(new char[] { message.charAt(3) }); int nextDelimiter = message.indexOf(message.charAt(3), 4); if (nextDelimiter == -1) { // If the message is just MSH|^~\&, we still want to extract the encoding characters nextDelimiter = message.length(); } if (nextDelimiter > 4) { // usually ^ componentSeparator = new String(new char[] { message.charAt(4) }); } if (nextDelimiter > 5) { // usually ~ repetitionSeparator = new String(new char[] { message.charAt(5) }); } if (nextDelimiter > 6) { // usually \ escapeCharacter = new String(new char[] { message.charAt(6) }); } if (nextDelimiter > 7) { // usually & subcomponentSeparator = new String(new char[] { message.charAt(7) }); } } // replace the special case of ^~& with ^~\& (MIRTH-1544) if (message.length() >= 8 && "^~&|".equals(message.substring(4, 8))) { escapeCharacter = "\\"; subcomponentSeparator = "&"; repetitionSeparator = "~"; componentSeparator = "^"; } // tokenize the segments first String[] segments = StringUtils.split(message, segmentDelimiter); String documentHead = handleSegments("", contentHandler, fieldSeparator, componentSeparator, subcomponentSeparator, repetitionSeparator, escapeCharacter, segments); contentHandler.endElement("", documentHead, ""); contentHandler.endDocument(); }
From source file:com.mirth.connect.model.converters.NCPDPReader.java
@Override public void parse(InputSource input) throws SAXException, IOException { // convert the InputSource to a String and trim the whitespace String message = IOUtils.toString(input.getCharacterStream()).trim(); ContentHandler contentHandler = getContentHandler(); contentHandler.startDocument();//w w w . j av a 2s . c o m if ((message == null) || (message.length() < 3)) { throw new SAXException("Unable to parse, message is null or too short: " + message); } // process header String header = parseHeader(message, contentHandler); // process body int groupDelimeterIndex = message.indexOf(groupDelimeter); int segmentDelimeterIndex = message.indexOf(segmentDelimeter); int bodyIndex = 0; if ((groupDelimeterIndex == -1) || (segmentDelimeterIndex < groupDelimeterIndex)) { bodyIndex = segmentDelimeterIndex; } else { bodyIndex = groupDelimeterIndex; } String body = message.substring(bodyIndex, message.length()); boolean hasMoreSegments = true; boolean inGroup = false; boolean firstTransaction = true; int groupCounter = 0; while (hasMoreSegments) { // get next segment or group groupDelimeterIndex = body.indexOf(groupDelimeter); segmentDelimeterIndex = body.indexOf(segmentDelimeter); if ((groupDelimeterIndex > -1) && (groupDelimeterIndex < segmentDelimeterIndex)) { // case: next part is a group // process last segment before group parseSegment(body.substring(0, groupDelimeterIndex), contentHandler); if (inGroup) { contentHandler.endElement("", "TRANSACTION", ""); } if (firstTransaction) { firstTransaction = false; contentHandler.startElement("", "TRANSACTIONS", "", null); } // process a group AttributesImpl attr = new AttributesImpl(); attr.addAttribute("", "counter", "counter", "", Integer.toString(++groupCounter)); contentHandler.startElement("", "TRANSACTION", "", attr); inGroup = true; } else if (groupDelimeterIndex == -1 && segmentDelimeterIndex == -1) { // case: last segment parseSegment(body, contentHandler); hasMoreSegments = false; } else { // case: next part is a segment String segment = body.substring(0, segmentDelimeterIndex); parseSegment(segment, contentHandler); } // remove processed segment from message body body = body.substring(segmentDelimeterIndex + segmentDelimeter.length()); } // end group if we have started one if (inGroup) { contentHandler.endElement("", "TRANSACTION", ""); contentHandler.endElement("", "TRANSACTIONS", ""); } contentHandler.endElement("", header, ""); contentHandler.endDocument(); }
From source file:net.ontopia.persistence.rdbms.DatabaseProjectReader.java
public static void saveProject(Project project, ContentHandler dh) throws SAXException { AttributesImpl atts = new AttributesImpl(); dh.startDocument();/*from w ww .j a va 2 s .c om*/ dh.startElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "dbschema", EMPTY_ATTR_LIST); Iterator<String> platforms = project.getDataTypePlatforms().iterator(); if (platforms.hasNext()) { while (platforms.hasNext()) { String platform = platforms.next(); atts.clear(); atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, PLATFORM, CDATA, platform); dh.startElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "datatypes", atts); Iterator<DataType> datatypes = project.getDataTypes(platform).iterator(); while (datatypes.hasNext()) { // Platform datatypes DataType datatype = datatypes.next(); atts.clear(); atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, NAME, CDATA, (datatype.getName())); atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, TYPE, CDATA, (datatype.getType())); atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, SIZE, CDATA, (datatype.getSize() == null ? "" : datatype.getSize())); atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "class", CDATA, (datatype.isVariable() ? "variable" : "constant")); dh.startElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "datatype", atts); // Datatype properties Iterator<String> properties = datatype.getProperties().iterator(); while (properties.hasNext()) { String name = properties.next(); String value = datatype.getProperty(name); if (value != null) { atts.clear(); atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, NAME, CDATA, name); atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, VALUE, CDATA, value); dh.startElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, PROPERTY, atts); dh.endElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, PROPERTY); } } dh.endElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "datatype"); } } dh.endElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "datatypes"); } Iterator<Table> tables = project.getTables().iterator(); while (tables.hasNext()) { Table table = tables.next(); // Table attributes atts.clear(); atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, NAME, CDATA, table.getName()); if (table.getShortName() != null) atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "short", CDATA, table.getShortName()); if (table.getPrimaryKeys() != null) atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "pks", CDATA, StringUtils.join(table.getPrimaryKeys(), " ")); dh.startElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "table", atts); // Table properties Iterator<String> properties = table.getProperties().iterator(); while (properties.hasNext()) { String name = properties.next(); String value = table.getProperty(name); if (value != null) { atts.clear(); atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, NAME, CDATA, name); atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, VALUE, CDATA, value); dh.startElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, PROPERTY, atts); dh.endElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, PROPERTY); } } Iterator<Column> columns = table.getColumns().iterator(); while (columns.hasNext()) { Column column = columns.next(); // Column attributes atts.clear(); atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, NAME, CDATA, column.getName()); atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, TYPE, CDATA, column.getType()); if (column.isReference()) { atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "reftab", CDATA, column.getReferencedTable()); atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "refcol", CDATA, column.getReferencedColumn()); } if (column.getSize() != null) atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, SIZE, CDATA, column.getName()); if (column.isNullable()) atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "null", CDATA, "yes"); if (column.getDefault() != null) atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "default", CDATA, column.getDefault()); dh.startElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "column", atts); // Column properties Iterator<String> properties2 = column.getProperties().iterator(); while (properties2.hasNext()) { String name = properties2.next(); String value = column.getProperty(name); if (value != null) { atts.clear(); atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, NAME, CDATA, name); atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, VALUE, CDATA, value); dh.startElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, PROPERTY, atts); dh.endElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, PROPERTY); } } dh.endElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "column"); } dh.endElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "table"); } dh.endElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "dbschema"); dh.endDocument(); }