Java Code Examples for javax.xml.stream.XMLStreamReader#START_ELEMENT

The following examples show how to use javax.xml.stream.XMLStreamReader#START_ELEMENT . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XMLStreamReaderTest.java    From FHIR with Apache License 2.0 6 votes vote down vote up
private static void debug(XMLStreamReader reader) throws XMLStreamException {
    int eventType = reader.getEventType();
    switch (eventType) {
    case XMLStreamReader.START_DOCUMENT:
        System.out.println("START_DOCUMENT");
        break;
    case XMLStreamReader.START_ELEMENT:
        System.out.println("START_ELEMENT: " + reader.getLocalName());
        break;
    case XMLStreamReader.SPACE:
        System.out.println("SPACE");
        break;
    case XMLStreamReader.CHARACTERS:
        System.out.println("CHARACTERS");
        break;
    case XMLStreamReader.END_ELEMENT:
        System.out.println("END_ELEMENT: " + reader.getLocalName());
        break;
    case XMLStreamReader.END_DOCUMENT:
        System.out.println("END_DOCUMENT");
        break;
    default:
        System.out.println("OTHER: " + eventType);
        break;
    }
}
 
Example 2
Source File: TestCharacterLimits.java    From woodstox with Apache License 2.0 6 votes vote down vote up
public void testLongElementText() throws Exception {
    try {
        Reader reader = createLongReader("", "", false);
        XMLInputFactory factory = getNewInputFactory();
        factory.setProperty(WstxInputProperties.P_MAX_TEXT_LENGTH, Integer.valueOf(100000));
        XMLStreamReader xmlreader = factory.createXMLStreamReader(reader);
        while (xmlreader.next() != XMLStreamReader.START_ELEMENT) {
        }
        assertEquals(XMLStreamReader.CHARACTERS, xmlreader.next());
        while (xmlreader.next() != XMLStreamReader.START_ELEMENT) {
        }
        fail("Should have failed");
    } catch (XMLStreamException ex) {
        _verifyTextLimitException(ex);
    }
}
 
Example 3
Source File: IfcXmlDeserializer.java    From IfcPlugins with GNU Affero General Public License v3.0 6 votes vote down vote up
private void parseIso_10303_28(XMLStreamReader reader) throws DeserializeException {
	try {
		while (reader.hasNext()) {
			reader.next();
			if (reader.getEventType() == XMLStreamReader.START_ELEMENT) {
				if (reader.getLocalName().equalsIgnoreCase("uos")) {
					parseUos(reader);
				}
			} else if (reader.getEventType() == XMLStreamReader.END_ELEMENT) {
				if (reader.getLocalName().equals("iso_10303_28")) {
					return;
				}
			}
		}
	} catch (XMLStreamException e) {
		throw new DeserializeException(e);
	}
}
 
Example 4
Source File: XMLStreamReaderTest.java    From FHIR with Apache License 2.0 6 votes vote down vote up
private static void parseNarrative(java.lang.String elementName, XMLStreamReader reader) throws XMLStreamException {        
    while (reader.hasNext()) {
        int eventType = reader.next();
        debug(reader);

        switch (eventType) {
        case XMLStreamReader.START_ELEMENT:
            java.lang.String localName = reader.getLocalName();
            switch (localName) {
            case "status":
                parseString("status", reader);
                break;
            case "div":
                java.lang.String div = XMLSupport.parseDiv(reader);
                System.out.println("    div: " + div);
                break;
            }
            break;
        case XMLStreamReader.END_ELEMENT:
            if (reader.getLocalName().equals(elementName)) {
                return;
            }
            break;
        }
    }        
}
 
