List of usage examples for javax.xml.stream XMLStreamReader getElementText
public String getElementText() throws XMLStreamException;
From source file:org.xwiki.xar.internal.XarUtils.java
/** * Extract {@link LocalDocumentReference} from a XAR document XML stream. * // w w w.jav a2 s .c om * @param documentStream the stream to parse * @return the reference extracted from the stream * @throws XarException when failing to parse the document stream * @since 5.4M1 */ public static LocalDocumentReference getReference(InputStream documentStream) throws XarException { XMLStreamReader xmlReader; try { xmlReader = XML_INPUT_FACTORY.createXMLStreamReader(documentStream); } catch (XMLStreamException e) { throw new XarException("Failed to create a XML read", e); } EntityReference reference = null; Locale locale = null; String legacySpace = null; String legacyPage = null; try { // <xwikidoc> xmlReader.nextTag(); xmlReader.require(XMLStreamReader.START_ELEMENT, null, XarDocumentModel.ELEMENT_DOCUMENT); // Reference String referenceString = xmlReader.getAttributeValue(null, XarDocumentModel.ATTRIBUTE_DOCUMENT_REFERENCE); if (referenceString != null) { reference = RESOLVER.resolve(referenceString, EntityType.DOCUMENT); } // Locale String localeString = xmlReader.getAttributeValue(null, XarDocumentModel.ATTRIBUTE_DOCUMENT_LOCALE); if (localeString != null) { if (localeString.isEmpty()) { locale = Locale.ROOT; } else { locale = LocaleUtils.toLocale(localeString); } } // Legacy fallback if (reference == null || locale == null) { for (xmlReader.nextTag(); xmlReader.isStartElement(); xmlReader.nextTag()) { String elementName = xmlReader.getLocalName(); if (XarDocumentModel.ELEMENT_NAME.equals(elementName)) { if (reference == null) { legacyPage = xmlReader.getElementText(); if (legacySpace != null && locale != null) { break; } } else if (locale != null) { break; } } else if (XarDocumentModel.ELEMENT_SPACE.equals(elementName)) { if (reference == null) { legacySpace = xmlReader.getElementText(); if (legacyPage != null && locale != null) { break; } } else if (locale != null) { break; } } else if (XarDocumentModel.ELEMENT_LOCALE.equals(elementName)) { if (locale == null) { String value = xmlReader.getElementText(); if (value.length() == 0) { locale = Locale.ROOT; } else { locale = LocaleUtils.toLocale(value); } } if (reference != null || (legacySpace != null && legacyPage != null)) { break; } } else { StAXUtils.skipElement(xmlReader); } } } } catch (XMLStreamException e) { throw new XarException("Failed to parse document", e); } finally { try { xmlReader.close(); } catch (XMLStreamException e) { throw new XarException("Failed to close XML reader", e); } } if (reference == null) { if (legacySpace == null) { throw new XarException("Missing space element"); } if (legacyPage == null) { throw new XarException("Missing page element"); } reference = new LocalDocumentReference(legacySpace, legacyPage); } if (locale == null) { throw new XarException("Missing locale element"); } return new LocalDocumentReference(reference, locale); }
From source file:pl.datamatica.traccar.api.GPXParser.java
public Result parse(InputStream inputStream, Device device) throws XMLStreamException, ParseException, IOException { Result result = new Result(); TimeZone tz = TimeZone.getTimeZone("UTC"); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); DateFormat dateFormatWithMS = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); dateFormat.setTimeZone(tz);/*from ww w.java 2s . c o m*/ dateFormatWithMS.setTimeZone(tz); XMLStreamReader xsr = XMLInputFactory.newFactory().createXMLStreamReader(inputStream); ObjectMapper jsonMapper = new ObjectMapper(); result.positions = new LinkedList<>(); Position position = null; Stack<String> extensionsElements = new Stack<>(); boolean extensionsStarted = false; Map<String, Object> other = null; while (xsr.hasNext()) { xsr.next(); if (xsr.getEventType() == XMLStreamReader.START_ELEMENT) { if (xsr.getLocalName().equalsIgnoreCase("trkpt")) { position = new Position(); position.setLongitude(Double.parseDouble(xsr.getAttributeValue(null, "lon"))); position.setLatitude(Double.parseDouble(xsr.getAttributeValue(null, "lat"))); position.setValid(Boolean.TRUE); position.setDevice(device); } else if (xsr.getLocalName().equalsIgnoreCase("time")) { if (position != null) { String strTime = xsr.getElementText(); if (strTime.length() == 20) { position.setTime(dateFormat.parse(strTime)); } else { position.setTime(dateFormatWithMS.parse(strTime)); } } } else if (xsr.getLocalName().equalsIgnoreCase("ele") && position != null) { position.setAltitude(Double.parseDouble(xsr.getElementText())); } else if (xsr.getLocalName().equalsIgnoreCase("address") && position != null) { position.setAddress(StringEscapeUtils.unescapeXml(xsr.getElementText())); } else if (xsr.getLocalName().equalsIgnoreCase("protocol") && position != null) { position.setProtocol(xsr.getElementText()); } else if (xsr.getLocalName().equalsIgnoreCase("speed") && position != null) { position.setSpeed(Double.parseDouble(xsr.getElementText())); } else if (xsr.getLocalName().equalsIgnoreCase("power") && position != null) { position.setPower(Double.parseDouble(xsr.getElementText())); } else if (xsr.getLocalName().equalsIgnoreCase("course") && position != null) { position.setCourse(Double.parseDouble(xsr.getElementText())); } else if (xsr.getLocalName().equalsIgnoreCase("other") && position != null) { position.setOther(StringEscapeUtils.unescapeXml(xsr.getElementText())); } else if (xsr.getLocalName().equalsIgnoreCase("extensions")) { other = new LinkedHashMap<>(); extensionsStarted = true; } else if (position != null && extensionsStarted && other != null) { extensionsElements.push(xsr.getLocalName()); } } else if (xsr.getEventType() == XMLStreamReader.END_ELEMENT) { if (xsr.getLocalName().equalsIgnoreCase("trkpt")) { if (other == null) { other = new HashMap<>(); } if (position.getOther() != null) { if (position.getOther().startsWith("<")) { XMLStreamReader otherReader = XMLInputFactory.newFactory() .createXMLStreamReader(new StringReader(position.getOther())); while (otherReader.hasNext()) { if (otherReader.next() == XMLStreamReader.START_ELEMENT && !otherReader.getLocalName().equals("info")) { other.put(otherReader.getLocalName(), otherReader.getElementText()); } } } else { Map<String, Object> parsedOther = jsonMapper.readValue(position.getOther(), LinkedHashMap.class); other.putAll(parsedOther); } } if (other.containsKey("protocol") && position.getProtocol() == null) { position.setProtocol(other.get("protocol").toString()); } else if (!other.containsKey("protocol") && position.getProtocol() == null) { position.setProtocol("gpx_import"); } other.put("import_type", (result.positions.isEmpty() ? "import_start" : "import")); position.setOther(jsonMapper.writeValueAsString(other)); result.positions.add(position); if (result.latestPosition == null || result.latestPosition.getTime().compareTo(position.getTime()) < 0) { result.latestPosition = position; } position = null; other = null; } else if (xsr.getLocalName().equalsIgnoreCase("extensions")) { extensionsStarted = false; } else if (extensionsStarted) { extensionsElements.pop(); } } else if (extensionsStarted && other != null && xsr.getEventType() == XMLStreamReader.CHARACTERS && !xsr.getText().trim().isEmpty() && !extensionsElements.empty()) { String name = ""; for (int i = 0; i < extensionsElements.size(); i++) { name += (name.length() > 0 ? "-" : "") + extensionsElements.get(i); } other.put(name, xsr.getText()); } } if (result.positions.size() > 1) { Position last = ((LinkedList<Position>) result.positions).getLast(); Map<String, Object> parsedOther = jsonMapper.readValue(last.getOther(), LinkedHashMap.class); parsedOther.put("import_type", "import_end"); last.setOther(jsonMapper.writeValueAsString(parsedOther)); } return result; }
From source file:savant.plugin.PluginIndex.java
public PluginIndex(URL url) throws IOException { urls = new HashMap<String, URL>(); try {//from w w w.ja v a2 s.c o m XMLStreamReader reader = XMLInputFactory.newInstance() .createXMLStreamReader(NetworkUtils.openStream(url)); boolean done = false; String id = null; do { switch (reader.next()) { case XMLStreamConstants.START_ELEMENT: String elemName = reader.getLocalName(); if (elemName.equals("leaf")) { id = reader.getAttributeValue(null, "id"); } else if (elemName.equals("url")) { if (id != null) { try { urls.put(id, new URL(reader.getElementText())); } catch (MalformedURLException x) { LOG.info("Unable to parse \"" + reader.getElementText() + "\" as a plugin URL."); } id = null; } } break; case XMLStreamConstants.END_DOCUMENT: reader.close(); done = true; break; } } while (!done); } catch (XMLStreamException x) { throw new IOException("Unable to get version number from web-site.", x); } }
From source file:savant.plugin.Tool.java
/** * The tool's arguments are contained in the associated plugin.xml file. *///ww w. j av a2s .c om void parseDescriptor() throws XMLStreamException, FileNotFoundException { XMLStreamReader reader = XMLInputFactory.newInstance() .createXMLStreamReader(new FileInputStream(getDescriptor().getFile())); do { switch (reader.next()) { case XMLStreamConstants.START_ELEMENT: String elemName = reader.getLocalName().toLowerCase(); if (elemName.equals("tool")) { baseCommand = reader.getElementText(); } else if (elemName.equals("arg")) { // There's lots of crud in the XML file; we're just interested in the <arg> elements. arguments.add(new ToolArgument(reader)); } else if (elemName.equals("progress")) { progressRegex = Pattern.compile(reader.getElementText()); } else if (elemName.equals("error")) { errorRegex = Pattern.compile(reader.getElementText()); } break; case XMLStreamConstants.END_DOCUMENT: reader.close(); reader = null; break; } } while (reader != null); }
From source file:savant.util.Version.java
/** * Factory method which construct a Version object from a URL pointing to an XML file. * @param url URL of our version.xml file * @return the version number read from the file *//*from w w w . j a va 2 s. co m*/ public static Version fromURL(URL url) throws IOException { try { XMLStreamReader reader = XMLInputFactory.newInstance() .createXMLStreamReader(NetworkUtils.openStream(url)); boolean done = false; boolean foundCurrentVersion = false; do { switch (reader.next()) { case XMLStreamConstants.START_ELEMENT: String elemName = reader.getLocalName(); if (elemName.equals("version") && "current_release".equals(reader.getAttributeValue(null, "status"))) { foundCurrentVersion = true; } else if (foundCurrentVersion && elemName.equals("name")) { return new Version(reader.getElementText()); } else { foundCurrentVersion = false; } break; case XMLStreamConstants.END_DOCUMENT: reader.close(); done = true; break; } } while (!done); } catch (XMLStreamException x) { throw new IOException("Unable to get version number from web-site.", x); } return null; }
From source file:tkwatch.Utilities.java
/** * Finds the value of the named element, if any, in an XML string. Adapted * from Vohra and Vohra, <i>Pro XML Development with Java Technology</i>, p. * 47.//from w w w .j a v a2 s. c o m * * @param desiredElementName * The name of the element to search for in the XML string. * @param xmlString * The XML string to search for an account number. * * @return Returns the element value(s) as formatted in the incoming string * (if found) as a <code>Vector<String></code>, <code>null</code> * otherwise. */ public static final Vector<String> getValueFromXml(final String desiredElementName, final String xmlString) { Vector<String> elementValue = new Vector<String>(); String elementValueText = null; XMLInputFactory inputFactory = XMLInputFactory.newInstance(); try { String elementName = ""; StringReader stringReader = new StringReader(xmlString); XMLStreamReader reader = inputFactory.createXMLStreamReader(stringReader); while (reader.hasNext()) { int eventType = reader.getEventType(); switch (eventType) { case XMLStreamConstants.START_ELEMENT: elementName = reader.getLocalName(); if (elementName.equals(desiredElementName)) { elementValueText = reader.getAttributeValue(0); System.out.println("START_ELEMENT case, element name is " + elementName + ", element value is " + elementValueText); } break; case XMLStreamConstants.ATTRIBUTE: elementName = reader.getLocalName(); if (elementName.equals(desiredElementName)) { elementValueText = reader.getElementText(); System.out.println("ATTRIBUTE case, element name is " + elementName + ", element value is " + elementValueText); elementValue.add(elementValueText); } break; case XMLStreamConstants.END_ELEMENT: elementName = reader.getLocalName(); if (elementName.equals(desiredElementName)) { System.out.println("END_ELEMENT case, element name is " + elementName + ", element value is " + elementValueText); } break; default: elementName = reader.getLocalName(); if (elementName != null) { if (elementName.equals(desiredElementName)) { System.out.println("default case, element name is " + elementName); } } break; } reader.next(); } } catch (XMLStreamException e) { TradekingException.handleException(e); } return (elementValue.isEmpty() ? null : elementValue); }