@Override
  public String pullNextXmlChunk() throws KettleException {
    Stack<String> elementStack = xmlChunkerState.getElementStack();
    XMLStreamReader xmlStreamReader = xmlChunkerState.getXmlStreamReader();

    try {

      while (xmlStreamReader.hasNext()) {

        switch (xmlStreamReader.next()) {
          case XMLStreamConstants.END_DOCUMENT:
            return null;

          case XMLStreamConstants.END_ELEMENT:
            elementStack.pop();
            break;

          case XMLStreamConstants.START_ELEMENT:
            elementStack.push(xmlStreamReader.getLocalName());

            if (actualElementStackHasExpectedElements(xmlChunkerState)) {
              return pullNextXmlChunkFromTopElementOnStack(xmlChunkerState);
            }

            break;
        }
      }
    } catch (Exception e) {
      throw new KettleException("a problem has arisen reading the xero xml stream", e);
    }

    return null;
  }
 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;
 }
Beispiel #3
0
 private static Element parse(XMLStreamReader xsr) throws XMLStreamException {
   // we skip the events until we reach the root element
   while (xsr.getEventType() != XMLStreamConstants.START_ELEMENT) {
     xsr.next();
   }
   return parseElement(xsr);
 }
Beispiel #4
0
 private static Element parseElement(XMLStreamReader xsr) throws XMLStreamException {
   // xsr points to a START_ELEMENT event. Create the element and read all its attributes
   // Then read all its children events
   Element element = new Element(xsr.getLocalName());
   // text that will be added to the element. Text can come in different events, so we add it here
   // and add it to the element at the end
   StringBuilder elementText = new StringBuilder();
   int attributeCount = xsr.getAttributeCount();
   for (int i = 0; i < attributeCount; i++) {
     element.putAttribute(xsr.getAttributeLocalName(i), xsr.getAttributeValue(i));
   }
   while (xsr.hasNext()) {
     xsr.next();
     if (xsr.getEventType() == XMLStreamConstants.END_ELEMENT) {
       // element is closed. Move the cursor and return it
       // check if there is some text to add before (empty text is not added, but added text is not
       // trimmed)
       // we set empty text also if the element has no children
       if (!elementText.toString().trim().isEmpty() || !element.hasChildren()) {
         element.setText(elementText.toString());
       }
       //                xsr.next();
       return element;
     } else if (xsr.getEventType() == XMLStreamConstants.CHARACTERS) {
       // an attribute of the current element
       elementText.append(xsr.getText());
     } else if (xsr.getEventType() == XMLStreamConstants.START_ELEMENT) {
       // new element begins -> read it recursively and add it to the current element
       element.addChild(parseElement(xsr));
     }
   }
   // we reached the end of the document without the tag end -> error parsing
   throw new XMLStreamException(
       "End of the document unexpectedly reached. Element " + element.getName() + " not closed");
 }
  /** Deserializes the object from XML */
  public DataReported parseReported(XMLStreamReader in) throws IOException, XMLStreamException {
    DataReported reported = new DataReported();

    ArrayList<DataField> fieldList = new ArrayList<DataField>();

    int tag = in.nextTag();
    while (tag > 0) {
      if (_isFinest) debug(in);

      if (XMLStreamReader.END_ELEMENT == tag) {
        reported.setFieldList(fieldList);

        return reported;
      }

      if (XMLStreamReader.START_ELEMENT == tag && "field".equals(in.getLocalName())) {
        fieldList.add(parseField(in));
      } else if (XMLStreamReader.START_ELEMENT == tag) {
        log.finer(this + " <" + in.getLocalName() + "> is an unknown tag");

        skipToEnd(in, in.getLocalName());
      }

      tag = in.nextTag();
    }

    skipToEnd(in, "reported");

    return reported;
  }
Beispiel #6
0
 @Override
 protected void readAttributes(XMLStreamReader reader, MdmiBusinessElementRule object) {
   object.setName(reader.getAttributeValue(null, BusinessElemRuleValidate.s_nameField));
   object.setDescription(reader.getAttributeValue(null, BusinessElemRuleValidate.s_descName));
   object.setRuleExpressionLanguage(
       reader.getAttributeValue(null, BusinessElemRuleValidate.s_ruleLangName));
 }
 private final void expectTag(String expElem, XMLStreamReader sr) throws XMLStreamException {
   if (!expElem.equals(sr.getLocalName())) {
     throw new XMLStreamException(
         "Unexpected element <" + sr.getLocalName() + ">: expecting <" + expElem + ">",
         sr.getLocation());
   }
 }