Example 5
Source File: BooksReader.java    From softwarecave with GNU General Public License v3.0 6 votes vote down vote up
private Book readBook(XMLStreamReader reader) throws XMLStreamException {
    Book book = new Book("", "", null, "", 0);
    book.setLanguage(reader.getAttributeValue(null, "language"));
    
    while (reader.hasNext()) {
        int eventType = reader.next();
        switch (eventType) {
            case XMLStreamReader.START_ELEMENT:
                String elementName = reader.getLocalName();
                if (elementName.equals("authors"))
                    book.setAuthors(readAuthors(reader));
                else if (elementName.equals("title"))
                    book.setTitle(readCharacters(reader));
                else if (elementName.equals("category"))
                    book.setCategory(readCategory(reader));
                else if (elementName.equals("year"))
                    book.setYear(readInt(reader));
                break;
            case XMLStreamReader.END_ELEMENT:
                return book;
        }
    }
    throw new XMLStreamException("Premature end of file");
}
 
Example 6
Source File: ResponseBuilder.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
final Object readResponse(Object[] args, XMLStreamReader r, AttachmentSet att) throws JAXBException {
    Object obj;
    AttachmentUnmarshallerImpl au = (att != null)?new AttachmentUnmarshallerImpl(att):null;
    if (bridge instanceof RepeatedElementBridge) {
        RepeatedElementBridge rbridge = (RepeatedElementBridge)bridge;
        ArrayList list = new ArrayList();
        QName name = r.getName();
        while (r.getEventType()==XMLStreamReader.START_ELEMENT && name.equals(r.getName())) {
            list.add(rbridge.unmarshal(r, au));
            XMLStreamReaderUtil.toNextTag(r, name);
        }
        obj = rbridge.collectionHandler().convert(list);
    } else {
        obj = bridge.unmarshal(r, au);
    }
    return setter.put(obj,args);
}
 
Example 7
Source File: SAAJMessage.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
protected XMLStreamReader soapBodyFirstChildReader() {
    if (soapBodyFirstChildReader != null) return soapBodyFirstChildReader;
    soapBodyFirstChild();
    if (soapBodyFirstChild != null) {
        soapBodyFirstChildReader = getXMLStreamReader(soapBodyFirstChild);
        if (soapBodyFirstChildReader == null) soapBodyFirstChildReader =
            createXMLStreamReader(soapBodyFirstChild);
        if (soapBodyFirstChildReader.getEventType() == XMLStreamReader.START_DOCUMENT) {
            try {
                while(soapBodyFirstChildReader.getEventType() != XMLStreamReader.START_ELEMENT)
                    soapBodyFirstChildReader.next();
            } catch (XMLStreamException e) {
                throw new RuntimeException(e);
            }
        }
        return soapBodyFirstChildReader;
    } else {
        payloadLocalName = null;
        payloadNamespace = null;
        return null;
    }
}
 
Example 8
Source File: EndpointArgumentsBuilder.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
protected void readWrappedRequest(Message msg, Object[] args) throws JAXBException, XMLStreamException {
    if (!msg.hasPayload()) {
        throw new WebServiceException("No payload. Expecting payload with "+wrapperName+" element");
    }
    XMLStreamReader reader = msg.readPayload();
    XMLStreamReaderUtil.verifyTag(reader,wrapperName);
    reader.nextTag();
    while(reader.getEventType()==XMLStreamReader.START_ELEMENT) {
        // TODO: QName has a performance issue
        QName name = reader.getName();
        WrappedPartBuilder part = wrappedParts.get(name);
        if(part==null) {
            // no corresponding part found. ignore
            XMLStreamReaderUtil.skipElement(reader);
            reader.nextTag();
        } else {
            part.readRequest(args,reader, msg.getAttachments());
        }
        XMLStreamReaderUtil.toNextTag(reader, name);
    }

    // we are done with the body
    reader.close();
    XMLStreamReaderFactory.recycle(reader);
}
 
Example 9
Source File: Jaxb2CollectionHttpMessageConverter.java    From java-technology-stack with MIT License 5 votes vote down vote up
private int moveToNextElement(XMLStreamReader streamReader) throws XMLStreamException {
	int event = streamReader.getEventType();
	while (event != XMLStreamReader.START_ELEMENT && event != XMLStreamReader.END_DOCUMENT) {
		event = streamReader.next();
	}
	return event;
}
 
