/** * 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); } } }
@Override public Scenario parse(InputStream inputStream) throws IOException, SAXException, ParserConfigurationException { OutputStream output = new ByteArrayOutputStream(); JsonXMLConfig config = new JsonXMLConfigBuilder().autoArray(true).prettyPrint(false).build(); try { /* Create source (XML). */ XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(inputStream); // XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream); // Source source = new StAXSource(reader); /* Create result (JSON). */ XMLEventWriter writer = new JsonXMLOutputFactory(config).createXMLEventWriter(output); // XMLStreamWriter writer = new JsonXMLOutputFactory(config).createXMLStreamWriter(output); // Result result = new StAXResult(writer); /* * Copy events from reader to writer. */ writer.add(reader); /* Copy source to result via "identity transform". */ // TransformerFactory.newInstance().newTransformer().transform(source, result); } catch (XMLStreamException e) { e.printStackTrace(); } finally { /* As per StAX specification, XMLStreamReader/Writer.close() doesn't close the underlying stream. */ output.close(); inputStream.close(); } /* try { json = xmlToJson(inputStream); } catch (JSONException e) { e.printStackTrace(); } String jsonString = json.toString(); System.out.println(jsonString); GenericDocument genericDocument = new GenericDocument(); genericDocument.setJson(jsonString); */ GenericDocument genericDocument = new GenericDocument(); String json = output.toString(); genericDocument.setJson(json); return genericDocument; }
private void addMessageIdElement(XMLEventWriter writer, String id, StartElement startElement) throws XMLStreamException { writer.add( eventFactory.createStartElement( "", startElement.getName().getNamespaceURI(), startElement.getName().getLocalPart(), startElement.getAttributes(), startElement.getNamespaces(), startElement.getNamespaceContext())); writer.add(eventFactory.createCharacters(id)); writer.add( eventFactory.createEndElement( "", startElement.getName().getNamespaceURI(), startElement.getName().getLocalPart(), startElement.getNamespaces())); }
private void addRewrittenAddressElement( XMLEventWriter writer, String address, int port, StartElement startElement) throws XMLStreamException { writer.add( eventFactory.createStartElement( "", startElement.getName().getNamespaceURI(), startElement.getName().getLocalPart(), startElement.getAttributes(), startElement.getNamespaces(), startElement.getNamespaceContext())); writer.add(eventFactory.createCharacters(address.replaceFirst(":\\d+/", ":" + port + "/"))); writer.add( eventFactory.createEndElement( "", startElement.getName().getNamespaceURI(), startElement.getName().getLocalPart(), startElement.getNamespaces())); }
public void rewriteEndpoint(Reader reader, Writer writer, int port, Exchange exc) throws XMLStreamException { XMLEventReader parser = inputFactory.createXMLEventReader(reader); XMLEventWriter eventWriter = XMLOutputFactory.newInstance().createXMLEventWriter(writer); String id = null; String url = null; skip: while (parser.hasNext()) { XMLEvent e = parser.nextEvent(); if (e.isStartElement()) { if (isReplyTo(e.asStartElement())) { while (e.isStartElement() || !isReplyTo(e.asEndElement())) { if (e.isStartElement() && isAddress(e.asStartElement())) { url = parser.getElementText(); addRewrittenAddressElement(eventWriter, url, port, e.asStartElement()); continue skip; } eventWriter.add(e); e = parser.nextTag(); } } if (isMessageId(e.asStartElement())) { id = parser.getElementText(); exc.setProperty("messageId", id); addMessageIdElement(eventWriter, id, e.asStartElement()); continue skip; } } eventWriter.add(e); } registry.register(id, url); }
/** * 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(); }
public void doFilter( ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; RequestURIParts parts = parseParts(request); if (parts != null) { String realBaseAddress = parts.getBaseAddress(); String idlPath = parts.getFilePath(); InputStream idl = this.servletContext.getResourceAsStream(idlPath); if (idl != null) { servletResponse.setContentType("text/xml"); String assumedBaseAddress = null; try { XMLEventReader eventReader = this.inputFactory.createXMLEventReader(idl); XMLEventWriter eventWriter = this.outputFactory.createXMLEventWriter(servletResponse.getWriter()); while (eventReader.hasNext()) { XMLEvent event = eventReader.nextEvent(); if (event.isProcessingInstruction()) { String target = ((ProcessingInstruction) event).getTarget(); if ("enunciate-assumed-base-uri".equals(target)) { assumedBaseAddress = ((ProcessingInstruction) event).getData(); if (assumedBaseAddress.endsWith("/")) { assumedBaseAddress = assumedBaseAddress.substring(0, assumedBaseAddress.length() - 1); } } continue; } else if (event.getEventType() == XMLStreamConstants.CDATA || event.getEventType() == XMLStreamConstants.CHARACTERS) { String data = ((Characters) event).getData(); if (assumedBaseAddress != null && data.contains(assumedBaseAddress)) { data = data.replace(assumedBaseAddress, realBaseAddress); event = new DelegatingCharacters(((Characters) event), data); } } else if (event.getEventType() == XMLStreamConstants.START_ELEMENT) { List<Attribute> attributes = new ArrayList<Attribute>(); Iterator attributesIt = ((StartElement) event).getAttributes(); while (attributesIt.hasNext()) { Attribute attribute = (Attribute) attributesIt.next(); String value = attribute.getValue(); if (assumedBaseAddress != null && value.contains(assumedBaseAddress)) { value = value.replace(assumedBaseAddress, realBaseAddress); attribute = new DelegatingAttribute(attribute, value); event = new DelegatingStartElement(((StartElement) event), attributes); } attributes.add(attribute); } } eventWriter.add(event); } eventReader.close(); eventWriter.flush(); eventWriter.close(); return; } catch (XMLStreamException e) { throw new ServletException(e); } } } chain.doFilter(servletRequest, servletResponse); }