use of javax.xml.stream.events.EndElement in project jvarkit by lindenb.
the class PubmedCodingLanguages method scanArticle.
private void scanArticle(PrintStream out, final String rootName, final XMLEventReader r) throws XMLStreamException, IOException {
String article_pmid = null;
String article_title = "";
String article_year = null;
String abstractText = "";
boolean PubDate = false;
while (r.hasNext()) {
final XMLEvent evt = r.nextEvent();
if (evt.isStartElement()) {
final StartElement start = evt.asStartElement();
final String eltName = start.getName().getLocalPart();
if (article_pmid == null && eltName.equals("PMID")) {
article_pmid = r.getElementText();
} else if (article_title.isEmpty() && eltName.equals("ArticleTitle")) {
article_title = textContent(r);
} else if (eltName.equals("PubDate")) {
PubDate = true;
} else if (article_year == null && PubDate && eltName.equals("Year")) {
article_year = r.getElementText();
} else if (eltName.equals("Abstract")) {
abstractText = textContent(r);
}
} else if (evt.isEndElement()) {
final EndElement end = evt.asEndElement();
final String eltName = end.getName().getLocalPart();
if (eltName.equals("PubDate")) {
PubDate = false;
} else if (eltName.equals(rootName)) {
break;
}
}
}
// end of xml read
final String normalizedAbstract = (article_title + " " + abstractText).replace('\n', ' ').toLowerCase();
boolean found_something = false;
for (final ProgLanguage pg : this.languages) {
if (this.common_only_false && !pg.isCommon())
continue;
final String context = pg.match(normalizedAbstract);
if (context == null)
continue;
out.print(article_pmid);
out.print('\t');
out.print(pg.getName());
out.print('\t');
out.print(article_title);
out.print('\t');
out.print(article_year);
out.print('\t');
out.print(context);
out.println();
found_something = true;
}
if (!found_something && (normalizedAbstract.contains("programming language") || normalizedAbstract.contains("written in"))) {
LOG.warning("Cannot get information in:\t" + normalizedAbstract);
}
}
use of javax.xml.stream.events.EndElement in project jvarkit by lindenb.
the class PubmedOrcidGraph method scanArticle.
private void scanArticle(final String rootName, final XMLEventReader r, int depth) throws XMLStreamException, IOException {
final Article article = new Article();
boolean PubDate = false;
final List<Author> authors = new ArrayList<>();
while (r.hasNext()) {
final XMLEvent evt = r.nextEvent();
if (evt.isStartElement()) {
final StartElement start = evt.asStartElement();
final String eltName = start.getName().getLocalPart();
if (article.pmid == null && eltName.equals("PMID")) {
article.pmid = r.getElementText();
} else if (article.ISOAbbreviation == null && eltName.equals("ISOAbbreviation")) {
article.ISOAbbreviation = r.getElementText();
} else if (article.ArticleTitle == null && eltName.equals("ArticleTitle")) {
article.ArticleTitle = this.textContent(r);
} else if (eltName.equals("PubDate")) {
PubDate = true;
} else if (article.doi == null && eltName.equals("ArticleId")) {
final Attribute idType = start.getAttributeByName(new QName("IdType"));
if (idType != null && idType.getValue().equalsIgnoreCase("doi")) {
article.doi = r.getElementText().trim();
}
} else if (article.Year == null && PubDate && eltName.equals("Year")) {
article.Year = r.getElementText();
} else if (eltName.equals("Author")) {
final Author author = parseAuthor(r, article.pmid, depth + 1);
if (author != null && author.orcid != null) {
authors.add(author);
}
}
} else if (evt.isEndElement()) {
final EndElement end = evt.asEndElement();
final String eltName = end.getName().getLocalPart();
if (eltName.equals("PubDate")) {
PubDate = false;
}
if (eltName.equals(rootName))
break;
}
}
if (authors.isEmpty()) {
// do nothing
return;
}
DatabaseEntry key = new DatabaseEntry();
DatabaseEntry data = new DatabaseEntry();
Collections.sort(authors);
for (int x = 0; x + 1 < authors.size(); ++x) {
for (int y = x + 1; y < authors.size(); ++y) {
Link L = null;
final String orcid1 = authors.get(x).orcid;
final String orcid2 = authors.get(y).orcid;
if (this.all_links_between_authors) {
LongBinding.longToEntry(++ID_GENERATOR, key);
} else {
StringBinding.stringToEntry(orcid1 + "~" + orcid2, key);
if (this.linkDatabase.get(txn, key, data, LockMode.DEFAULT) != OperationStatus.NOTFOUND) {
L = this.linkBinding.entryToObject(data);
}
}
if (L == null) {
L = new Link();
L.orcid1 = orcid1;
L.orcid2 = orcid2;
}
L.pmids.add(article.pmid);
this.linkBinding.objectToEntry(L, data);
if (this.linkDatabase.put(txn, key, data) != OperationStatus.SUCCESS) {
throw new JvarkitException.BerkeleyDbError("Cannot put in article db");
}
}
}
// for comparing names
final Collator collator = Collator.getInstance(Locale.US);
collator.setStrength(Collator.PRIMARY);
StringBinding.stringToEntry(article.pmid, key);
if (this.articleDatabase.get(txn, key, data, LockMode.DEFAULT) != OperationStatus.NOTFOUND) {
LOG.debug("Article already in database : " + article.pmid);
} else {
LOG.debug("inserting article " + article.pmid);
this.articleBinding.objectToEntry(article, data);
if (this.articleDatabase.put(txn, key, data) != OperationStatus.SUCCESS) {
throw new JvarkitException.BerkeleyDbError("Cannot put in article db");
}
for (final Author au : authors) {
StringBinding.stringToEntry(au.orcid, key);
if (this.authorDatabase.get(txn, key, data, LockMode.DEFAULT) != OperationStatus.NOTFOUND) {
LOG.debug("Author already in database : " + au.orcid);
final Author other = this.authorBinding.entryToObject(data);
if (!StringUtil.isBlank(other.lastName) && !StringUtil.isBlank(au.lastName) && collator.compare(au.lastName, other.lastName) != 0) {
this.errPrintWriter.println("Conflict\t" + au.orcid + "\t" + au.foreName + "\t" + other.foreName);
}
} else {
this.authorBinding.objectToEntry(au, data);
if (this.authorDatabase.put(txn, key, data) != OperationStatus.SUCCESS) {
throw new JvarkitException.BerkeleyDbError("Cannot put in author db");
}
}
}
}
}
use of javax.xml.stream.events.EndElement in project rpki-validator-3 by RIPE-NCC.
the class RrdpParser method delta.
public Delta delta(final InputStream inputStream) {
final Map<String, DeltaElement> objects = new HashMap<>();
try {
final XMLInputFactory factory = XMLInputFactory.newInstance();
final XMLEventReader eventReader = factory.createXMLEventReader(inputStream);
String sessionId = null;
BigInteger serial = null;
String uri = null;
String hash = null;
StringBuilder base64 = new StringBuilder();
boolean inPublishElement = false;
final Base64.Decoder decoder = Base64.getDecoder();
while (eventReader.hasNext()) {
final XMLEvent event = eventReader.nextEvent();
switch(event.getEventType()) {
case XMLStreamConstants.START_ELEMENT:
final StartElement startElement = event.asStartElement();
final String qName = startElement.getName().getLocalPart();
switch(qName) {
case "publish":
uri = getAttr(startElement, "uri", "Uri is not present in 'publish' element");
hash = getAttr(startElement, "hash");
inPublishElement = true;
break;
case "withdraw":
uri = getAttr(startElement, "uri", "Uri is not present in 'publish' element");
hash = getAttr(startElement, "hash", "Hash is not present in 'withdraw' element");
break;
case "delta":
serial = new BigInteger(getAttr(startElement, "serial", "Notification serial is not present"));
sessionId = getAttr(startElement, "session_id", "Session id is not present");
break;
}
break;
case XMLStreamConstants.CHARACTERS:
final Characters characters = event.asCharacters();
if (inPublishElement) {
final String thisBase64 = characters.getData();
base64.append(thisBase64.replaceAll("\\s", ""));
}
break;
case XMLStreamConstants.END_ELEMENT:
final EndElement endElement = event.asEndElement();
final String qqName = endElement.getName().getLocalPart();
switch(qqName) {
case "publish":
final byte[] decoded = decoder.decode(base64.toString());
objects.put(uri, new DeltaPublish(decoded, uri, Hex.parse(hash)));
base64 = new StringBuilder();
break;
case "withdraw":
objects.put(uri, new DeltaWithdraw(uri, Hex.parse(hash)));
break;
}
break;
}
}
return new Delta(objects, sessionId, serial);
} catch (XMLStreamException e) {
throw new RrdpException("Couldn't parse snapshot: ", e);
}
}
use of javax.xml.stream.events.EndElement in project spring-framework by spring-projects.
the class XMLEventStreamWriter method writeEndElement.
@Override
public void writeEndElement() throws XMLStreamException {
closeEmptyElementIfNecessary();
int last = this.endElements.size() - 1;
EndElement lastEndElement = this.endElements.remove(last);
this.eventWriter.add(lastEndElement);
}
use of javax.xml.stream.events.EndElement in project spring-framework by spring-projects.
the class XMLEventStreamWriter method doWriteNamespace.
@SuppressWarnings("rawtypes")
private void doWriteNamespace(Namespace namespace) throws XMLStreamException {
int last = this.endElements.size() - 1;
EndElement oldEndElement = this.endElements.get(last);
Iterator oldNamespaces = oldEndElement.getNamespaces();
List<Namespace> newNamespaces = new ArrayList<>();
while (oldNamespaces.hasNext()) {
Namespace oldNamespace = (Namespace) oldNamespaces.next();
newNamespaces.add(oldNamespace);
}
newNamespaces.add(namespace);
EndElement newEndElement = this.eventFactory.createEndElement(oldEndElement.getName(), newNamespaces.iterator());
this.eventWriter.add(namespace);
this.endElements.set(last, newEndElement);
}
Aggregations