Example 10
Source File: StreamReaderBufferCreator.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates the buffer from a stream reader that is an element fragment.
 * <p>
 * The stream reader will be moved to the position of the next start of
 * an element if the stream reader is not already positioned at the start
 * of an element.
 * <p>
 * The element and all its children will be stored and after storing the stream
 * will be positioned at the next event after the end of the element.
 * <p>
 * @param storeInScopeNamespaces true if in-scope namespaces of the element
 *        fragment should be stored.
 * @return the mutable stream buffer.
 * @throws XMLStreamException if the stream reader cannot be positioned at
 *         the start of an element.
 */
public MutableXMLStreamBuffer createElementFragment(XMLStreamReader reader,
        boolean storeInScopeNamespaces) throws XMLStreamException {
    if (_buffer == null) {
        createBuffer();
    }

    if (!reader.hasNext()) {
        return _buffer;
    }

    _storeInScopeNamespacesOnElementFragment = storeInScopeNamespaces;

    _eventType = reader.getEventType();
    if (_eventType != XMLStreamReader.START_ELEMENT) {
        do {
            _eventType = reader.next();
        } while(_eventType != XMLStreamReader.START_ELEMENT && _eventType != XMLStreamReader.END_DOCUMENT);
    }

    if (storeInScopeNamespaces) {
        _inScopePrefixes = new HashMap<String,Integer>();
    }

    storeElementAndChildren(reader);

    return getXMLStreamBuffer();
}
 
Example 11
Source File: VespaXMLFieldReader.java    From vespa with Apache License 2.0 5 votes vote down vote up
public <T extends FieldValue> void read(FieldBase field, WeightedSet<T> value) {
    try {
        while (reader.hasNext()) {
            int type = reader.next();

            if (type == XMLStreamReader.START_ELEMENT) {
                if ("item".equals(reader.getName().toString())) {
                    FieldValue fv = value.getDataType().getNestedType().createFieldValue();

                    int weight = 1;
                    for (int i = 0; i < reader.getAttributeCount(); i++) {
                        if ("weight".equals(reader.getAttributeName(i).toString())) {
                            weight = Integer.parseInt(reader.getAttributeValue(i));
                        }
                    }

                    deserializeFieldValue(field, fv);
                    // noinspection unchecked
                    value.put((T)fv, weight);
                    skipToEnd("item");
                } else {
                    throw newDeserializeException(field, "Illegal tag " + reader.getName() + " expected 'item'");
                }
            } else if (type == XMLStreamReader.END_ELEMENT) {
                return;
            }
        }
    } catch (XMLStreamException e) {
        throw newException(field, e);
    }
}
 
Example 12
Source File: XMLReaderComposite.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int getEventType() {
    switch (state) {
    case StartTag: return XMLStreamReader.START_ELEMENT;
    case EndTag: return XMLStreamReader.END_ELEMENT;
    case Payload:
    default:
        return payloadReader.getEventType();
    }
}
 
Example 13
Source File: XMLReaderComposite.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int nextTag() throws XMLStreamException {
    int e = next();
    if (e == XMLStreamReader.END_DOCUMENT) return e;
    while (e != XMLStreamReader.END_DOCUMENT) {
        if (e == XMLStreamReader.START_ELEMENT) return e;
        if (e == XMLStreamReader.END_ELEMENT) return e;
        e = next();
    }
    return e;
}
 
Example 14
Source File: XMLReaderComposite.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int nextTag() throws XMLStreamException {
    int e = next();
    if (e == XMLStreamReader.END_DOCUMENT) return e;
    while (e != XMLStreamReader.END_DOCUMENT) {
        if (e == XMLStreamReader.START_ELEMENT) return e;
        if (e == XMLStreamReader.END_ELEMENT) return e;
        e = next();
    }
    return e;
}
 
