Java Code Examples for javax.xml.stream.events.Attribute#getValue()

The following examples show how to use javax.xml.stream.events.Attribute#getValue() . 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: CheckstyleConfigurationsParser.java    From contribution with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Parses single "module" tag.
 *
 * @param reader
 *        StAX parser interface.
 * @param startElement
 *        start element of the tag.
 * @param parent
 *        parent module instance.
 * @throws XMLStreamException
 *         on internal StAX failure.
 */
private static void processModuleTag(XMLEventReader reader, StartElement startElement,
        ConfigurationModule parent) throws XMLStreamException {
    String childModuleName = null;
    final Iterator<Attribute> attributes = startElement
            .getAttributes();
    while (attributes.hasNext()) {
        final Attribute attribute = attributes.next();
        if (attribute.getName().toString()
                .equals(NAME_ATTR)) {
            childModuleName = attribute.getValue();
        }
    }
    final ConfigurationModule childModule =
            new ConfigurationModule(childModuleName);
    parseModule(reader, childModule);
    parent.addChild(childModule);
}
 
Example 2
Source File: StaxUnmarshallerContext.java    From ibm-cos-sdk-java with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the text contents of the current element being parsed.
 *
 * @return The text contents of the current element being parsed.
 * @throws XMLStreamException
 */
public String readText() throws XMLStreamException {
    if (isInsideResponseHeader()) {
        return getHeader(currentHeader);
    }
    if (currentEvent.isAttribute()) {
        Attribute attribute = (Attribute)currentEvent;
        return attribute.getValue();
    }

    StringBuilder sb = new StringBuilder();
    while (true) {
        XMLEvent event = eventReader.peek();
        if (event.getEventType() == XMLStreamConstants.CHARACTERS) {
            eventReader.nextEvent();
            sb.append(event.asCharacters().getData());
        } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT) {
            return sb.toString();
        } else {
            throw new RuntimeException("Encountered unexpected event: " + event.toString());
        }
    }
}
 
Example 3
Source File: JpaOrmXmlEventReader.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private Attribute mapAttribute(StartElement startElement, Attribute originalAttribute) {
	// Here we look to see if this attribute is the JPA version attribute, and if so do 2 things:
	//		1) validate its version attribute is valid
	//		2) update its version attribute to the default version if not already
	//
	// NOTE : atm this is a very simple check using just the attribute's local name
	// rather than checking its qualified name.  It is possibly (though unlikely)
	// that this could match on "other" version attributes in the same element

	if ( ROOT_ELEMENT_NAME.equals( startElement.getName().getLocalPart() ) ) {
		if ( VERSION_ATTRIBUTE_NAME.equals( originalAttribute.getName().getLocalPart() ) ) {
			final String specifiedVersion = originalAttribute.getValue();

			if ( !LocalXsdResolver.isValidJpaVersion( specifiedVersion ) ) {
				throw new BadVersionException( specifiedVersion );
			}

			return xmlEventFactory.createAttribute( VERSION_ATTRIBUTE_NAME, LocalXsdResolver.latestJpaVerison() );
		}
	}

	return originalAttribute;
}
 
Example 4
Source File: DefParser.java    From OpenRTS with MIT License 5 votes vote down vote up
private Definition parseEvent(XMLEvent event, Definition def) {
	StartElement se = event.asStartElement();
	String elementName = se.getName().getLocalPart();
	if (elementName.equals("catalog")) {
		return null;
	}

	Iterator<Attribute> attributes = se.getAttributes();

	if (def == null) {
		Attribute id = attributes.next();
		if (id.getName().toString() != ID) {
			throw new RuntimeException("At line " + event.getLocation().getLineNumber() + ", problem with definition '" + elementName
					+ "'. The first attribute of a definition must be called '" + ID + "'.");
		}
		def = new Definition(elementName, id.getValue());
		// LogUtil.logger.info("def cree "+def.type+" - "+def.id);
	} else {
		DefElement de = new DefElement(elementName);
		while (attributes.hasNext()) {
			Attribute a = attributes.next();
			de.addVal(a.getName().toString(), a.getValue());
		}
		def.getElements().add(de);
		// LogUtil.logger.info("    element ajouté : "+de.name+" - "+de.getVal());
	}
	return def;
}
 
