use of javax.xml.stream.events.Attribute in project sirix by sirixdb.
the class WikipediaImport method checkStAXStartElement.
/**
* Check if start element of two StAX parsers match.
*
* @param startTag StartTag of the StAX parser, where it is currently (the "real" StAX parser over
* the whole document)
* @param elem StartTag to check against
* @return {@code true} if start elements match, {@code false} otherwise
* @throws XMLStreamException handling XML Stream Exception
*/
private static boolean checkStAXStartElement(final StartElement startTag, final StartElement elem) throws XMLStreamException {
assert startTag != null && elem != null;
boolean retVal = false;
if (startTag.getEventType() == XMLStreamConstants.START_ELEMENT && startTag.getName().equals(elem.getName())) {
// Check attributes.
boolean foundAtts = false;
boolean hasAtts = false;
for (final Iterator<?> itStartTag = startTag.getAttributes(); itStartTag.hasNext(); ) {
hasAtts = true;
final Attribute attStartTag = (Attribute) itStartTag.next();
for (final Iterator<?> itElem = elem.getAttributes(); itElem.hasNext(); ) {
final Attribute attElem = (Attribute) itElem.next();
if (attStartTag.getName().equals(attElem.getName()) && attStartTag.getName().equals(attElem.getName())) {
foundAtts = true;
break;
}
}
if (!foundAtts) {
break;
}
}
if (!hasAtts) {
foundAtts = true;
}
// Check namespaces.
boolean foundNamesps = false;
boolean hasNamesps = false;
for (final Iterator<?> itStartTag = startTag.getNamespaces(); itStartTag.hasNext(); ) {
hasNamesps = true;
final Namespace nsStartTag = (Namespace) itStartTag.next();
for (final Iterator<?> itElem = elem.getNamespaces(); itElem.hasNext(); ) {
final Namespace nsElem = (Namespace) itElem.next();
if (nsStartTag.getName().equals(nsElem.getName()) && nsStartTag.getName().equals(nsElem.getName())) {
foundNamesps = true;
break;
}
}
if (!foundNamesps) {
break;
}
}
if (!hasNamesps) {
foundNamesps = true;
}
// Check if qname, atts and namespaces are the same.
if (foundAtts && foundNamesps) {
retVal = true;
} else {
retVal = false;
}
}
return retVal;
}
use of javax.xml.stream.events.Attribute in project sirix by sirixdb.
the class XMLShredder method addNewElement.
/**
* Add a new element node.
*
* @param pLeftSiblingKeyStack stack used to determine if the new element has to be inserted as a
* right sibling or as a new child (in the latter case is NULL on top of the stack)
* @param event the current event from the StAX parser
* @return the modified stack
* @throws SirixException if adding {@link ElementNode} fails
*/
private void addNewElement(final StartElement event) throws SirixException {
assert event != null;
final QName qName = event.getName();
final QNm name = new QNm(qName.getNamespaceURI(), qName.getPrefix(), qName.getLocalPart());
processStartTag(name);
// Parse namespaces.
for (final Iterator<?> it = event.getNamespaces(); it.hasNext(); ) {
final Namespace namespace = (Namespace) it.next();
mWtx.insertNamespace(new QNm(namespace.getNamespaceURI(), namespace.getPrefix(), ""));
mWtx.moveToParent();
}
// Parse attributes.
for (final Iterator<?> it = event.getAttributes(); it.hasNext(); ) {
final Attribute attribute = (Attribute) it.next();
final QName attName = attribute.getName();
mWtx.insertAttribute(new QNm(attName.getNamespaceURI(), attName.getPrefix(), attName.getLocalPart()), attribute.getValue());
mWtx.moveToParent();
}
}
use of javax.xml.stream.events.Attribute in project sirix by sirixdb.
the class XMLUpdateShredder method checkElement.
/**
* Check if current element matches the element in the shreddered file.
*
* @param mEvent StartElement event, from the XML file to shredder.
* @return true if they are equal, false otherwise.
*/
private boolean checkElement(final StartElement mEvent) {
assert mEvent != null;
boolean retVal = false;
// Matching element names?
final QName name = mEvent.getName();
final QNm currName = mWtx.getName();
if (mWtx.getKind() == Kind.ELEMENT && currName.getNamespaceURI().equals(name.getNamespaceURI()) && currName.getLocalName().equals(name.getLocalPart())) {
// Check if atts and namespaces are the same.
final long nodeKey = mWtx.getNodeKey();
// Check attributes.
boolean foundAtts = false;
boolean hasAtts = false;
for (final Iterator<?> it = mEvent.getAttributes(); it.hasNext(); ) {
hasAtts = true;
final Attribute attribute = (Attribute) it.next();
for (int i = 0, attCount = mWtx.getAttributeCount(); i < attCount; i++) {
mWtx.moveToAttribute(i);
final QName attName = attribute.getName();
final QNm currAttName = mWtx.getName();
if (attName.getNamespaceURI().equals(currAttName.getNamespaceURI()) && attName.getLocalPart().equals(currAttName.getLocalName()) && attribute.getValue().equals(mWtx.getValue())) {
foundAtts = true;
mWtx.moveTo(nodeKey);
break;
}
mWtx.moveTo(nodeKey);
}
if (!foundAtts) {
break;
}
}
if (!hasAtts && mWtx.getAttributeCount() == 0) {
foundAtts = true;
}
// Check namespaces.
boolean foundNamesps = false;
boolean hasNamesps = false;
for (final Iterator<?> namespIt = mEvent.getNamespaces(); namespIt.hasNext(); ) {
hasNamesps = true;
final Namespace namespace = (Namespace) namespIt.next();
for (int i = 0, namespCount = mWtx.getNamespaceCount(); i < namespCount; i++) {
mWtx.moveToNamespace(i);
if (namespace.getNamespaceURI().equals(mWtx.nameForKey(mWtx.getURIKey()))) {
final String prefix = namespace.getPrefix();
if (prefix.isEmpty()) {
foundNamesps = true;
mWtx.moveTo(nodeKey);
break;
} else if (prefix.equals(mWtx.nameForKey(mWtx.getPrefixKey()))) {
foundNamesps = true;
mWtx.moveTo(nodeKey);
break;
}
}
mWtx.moveTo(nodeKey);
}
if (!foundNamesps) {
break;
}
}
if (!hasNamesps && mWtx.getNamespaceCount() == 0) {
foundNamesps = true;
}
// Check if atts and namespaces are the same.
if (foundAtts && foundNamesps) {
retVal = true;
} else {
retVal = false;
}
}
return retVal;
}
use of javax.xml.stream.events.Attribute in project bw-calendar-engine by Bedework.
the class DmozStructure method processStartElement.
void processStartElement(XMLEventReader reader, StartElement element) {
QName elname = element.getName();
if (skipEls.containsKey(elname)) {
skippingElement = elname;
return;
}
if (elname.equals(qnTopic)) {
processTopicStart(element);
return;
}
if (elname.equals(qnAltlang)) {
processAltLang(element);
return;
}
if (elname.equals(qnAltlang1)) {
processAltLang(element);
return;
}
if (elname.equals(qnDescription)) {
curText = new StringBuilder();
return;
}
if (elname.equals(qnTitle)) {
curText = new StringBuilder();
return;
}
if (elname.equals(qnNarrow)) {
if (curTopic != null) {
curTopic.narrows.add(getResource(element));
}
return;
}
if (elname.equals(qnNarrow1)) {
if (curTopic != null) {
curTopic.narrow1s.add(getResource(element));
}
return;
}
if (elname.equals(qnNarrow2)) {
if (curTopic != null) {
curTopic.narrow2s.add(getResource(element));
}
return;
}
info("Start Element: " + elname);
Iterator iterator = element.getAttributes();
while (iterator.hasNext()) {
Attribute attribute = (Attribute) iterator.next();
QName name = attribute.getName();
String value = attribute.getValue();
System.out.println("Attribute name/value: " + name + "/" + value);
}
}
use of javax.xml.stream.events.Attribute in project tutorials by eugenp.
the class StaxParser method getAllTutorial.
public List<Tutorial> getAllTutorial() {
boolean bTitle = false;
boolean bDescription = false;
boolean bDate = false;
boolean bAuthor = false;
List<Tutorial> tutorials = new ArrayList<Tutorial>();
try {
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLEventReader eventReader = factory.createXMLEventReader(new FileReader(this.getFile()));
Tutorial current = null;
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
switch(event.getEventType()) {
case XMLStreamConstants.START_ELEMENT:
StartElement startElement = event.asStartElement();
String qName = startElement.getName().getLocalPart();
if (qName.equalsIgnoreCase("tutorial")) {
current = new Tutorial();
Iterator<Attribute> attributes = startElement.getAttributes();
while (attributes.hasNext()) {
Attribute currentAt = attributes.next();
if (currentAt.getName().toString().equalsIgnoreCase("tutId")) {
current.setTutId(currentAt.getValue());
} else if (currentAt.getName().toString().equalsIgnoreCase("type")) {
current.setType(currentAt.getValue());
}
}
} else if (qName.equalsIgnoreCase("title")) {
bTitle = true;
} else if (qName.equalsIgnoreCase("description")) {
bDescription = true;
} else if (qName.equalsIgnoreCase("date")) {
bDate = true;
} else if (qName.equalsIgnoreCase("author")) {
bAuthor = true;
}
break;
case XMLStreamConstants.CHARACTERS:
Characters characters = event.asCharacters();
if (bTitle) {
if (current != null) {
current.setTitle(characters.getData());
}
bTitle = false;
}
if (bDescription) {
if (current != null) {
current.setDescription(characters.getData());
}
bDescription = false;
}
if (bDate) {
if (current != null) {
current.setDate(characters.getData());
}
bDate = false;
}
if (bAuthor) {
if (current != null) {
current.setAuthor(characters.getData());
}
bAuthor = false;
}
break;
case XMLStreamConstants.END_ELEMENT:
EndElement endElement = event.asEndElement();
if (endElement.getName().getLocalPart().equalsIgnoreCase("tutorial")) {
if (current != null) {
tutorials.add(current);
}
}
break;
}
}
} catch (FileNotFoundException | XMLStreamException e) {
e.printStackTrace();
}
return tutorials;
}
Aggregations