public void eventReaderTest(XMLInputFactory xif) { XMLEventReader eventReader = null; try { eventReader = xif.createXMLEventReader(this.getClass().getResourceAsStream(INPUT_FILE)); XMLEventReader filteredEventReader = xif.createFilteredReader(eventReader, new EventFilter() { public boolean accept(XMLEvent event) { if (!event.isStartElement()) { return false; } QName elementQName = event.asStartElement().getName(); if ((elementQName.getLocalPart().equals(ATTACHMENT_NAME.getLocalPart()) || elementQName.getLocalPart().equals("Attachment")) && elementQName.getNamespaceURI().equals(VF_GENERIC_TT_NAMESPACE)) { return true; } return false; } }); if (filteredEventReader.hasNext()) { System.out.println("containsAttachments() returns true"); } } catch (Exception e) { e.printStackTrace(); Assert.fail(e.getMessage()); } finally { if (eventReader != null) { try { eventReader.close(); } catch (XMLStreamException xse) { // Ignored by intention } } } }
/** * @param dataFile * @throws FileNotFoundException * @throws XMLStreamException * @throws JAXBException */ private void initialize(InputStream is) throws FileNotFoundException, XMLStreamException, JAXBException { this.is = is; ctx = JAXBContext.newInstance(entryClass); um = ctx.createUnmarshaller(); XMLInputFactory xmlif = XMLInputFactory.newInstance(); xmler = xmlif.createXMLEventReader(is); EventFilter filter = new EventFilter() { public boolean accept(XMLEvent event) { return event.isStartElement(); } }; xmlfer = xmlif.createFilteredReader(xmler, filter); // Jump to the first element in the document, the enclosing Uniprot in // the case of uniprot // xml StartElement e = (StartElement) xmlfer.nextEvent(); advanceToRecordWithTaxonOfInterest(); }
@Test public void testRepairingNamespaces() throws Exception { XMLOutputFactory factory = XMLSystemFunctions.getOutputFactory(true); ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLEventWriter writer = factory.createXMLEventWriter(baos); XMLEventReader reader = XMLType.getXmlInputFactory().createXMLEventReader(new StringReader("<a xmlns:x=\"http://foo\"><b x:y=\"1\"/></a>")); reader.nextTag(); reader = XMLType.getXmlInputFactory().createFilteredReader(reader, new EventFilter() { @Override public boolean accept(XMLEvent arg0) { if (arg0.isStartDocument() || arg0.isEndDocument()) { return false; } if (arg0.isEndElement() && ((EndElement)arg0).getName().getLocalPart().equals("a")) { return false; } return true; } }); writer.add(reader); writer.close(); assertEquals("<b xmlns=\"\" xmlns:x=\"http://foo\" x:y=\"1\"></b>", new String(baos.toByteArray(), "UTF-8")); }
private EventFilter createEventFilter() { EventFilter filter = new EventFilter() { @Override public boolean accept(XMLEvent event) { if (event.isEndDocument() /*|| event.getEventType() == XMLStreamConstants.DTD*/) return false; else return true; } }; return filter; }
public StAXFilteredEvent(XMLEventReader reader, EventFilter filter) throws XMLStreamException { eventReader = reader; _filter = filter; }
public void setFilter(EventFilter filter) { _filter = filter; }
/** Creates a new instance of EventFilterSupport */ public EventFilterSupport(XMLEventReader eventReader, EventFilter eventFilter) { setParent(eventReader); fEventFilter = eventFilter; }
@Override public XMLEventReader createFilteredReader(XMLEventReader reader, EventFilter filter) throws XMLStreamException { throw new UnsupportedOperationException("Not supported yet."); }
@Override public XMLEventReader createFilteredReader(XMLEventReader reader, EventFilter filter) throws XMLStreamException { return defaultImpl.createFilteredReader(reader, filter); }
@Override public XMLEventReader createFilteredReader(XMLEventReader reader, EventFilter filter) throws XMLStreamException { return null; }
public XMLEventReader createFilteredReader(XMLEventReader reader, EventFilter filter) throws XMLStreamException { return new FilteredEventReader(reader, filter); }
FilteredEventReader(XMLEventReader reader, EventFilter filter) { super(reader); this.filter = filter; }
public XMLFilteredEventReader withFilter(EventFilter filter) { this.f = filter; return this; }
public XMLEventReader createFilteredReader(XMLEventReader arg0, EventFilter arg1) throws XMLStreamException { // TODO Auto-generated method stub return null; }
@Override public XMLEventReader createFilteredReader( XMLEventReader reader, EventFilter filter ) throws XMLStreamException { return delegate.createFilteredReader( reader, filter ); }
/** * <p>parseHypericAlerts</p> * * @param reader a {@link java.io.Reader} object. * @return a {@link java.util.List} object. * @throws javax.xml.bind.JAXBException if any. * @throws javax.xml.stream.XMLStreamException if any. */ public static List<HypericAlertStatus> parseHypericAlerts(Reader reader) throws JAXBException, XMLStreamException { List<HypericAlertStatus> retval = new ArrayList<HypericAlertStatus>(); // Instantiate a JAXB context to parse the alert status JAXBContext context = JAXBContext.newInstance(new Class[] { HypericAlertStatuses.class, HypericAlertStatus.class }); XMLInputFactory xmlif = XMLInputFactory.newInstance(); XMLEventReader xmler = xmlif.createXMLEventReader(reader); EventFilter filter = new EventFilter() { public boolean accept(XMLEvent event) { return event.isStartElement(); } }; XMLEventReader xmlfer = xmlif.createFilteredReader(xmler, filter); // Read up until the beginning of the root element StartElement startElement = (StartElement)xmlfer.nextEvent(); // Fetch the root element name for {@link HypericAlertStatus} objects String rootElementName = context.createJAXBIntrospector().getElementName(new HypericAlertStatuses()).getLocalPart(); if (rootElementName.equals(startElement.getName().getLocalPart())) { Unmarshaller unmarshaller = context.createUnmarshaller(); // Use StAX to pull parse the incoming alert statuses while (xmlfer.peek() != null) { Object object = unmarshaller.unmarshal(xmler); if (object instanceof HypericAlertStatus) { HypericAlertStatus alertStatus = (HypericAlertStatus)object; retval.add(alertStatus); } } } else { // Try to pull in the HTTP response to give the user a better idea of what went wrong StringBuffer errorContent = new StringBuffer(); LineNumberReader lineReader = new LineNumberReader(reader); try { String line; while (true) { line = lineReader.readLine(); if (line == null) { break; } else { errorContent.append(line.trim()); } } } catch (IOException e) { errorContent.append("Exception while trying to print out message content: " + e.getMessage()); } // Throw an exception and include the erroneous HTTP response in the exception text throw new JAXBException("Found wrong root element in Hyperic XML document, expected: \"" + rootElementName + "\", found \"" + startElement.getName().getLocalPart() + "\"\n" + errorContent.toString()); } return retval; }