Example 5
Source File: StAXEvent2SAX.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the attributes associated with the given START_ELEMENT StAXevent.
 *
 * @return the StAX attributes converted to an org.xml.sax.Attributes
 */
private Attributes getAttributes(StartElement event) {
    AttributesImpl attrs = new AttributesImpl();

    if ( !event.isStartElement() ) {
        throw new InternalError(
            "getAttributes() attempting to process: " + event);
    }

    // in SAX, namespace declarations are not part of attributes by default.
    // (there's a property to control that, but as far as we are concerned
    // we don't use it.) So don't add xmlns:* to attributes.

    // gather non-namespace attrs
    for (Iterator i = event.getAttributes(); i.hasNext();) {
        Attribute staxAttr = (javax.xml.stream.events.Attribute)i.next();

        String uri = staxAttr.getName().getNamespaceURI();
        if (uri == null) {
            uri = "";
        }
        String localName = staxAttr.getName().getLocalPart();
        String prefix = staxAttr.getName().getPrefix();
        String qName;
        if (prefix == null || prefix.length() == 0) {
            qName = localName;
        } else {
            qName = prefix + ':' + localName;
        }
        String type = staxAttr.getDTDType();
        String value = staxAttr.getValue();

        attrs.addAttribute(uri, localName, qName, type, value);
    }

    return attrs;
}
 
Example 6
Source File: StAXEvent2SAX.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the attributes associated with the given START_ELEMENT StAXevent.
 *
 * @return the StAX attributes converted to an org.xml.sax.Attributes
 */
private Attributes getAttributes(StartElement event) {
    AttributesImpl attrs = new AttributesImpl();

    if ( !event.isStartElement() ) {
        throw new InternalError(
            "getAttributes() attempting to process: " + event);
    }

    // in SAX, namespace declarations are not part of attributes by default.
    // (there's a property to control that, but as far as we are concerned
    // we don't use it.) So don't add xmlns:* to attributes.

    // gather non-namespace attrs
    for (Iterator i = event.getAttributes(); i.hasNext();) {
        Attribute staxAttr = (javax.xml.stream.events.Attribute)i.next();

        String uri = staxAttr.getName().getNamespaceURI();
        if (uri == null) {
            uri = "";
        }
        String localName = staxAttr.getName().getLocalPart();
        String prefix = staxAttr.getName().getPrefix();
        String qName;
        if (prefix == null || prefix.length() == 0) {
            qName = localName;
        } else {
            qName = prefix + ':' + localName;
        }
        String type = staxAttr.getDTDType();
        String value = staxAttr.getValue();

        attrs.addAttribute(uri, localName, qName, type, value);
    }

    return attrs;
}
 
Example 7
Source File: StAXEvent2SAX.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the attributes associated with the given START_ELEMENT StAXevent.
 *
 * @return the StAX attributes converted to an org.xml.sax.Attributes
 */
private Attributes getAttributes(StartElement event) {
    AttributesImpl attrs = new AttributesImpl();

    if ( !event.isStartElement() ) {
        throw new InternalError(
            "getAttributes() attempting to process: " + event);
    }

    // in SAX, namespace declarations are not part of attributes by default.
    // (there's a property to control that, but as far as we are concerned
    // we don't use it.) So don't add xmlns:* to attributes.

    // gather non-namespace attrs
    for (Iterator i = event.getAttributes(); i.hasNext();) {
        Attribute staxAttr = (javax.xml.stream.events.Attribute)i.next();

        String uri = staxAttr.getName().getNamespaceURI();
        if (uri == null) {
            uri = "";
        }
        String localName = staxAttr.getName().getLocalPart();
        String prefix = staxAttr.getName().getPrefix();
        String qName;
        if (prefix == null || prefix.length() == 0) {
            qName = localName;
        } else {
            qName = prefix + ':' + localName;
        }
        String type = staxAttr.getDTDType();
        String value = staxAttr.getValue();

        attrs.addAttribute(uri, localName, qName, type, value);
    }

    return attrs;
}
 
