下面列出了javax.xml.stream.events.Attribute#getValue ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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);
}
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;
}
/**
* 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());
}
}
}
/**
* 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;
}
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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
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;
}
/**
* 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));
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
}
}
}
}
private String getAttribute(StartElement element, String name) {
Attribute att = element.getAttributeByName(new QName(name));
return (att != null) ? att.getValue() : null;
}
/**
* <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;
}
/**
* <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;
}
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;
}
/**
* 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();
}