public void testSimpleValid() throws XMLStreamException { /* Whether prolog/epilog white space is reported is not defined * by StAX specs, thus, let's not add any */ String XML = "<?xml version='1.0' ?>" + "<!DOCTYPE root [ ]>" + "<root attr='123'><!-- comment -->\n" + "</root>"; for (int i = 0; i < 4; ++i) { boolean ns = (i & 1) != 0; boolean coal = (i & 2) != 0; XMLEventReader er = getReader(XML, ns, coal); assertTokenType(START_DOCUMENT, er.nextEvent().getEventType()); assertTokenType(DTD, er.nextEvent().getEventType()); assertTokenType(START_ELEMENT, er.nextEvent().getEventType()); assertTokenType(COMMENT, er.nextEvent().getEventType()); // for fun, let's just use next() instead of nextEvent() XMLEvent evt = (XMLEvent) er.next(); assertTokenType(CHARACTERS, evt.getEventType()); assertTokenType(END_ELEMENT, er.nextEvent().getEventType()); assertTokenType(END_DOCUMENT, er.nextEvent().getEventType()); assertFalse(er.hasNext()); er.close(); } }
/** * Annotates a block of values of a heatmap.plus SVG file by giving them an id and row/column * attributes. A heatmap.plus can contain up to three 'blocks': row annotations, column * annotations and the matrix. * * @param nRow number of rows of this block * @param nCol number of columns of this block * @param blockId value of the id attribute to give elements in this block * @throws XMLStreamException */ private void annotateHeatMapBlock( int nRow, int nCol, String blockId, XMLEventWriter writer, XMLEventReader reader) throws XMLStreamException { int counter = 0; int nPath = nRow * nCol; int currentRow = nRow; // elements drawn from bottom to top, so start counting at last row int currentCol = 1; while (counter < nPath) { XMLEvent event = (XMLEvent) reader.next(); if (event.isStartElement() && event.asStartElement().getName().getLocalPart().equals(PATH)) { // make a new start element with the same attributes plus the extra annotations @SuppressWarnings("unchecked") Iterator<Attribute> attributes = event.asStartElement().getAttributes(); StartElement newSe = eventFactory.createStartElement(new QName(PATH), attributes, null); writer.add(newSe); writer.add(eventFactory.createAttribute(ID, blockId)); writer.add(eventFactory.createAttribute(new QName("row"), Integer.toString(currentRow))); writer.add(eventFactory.createAttribute(new QName("col"), Integer.toString(currentCol))); currentRow--; if (currentRow == 0) { // finished one column, reset currentRow and increment currentCol currentRow = nRow; currentCol++; } counter++; } else { // write the rest untouched writer.add(event); } } }
public static void main(String[] args) { for (String arg : args) { try { XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance(); xmlInputFactory.setXMLReporter( new XMLReporter() { @Override public void report( String message, String errorType, Object relatedInformation, Location location) throws XMLStreamException { System.err.println("Problem in " + location); System.err.println( "at line " + location.getLineNumber() + ", column " + location.getColumnNumber()); System.err.println(message); System.err.println("errorType = " + errorType); System.err.println("relatedInformation = " + relatedInformation); } }); XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(new FileReader(arg)); while (eventReader.hasNext()) { XMLEvent event = (XMLEvent) eventReader.next(); switch (event.getEventType()) { case XMLEvent.START_DOCUMENT: { StartElement startElement = event.asStartElement(); if (startElement.getName().getLocalPart().equals("target")) { Attribute attribute = startElement.getAttributeByName(new QName("name")); System.out.println(attribute.getValue()); } break; } // case XMLEvent.COMMENT:{ event.} } } } catch (XMLStreamException e) { e .printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } catch (FileNotFoundException e) { e .printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } } }
/** * Can annotate SVG heatmap.plus charts made by R. Reads and writes using StAX, adding row and col * attributes to <path> elements corresponding to data points in the heatmap. All indexes can be * calculated using nRow, nCol, nRowAnnotations and nColAnnotations. * * @param chart * @throws FactoryConfigurationError * @throws XMLStreamException * @throws FileNotFoundException */ public void annotateHeatMap(HeatMapChart chart) throws XMLStreamException, FactoryConfigurationError, FileNotFoundException { XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(new FileInputStream(inFile)); OutputStream os = new FileOutputStream(outFile); XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(os); // get values from HeatMapChart int nRow = chart.getData().getRowTargets().size(); int nCol = chart.getData().getColumnTargets().size(); // TODO get from HeatMapChart: int nRowAnnotations = 0; int nColAnnotations = 0; // skip the headers and <def> bit until we reach <g id=""> while (true) { XMLEvent event = (XMLEvent) reader.next(); if (event.isStartElement()) { StartElement se = event.asStartElement(); if (se.getName().getLocalPart().equals(G) && se.getAttributeByName(ID) != null) { LOG.info("<g id=\"\"> reached"); writer.add(event); break; } } writer.add(event); } // annotation begins here // ROW ANNOTATIONS if (nRowAnnotations > 0) { LOG.info("parsing " + nRowAnnotations + " row annotations"); annotateHeatMapBlock(nRow, nRowAnnotations, "rowAnnotation", writer, reader); } // COLUMN ANNOTATIONS if (nColAnnotations > 0) { LOG.info("parsing " + nColAnnotations + " col annotations"); annotateHeatMapBlock(nColAnnotations, nCol, "colAnnotatation", writer, reader); } // MATRIX ANNOTATIONS LOG.info("parsing " + (nRow * nCol) + " matrix values"); annotateHeatMapBlock(nRow, nCol, "matrix", writer, reader); // COLUMN NAMES LOG.info("parsing " + nCol + " column names"); int counter = 0; while (counter < nCol) { XMLEvent event = (XMLEvent) reader.next(); if (event.isStartElement() && event.asStartElement().getName().getLocalPart().equals(G)) { @SuppressWarnings("unchecked") Iterator<Attribute> attributes = event.asStartElement().getAttributes(); StartElement newSe = eventFactory.createStartElement(new QName(G), attributes, null); writer.add(newSe); writer.add(eventFactory.createAttribute(ID, "colName")); writer.add(eventFactory.createAttribute(new QName("col"), Integer.toString(counter + 1))); counter++; } else { writer.add(event); } } // ROW NAMES LOG.info("parsing " + nRow + " row names"); counter = 0; while (counter < nRow) { XMLEvent event = (XMLEvent) reader.next(); if (event.isStartElement() && event.asStartElement().getName().getLocalPart().equals(G)) { @SuppressWarnings("unchecked") Iterator<Attribute> attributes = event.asStartElement().getAttributes(); StartElement newSe = eventFactory.createStartElement(new QName(G), attributes, null); writer.add(newSe); writer.add(eventFactory.createAttribute(ID, "rowName")); writer.add( eventFactory.createAttribute(new QName("row"), Integer.toString(nRow - counter))); counter++; } else { writer.add(event); } } // finish rest of file while (reader.hasNext()) { XMLEvent event = (XMLEvent) reader.next(); if (event.isEndElement()) { // close the <g id=""> tag, right before the </svg> end element if (event.asEndElement().getName().getLocalPart().equals("svg")) { EndElement newEe = eventFactory.createEndElement(new QName(G), null); writer.add(newEe); } } writer.add(event); } writer.close(); }