List of usage examples for javax.xml.stream XMLStreamReader getAttributeValue
public String getAttributeValue(String namespaceURI, String localName);
From source file:org.xwiki.wikistream.confluence.xml.internal.ConfluenceXMLPackage.java
private int readObjectProperties(XMLStreamReader xmlReader, PropertiesConfiguration properties) throws XMLStreamException, WikiStreamException, ConfigurationException, IOException { int id = -1;/*w w w . j a v a 2s.c o m*/ for (xmlReader.nextTag(); xmlReader.isStartElement(); xmlReader.nextTag()) { String elementName = xmlReader.getLocalName(); if (elementName.equals("id")) { id = Integer.valueOf(xmlReader.getElementText()); properties.setProperty("id", id); } else if (elementName.equals("property")) { String propertyName = xmlReader.getAttributeValue(null, "name"); properties.setProperty(propertyName, readProperty(xmlReader)); } else if (elementName.equals("collection")) { String propertyName = xmlReader.getAttributeValue(null, "name"); properties.setProperty(propertyName, readProperty(xmlReader)); } else { StAXUtils.skipElement(xmlReader); } } return id; }
From source file:org.xwiki.wikistream.confluence.xml.internal.ConfluenceXMLPackage.java
private Object readProperty(XMLStreamReader xmlReader) throws XMLStreamException, WikiStreamException { String propertyClass = xmlReader.getAttributeValue(null, "class"); if (propertyClass == null) { return fixCData(xmlReader.getElementText()); } else if (propertyClass.equals("java.util.List") || propertyClass.equals("java.util.Collection")) { return readListProperty(xmlReader); } else if (propertyClass.equals("java.util.Set")) { return readSetProperty(xmlReader); } else if (propertyClass.equals("Page") || propertyClass.equals("Space") || propertyClass.equals("BodyContent") || propertyClass.equals("Attachment") || propertyClass.equals("SpaceDescription") || propertyClass.equals("Labelling") || propertyClass.equals("SpacePermission") || propertyClass.equals("InternalGroup") || propertyClass.equals("InternalUser") || propertyClass.equals("Comment")) { return readIdProperty(xmlReader); } else {// ww w . java 2s . c o m StAXUtils.skipElement(xmlReader); } return null; }
From source file:org.xwiki.xar.internal.XarUtils.java
/** * Extract {@link LocalDocumentReference} from a XAR document XML stream. * /*from ww w . j a v a 2 s . c o m*/ * @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);/* ww w.java2 s . co 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 {/*w w w . j av a 2s .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.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 ww w . j a v a 2 s . c om*/ 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; }