List of usage examples for javax.xml.stream XMLStreamReader getLocation
public Location getLocation();
From source file:org.flowable.cmmn.converter.util.CmmnXmlUtil.java
public static void addXMLLocation(GraphicInfo graphicInfo, XMLStreamReader xtr) { Location location = xtr.getLocation(); graphicInfo.setXmlRowNumber(location.getLineNumber()); graphicInfo.setXmlColumnNumber(location.getColumnNumber()); }
From source file:org.openvpms.tools.data.loader.Data.java
/** * Creates a new <tt>Data</tt>. * * @param reader the stream to read from *//*from ww w. j a v a2 s. c o m*/ public Data(XMLStreamReader reader) { shortName = reader.getAttributeValue(null, "archetype"); location = reader.getLocation(); if (StringUtils.isEmpty(shortName)) { throw new ArchetypeDataLoaderException(InvalidArchetype, location.getLineNumber(), location.getColumnNumber(), "<null>"); } if (!"data".equals(reader.getLocalName())) { throw new ArchetypeDataLoaderException(UnexpectedElement, reader.getLocalName(), location.getLineNumber(), location.getColumnNumber()); } id = reader.getAttributeValue(null, "id"); collection = reader.getAttributeValue(null, "collection"); for (int i = 0; i < reader.getAttributeCount(); i++) { String name = reader.getAttributeLocalName(i); String value = reader.getAttributeValue(i); if (!"archetype".equals(name) && !"id".equals(name) && !"collection".equals(name) && !StringUtils.isEmpty(value)) { attributes.put(name, value); } } }
From source file:org.openvpms.tools.data.loader.DataLoader.java
/** * Starts a data element, pushing it on the stack. * * @param reader the reader/* www . j a v a 2 s .c o m*/ * @param stack the stack of load states * @param path a path representing the stream source, for logging purposes */ private void startData(XMLStreamReader reader, Stack<LoadState> stack, String path) { LoadState current; Data data = new Data(reader); if (verbose) { String archetypeId = stack.isEmpty() ? "none" : stack.peek().getArchetypeId().toString(); log.info("[START PROCESSING element, parent=" + archetypeId + "]" + data); } try { if (!stack.isEmpty()) { current = processData(stack.peek(), data, path); } else { current = processData(null, data, path); } stack.push(current); } catch (Exception exception) { Location location = reader.getLocation(); log.error("Error in start element, line " + location.getLineNumber() + ", column " + location.getColumnNumber() + "" + data + "", exception); } }
From source file:org.osaf.cosmo.model.text.BaseXhtmlFormat.java
protected void handleParseException(String message, XMLStreamReader reader) throws ParseException { handleException(message, reader.getLocation()); }
From source file:org.osaf.cosmo.model.text.XhtmlCollectionFormat.java
public CollectionItem parse(String source, EntityFactory entityFactory) throws ParseException { CollectionItem collection = entityFactory.createCollection(); try {//from w w w . j a v a 2 s. co m if (source == null) throw new ParseException("Source has no XML data", -1); StringReader sr = new StringReader(source); XMLStreamReader reader = createXmlReader(sr); boolean inCollection = false; while (reader.hasNext()) { reader.next(); if (!reader.isStartElement()) continue; if (hasClass(reader, "collection")) { if (log.isDebugEnabled()) log.debug("found collection element"); inCollection = true; continue; } if (inCollection && hasClass(reader, "name")) { if (log.isDebugEnabled()) log.debug("found name element"); String name = reader.getElementText(); if (StringUtils.isBlank(name)) throw new ParseException("Empty name not allowed", reader.getLocation().getCharacterOffset()); collection.setDisplayName(name); continue; } if (inCollection && hasClass(reader, "uuid")) { if (log.isDebugEnabled()) log.debug("found uuid element"); String uuid = reader.getElementText(); if (StringUtils.isBlank(uuid)) throw new ParseException("Empty uuid not allowed", reader.getLocation().getCharacterOffset()); collection.setUid(uuid); continue; } } reader.close(); } catch (XMLStreamException e) { handleXmlException("Error reading XML", e); } return collection; }
From source file:org.unitedinternet.cosmo.model.text.XhtmlCollectionFormat.java
public CollectionItem parse(String source, EntityFactory entityFactory) throws ParseException { CollectionItem collection = entityFactory.createCollection(); try {//from w w w . j a v a2 s .co m if (source == null) { throw new ParseException("Source has no XML data", -1); } StringReader sr = new StringReader(source); XMLStreamReader reader = createXmlReader(sr); boolean inCollection = false; while (reader.hasNext()) { reader.next(); if (!reader.isStartElement()) { continue; } if (hasClass(reader, "collection")) { if (LOG.isDebugEnabled()) { LOG.debug("found collection element"); } inCollection = true; continue; } if (inCollection && hasClass(reader, "name")) { if (LOG.isDebugEnabled()) { LOG.debug("found name element"); } String name = reader.getElementText(); if (StringUtils.isBlank(name)) { throw new ParseException("Empty name not allowed", reader.getLocation().getCharacterOffset()); } collection.setDisplayName(name); continue; } if (inCollection && hasClass(reader, "uuid")) { if (LOG.isDebugEnabled()) { LOG.debug("found uuid element"); } String uuid = reader.getElementText(); if (StringUtils.isBlank(uuid)) { throw new ParseException("Empty uuid not allowed", reader.getLocation().getCharacterOffset()); } collection.setUid(uuid); continue; } } reader.close(); } catch (XMLStreamException e) { handleXmlException("Error reading XML", e); } return collection; }
From source file:tpt.dbweb.cat.io.TaggedTextXMLReader.java
private Iterator<TaggedText> getIterator(InputStream is, String errorMessageInfo) { XMLStreamReader tmpxsr = null; try {//from www . j ava 2s .c o m 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; } }; }