List of usage examples for javax.xml.stream.events StartElement getLocation
javax.xml.stream.Location getLocation();
From source file:Main.java
/** * Constructs a new StartElement that merges the attributes and namespaces * found in the specified StartElement, with the provided attributes. The * returned StartElement will contain all the attributes and namespaces of * the original, plus those defined in the map. * /*w w w. j a va 2 s. c o m*/ * @param tag The original StartElement * @param attrs An iterator of Atributes to add to the element. * @return A new StartElement that contains all the original attributes and * namespaces, plus the provided attributes. */ public static StartElement mergeAttributes(StartElement tag, Iterator attrs, XMLEventFactory factory) { // create Attribute map Map attributes = new HashMap(); // iterate through start tag's attributes for (Iterator i = tag.getAttributes(); i.hasNext();) { Attribute attr = (Attribute) i.next(); attributes.put(attr.getName(), attr); } // iterate through new attributes while (attrs.hasNext()) { Attribute attr = (Attribute) attrs.next(); attributes.put(attr.getName(), attr); } factory.setLocation(tag.getLocation()); QName tagName = tag.getName(); return factory.createStartElement(tagName.getPrefix(), tagName.getNamespaceURI(), tagName.getLocalPart(), attributes.values().iterator(), tag.getNamespaces(), tag.getNamespaceContext()); }
From source file:edu.jhu.hlt.concrete.ingesters.bolt.BoltForumPostIngester.java
private static Section handleHeadline(final XMLEventReader rdr, final String content) throws XMLStreamException, ConcreteException { // The first type is always a document start event. Skip it. rdr.nextEvent();/*from w w w . j av a 2 s . c om*/ // The second type is a document ID block. Skip it. rdr.nextEvent(); // The third type is a whitespace block. Skip it. rdr.nextEvent(); // The next type is a headline start tag. XMLEvent hl = rdr.nextEvent(); StartElement hlse = hl.asStartElement(); QName hlqn = hlse.getName(); final String hlPart = hlqn.getLocalPart(); LOGGER.debug("QN: {}", hlPart); int hlPartOff = hlse.getLocation().getCharacterOffset(); LOGGER.debug("HL part offset: {}", hlPartOff); // Text of the headline. This would be useful for purely getting // the content, but for offsets, it's not that useful. Characters cc = rdr.nextEvent().asCharacters(); int charOff = cc.getLocation().getCharacterOffset(); int clen = cc.getData().length(); // The next part is the headline end element. Skip. rdr.nextEvent(); // Whitespace. Skip. rdr.nextEvent(); // Reader is now pointing at the first post. // Construct section, text span, etc. final int charOffPlusLen = charOff + clen; // Strip whitespace off TextSpan ts; if (STRIP_WHITESPACE_OFF_HEADLINE) { final String hlText = content.substring(charOff, charOffPlusLen); SimpleImmutableEntry<Integer, Integer> pads = trimSpacing(hlText); ts = new TextSpan(charOff + pads.getKey(), charOffPlusLen - pads.getValue()); } else { ts = new TextSpan(charOff, charOffPlusLen); } assert ts.getStart() <= ts.getEnding() : "ts=" + ts; Section s = new Section(); s.setKind("headline"); s.setTextSpan(ts); List<Integer> intList = new ArrayList<>(); intList.add(0); s.setNumberList(intList); return s; }
From source file:Main.java
/** * Like {@link javanet.staxutils.XMLStreamUtils#mergeAttributes} but it can * also merge namespaces//from ww w. j a v a 2 s. com * * @param tag * @param attrs * @param nsps */ public static StartElement mergeAttributes(StartElement tag, Iterator<? extends Attribute> attrs, Iterator<? extends Namespace> nsps, XMLEventFactory factory) { // create Attribute map Map<QName, Attribute> attributes = new HashMap<QName, Attribute>(); // iterate through start tag's attributes for (Iterator i = tag.getAttributes(); i.hasNext();) { Attribute attr = (Attribute) i.next(); attributes.put(attr.getName(), attr); } if (attrs != null) { // iterate through new attributes while (attrs.hasNext()) { Attribute attr = attrs.next(); attributes.put(attr.getName(), attr); } } Map<QName, Namespace> namespaces = new HashMap<QName, Namespace>(); for (Iterator i = tag.getNamespaces(); i.hasNext();) { Namespace ns = (Namespace) i.next(); namespaces.put(ns.getName(), ns); } if (nsps != null) { while (nsps.hasNext()) { Namespace ns = nsps.next(); namespaces.put(ns.getName(), ns); } } factory.setLocation(tag.getLocation()); QName tagName = tag.getName(); return factory.createStartElement(tagName.getPrefix(), tagName.getNamespaceURI(), tagName.getLocalPart(), attributes.values().iterator(), namespaces.values().iterator(), tag.getNamespaceContext()); }
From source file:com.aionengine.gameserver.dataholders.loadingutils.XmlMerger.java
/** * Extract an attribute value from a <code>StartElement </code> event. * * @param element Event object.//from w w w . j a v a 2s .c o m * @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(); }
From source file:com.aionemu.gameserver.dataholders.loadingutils.XmlMerger.java
/** * Extract an attribute value from a// ww w .ja va2s . c o m * <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(); }
From source file:sapience.injectors.stax.inject.ModelBasedStaxStreamInjector.java
/** * If the reference is a attribute (e.g. sawsdl:modelreference), we add it here (by creating * the according XMLEvent). The ref/* w ww. j a v a 2s . c om*/ * @param w * @param ref * @param se * @throws XMLStreamException */ private StartElement handleAttribute(XMLEventWriter w, Reference ref, StartElement se) throws XMLStreamException { /* we are having attributes which are in both, the reference and the current element. We only add * a new Attribute event, if it is not already contained in the Start Element * * Example: * reference <element ns:attr1="value" reference="http://example.com"> * element <element ns:attr1="value"> */ StringBuilder referenceString = new StringBuilder(ref.getTarget().toString()); Matcher matcher = findAttributeInReference.matcher(referenceString); List<Attribute> attributeList = new ArrayList<Attribute>(); // copy namespaces LocalNamespaceContext lnc = new LocalNamespaceContext((BaseNsContext) se.getNamespaceContext()); while (matcher.find()) { int start = matcher.start(); int end = matcher.end(); String key = null; String prefix = null; String value = null; // [ns:attr1, "value"] String[] l = referenceString.substring(start, end).split("="); if (l.length > 0) { // [ns, attr1] String[] n = l[0].split(":"); if (n.length == 2) { key = n[1]; prefix = n[0]; } else { key = n[0]; } if (l.length == 2) { value = l[1].substring(1, l[1].length() - 1); // remove "" } } // check if this is a namespace definition if ((prefix != null) && ("xmlns".contentEquals(prefix))) { lnc.put(key, value); } else { QName name = null; // create QName if (prefix != null) { name = new QName(null, key, prefix); } else { String namespaceURI = se.getNamespaceContext().getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX); name = new QName(namespaceURI, key); } if (name != null) { Attribute created = getXMLEventFactory().createAttribute(name, value); attributeList.add(created); } } } // remove redundant attribute from reference list Iterator<?> it = se.getAttributes(); while (it.hasNext()) { Attribute ae = (Attribute) it.next(); for (Attribute ar : attributeList) { if ((ar.getName().getLocalPart().contentEquals(ae.getName().getLocalPart())) && (ar.getValue().contentEquals(ae.getValue()))) { //System.out.println("Attribute removed! -> " + ar.getName() + "= " + ar.getValue()); attributeList.remove(ar); break; } } } // merge everything again Iterator<? extends Attribute> it2 = se.getAttributes(); while (it2.hasNext()) { attributeList.add(it2.next()); } // create a new element with the attribute set and return it return StartElementEventImpl.construct(se.getLocation(), se.getName(), attributeList.iterator(), lnc.getNamespaces().iterator(), lnc); }
From source file:org.apache.hadoop.util.ConfTest.java
public static List<String> checkConf(InputStream in) { List<NodeInfo> nodes = null; List<String> errors = new ArrayList<String>(); try {/*from w w w .j a v a 2 s . c o m*/ nodes = parseConf(in); if (nodes == null) { errors.add("bad conf file: top-level element not <configuration>"); } } catch (XMLStreamException e) { errors.add("bad conf file: " + e.getMessage()); } if (!errors.isEmpty()) { return errors; } Map<String, List<Integer>> duplicatedProperties = new HashMap<String, List<Integer>>(); for (NodeInfo node : nodes) { StartElement element = node.getStartElement(); int line = element.getLocation().getLineNumber(); if (!element.getName().equals(new QName("property"))) { errors.add(String.format("Line %d: element not <property>", line)); continue; } List<XMLEvent> events = node.getXMLEventsForQName(new QName("name")); if (events == null) { errors.add(String.format("Line %d: <property> has no <name>", line)); } else { String v = null; for (XMLEvent event : events) { if (event.isAttribute()) { v = ((Attribute) event).getValue(); } else { Characters c = node.getElement(event.asStartElement()); if (c != null) { v = c.getData(); } } if (v == null || v.isEmpty()) { errors.add(String.format("Line %d: <property> has an empty <name>", line)); } } if (v != null && !v.isEmpty()) { List<Integer> lines = duplicatedProperties.get(v); if (lines == null) { lines = new ArrayList<Integer>(); duplicatedProperties.put(v, lines); } lines.add(node.getStartElement().getLocation().getLineNumber()); } } events = node.getXMLEventsForQName(new QName("value")); if (events == null) { errors.add(String.format("Line %d: <property> has no <value>", line)); } for (QName qName : node.getDuplicatedQNames()) { if (!qName.equals(new QName("source"))) { errors.add(String.format("Line %d: <property> has duplicated <%s>s", line, qName)); } } } for (Entry<String, List<Integer>> e : duplicatedProperties.entrySet()) { List<Integer> lines = e.getValue(); if (1 < lines.size()) { errors.add(String.format("Line %s: duplicated <property>s for %s", StringUtils.join(", ", lines), e.getKey())); } } return errors; }