Beispiel #8
0
 @Override
 protected void readAttributes(XMLStreamReader reader, MessageGroup object) {
   object.setName(reader.getAttributeValue(null, MessageGroupValidate.s_nameField));
   object.setDescription(reader.getAttributeValue(null, MessageGroupValidate.s_descField));
   object.setDefaultConstraintExprLang(
       reader.getAttributeValue(null, MessageGroupValidate.s_defConstrField));
   object.setDefaultLocationExprLang(
       reader.getAttributeValue(null, MessageGroupValidate.s_defLocField));
   object.setDefaultRuleExprLang(
       reader.getAttributeValue(null, MessageGroupValidate.s_defRuleField));
 }
Beispiel #9
0
 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());
   }
 }
Beispiel #10
0
 /** 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();
 }
  private String pullNextXmlChunkFromTopElementOnStack(XMLChunkerState data)
      throws KettleException {
    Stack<String> elementStack = data.getElementStack();
    XMLStreamReader xmlStreamReader = data.getXmlStreamReader();

    int elementStackDepthOnEntry = elementStack.size();
    StringWriter stringWriter = new StringWriter();

    try {
      XMLStreamWriter xmlStreamWriter =
          data.getXmlOutputFactory().createXMLStreamWriter(stringWriter);

      xmlStreamWriter.writeStartDocument(CharEncoding.UTF_8, "1.0");

      // put the current element on because presumably it's the open element for the one
      // that is being looked for.

      XmlReaderToWriter.write(xmlStreamReader, xmlStreamWriter);

      while (xmlStreamReader.hasNext() & elementStack.size() >= elementStackDepthOnEntry) {

        switch (xmlStreamReader.next()) {
          case XMLStreamConstants.END_DOCUMENT:
            break; // handled below explicitly.

          case XMLStreamConstants.END_ELEMENT:
            elementStack.pop();
            XmlReaderToWriter.write(xmlStreamReader, xmlStreamWriter);
            break;

          case XMLStreamConstants.START_ELEMENT:
            elementStack.push(xmlStreamReader.getLocalName());
            XmlReaderToWriter.write(xmlStreamReader, xmlStreamWriter);
            break;

          default:
            XmlReaderToWriter.write(xmlStreamReader, xmlStreamWriter);
            break;
        }
      }

      xmlStreamWriter.writeEndDocument();
      xmlStreamWriter.close();
    } catch (Exception e) {
      throw new KettleException("unable to process a chunk of the xero xml stream", e);
    }

    return stringWriter.toString();
  }
 /**
  * Method that can be used to verify that the current element pointed to by the stream reader does
  * not belong to a namespace.
  */
 protected static void assertElemNotInNamespace(XMLStreamReader sr) throws XMLStreamException {
   String uri = sr.getNamespaceURI();
   if (uri == null) {
     fail("Excepted empty String to indicate \"no namespace\": got null");
   } else if (uri.length() != 0) {
     fail("Excepted no (null) namespace URI: got '" + uri + "'");
   }
 }
Beispiel #13
0
 private static void readEndpoint(
     @NotNull XMLStreamReader reader, @NotNull EndPoint parent, @NotNull ClassLoader classLoader)
     throws XMLStreamException {
   reader.nextTag();
   while (reader.isStartElement()) {
     String localName = reader.getLocalName();
     switch (localName) {
       case "method":
         {
           String name = reader.getAttributeValue(null, "name");
           String handler = reader.getAttributeValue(null, "handler");
           try {
             parent.addHandler(
                 name,
                 Class.forName(handler, false, classLoader)
                     .asSubclass(Handler.class)
                     .newInstance());
           } catch (Exception e) {
             LOG.warn("Error creating instance of " + handler, e);
           }
           reader.nextTag();
           break;
         }
       case "endpoint":
         {
           String name = reader.getAttributeValue(null, "name");
           EndPoint endPoint = new EndPoint();
           readEndpoint(reader, endPoint, classLoader);
           parent.addChild(name, endPoint);
           break;
         }
       case "default":
         {
           EndPoint endPoint = new EndPoint();
           readEndpoint(reader, endPoint, classLoader);
           parent.setDefaultChild(endPoint);
           break;
         }
       default:
         throw new XMLStreamException("Unexpected element: " + localName, reader.getLocation());
     }
     reader.require(XMLStreamConstants.END_ELEMENT, null, localName);
     reader.nextTag();
   }
 }
 /**
  * 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"));
 }
  /**
   * Method that will iterate through contents of an XML document using specified stream reader;
   * will also access some of data to make sure reader reads most of lazy-loadable data. Method is
   * usually called to try to get an exception for invalid content.
   *
   * @return Dummy value calculated on contents; used to make sure no dead code is eliminated
   */
  protected int streamThrough(XMLStreamReader sr) throws XMLStreamException {
    int result = 0;

    while (sr.hasNext()) {
      int type = sr.next();
      result += type;
      if (sr.hasText()) {
        /* will also do basic verification for text content, to
         * see that all text accessor methods return same content
         */
        result += getAndVerifyText(sr).hashCode();
      }
      if (sr.hasName()) {
        result += sr.getName().hashCode();
      }
    }

    return result;
  }
  @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;
  }