Example 15
Source File: FHIRPathSpecTest.java    From FHIR with Apache License 2.0 5 votes vote down vote up
@DataProvider
public static Object[][] provideAllTestData() throws Exception {
    List<Object[]> testData = new ArrayList<>();

    Resource observation = TestUtil.readLocalResource("FHIRPath/input/observation-example.xml");
    Resource patient = TestUtil.readLocalResource("FHIRPath/input/patient-example.xml");
    Resource questionnaire = TestUtil.readLocalResource("FHIRPath/input/questionnaire-example.xml");
    Resource valueset = TestUtil.readLocalResource("FHIRPath/input/valueset-example-expansion.xml");
    observationContext = new EvaluationContext(observation);
    patientContext = new EvaluationContext(patient);
    questionnaireContext = new EvaluationContext(questionnaire);
    valuesetContext = new EvaluationContext(valueset);

    InputStream testFile = TestUtil.resolveFileLocation("FHIRPath/tests-fhir-r4.xml");
    testFileReader = XMLSupport.createXMLStreamReader(testFile);

    while (testFileReader.hasNext()) {
        switch (testFileReader.next()) {
        case XMLStreamReader.START_ELEMENT:
            String localName = testFileReader.getLocalName();
            switch (localName) {
            case "group":
                currentGroupName = testFileReader.getAttributeValue(null, "name");
                currentGroupDescription = testFileReader.getAttributeValue(null, "description");
                // TODO: use the groupName to define a TestNG group (or suite)
                break;
            case "test":
                testData.add(createSingleTest());
                break;
            default:
                break;
            }
        }
    }
    return testData.toArray(new Object[testData.size()][]);
}
 
Example 16
Source File: ResourceExtension.java    From java-client-api with Apache License 2.0 5 votes vote down vote up
public boolean isCorrect(String word, String... uris) {
  try {
    RequestParameters params = new RequestParameters();
    params.add("service", "is-correct");
    params.add("word",    word);
    params.add("uris",    uris);

    XMLStreamReaderHandle readHandle = new XMLStreamReaderHandle();

    // call the service
    getServices().get(params, readHandle);

    QName correctName = new QName(XMLConstants.DEFAULT_NS_PREFIX, "correct");

    XMLStreamReader streamReader = readHandle.get();
    while (streamReader.hasNext()) {
      int current = streamReader.next();
      if (current == XMLStreamReader.START_ELEMENT) {
        if (correctName.equals(streamReader.getName())) {
          return "true".equals(streamReader.getElementText());
        }
      }
    }

    return false;
  } catch(XMLStreamException ex) {
    throw new RuntimeException(ex);
  }
}
 
Example 17
Source File: XMLReaderComposite.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int getEventType() {
    switch (state) {
    case StartTag: return XMLStreamReader.START_ELEMENT;
    case EndTag: return XMLStreamReader.END_ELEMENT;
    case Payload:
    default:
        return payloadReader.getEventType();
    }
}
 
Example 18
Source File: DataReader.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Reads all relevant sub elements that match the columns specified by the given table object from the xml reader into the given bean.
 *  
 * @param xmlReader The reader
 * @param data      Where to store the values
 */
private void readColumnSubElements(XMLStreamReader xmlReader, Map data) throws XMLStreamException, DdlUtilsXMLException
{
    int eventType = XMLStreamReader.START_ELEMENT;

    while (eventType != XMLStreamReader.END_ELEMENT)
    {
        eventType = xmlReader.next();
        if (eventType == XMLStreamReader.START_ELEMENT)
        {
            readColumnSubElement(xmlReader, data);
        }
    }
}
 
