protected XMLStreamReader2 constructNonNsStreamReader(String content, boolean coal)
     throws XMLStreamException {
   XMLInputFactory f = getInputFactory();
   setNamespaceAware(f, false);
   setCoalescing(f, coal);
   return (XMLStreamReader2) f.createXMLStreamReader(new StringReader(content));
 }
 protected XMLStreamReader2 constructNsStreamReader(InputStream in, boolean coal)
     throws XMLStreamException {
   XMLInputFactory f = getInputFactory();
   setNamespaceAware(f, true);
   setCoalescing(f, coal);
   return (XMLStreamReader2) f.createXMLStreamReader(in);
 }
Exemple #3
0
 public static Element parseWithCRC(InputStream inputStream)
     throws XMLStreamException, CRCMismatchException {
   XMLInputFactory xif = XMLInputFactory.newFactory();
   Element element = parse(xif.createXMLStreamReader(inputStream));
   checkCRC(element);
   element.removeChildren(CRC_ELEMENT);
   return element;
 }
  public void testXMLStreamWriter() throws Exception {
    FlushRoot control = getControlObject();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    XMLOutputFactory xof = XMLOutputFactory.newFactory();
    XMLStreamWriter xsw = xof.createXMLStreamWriter(baos);
    marshaller.marshal(control, xsw);

    XMLInputFactory xif = XMLInputFactory.newFactory();
    XMLStreamReader xsr = xif.createXMLStreamReader(new ByteArrayInputStream(baos.toByteArray()));
    Object test = unmarshaller.unmarshal(xsr);

    assertEquals(control, test);
  }
 protected static XMLStreamReader2 constructStreamReaderForFile(XMLInputFactory f, String filename)
     throws IOException, XMLStreamException {
   File inf = new File(filename);
   XMLStreamReader sr = f.createXMLStreamReader(inf.toURL().toString(), new FileReader(inf));
   assertEquals(sr.getEventType(), START_DOCUMENT);
   return (XMLStreamReader2) sr;
 }
  @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;
  }
 protected static boolean setSupportDTD(XMLInputFactory f, boolean state)
     throws XMLStreamException {
   try {
     f.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.valueOf(state));
     return (willSupportDTD(f) == state);
   } catch (IllegalArgumentException e) {
     // Let's assume that the property (or specific value) is NOT supported...
     return false;
   }
 }
Exemple #8
0
 public static void main(String[] args) throws Exception {
   String urlString;
   if (args.length == 0) {
     urlString = "http://www.w3c.org";
     System.out.println("Using " + urlString);
   } else urlString = args[0];
   URL url = new URL(urlString);
   InputStream in = url.openStream();
   XMLInputFactory factory = XMLInputFactory.newInstance();
   XMLStreamReader parser = factory.createXMLStreamReader(in);
   while (parser.hasNext()) {
     int event = parser.next();
     if (event == XMLStreamConstants.START_ELEMENT) {
       if (parser.getLocalName().equals("a")) {
         String href = parser.getAttributeValue(null, "href");
         if (href != null) System.out.println(href);
       }
     }
   }
 }
  public void testOutputStreamUTF8() throws Exception {
    FlushRoot control = getControlObject();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    marshaller.marshal(control, baos);

    XMLInputFactory xif = XMLInputFactory.newFactory();
    Object test = unmarshaller.unmarshal(new ByteArrayInputStream(baos.toByteArray()));

    assertEquals(control, test);
  }
  public void testJSONOutputStreamUTF8() throws Exception {
    marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
    unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");

    FlushRoot control = getControlObject();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    marshaller.marshal(control, baos);

    XMLInputFactory xif = XMLInputFactory.newFactory();
    Object test = unmarshaller.unmarshal(new ByteArrayInputStream(baos.toByteArray()));

    assertEquals(control, test);
  }
 protected static boolean setNamespaceAware(XMLInputFactory f, boolean state)
     throws XMLStreamException {
   /* Let's not assert, but see if it sticks. Some implementations
    * might choose to silently ignore setting, at least for 'false'?
    */
   try {
     f.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, state ? Boolean.TRUE : Boolean.FALSE);
     return (isNamespaceAware(f) == state);
   } catch (IllegalArgumentException e) {
     /* Let's assume, then, that the property (or specific value for it)
      * is NOT supported...
      */
     return false;
   }
 }
 /**
  * ns4 is declared on Envelope and Body and is used in faultcode. So making sure the correct ns4
  * is picked up for payload source
  */
 public void testPayloadSource1() throws Exception {
   String soap18Msg =
       "<S:Envelope xmlns:S='http://schemas.xmlsoap.org/soap/envelope/' xmlns:ns4='A'>"
           + "<S:Body xmlns:ns4='http://schemas.xmlsoap.org/soap/envelope/'>"
           + "<S:Fault>"
           + "<faultcode>ns4:Server</faultcode>"
           + "<faultstring>com.sun.istack.XMLStreamException2</faultstring>"
           + "</S:Fault>"
           + "</S:Body>"
           + "</S:Envelope>";
   Message message = useStreamCodec(soap18Msg);
   Source source = message.readPayloadAsSource();
   InputStream is = getInputStream(source);
   XMLStreamReader xsr = XMLInputFactory.newInstance().createXMLStreamReader(is);
   xsr.next();
   xsr.next();
   assertEquals("http://schemas.xmlsoap.org/soap/envelope/", xsr.getNamespaceURI("ns4"));
 }
  @Override
  public DbData readData(InputStream in) throws XMLStreamException {
    XMLStreamReader sr = _staxInFactory.createXMLStreamReader(in);
    DbData result = new DbData();

    sr.nextTag();
    expectTag(FIELD_TABLE, sr);

    try {
      while (sr.nextTag() == XMLStreamReader.START_ELEMENT) {
        result.addRow(readRow(sr));
      }
    } catch (IllegalArgumentException iae) {
      throw new XMLStreamException("Data problem: " + iae.getMessage(), sr.getLocation());
    }

    sr.close();
    return result;
  }