Beispiel #17
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);
       }
     }
   }
 }
 /**
  * Method that can be used to verify that the current element pointed to by the stream reader has
  * no prefix.
  */
 protected static void assertNoElemPrefix(XMLStreamReader sr) throws XMLStreamException {
   String prefix = sr.getPrefix();
   if (prefix != ELEM_NO_PREFIX) {
     fail(
         "Element that does not have a prefix should be indicated with <"
             + ELEM_NO_PREFIX
             + ">, not <"
             + prefix
             + ">");
   }
 }
  private final DbRow readRow(XMLStreamReader sr) throws XMLStreamException {
    expectTag(FIELD_ROW, sr);
    DbRow row = new DbRow();
    while (sr.nextTag() == XMLStreamReader.START_ELEMENT) {
      String elemName = sr.getLocalName();
      String value = sr.getElementText();

      try {
        if (!row.assign(elemName, value)) {
          throw new XMLStreamException(
              "Unexpected element <" + elemName + ">: not one of recognized field names");
        }
      } catch (IllegalArgumentException iae) {
        throw new XMLStreamException(
            "Typed access problem with input '" + value + "': " + iae.getMessage(),
            sr.getLocation(),
            iae);
      }
    }
    return row;
  }
  /**
   * Method that not only gets currently available text from the reader, but also checks that its
   * consistenly accessible using different (basic) StAX methods.
   */
  protected static String getAndVerifyText(XMLStreamReader sr) throws XMLStreamException {
    /* 05-Apr-2006, TSa: Although getText() is available for DTD
     *   and ENTITY_REFERENCE, getTextXxx() are not. Thus, can not
     *   do more checks for those types.
     */
    int type = sr.getEventType();
    if (type == ENTITY_REFERENCE || type == DTD) {
      return sr.getText();
    }

    int expLen = sr.getTextLength();
    /* Hmmh. It's only ok to return empty text for DTD event... well,
     * maybe also for CDATA, since empty CDATA blocks are legal?
     */
    /* !!! 01-Sep-2004, TSa:
     *  note: theoretically, in coalescing mode, it could be possible
     *  to have empty CDATA section(s) get converted to CHARACTERS,
     *  which would be empty... may need to enhance this to check that
     *  mode is not coalescing? Or something
     */
    if (type == CHARACTERS) {
      assertTrue("Stream reader should never return empty Strings.", (expLen > 0));
    }
    String text = sr.getText();
    assertNotNull("getText() should never return null.", text);
    assertEquals(
        "Expected text length of " + expLen + ", got " + text.length(), expLen, text.length());
    char[] textChars = sr.getTextCharacters();
    int start = sr.getTextStart();
    String text2 = new String(textChars, start, expLen);
    assertEquals(text, text2);
    return text;
  }
  /**
   * @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();
      }
    }
  }
  public void testSimpleValidExternalSubset() throws XMLStreamException {
    String XML = "<!DOCTYPE root SYSTEM 'myurl' >" + "<root>text</root>";
    String EXT_ENTITY_VALUE = "just testing";
    String EXT_SUBSET = "<!ELEMENT root (#PCDATA)>\n" + "<!-- comments are ok!!! -->";

    XMLStreamReader sr = getReader(XML, true, new SimpleResolver(EXT_SUBSET));
    assertTokenType(DTD, sr.next());
    assertTokenType(START_ELEMENT, sr.next());
    assertEquals("root", sr.getLocalName());
    assertTokenType(CHARACTERS, sr.next());
    assertEquals("text", getAndVerifyText(sr));
    assertTokenType(END_ELEMENT, sr.next());
    sr.close();
  }
Beispiel #23
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);
      }
    }
  private static void printAttributes(XMLStreamReader xmlr) {

    if (xmlr.getAttributeCount() > 0) {

      int count = xmlr.getAttributeCount();
      for (int i = 0; i < count; i++) {

        QName name = xmlr.getAttributeName(i);
        String namespace = xmlr.getAttributeNamespace(i);
        String type = xmlr.getAttributeType(i);
        String prefix = xmlr.getAttributePrefix(i);
        String value = xmlr.getAttributeValue(i);

        System.out.println(
            "\tAttribute: {" + namespace + "}:" + name.toString() + "(" + type + ")=" + value);
      }
    }
  }
  /** Deserializes the object from XML */
  public DataOption parseOption(XMLStreamReader in) throws IOException, XMLStreamException {
    String label = in.getAttributeValue(null, "label");

    DataOption option = new DataOption(label);

    ArrayList<DataValue> valueList = new ArrayList<DataValue>();

    int tag = in.nextTag();
    while (tag > 0) {
      if (_isFinest) debug(in);

      if (XMLStreamReader.END_ELEMENT == tag) {
        option.setValueList(valueList);

        return option;
      }

      if (XMLStreamReader.START_ELEMENT == tag && "value".equals(in.getLocalName())) {
        String value = in.getElementText();

        valueList.add(new DataValue(value));

        skipToEnd(in, "value");
      } else if (XMLStreamReader.START_ELEMENT == tag) {
        log.finer(this + " <" + in.getLocalName() + "> is an unknown tag");

        skipToEnd(in, in.getLocalName());
      }

      tag = in.nextTag();
    }

    skipToEnd(in, "option");

    return option;
  }