Example 19
Source File: IfcXmlDeserializer.java    From IfcPlugins with GNU Affero General Public License v3.0 4 votes vote down vote up
private void parseHeader(XMLStreamReader reader) throws XMLStreamException {
	/*
	 * 
	 * <name>Lise-Meitner-Str.1, 10589 Berlin</name>
   <time_stamp>2015-10-05T10:20:47.3587387+02:00</time_stamp>
   <author>Lellonek</author>
   <organization>eTASK Service-Management GmbH</organization>
   <preprocessor_version>.NET API etask.ifc</preprocessor_version>
   <originating_system>.NET API etask.ifc</originating_system>
   <authorization>file created with .NET API etask.ifc</authorization>
   <documentation>ViewDefinition [notYetAssigned]</documentation>
	 */
	
	IfcHeader ifcHeader = model.getModelMetaData().getIfcHeader();
	if (ifcHeader == null) {
		ifcHeader = StoreFactory.eINSTANCE.createIfcHeader();
		model.getModelMetaData().setIfcHeader(ifcHeader);
	}

	while (reader.hasNext()) {
		reader.next();
		if (reader.getEventType() == XMLStreamReader.START_ELEMENT) {
			String localname = reader.getLocalName();
			if (localname.equalsIgnoreCase("name")) {
				reader.next();
				ifcHeader.setFilename(reader.getText());
			} else if (localname.equals("time_stamp")) {
				reader.next();
				try {
					ifcHeader.setTimeStamp(DatatypeFactory
					  .newInstance()
					  .newXMLGregorianCalendar(reader.getText()).toGregorianCalendar().getTime());
				} catch (DatatypeConfigurationException e1) {
					e1.printStackTrace();
				}
			} else if (localname.equals("author")) {
				reader.next();
				ifcHeader.getAuthor().add(reader.getText());
			} else if (localname.equals("organization")) {
				reader.next();
				ifcHeader.getOrganization().add(reader.getText());
			} else if (localname.equals("preprocessor_version")) {
				reader.next();
				ifcHeader.setPreProcessorVersion(reader.getText());
			} else if (localname.equals("originating_system")) {
				reader.next();
				ifcHeader.setOriginatingSystem(reader.getText());
			} else if (localname.equals("authorization")) {
				reader.next();
				ifcHeader.setAuthorization(reader.getText());
			} else if (localname.equals("documentation")) {
			}
		} else if (reader.getEventType() == XMLStreamReader.END_ELEMENT) {
			if (reader.getLocalName().equals("header")) {
				return;
			}
		}
	}
}
 
Example 20
Source File: VespaXMLFieldReader.java    From vespa with Apache License 2.0 4 votes vote down vote up
public void read(FieldBase field, Struct value) {
    try {
        boolean base64 = isBase64EncodedElement(reader);
        boolean foundField = false;
        StringBuilder positionBuilder = null;
        while (reader.hasNext()) {
            int type = reader.next();
            if (type == XMLStreamReader.START_ELEMENT) {
                Field structField = value.getField(reader.getName().toString());
                if (structField == null) {
                    throw newDeserializeException(field, "Field " + reader.getName() + " not found.");
                }
                FieldValue fieldValue = structField.getDataType().createFieldValue();
                fieldValue.deserialize(structField, this);
                value.setFieldValue(structField, fieldValue);
                skipToEnd(structField.getName());
                foundField = true;
            } else if (type == XMLStreamReader.CHARACTERS) {
                if (foundField) {
                    continue;
                }
                // The text of an XML element may be output using 1-n CHARACTERS
                // events, so we have to buffer up until the end of the element to
                // ensure we get everything.
                String chars = reader.getText();
                if (positionBuilder == null) {
                    positionBuilder = new StringBuilder(chars);
                } else {
                    positionBuilder.append(chars);
                }
            } else if (type == XMLStreamReader.END_ELEMENT) {
                if (positionBuilder != null) {
                    assignPositionFieldFromStringIfNonEmpty(value, positionBuilder.toString(), base64);
                }
                break;
            }
        }
    } catch (XMLStreamException e) {
        throw newException(field, e);
    }
}