Exemple #14
0
    @Override
    protected void map(LongWritable key, Text value, Mapper.Context context)
        throws IOException, InterruptedException {
      String document = value.toString();
      System.out.println("'" + document + "'");
      try {
        XMLStreamReader reader =
            XMLInputFactory.newInstance()
                .createXMLStreamReader(new ByteArrayInputStream(document.getBytes()));
        String propertyName = "";
        String propertyValue = "";
        String currentElement = "";
        while (reader.hasNext()) {
          int code = reader.next();
          switch (code) {
            case XMLStreamConstants.START_ELEMENT: // START_ELEMENT:
              currentElement = reader.getLocalName();
              break;
            case XMLStreamConstants.CHARACTERS: // CHARACTERS:
              if (currentElement.equalsIgnoreCase("uid")) {
                propertyName += reader.getText().trim();
                System.out.println(propertyName);
              } else if (currentElement.equalsIgnoreCase("location")) {
                propertyValue += reader.getText().trim();
                System.out.println(propertyValue);

              } else if (currentElement.equalsIgnoreCase("age")) {
                propertyValue += ("," + reader.getText().trim());
                System.out.println(propertyValue);
              }
              break;
          }
        }
        reader.close();
        context.write(new Text(propertyName.trim()), new Text(propertyValue.trim()));

      } catch (Exception e) {
        throw new IOException(e);
      }
    }
  /**
   * @param args
   * @throws Exception
   */
  public static void main(String[] args) throws Exception {

    if (args.length != 1) {
      printUsage();
    }

    filename = args[0];

    XMLInputFactory xmlif = null;
    try {
      xmlif = XMLInputFactory.newInstance();
      xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
      xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
      xmlif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
      xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    System.out.println("XMLInputFactory: " + xmlif);
    System.out.println("filename = " + filename);

    XMLStreamReader xmlr = null;
    try {

      FileInputStream fis = new FileInputStream(filename);

      xmlr = xmlif.createXMLStreamReader(fis);

    } catch (Exception ex) {
      ex.printStackTrace();
    }

    for (int event = xmlr.next(); event != XMLStreamConstants.END_DOCUMENT; event = xmlr.next()) {
      if (event == XMLStreamConstants.START_ELEMENT) {
        String element = xmlr.getLocalName();
      }
    }
  }
Exemple #16
0
public abstract class StaxMateTestBase extends junit.framework.TestCase {
  protected static final String BASE64_ENCODED =
      "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz"
          + "IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg"
          + "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu"
          + "dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo"
          + "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=";

  protected static final String BASE64_DECODED_STRING =
      "Man is distinguished, not only by his reason, but by this singular passion"
          + " from other animals, which is a lust of the mind, that by a perseverance of delight"
          + " in the continued and indefatigable generation of knowledge, exceeds the short vehemence"
          + " of any carnal pleasure.";
  protected static final byte[] BASE64_DECODED_BYTES;

  static {
    byte[] data = null;

    try {
      data = BASE64_DECODED_STRING.getBytes("UTF-8");
    } catch (Exception e) {
    }
    BASE64_DECODED_BYTES = data;
  }

  protected static XMLInputFactory _staxInputFactory = XMLInputFactory.newInstance();

  protected SMInputFactory _inputFactory;

  /*
  /**********************************************************************
  /* Factory methods
  /**********************************************************************
   */

  protected SMInputFactory getInputFactory() {
    if (_inputFactory == null) {
      _inputFactory = new SMInputFactory(getStaxInputFactory());
    }
    return _inputFactory;
  }

  protected XMLInputFactory getStaxInputFactory() {
    return _staxInputFactory;
  }

  protected XMLStreamReader getCoalescingReader(String content) throws XMLStreamException {
    XMLInputFactory f = XMLInputFactory.newInstance();
    f.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
    return f.createXMLStreamReader(new StringReader(content));
  }

  protected XMLStreamWriter getSimpleWriter(Writer w) throws XMLStreamException {
    XMLOutputFactory f = XMLOutputFactory.newInstance();
    return f.createXMLStreamWriter(w);
  }

  /*
  /**********************************************************************
  /* Assertion support
  /**********************************************************************
   */

  protected void assertTokenType(int expType, int actType) throws XMLStreamException {
    assertEquals(expType, actType);
  }

  protected void assertTokenType(int expType, XMLStreamReader sr) throws XMLStreamException {
    assertTokenType(expType, sr.getEventType());
  }

  protected void assertTokenType(int expType, SMInputCursor crsr) throws XMLStreamException {
    assertTokenType(expType, crsr.getCurrEventCode());
  }

  protected void assertToken(SMEvent expEvent, SMEvent actEvent) throws XMLStreamException {
    assertTokenType(expEvent.getEventCode(), actEvent.getEventCode());
  }

  protected void assertElem(XMLStreamReader sr, String expURI, String expLN)
      throws XMLStreamException {
    assertEquals(expLN, sr.getLocalName());
    String actURI = sr.getNamespaceURI();
    if (expURI == null || expURI.length() == 0) {
      if (actURI != null && actURI.length() > 0) {
        fail("Expected no namespace, got URI '" + actURI + "'");
      }
    } else {
      assertEquals(expURI, sr.getNamespaceURI());
    }
  }

  protected void assertException(Throwable e, String match) {
    String msg = e.getMessage();
    String lmsg = msg.toLowerCase();
    String lmatch = match.toLowerCase();
    if (lmsg.indexOf(lmatch) < 0) {
      fail(
          "Expected an exception with sub-string \""
              + match
              + "\": got one with message \""
              + msg
              + "\"");
    }
  }

  /*
  /**********************************************************************
  /* Other accessors
  /**********************************************************************
   */

  /** Note: calling this method will move stream to the next non-textual event. */
  protected String collectAllText(XMLStreamReader sr) throws XMLStreamException {
    StringBuilder sb = new StringBuilder(100);
    while (true) {
      int type = sr.getEventType();
      if (type == CHARACTERS || type == SPACE || type == CDATA) {
        sb.append(sr.getText());
        sr.next();
      } else {
        break;
      }
    }
    return sb.toString();
  }
}
Exemple #17
0
 protected XMLStreamReader getCoalescingReader(String content) throws XMLStreamException {
   XMLInputFactory f = XMLInputFactory.newInstance();
   f.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
   return f.createXMLStreamReader(new StringReader(content));
 }
 protected static void setLazyParsing(XMLInputFactory f, boolean state) throws XMLStreamException {
   f.setProperty(XMLInputFactory2.P_LAZY_PARSING, state ? Boolean.TRUE : Boolean.FALSE);
 }
 protected static void setSupportExternalEntities(XMLInputFactory f, boolean state)
     throws XMLStreamException {
   f.setProperty(
       XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, state ? Boolean.TRUE : Boolean.FALSE);
 }
 protected static void setReplaceEntities(XMLInputFactory f, boolean state)
     throws XMLStreamException {
   f.setProperty(
       XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, state ? Boolean.TRUE : Boolean.FALSE);
 }
 protected static XMLEventReader2 constructEventReader(XMLInputFactory f, String content)
     throws XMLStreamException {
   return (XMLEventReader2) f.createXMLEventReader(new StringReader(content));
 }
Exemple #22
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();
  }
 protected static void setValidating(XMLInputFactory f, boolean state) throws XMLStreamException {
   f.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.valueOf(state));
 }
 protected static void setCoalescing(XMLInputFactory f, boolean state) throws XMLStreamException {
   f.setProperty(XMLInputFactory.IS_COALESCING, Boolean.valueOf(state));
 }
 protected static boolean isNamespaceAware(XMLInputFactory f) throws XMLStreamException {
   return ((Boolean) f.getProperty(XMLInputFactory.IS_NAMESPACE_AWARE)).booleanValue();
 }
 protected static XMLInputFactory2 getNewInputFactory() {
   return (XMLInputFactory2) XMLInputFactory.newInstance();
 }
Exemple #27
0
 public static Element parse(InputStream inputStream) throws XMLStreamException {
   XMLInputFactory xif = XMLInputFactory.newFactory();
   return parse(xif.createXMLStreamReader(inputStream));
 }
 protected static boolean willSupportDTD(XMLInputFactory f) throws XMLStreamException {
   return ((Boolean) f.getProperty(XMLInputFactory.SUPPORT_DTD)).booleanValue();
 }
Exemple #29
0
 public static Element parse(InputStreamReader inputStreamReader)
     throws FileNotFoundException, XMLStreamException {
   XMLInputFactory xif = XMLInputFactory.newFactory();
   return parse(xif.createXMLStreamReader(inputStreamReader));
 }
 protected static XMLStreamReader2 constructStreamReader(XMLInputFactory f, byte[] data)
     throws XMLStreamException {
   return (XMLStreamReader2) f.createXMLStreamReader(new ByteArrayInputStream(data));
 }