@Test public void testExtractSamePathToMultipleFieldsByAttribute() { Pattern nlmta = Pattern.compile("nlm-ta"); Pattern iso = Pattern.compile("iso-abbrev"); Pattern pub = Pattern.compile("publisher-id"); ElementSpec journal_nlm_ta = new ElementSpec("journal_nlm_ta"); journal_nlm_ta.matchOnAttrValue(null, "journal-id-type", nlmta); ElementSpec journal_iso = new ElementSpec("journal_iso"); journal_iso.matchOnAttrValue(null, "journal-id-type", iso); ElementSpec journal_pub = new ElementSpec("journal_pub"); journal_pub.matchOnAttrValue(null, "journal-id-type", pub); StaxExtractingProcessor proc = new StaxExtractingProcessor.Builder() .named("testExtractSamePathToMultipleFieldsByAttribute") .failOnLongPath(true) .withPathBuffer(2048) .extracting("/article/front/journal-meta/journal-id", journal_nlm_ta) .extracting("/article/front/journal-meta/journal-id", journal_iso) .extracting("/article/front/journal-meta/journal-id", journal_pub) .build(); expect(mockDocument.getRawData()).andReturn(xmlBytes).times(StaxExtractingProcessor.log.isTraceEnabled() ? 2 : 1); expect(mockDocument.put("journal_nlm_ta", "Ethology")).andReturn(true); expect(mockDocument.put("journal_iso", "Ethology")).andReturn(true); expect(mockDocument.put("journal_pub", "eth")).andReturn(true); replay(); proc.processDocument(mockDocument); }
@Override public Document[] processDocument(Document document) { List<LimitedStaxHandler> handlers = new ArrayList<>(); CharBuffer path = CharBuffer.allocate(this.capacity); path.flip(); if (log.isTraceEnabled()) { log.trace(new String(document.getRawData())); } String trim = new String(document.getRawData()).trim(); byte[] buf = trim.getBytes(); InputStream xmlInputStream = new ByteArrayInputStream(buf); XMLInputFactory2 xmlInputFactory = (XMLInputFactory2) XMLInputFactory .newFactory("javax.xml.stream.XMLInputFactory", Thread.currentThread().getContextClassLoader()); if (supportExternalEntities) { if (!xmlInputFactory.isPropertySupported("javax.xml.stream.isSupportingExternalEntities")) { throw new RuntimeException(xmlInputFactory + " doesn't support javax.xml.stream.isSupportingExternalEntities"); } xmlInputFactory.setProperty("javax.xml.stream.isSupportingExternalEntities", true); } xmlInputFactory.setXMLResolver(resolver); XMLStreamReader2 xmlStreamReader; try { xmlStreamReader = (XMLStreamReader2) xmlInputFactory.createXMLStreamReader(xmlInputStream); while (xmlStreamReader.hasNext()) { int eventType = xmlStreamReader.next(); switch (eventType) { case XMLEvent.START_ELEMENT: String s = xmlStreamReader.getName().toString(); if (!addToPath(s, path) && failOnLongPath) { document.setStatus(Status.ERROR); log.info("Errored Document:{}",document.getId()); return new Document[]{document}; } log.trace("Starting {}", path.toString()); List<ElementSpec> specList = extractMapping.get(path); if (specList != null) { for (ElementSpec spec : specList) { LimitedStaxHandler handler = spec.handleIfMatches(xmlStreamReader, spec); if (handler != null) { log.trace("{} adding handler {} for {}", document::getId, () -> handler.getSpec().getDestField(), path::toString); handlers.add(handler); } } } for (LimitedStaxHandler handler : handlers) { log.trace("{} start element called for {} ({})", document::getId,() -> path, () -> handler.getSpec().getDestField()); handler.onStartElement(xmlStreamReader); } break; case XMLEvent.END_ELEMENT: log.trace("Ending path {}", path.toString()); List<ElementSpec> specEndingList = extractMapping.get(path); if (specEndingList != null) { for (LimitedStaxHandler handler : handlers) { for (ElementSpec elementSpec : specEndingList) { if (handler.getSpec() == elementSpec) { log.trace("{} calling onEndElement for {} ({})", document::getId, path::toString, () -> handler.getSpec().getDestField()); handler.onEndElement(xmlStreamReader); log.trace("{} putting field {} for path {}", document::getId, elementSpec::getDestField, () -> path); document.put(elementSpec.getDestField(), handler.toString()); handler.reset(); } } } handlers.removeIf(handler -> specEndingList.contains(handler.getSpec())); } for (LimitedStaxHandler handler : handlers) { log.trace("{} calling onEndElement for {} ({})", document::getId, path::toString, () -> handler.getSpec().getDestField()); handler.onEndElement(xmlStreamReader); } decrementPath(path); break; case XMLEvent.CHARACTERS: for (LimitedStaxHandler handler : handlers) { log.trace("{} calling onCharacters for {} ({})", document::getId, path::toString, () -> handler.getSpec().getDestField()); handler.onCharacters(xmlStreamReader); } break; default: break; } } } catch (Throwable e) { document.setStatus(Status.ERROR); log.error("Exception Processing XML in StaxExtractingProcessor:",e); log.trace("Offending XML:\n{}", trim); log.error(e); if (e instanceof Error) { throw (Error) e; } } return new Document[] {document}; }
StaxExtractingProcessor implements DocumentProcessor { @Override public Document[] processDocument(Document document) { List<LimitedStaxHandler> handlers = new ArrayList<>(); CharBuffer path = CharBuffer.allocate(this.capacity); path.flip(); if (log.isTraceEnabled()) { log.trace(new String(document.getRawData())); } String trim = new String(document.getRawData()).trim(); byte[] buf = trim.getBytes(); InputStream xmlInputStream = new ByteArrayInputStream(buf); XMLInputFactory2 xmlInputFactory = (XMLInputFactory2) XMLInputFactory .newFactory("javax.xml.stream.XMLInputFactory", Thread.currentThread().getContextClassLoader()); if (supportExternalEntities) { if (!xmlInputFactory.isPropertySupported("javax.xml.stream.isSupportingExternalEntities")) { throw new RuntimeException(xmlInputFactory + " doesn't support javax.xml.stream.isSupportingExternalEntities"); } xmlInputFactory.setProperty("javax.xml.stream.isSupportingExternalEntities", true); } xmlInputFactory.setXMLResolver(resolver); XMLStreamReader2 xmlStreamReader; try { xmlStreamReader = (XMLStreamReader2) xmlInputFactory.createXMLStreamReader(xmlInputStream); while (xmlStreamReader.hasNext()) { int eventType = xmlStreamReader.next(); switch (eventType) { case XMLEvent.START_ELEMENT: String s = xmlStreamReader.getName().toString(); if (!addToPath(s, path) && failOnLongPath) { document.setStatus(Status.ERROR); log.info("Errored Document:{}",document.getId()); return new Document[]{document}; } log.trace("Starting {}", path.toString()); List<ElementSpec> specList = extractMapping.get(path); if (specList != null) { for (ElementSpec spec : specList) { LimitedStaxHandler handler = spec.handleIfMatches(xmlStreamReader, spec); if (handler != null) { log.trace("{} adding handler {} for {}", document::getId, () -> handler.getSpec().getDestField(), path::toString); handlers.add(handler); } } } for (LimitedStaxHandler handler : handlers) { log.trace("{} start element called for {} ({})", document::getId,() -> path, () -> handler.getSpec().getDestField()); handler.onStartElement(xmlStreamReader); } break; case XMLEvent.END_ELEMENT: log.trace("Ending path {}", path.toString()); List<ElementSpec> specEndingList = extractMapping.get(path); if (specEndingList != null) { for (LimitedStaxHandler handler : handlers) { for (ElementSpec elementSpec : specEndingList) { if (handler.getSpec() == elementSpec) { log.trace("{} calling onEndElement for {} ({})", document::getId, path::toString, () -> handler.getSpec().getDestField()); handler.onEndElement(xmlStreamReader); log.trace("{} putting field {} for path {}", document::getId, elementSpec::getDestField, () -> path); document.put(elementSpec.getDestField(), handler.toString()); handler.reset(); } } } handlers.removeIf(handler -> specEndingList.contains(handler.getSpec())); } for (LimitedStaxHandler handler : handlers) { log.trace("{} calling onEndElement for {} ({})", document::getId, path::toString, () -> handler.getSpec().getDestField()); handler.onEndElement(xmlStreamReader); } decrementPath(path); break; case XMLEvent.CHARACTERS: for (LimitedStaxHandler handler : handlers) { log.trace("{} calling onCharacters for {} ({})", document::getId, path::toString, () -> handler.getSpec().getDestField()); handler.onCharacters(xmlStreamReader); } break; default: break; } } } catch (Throwable e) { document.setStatus(Status.ERROR); log.error("Exception Processing XML in StaxExtractingProcessor:",e); log.trace("Offending XML:\n{}", trim); log.error(e); if (e instanceof Error) { throw (Error) e; } } return new Document[] {document}; } }
StaxExtractingProcessor implements DocumentProcessor { @Override public Document[] processDocument(Document document) { List<LimitedStaxHandler> handlers = new ArrayList<>(); CharBuffer path = CharBuffer.allocate(this.capacity); path.flip(); if (log.isTraceEnabled()) { log.trace(new String(document.getRawData())); } String trim = new String(document.getRawData()).trim(); byte[] buf = trim.getBytes(); InputStream xmlInputStream = new ByteArrayInputStream(buf); XMLInputFactory2 xmlInputFactory = (XMLInputFactory2) XMLInputFactory .newFactory("javax.xml.stream.XMLInputFactory", Thread.currentThread().getContextClassLoader()); if (supportExternalEntities) { if (!xmlInputFactory.isPropertySupported("javax.xml.stream.isSupportingExternalEntities")) { throw new RuntimeException(xmlInputFactory + " doesn't support javax.xml.stream.isSupportingExternalEntities"); } xmlInputFactory.setProperty("javax.xml.stream.isSupportingExternalEntities", true); } xmlInputFactory.setXMLResolver(resolver); XMLStreamReader2 xmlStreamReader; try { xmlStreamReader = (XMLStreamReader2) xmlInputFactory.createXMLStreamReader(xmlInputStream); while (xmlStreamReader.hasNext()) { int eventType = xmlStreamReader.next(); switch (eventType) { case XMLEvent.START_ELEMENT: String s = xmlStreamReader.getName().toString(); if (!addToPath(s, path) && failOnLongPath) { document.setStatus(Status.ERROR); log.info("Errored Document:{}",document.getId()); return new Document[]{document}; } log.trace("Starting {}", path.toString()); List<ElementSpec> specList = extractMapping.get(path); if (specList != null) { for (ElementSpec spec : specList) { LimitedStaxHandler handler = spec.handleIfMatches(xmlStreamReader, spec); if (handler != null) { log.trace("{} adding handler {} for {}", document::getId, () -> handler.getSpec().getDestField(), path::toString); handlers.add(handler); } } } for (LimitedStaxHandler handler : handlers) { log.trace("{} start element called for {} ({})", document::getId,() -> path, () -> handler.getSpec().getDestField()); handler.onStartElement(xmlStreamReader); } break; case XMLEvent.END_ELEMENT: log.trace("Ending path {}", path.toString()); List<ElementSpec> specEndingList = extractMapping.get(path); if (specEndingList != null) { for (LimitedStaxHandler handler : handlers) { for (ElementSpec elementSpec : specEndingList) { if (handler.getSpec() == elementSpec) { log.trace("{} calling onEndElement for {} ({})", document::getId, path::toString, () -> handler.getSpec().getDestField()); handler.onEndElement(xmlStreamReader); log.trace("{} putting field {} for path {}", document::getId, elementSpec::getDestField, () -> path); document.put(elementSpec.getDestField(), handler.toString()); handler.reset(); } } } handlers.removeIf(handler -> specEndingList.contains(handler.getSpec())); } for (LimitedStaxHandler handler : handlers) { log.trace("{} calling onEndElement for {} ({})", document::getId, path::toString, () -> handler.getSpec().getDestField()); handler.onEndElement(xmlStreamReader); } decrementPath(path); break; case XMLEvent.CHARACTERS: for (LimitedStaxHandler handler : handlers) { log.trace("{} calling onCharacters for {} ({})", document::getId, path::toString, () -> handler.getSpec().getDestField()); handler.onCharacters(xmlStreamReader); } break; default: break; } } } catch (Throwable e) { document.setStatus(Status.ERROR); log.error("Exception Processing XML in StaxExtractingProcessor:",e); log.trace("Offending XML:\n{}", trim); log.error(e); if (e instanceof Error) { throw (Error) e; } } return new Document[] {document}; }  }
StaxExtractingProcessor implements DocumentProcessor { @Override public Document[] processDocument(Document document) { List<LimitedStaxHandler> handlers = new ArrayList<>(); CharBuffer path = CharBuffer.allocate(this.capacity); path.flip(); if (log.isTraceEnabled()) { log.trace(new String(document.getRawData())); } String trim = new String(document.getRawData()).trim(); byte[] buf = trim.getBytes(); InputStream xmlInputStream = new ByteArrayInputStream(buf); XMLInputFactory2 xmlInputFactory = (XMLInputFactory2) XMLInputFactory .newFactory("javax.xml.stream.XMLInputFactory", Thread.currentThread().getContextClassLoader()); if (supportExternalEntities) { if (!xmlInputFactory.isPropertySupported("javax.xml.stream.isSupportingExternalEntities")) { throw new RuntimeException(xmlInputFactory + " doesn't support javax.xml.stream.isSupportingExternalEntities"); } xmlInputFactory.setProperty("javax.xml.stream.isSupportingExternalEntities", true); } xmlInputFactory.setXMLResolver(resolver); XMLStreamReader2 xmlStreamReader; try { xmlStreamReader = (XMLStreamReader2) xmlInputFactory.createXMLStreamReader(xmlInputStream); while (xmlStreamReader.hasNext()) { int eventType = xmlStreamReader.next(); switch (eventType) { case XMLEvent.START_ELEMENT: String s = xmlStreamReader.getName().toString(); if (!addToPath(s, path) && failOnLongPath) { document.setStatus(Status.ERROR); log.info("Errored Document:{}",document.getId()); return new Document[]{document}; } log.trace("Starting {}", path.toString()); List<ElementSpec> specList = extractMapping.get(path); if (specList != null) { for (ElementSpec spec : specList) { LimitedStaxHandler handler = spec.handleIfMatches(xmlStreamReader, spec); if (handler != null) { log.trace("{} adding handler {} for {}", document::getId, () -> handler.getSpec().getDestField(), path::toString); handlers.add(handler); } } } for (LimitedStaxHandler handler : handlers) { log.trace("{} start element called for {} ({})", document::getId,() -> path, () -> handler.getSpec().getDestField()); handler.onStartElement(xmlStreamReader); } break; case XMLEvent.END_ELEMENT: log.trace("Ending path {}", path.toString()); List<ElementSpec> specEndingList = extractMapping.get(path); if (specEndingList != null) { for (LimitedStaxHandler handler : handlers) { for (ElementSpec elementSpec : specEndingList) { if (handler.getSpec() == elementSpec) { log.trace("{} calling onEndElement for {} ({})", document::getId, path::toString, () -> handler.getSpec().getDestField()); handler.onEndElement(xmlStreamReader); log.trace("{} putting field {} for path {}", document::getId, elementSpec::getDestField, () -> path); document.put(elementSpec.getDestField(), handler.toString()); handler.reset(); } } } handlers.removeIf(handler -> specEndingList.contains(handler.getSpec())); } for (LimitedStaxHandler handler : handlers) { log.trace("{} calling onEndElement for {} ({})", document::getId, path::toString, () -> handler.getSpec().getDestField()); handler.onEndElement(xmlStreamReader); } decrementPath(path); break; case XMLEvent.CHARACTERS: for (LimitedStaxHandler handler : handlers) { log.trace("{} calling onCharacters for {} ({})", document::getId, path::toString, () -> handler.getSpec().getDestField()); handler.onCharacters(xmlStreamReader); } break; default: break; } } } catch (Throwable e) { document.setStatus(Status.ERROR); log.error("Exception Processing XML in StaxExtractingProcessor:",e); log.trace("Offending XML:\n{}", trim); log.error(e); if (e instanceof Error) { throw (Error) e; } } return new Document[] {document}; }  @Override Document[] processDocument(Document document); @Override String getName(); }
StaxExtractingProcessor implements DocumentProcessor { @Override public Document[] processDocument(Document document) { List<LimitedStaxHandler> handlers = new ArrayList<>(); CharBuffer path = CharBuffer.allocate(this.capacity); path.flip(); if (log.isTraceEnabled()) { log.trace(new String(document.getRawData())); } String trim = new String(document.getRawData()).trim(); byte[] buf = trim.getBytes(); InputStream xmlInputStream = new ByteArrayInputStream(buf); XMLInputFactory2 xmlInputFactory = (XMLInputFactory2) XMLInputFactory .newFactory("javax.xml.stream.XMLInputFactory", Thread.currentThread().getContextClassLoader()); if (supportExternalEntities) { if (!xmlInputFactory.isPropertySupported("javax.xml.stream.isSupportingExternalEntities")) { throw new RuntimeException(xmlInputFactory + " doesn't support javax.xml.stream.isSupportingExternalEntities"); } xmlInputFactory.setProperty("javax.xml.stream.isSupportingExternalEntities", true); } xmlInputFactory.setXMLResolver(resolver); XMLStreamReader2 xmlStreamReader; try { xmlStreamReader = (XMLStreamReader2) xmlInputFactory.createXMLStreamReader(xmlInputStream); while (xmlStreamReader.hasNext()) { int eventType = xmlStreamReader.next(); switch (eventType) { case XMLEvent.START_ELEMENT: String s = xmlStreamReader.getName().toString(); if (!addToPath(s, path) && failOnLongPath) { document.setStatus(Status.ERROR); log.info("Errored Document:{}",document.getId()); return new Document[]{document}; } log.trace("Starting {}", path.toString()); List<ElementSpec> specList = extractMapping.get(path); if (specList != null) { for (ElementSpec spec : specList) { LimitedStaxHandler handler = spec.handleIfMatches(xmlStreamReader, spec); if (handler != null) { log.trace("{} adding handler {} for {}", document::getId, () -> handler.getSpec().getDestField(), path::toString); handlers.add(handler); } } } for (LimitedStaxHandler handler : handlers) { log.trace("{} start element called for {} ({})", document::getId,() -> path, () -> handler.getSpec().getDestField()); handler.onStartElement(xmlStreamReader); } break; case XMLEvent.END_ELEMENT: log.trace("Ending path {}", path.toString()); List<ElementSpec> specEndingList = extractMapping.get(path); if (specEndingList != null) { for (LimitedStaxHandler handler : handlers) { for (ElementSpec elementSpec : specEndingList) { if (handler.getSpec() == elementSpec) { log.trace("{} calling onEndElement for {} ({})", document::getId, path::toString, () -> handler.getSpec().getDestField()); handler.onEndElement(xmlStreamReader); log.trace("{} putting field {} for path {}", document::getId, elementSpec::getDestField, () -> path); document.put(elementSpec.getDestField(), handler.toString()); handler.reset(); } } } handlers.removeIf(handler -> specEndingList.contains(handler.getSpec())); } for (LimitedStaxHandler handler : handlers) { log.trace("{} calling onEndElement for {} ({})", document::getId, path::toString, () -> handler.getSpec().getDestField()); handler.onEndElement(xmlStreamReader); } decrementPath(path); break; case XMLEvent.CHARACTERS: for (LimitedStaxHandler handler : handlers) { log.trace("{} calling onCharacters for {} ({})", document::getId, path::toString, () -> handler.getSpec().getDestField()); handler.onCharacters(xmlStreamReader); } break; default: break; } } } catch (Throwable e) { document.setStatus(Status.ERROR); log.error("Exception Processing XML in StaxExtractingProcessor:",e); log.trace("Offending XML:\n{}", trim); log.error(e); if (e instanceof Error) { throw (Error) e; } } return new Document[] {document}; }  @Override Document[] processDocument(Document document); @Override String getName();  }