Example 8
Source File: StAXEventConnector.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the attributes associated with the given START_ELEMENT StAXevent.
 *
 * @return the StAX attributes converted to an org.xml.sax.Attributes
 */
private Attributes getAttributes(StartElement event) {
    attrs.clear();

    // in SAX, namespace declarations are not part of attributes by default.
    // (there's a property to control that, but as far as we are concerned
    // we don't use it.) So don't add xmlns:* to attributes.

    // gather non-namespace attrs
    for (Iterator i = event.getAttributes(); i.hasNext();) {
        Attribute staxAttr = (Attribute)i.next();

        QName name = staxAttr.getName();
        String uri = fixNull(name.getNamespaceURI());
        String localName = name.getLocalPart();
        String prefix = name.getPrefix();
        String qName;
        if (prefix == null || prefix.length() == 0)
            qName = localName;
        else
            qName = prefix + ':' + localName;
        String type = staxAttr.getDTDType();
        String value = staxAttr.getValue();

        attrs.addAttribute(uri, localName, qName, type, value);
    }

    return attrs;
}
 
Example 9
Source File: StAXEventConnector.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the attributes associated with the given START_ELEMENT StAXevent.
 *
 * @return the StAX attributes converted to an org.xml.sax.Attributes
 */
private Attributes getAttributes(StartElement event) {
    attrs.clear();

    // in SAX, namespace declarations are not part of attributes by default.
    // (there's a property to control that, but as far as we are concerned
    // we don't use it.) So don't add xmlns:* to attributes.

    // gather non-namespace attrs
    for (Iterator i = event.getAttributes(); i.hasNext();) {
        Attribute staxAttr = (Attribute)i.next();

        QName name = staxAttr.getName();
        String uri = fixNull(name.getNamespaceURI());
        String localName = name.getLocalPart();
        String prefix = name.getPrefix();
        String qName;
        if (prefix == null || prefix.length() == 0)
            qName = localName;
        else
            qName = prefix + ':' + localName;
        String type = staxAttr.getDTDType();
        String value = staxAttr.getValue();

        attrs.addAttribute(uri, localName, qName, type, value);
    }

    return attrs;
}
 
Example 10
Source File: StAXEvent2SAX.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the attributes associated with the given START_ELEMENT StAXevent.
 *
 * @return the StAX attributes converted to an org.xml.sax.Attributes
 */
private Attributes getAttributes(StartElement event) {
    AttributesImpl attrs = new AttributesImpl();

    if ( !event.isStartElement() ) {
        throw new InternalError(
            "getAttributes() attempting to process: " + event);
    }

    // in SAX, namespace declarations are not part of attributes by default.
    // (there's a property to control that, but as far as we are concerned
    // we don't use it.) So don't add xmlns:* to attributes.

    // gather non-namespace attrs
    for (Iterator i = event.getAttributes(); i.hasNext();) {
        Attribute staxAttr = (javax.xml.stream.events.Attribute)i.next();

        String uri = staxAttr.getName().getNamespaceURI();
        if (uri == null) {
            uri = "";
        }
        String localName = staxAttr.getName().getLocalPart();
        String prefix = staxAttr.getName().getPrefix();
        String qName;
        if (prefix == null || prefix.length() == 0) {
            qName = localName;
        } else {
            qName = prefix + ':' + localName;
        }
        String type = staxAttr.getDTDType();
        String value = staxAttr.getValue();

        attrs.addAttribute(uri, localName, qName, type, value);
    }

    return attrs;
}
 
