use of javax.xml.stream.events.Attribute in project jvarkit by lindenb.
the class TreePackApp method parseNode.
private Node parseNode(XMLEventReader r, StartElement E, Node parent) throws XMLStreamException {
Node node = new Node(parent);
Attribute att = E.getAttributeByName(new QName("label"));
node.label = (att != null ? att.getValue() : "");
while (r.hasNext()) {
XMLEvent evt = r.nextEvent();
if (evt.isStartElement()) {
QName qName = evt.asStartElement().getName();
if (qName.getLocalPart().equals("node")) {
Node child = parseNode(r, evt.asStartElement(), node);
if (node.children == null)
node.children = new HashMap<String, Node>();
node.children.put(node.label, child);
} else {
skip(r);
}
} else if (evt.isEndElement()) {
if (node.children == null || node.children.isEmpty()) {
att = E.getAttributeByName(new QName("weight"));
if (att == null) {
node.weight = 1.0;
} else {
node.weight = Double.parseDouble(att.getValue());
if (node.weight <= 0)
throw new XMLStreamException("bad @weight:" + node.weight, E.getLocation());
}
}
return node;
}
}
throw new IllegalStateException();
}
use of javax.xml.stream.events.Attribute in project jvarkit by lindenb.
the class XsltStream method parseDocument.
private Node parseDocument(final Document owner, final StartElement startE, final XMLEventReader r) throws XMLStreamException {
final QName qname = startE.getName();
final Element root;
if (qname.getNamespaceURI() == null || qname.getNamespaceURI().isEmpty()) {
root = owner.createElement(toQNAME(qname));
} else {
root = owner.createElementNS(qname.getNamespaceURI(), toQNAME(qname));
}
Iterator<?> it = startE.getNamespaces();
while (it.hasNext()) {
final Namespace att = Namespace.class.cast(it.next());
root.setAttribute("xmlns:" + att.getPrefix(), att.getNamespaceURI());
}
it = startE.getAttributes();
while (it.hasNext()) {
final Attribute att = Attribute.class.cast(it.next());
final QName attName = att.getName();
if (attName.getNamespaceURI() == null || attName.getNamespaceURI().isEmpty()) {
root.setAttribute(toQNAME(attName), att.getValue());
} else {
root.setAttributeNS(attName.getNamespaceURI(), toQNAME(attName), att.getValue());
}
}
while (r.hasNext()) {
final XMLEvent evt = r.nextEvent();
if (evt.isCharacters()) {
root.appendChild(owner.createTextNode(evt.asCharacters().getData()));
} else if (evt.isProcessingInstruction()) {
} else if (evt.isEndElement()) {
break;
} else if (evt.isStartElement()) {
if (this.skipQNames.contains(evt.asStartElement().getName())) {
skip(evt.asStartElement(), r);
} else {
root.appendChild(parseDocument(owner, evt.asStartElement(), r));
}
} else {
LOG.warn("Cannot handle " + evt);
}
}
return root;
}
use of javax.xml.stream.events.Attribute in project jvarkit by lindenb.
the class PubmedGraph method eSummary.
private void eSummary(final Article a) throws IOException, XMLStreamException {
final QName attName = new QName("Name");
final QName attType = new QName("Type");
final String url = NcbiConstants.esummary() + "?retmode=xml&db=pubmed&" + "id=" + a.pmid + this.ncbiApiKey.getAmpParamValue();
LOG.info(url);
StreamSource src = new StreamSource(url);
XMLEventReader reader = this.xmlInputFactory.createXMLEventReader(src);
int in_title = 0;
int in_date = 0;
while (reader.hasNext() && !(a.title != null && a.year != null)) {
XMLEvent evt = reader.nextEvent();
if (evt.isStartElement()) {
StartElement startE = evt.asStartElement();
String localName = startE.getName().getLocalPart();
if (localName.equals("Item")) {
Attribute name = startE.getAttributeByName(attName);
Attribute type = startE.getAttributeByName(attType);
if (name.getValue().equals("Title") && type.getValue().equals("String")) {
in_title = 1;
} else if (name.getValue().equals("PubDate") && type.getValue().equals("Date")) {
in_date = 1;
}
}
} else if (evt.isCharacters()) {
if (in_title == 1) {
a.title = evt.asCharacters().getData();
} else if (in_date == 1) {
a.year = evt.asCharacters().getData();
}
in_title = 0;
in_date = 0;
}
}
reader.close();
}
use of javax.xml.stream.events.Attribute in project jvarkit by lindenb.
the class PubmedMap method doWork.
@Override
public int doWork(final List<String> args) {
final String inputName = oneFileOrNull(args);
OutputStream out = null;
XMLEventReader r = null;
InputStream in = null;
XMLEventWriter w = null;
try {
final QName attDomainSuffix = new QName("domain");
final QName attPlaceSuffix = new QName("place");
final XMLEventFactory xmlEventFactory = XMLEventFactory.newFactory();
final XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
xmlInputFactory.setXMLResolver(new XMLResolver() {
@Override
public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace) throws XMLStreamException {
LOG.debug("Ignoring resolve Entity");
return new ByteArrayInputStream(new byte[0]);
}
});
in = (inputName == null ? stdin() : IOUtils.openURIForReading(inputName));
r = xmlInputFactory.createXMLEventReader(in);
out = super.openFileOrStdoutAsStream(this.outFile);
final XMLOutputFactory xof = XMLOutputFactory.newFactory();
w = xof.createXMLEventWriter(out, "UTF-8");
while (r.hasNext()) {
final XMLEvent evt = r.nextEvent();
if (evt.isStartElement() && evt.asStartElement().getName().getLocalPart().equals("Affiliation") && r.peek().isCharacters()) {
final List<Attribute> attributes = new ArrayList<>();
Iterator<?> t = evt.asStartElement().getAttributes();
while (t.hasNext()) {
final Attribute att = (Attribute) t.next();
if (att.getName().equals(attDomainSuffix))
continue;
if (att.getName().equals(attPlaceSuffix))
continue;
attributes.add(att);
}
final XMLEvent textEvt = r.nextEvent();
final String affiliation = textEvt.asCharacters().getData();
final Country country = decodeAffiliation(affiliation);
if (country != null) {
String suffix = country.suffix;
if (suffix.equals("gov"))
suffix = "us";
attributes.add(xmlEventFactory.createAttribute(attDomainSuffix, suffix));
attributes.add(xmlEventFactory.createAttribute(attPlaceSuffix, country.name));
}
w.add(xmlEventFactory.createStartElement(evt.asStartElement().getName(), attributes.iterator(), evt.asStartElement().getNamespaces()));
w.add(textEvt);
continue;
}
w.add(evt);
}
r.close();
r = null;
in.close();
in = null;
w.flush();
w.close();
w = null;
out.flush();
out.close();
out = null;
return 0;
} catch (final Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(r);
CloserUtil.close(in);
CloserUtil.close(w);
CloserUtil.close(out);
}
}
use of javax.xml.stream.events.Attribute 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");
}
}
}
}
}
Aggregations