Beispiel #26
0
 static {
   ClassLoader classLoader = APIServlet.class.getClassLoader();
   try (InputStream is = APIServlet.class.getResourceAsStream("endpoints.xml")) {
     assert is != null : "endpoints.xml is missing";
     XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(is);
     try {
       reader.nextTag();
       reader.require(XMLStreamConstants.START_ELEMENT, null, "endpoints");
       readEndpoint(reader, ROOT, classLoader);
       reader.require(XMLStreamConstants.END_ELEMENT, null, "endpoints");
     } catch (XMLStreamException e) {
       Location l = reader.getLocation();
       LOG.warn(
           "Error reading endpoints.xml [" + l.getLineNumber() + ", " + l.getColumnNumber() + ']',
           e);
     } finally {
       reader.close();
     }
   } catch (IOException | XMLStreamException e) {
     LOG.warn("Error reading endpoints.xml", e);
   }
 }
  /** Deserializes the object from XML */
  public DataField parseField(XMLStreamReader in) throws IOException, XMLStreamException {
    String label = in.getAttributeValue(null, "label");
    String type = in.getAttributeValue(null, "type");
    String var = in.getAttributeValue(null, "var");

    DataField field = new DataField(type, var, label);

    ArrayList<DataValue> valueList = new ArrayList<DataValue>();
    ArrayList<DataOption> optionList = new ArrayList<DataOption>();

    int tag = in.nextTag();
    while (tag > 0) {
      if (_isFinest) debug(in);

      if (XMLStreamReader.END_ELEMENT == tag) {
        field.setValueList(valueList);
        field.setOptionList(optionList);

        return field;
      }

      if (XMLStreamReader.START_ELEMENT == tag && "desc".equals(in.getLocalName())) {
        String desc = in.getElementText();

        field.setDesc(desc);

        skipToEnd(in, "desc");
      } else if (XMLStreamReader.START_ELEMENT == tag && "option".equals(in.getLocalName())) {
        optionList.add(parseOption(in));
      } else if (XMLStreamReader.START_ELEMENT == tag && "required".equals(in.getLocalName())) {
        field.setRequired(true);

        skipToEnd(in, "required");
      } else if (XMLStreamReader.START_ELEMENT == tag && "value".equals(in.getLocalName())) {
        String value = in.getElementText();

        valueList.add(new DataValue(value));

        skipToEnd(in, "value");
      } else if (XMLStreamReader.START_ELEMENT == tag) {
        log.finer(this + " <" + in.getLocalName() + "> is an unknown tag");

        skipToEnd(in, in.getLocalName());
      }

      tag = in.nextTag();
    }

    skipToEnd(in, "field");

    return field;
  }
Beispiel #28
0
 protected void assertTokenType(int expType, XMLStreamReader sr) throws XMLStreamException {
   assertTokenType(expType, sr.getEventType());
 }
  public void testDOMSource() throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();

    InputSource source = new InputSource(new StringReader(xml));
    Document doc = builder.parse(source);

    // Fails when using DOMWrappingReader
    XMLStreamReader reader = getInputFactory().createXMLStreamReader(new DOMSource(doc));

    reader.next(); // root
    assertEquals(0, reader.getAttributeCount());
    assertEquals(1, reader.getNamespaceCount());
    assertEquals("http://testnamespace/", reader.getNamespaceURI());
    assertEquals("ns2", reader.getPrefix());
    assertEquals("root", reader.getLocalName());

    reader.next(); // arg0
    reader.next(); // obj

    assertEquals("obj", reader.getLocalName());
    assertEquals(
        "ns2:mycomplextype",
        reader.getAttributeValue("http://www.w3.org/2001/XMLSchema-instance", "type"));
    assertEquals("http://testnamespace/", reader.getNamespaceURI("ns2"));
    assertEquals("http://testnamespace/", reader.getNamespaceContext().getNamespaceURI("ns2"));

    assertEquals("ns2", reader.getNamespaceContext().getPrefix("http://testnamespace/"));
  }