Example 11
Source File: CorbaStreamReader.java    From cxf with Apache License 2.0 5 votes vote down vote up
public String getAttributeValue(int arg0) {
    String ret = null;
    List<Attribute> currentAttributes = eventProducer.getAttributes();
    if (currentAttributes != null) {
        Attribute a = currentAttributes.get(arg0);
        if (a != null) {
            ret = a.getValue();
        }
    }
    return ret;
}
 
Example 12
Source File: StAXEventConnector.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the attributes associated with the given START_ELEMENT StAXevent.
 *
 * @return the StAX attributes converted to an org.xml.sax.Attributes
 */
private Attributes getAttributes(StartElement event) {
    attrs.clear();

    // in SAX, namespace declarations are not part of attributes by default.
    // (there's a property to control that, but as far as we are concerned
    // we don't use it.) So don't add xmlns:* to attributes.

    // gather non-namespace attrs
    for (Iterator i = event.getAttributes(); i.hasNext();) {
        Attribute staxAttr = (Attribute)i.next();

        QName name = staxAttr.getName();
        String uri = fixNull(name.getNamespaceURI());
        String localName = name.getLocalPart();
        String prefix = name.getPrefix();
        String qName;
        if (prefix == null || prefix.length() == 0)
            qName = localName;
        else
            qName = prefix + ':' + localName;
        String type = staxAttr.getDTDType();
        String value = staxAttr.getValue();

        attrs.addAttribute(uri, localName, qName, type, value);
    }

    return attrs;
}
 
Example 13
Source File: StaxParserUtil.java    From keycloak with Apache License 2.0 5 votes vote down vote up
/**
 * Given an {@code Attribute}, get its trimmed value, replacing every occurrence of ${..} by corresponding system property value
 *
 * @param attribute
 *
 * @return
 */
public static String getAttributeValueRP(Attribute attribute) {
    if (attribute == null) {
        return null;
    }

    final String value = attribute.getValue();

    return value == null ? null : trim(StringPropertyReplacer.replaceProperties(value));
}
 
Example 14
Source File: StAXEvent2SAX.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Get the attributes associated with the given START_ELEMENT StAXevent.
 *
 * @return the StAX attributes converted to an org.xml.sax.Attributes
 */
private Attributes getAttributes(StartElement event) {
    AttributesImpl attrs = new AttributesImpl();

    if ( !event.isStartElement() ) {
        throw new InternalError(
            "getAttributes() attempting to process: " + event);
    }

    // in SAX, namespace declarations are not part of attributes by default.
    // (there's a property to control that, but as far as we are concerned
    // we don't use it.) So don't add xmlns:* to attributes.

    // gather non-namespace attrs
    for (Iterator<Attribute> i = event.getAttributes(); i.hasNext();) {
        Attribute staxAttr = i.next();

        String uri = staxAttr.getName().getNamespaceURI();
        if (uri == null) {
            uri = "";
        }
        String localName = staxAttr.getName().getLocalPart();
        String prefix = staxAttr.getName().getPrefix();
        String qName;
        if (prefix == null || prefix.length() == 0) {
            qName = localName;
        } else {
            qName = prefix + ':' + localName;
        }
        String type = staxAttr.getDTDType();
        String value = staxAttr.getValue();

        attrs.addAttribute(uri, localName, qName, type, value);
    }

    return attrs;
}
 
Example 15
Source File: CheckstyleReportsParser.java    From contribution with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Parses portion of the XML report.
 *
 * @param diffReport
 *        container for parsed data.
 * @param reader
 *        StAX parser interface.
 * @param numOfFilenames
 *        number of "file" tags to parse.
 * @param index
 *        internal index of the parsed file.
 * @throws XMLStreamException
 *         on internal parser error.
 */
