예제 #1
0
  /**
   * 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();
  }
예제 #2
0
  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);
  }