List of usage examples for javax.xml.stream XMLStreamReader getText
public String getText();
From source file:org.unitedinternet.cosmo.util.DomReader.java
private static Node readNode(Document d, XMLStreamReader reader) throws XMLStreamException { Node root = null;/*from www. j av a 2 s . c o m*/ Node current = null; while (reader.hasNext()) { reader.next(); if (reader.isEndElement()) { //log.debug("Finished reading " + current.getNodeName()); if (current.getParentNode() == null) { break; } //log.debug("Setting current to " + //current.getParentNode().getNodeName()); current = current.getParentNode(); } if (reader.isStartElement()) { Element e = readElement(d, reader); if (root == null) { //log.debug("Setting root to " + e.getNodeName()); root = e; } if (current != null) { //log.debug("Appending child " + e.getNodeName() + " to " + //current.getNodeName()); current.appendChild(e); } //log.debug("Setting current to " + e.getNodeName()); current = e; continue; } if (reader.isCharacters()) { CharacterData cd = d.createTextNode(reader.getText()); if (root == null) { return cd; } if (current == null) { return cd; } //log.debug("Appending text '" + cd.getData() + "' to " + //current.getNodeName()); current.appendChild(cd); continue; } } return root; }
From source file:org.ut.biolab.medsavant.client.plugin.AppController.java
public AppDescriptor getDescriptorFromFile(File f) throws PluginVersionException { XMLStreamReader reader; try {// w ww . ja v a 2 s . c o m JarFile jar = new JarFile(f); ZipEntry entry = jar.getEntry("plugin.xml"); if (entry != null) { InputStream entryStream = jar.getInputStream(entry); reader = XMLInputFactory.newInstance().createXMLStreamReader(entryStream); String className = null; String id = null; String version = null; String sdkVersion = null; String name = null; String category = AppDescriptor.Category.UTILITY.toString(); String currentElement = null; String currentText = ""; do { switch (reader.next()) { case XMLStreamConstants.START_ELEMENT: switch (readElement(reader)) { case PLUGIN: className = readAttribute(reader, AppDescriptor.PluginXMLAttribute.CLASS); //category can be specified as an attribute or <property>. category = readAttribute(reader, AppDescriptor.PluginXMLAttribute.CATEGORY); break; case ATTRIBUTE: if ("sdk-version".equals(readAttribute(reader, AppDescriptor.PluginXMLAttribute.ID))) { sdkVersion = readAttribute(reader, AppDescriptor.PluginXMLAttribute.VALUE); } break; case PARAMETER: if ("name".equals(readAttribute(reader, AppDescriptor.PluginXMLAttribute.ID))) { name = readAttribute(reader, AppDescriptor.PluginXMLAttribute.VALUE); } break; case PROPERTY: if ("name".equals(readAttribute(reader, AppDescriptor.PluginXMLAttribute.NAME))) { name = readAttribute(reader, AppDescriptor.PluginXMLAttribute.VALUE); if (name == null) { currentElement = "name"; } } if ("version".equals(readAttribute(reader, AppDescriptor.PluginXMLAttribute.NAME))) { version = readAttribute(reader, AppDescriptor.PluginXMLAttribute.VALUE); if (version == null) { currentElement = "version"; } } if ("sdk-version" .equals(readAttribute(reader, AppDescriptor.PluginXMLAttribute.NAME))) { sdkVersion = readAttribute(reader, AppDescriptor.PluginXMLAttribute.VALUE); if (sdkVersion == null) { currentElement = "sdk-version"; } } if ("category".equals(readAttribute(reader, AppDescriptor.PluginXMLAttribute.NAME))) { category = readAttribute(reader, AppDescriptor.PluginXMLAttribute.VALUE); if (category == null) { currentElement = "category"; } } break; } break; case XMLStreamConstants.CHARACTERS: if (reader.isWhiteSpace()) { break; } else if (currentElement != null) { currentText += reader.getText().trim().replace("\t", ""); } break; case XMLStreamConstants.END_ELEMENT: if (readElement(reader) == AppDescriptor.PluginXMLElement.PROPERTY) { if (currentElement != null && currentText.length() > 0) { if (currentElement.equals("name")) { name = currentText; } else if (currentElement.equals("sdk-version")) { sdkVersion = currentText; } else if (currentElement.equals("category")) { category = currentText; } else if (currentElement.equals("version")) { version = currentText; } } currentText = ""; currentElement = null; } break; case XMLStreamConstants.END_DOCUMENT: reader.close(); reader = null; break; } } while (reader != null); System.out.println(className + " " + name + " " + version); if (className != null && name != null && version != null) { return new AppDescriptor(className, version, name, sdkVersion, category, f); } } } catch (Exception x) { LOG.error("Error parsing plugin.xml from " + f.getAbsolutePath() + ": " + x); } throw new PluginVersionException(f.getName() + " did not contain a valid plugin"); }
From source file:org.xwiki.filter.xar.internal.input.AttachmentReader.java
@Override public WikiAttachment read(XMLStreamReader xmlReader, XARInputProperties properties) throws XMLStreamException, FilterException { WikiAttachment wikiAttachment = new WikiAttachment(); for (xmlReader.nextTag(); xmlReader.isStartElement(); xmlReader.nextTag()) { String elementName = xmlReader.getLocalName(); EventParameter parameter = XARAttachmentModel.ATTACHMENT_PARAMETERS.get(elementName); if (parameter != null) { Object wsValue = convert(parameter.type, xmlReader.getElementText()); if (wsValue != null) { wikiAttachment.parameters.put(parameter.name, wsValue); }/*from w w w . j a v a 2 s . c o m*/ } else { if (XARAttachmentModel.ELEMENT_NAME.equals(elementName)) { wikiAttachment.name = xmlReader.getElementText(); } else if (XARAttachmentModel.ELEMENT_CONTENT_SIZE.equals(elementName)) { wikiAttachment.size = Long.valueOf(xmlReader.getElementText()); } else if (XARAttachmentModel.ELEMENT_CONTENT.equals(elementName)) { // We copy the attachment content to use it later. We can't directly send it as a stream because XAR // specification does not force any order for the attachment properties and we need to be sure we // have everything when sending the event. // Allocate a temporary file in case the attachment content is big File temporaryFile; try { temporaryFile = File.createTempFile("xar/attachments/attachment", ".bin"); temporaryFile.deleteOnExit(); } catch (IOException e) { throw new FilterException(e); } // Create a deferred file based content (if the content is bigger than 10000 bytes it will end up in // a file) wikiAttachment.content = new DeferredFileOutputStream(100000, temporaryFile); // Copy the content to byte array or file depending on its size for (xmlReader.next(); xmlReader.isCharacters(); xmlReader.next()) { try { wikiAttachment.content.write(xmlReader.getText().getBytes(StandardCharsets.UTF_8)); } catch (IOException e) { throw new FilterException(e); } } } } } return wikiAttachment; }
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 w ww . j a va 2 s .c om*/ 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:tpt.dbweb.cat.io.TaggedTextXMLReader.java
private Iterator<TaggedText> getIterator(InputStream is, String errorMessageInfo) { XMLStreamReader tmpxsr = null; try {/*from ww w. ja v a2 s. c om*/ XMLInputFactory xif = XMLInputFactory.newInstance(); xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); xif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false); xif.setProperty(XMLInputFactory.IS_VALIDATING, false); tmpxsr = xif.createXMLStreamReader(is); } catch (XMLStreamException | FactoryConfigurationError e) { e.printStackTrace(); return null; } final XMLStreamReader xsr = tmpxsr; return new PeekIterator<TaggedText>() { @Override protected TaggedText internalNext() { ArrayList<TextSpan> openMarks = new ArrayList<>(); StringBuilder pureTextSB = new StringBuilder(); ArrayList<TextSpan> marks = new ArrayList<>(); marks.add(new TextSpan(null, 0, 0)); TaggedText tt = null; try { loop: while (xsr.hasNext()) { xsr.next(); int event = xsr.getEventType(); switch (event) { case XMLStreamConstants.START_ELEMENT: if ("articles".equals(xsr.getLocalName())) { } else if ("article".equals(xsr.getLocalName())) { tt = new TaggedText(); for (int i = 0; i < xsr.getAttributeCount(); i++) { if ("id".equals(xsr.getAttributeLocalName(i))) { tt.id = xsr.getAttributeValue(i); } tt.info().put(xsr.getAttributeLocalName(i), xsr.getAttributeValue(i)); } } else if ("mark".equals(xsr.getLocalName())) { TextSpan tr = new TextSpan(null, pureTextSB.length(), pureTextSB.length()); for (int i = 0; i < xsr.getAttributeCount(); i++) { tr.info().put(xsr.getAttributeLocalName(i), xsr.getAttributeValue(i)); } openMarks.add(tr); } else if ("br".equals(xsr.getLocalName())) { // TODO: how to propagate tags from the input to the output? } else { log.warn("ignore tag " + xsr.getLocalName()); } break; case XMLStreamConstants.END_ELEMENT: if ("mark".equals(xsr.getLocalName())) { // search corresponding <mark ...> TextSpan tr = openMarks.remove(openMarks.size() - 1); if (tr == null) { log.warn("markend at " + xsr.getLocation().getCharacterOffset() + " has no corresponding mark tag"); break; } tr.end = pureTextSB.length(); marks.add(tr); } else if ("article".equals(xsr.getLocalName())) { tt.text = StringUtils.stripEnd(pureTextSB.toString().trim(), " \t\n"); pureTextSB = new StringBuilder(); tt.mentions = new ArrayList<>(); for (TextSpan mark : marks) { String entity = mark.info().get("entity"); if (entity == null) { entity = mark.info().get("annotation"); } if (entity != null) { EntityMention e = new EntityMention(tt.text, mark.start, mark.end, entity); String minMention = mark.info().get("min"); String mention = e.getMention(); if (minMention != null && !"".equals(minMention)) { Pattern p = Pattern.compile(Pattern.quote(minMention)); Matcher m = p.matcher(mention); if (m.find()) { TextSpan min = new TextSpan(e.text, e.start + m.start(), e.start + m.end()); e.min = min; if (m.find()) { log.warn("found " + minMention + " two times in \"" + mention + "\""); } } else { String prefix = Utility.findLongestPrefix(mention, minMention); log.warn("didn't find min mention '" + minMention + "' in text '" + mention + "', longest prefix found: '" + prefix + "' in article " + tt.id); } } mark.info().remove("min"); mark.info().remove("entity"); if (mark.info().size() > 0) { e.info().putAll(mark.info()); } tt.mentions.add(e); } } openMarks.clear(); marks.clear(); break loop; } break; case XMLStreamConstants.CHARACTERS: String toadd = xsr.getText(); if (pureTextSB.length() == 0) { toadd = StringUtils.stripStart(toadd, " \t\n"); } if (toadd.contains("thanks")) { log.info("test"); } pureTextSB.append(toadd); break; } } } catch (XMLStreamException e) { log.error("{}", errorMessageInfo); throw new RuntimeException(e); } if (tt != null && tt.mentions != null) { tt.mentions.sort(null); } return tt; } }; }