private static void parseXmlPortion(DiffReport diffReport,
        XMLEventReader reader, int numOfFilenames, int index)
                throws XMLStreamException {
    int counter = numOfFilenames;
    String filename = null;
    List<CheckstyleRecord> records = null;
    while (reader.hasNext()) {
        final XMLEvent event = reader.nextEvent();
        if (event.isStartElement()) {
            final StartElement startElement = event.asStartElement();
            final String startElementName = startElement.getName()
                    .getLocalPart();
            // file tag encounter
            if (startElementName.equals(FILE_TAG)) {
                counter--;
                diffReport.getStatistics().incrementFileCount(index);
                final Iterator<Attribute> attributes = startElement
                        .getAttributes();
                while (attributes.hasNext()) {
                    final Attribute attribute = attributes.next();
                    if (attribute.getName().toString()
                            .equals(FILENAME_ATTR)) {
                        filename = attribute.getValue();
                    }
                }
                records = new ArrayList<>();
            }
            // error tag encounter
            else if (startElementName.equals(ERROR_TAG)) {
                records.add(parseErrorTag(startElement, diffReport.getStatistics(), index,
                        filename));
            }
        }
        if (event.isEndElement()) {
            final EndElement endElement = event.asEndElement();
            if (endElement.getName().getLocalPart().equals(FILE_TAG)) {
                diffReport.addRecords(records, filename);
                if (counter == 0) {
                    break;
                }
            }
        }
    }
}
 
Example 16
Source File: SurefireReportReader.java    From smart-testing with Apache License 2.0 4 votes vote down vote up
private static Collection<TestResult> readTestResults(XMLEventReader eventReader) throws XMLStreamException {
    final Set<TestResult> testResults = new HashSet<>();
    TestResult currentTestResult = null;

    while (eventReader.hasNext()) {
        XMLEvent event = eventReader.nextEvent();

        if (event.isStartElement()) {
            final StartElement startElement = event.asStartElement();
            if ("testcase".equalsIgnoreCase(startElement.getName().getLocalPart())) {
                // Read attributes
                String name = null, classname = null;
                Status status = null;

                final Iterator<Attribute> attributes = startElement.getAttributes();

                while (attributes.hasNext()) {
                    final Attribute attribute = attributes.next();
                    if ("classname".equalsIgnoreCase(attribute.getName().toString())) {
                        classname = attribute.getValue();
                    }

                    if ("name".equalsIgnoreCase(attribute.getName().toString())) {
                        name = attribute.getValue();
                    }
                }

                currentTestResult = new TestResult(classname, name, Status.PASSED);
            }

            if ("failure".equalsIgnoreCase(startElement.getName().getLocalPart())) {
                currentTestResult.setStatus(Status.FAILURE);
            }

            if ("error".equalsIgnoreCase(startElement.getName().getLocalPart())) {
                currentTestResult.setStatus(Status.ERROR);
            }

            if ("skipped".equalsIgnoreCase(startElement.getName().getLocalPart())) {
                currentTestResult.setStatus(Status.SKIPPED);
            }

            if ("rerunFailure".equalsIgnoreCase(startElement.getName().getLocalPart())) {
                currentTestResult.setStatus(Status.RE_RUN_FAILURE);
            }
        }

        if (event.isEndElement()) {
            final EndElement endElementElement = event.asEndElement();
            if ("testcase".equalsIgnoreCase(endElementElement.getName().getLocalPart())) {
                testResults.add(currentTestResult);
            }
        }
    }
    return testResults;
}
 
Example 17
Source File: FileSourceGraphML.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
/**
 * <pre>
 * <!ELEMENT locator EMPTY>
 * <!ATTLIST locator
 *           xmlns:xlink   CDATA    #FIXED    "http://www.w3.org/TR/2000/PR-xlink-20001220/"
 *           xlink:href    CDATA    #REQUIRED
 *           xlink:type    (simple) #FIXED    "simple"
 * >
 * </pre>
 *
 * @return
 * @throws IOException
 * @throws XMLStreamException
 */
private Locator __locator() throws IOException, XMLStreamException {
	XMLEvent e;

	e = getNextEvent();
	checkValid(e, XMLEvent.START_ELEMENT, "locator");

	@SuppressWarnings ("unchecked") final Iterator<? extends Attribute> attributes =
			e.asStartElement().getAttributes();

	final Locator loc = new Locator();

	while (attributes.hasNext()) {
		final Attribute a = attributes.next();

		try {
			final LocatorAttribute attribute = LocatorAttribute.valueOf(toConstantName(a));

			switch (attribute) {
				case XMLNS_XLINK:
					loc.xlink = a.getValue();
					break;
				case XLINK_HREF:
					loc.href = a.getValue();
					break;
				case XLINK_TYPE:
					loc.type = a.getValue();
					break;
			}
		} catch (final IllegalArgumentException ex) {
			newParseError(e, false, "invalid locator attribute '%s'", a.getName().getLocalPart());
		}
	}

	e = getNextEvent();
	checkValid(e, XMLEvent.END_ELEMENT, "locator");

	if (loc.href == null) {
		newParseError(e, true, "locator requires an href");
	}

	return loc;
}
 
Example 18
Source File: FileSourceGraphML.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
/**
 * <pre>
 * <!ELEMENT data  (#PCDATA)>
 * <!ATTLIST data
 *           key      IDREF        #REQUIRED
 *           id       ID           #IMPLIED
 * >
 * </pre>
 *
 * @return
 * @throws IOException
 * @throws XMLStreamException
 */
private Data __data() throws IOException, XMLStreamException {
	XMLEvent e;

	e = getNextEvent();
	checkValid(e, XMLEvent.START_ELEMENT, "data");

	@SuppressWarnings ("unchecked") final Iterator<? extends Attribute> attributes =
			e.asStartElement().getAttributes();
	String key = null, id = null, value;

	while (attributes.hasNext()) {
		final Attribute a = attributes.next();

		try {
			final DataAttribute attribute = DataAttribute.valueOf(toConstantName(a));

			switch (attribute) {
				case KEY:
					key = a.getValue();
					break;
				case ID:
					id = a.getValue();
					break;
			}
		} catch (final IllegalArgumentException ex) {
			newParseError(e, false, "invalid attribute '%s' for '<data>'", a.getName().getLocalPart());
		}
	}

	if (key == null) {
		newParseError(e, true, "'<data>' element must have a 'key' attribute");
	}

	value = __characters();

	e = getNextEvent();
	checkValid(e, XMLEvent.END_ELEMENT, "data");

	if (!keys.containsKey(key)) {
		newParseError(e, true, "unknown key '%s'", key);
	}

	final Data d = new Data();

	d.key = keys.get(key);
	d.id = id;
	d.value = value;

	return d;
}
 
Example 19
Source File: SearchHandle.java    From java-client-api with Apache License 2.0 4 votes vote down vote up
private String getAttribute(StartElement element, String name) {
  Attribute att = element.getAttributeByName(new QName(name));
  return (att != null) ? att.getValue() : null;
}
 
Example 20
Source File: XmlMerger.java    From aion-germany with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Extract an attribute value from a <code>StartElement </code> event.
 *
 * @param element
 *            Event object.
 * @param name
 *            Attribute QName
 * @param def
 *            Default value.
 * @param onErrorMessage
 *            On error message.
 * @return attribute value
 * @throws XMLStreamException
 *             if attribute is missing and there is no default value set.
 */
private String getAttributeValue(StartElement element, QName name, String def, String onErrorMessage) throws XMLStreamException {
	Attribute attribute = element.getAttributeByName(name);

	if (attribute == null) {
		if (def == null) {
			throw new XMLStreamException(onErrorMessage, element.getLocation());
		}

		return def;
	}

	return attribute.getValue();
}