protected void init(InputStream is) throws XMLStreamException { XMLInputFactory factory = XMLInputFactory.newInstance(); reader = factory.createXMLEventReader(is); while(true) { XMLEvent event = reader.nextEvent(); if(event instanceof EndDocument) { reader.close(); break; } if(event instanceof StartElement) { onStartElement((StartElement)event); } if(event instanceof EndElement) { onEndElement((EndElement)event); } } }
@Override protected Map<Integer, List<String>> doGetValue(String entityType) { Map<Integer, List<String>> map = new HashMap<Integer, List<String>>(); try { InputStream is = restService.getForStream("customization/entities/{0}/lists", entityType); XMLInputFactory factory = XMLInputFactory.newInstance(); XMLEventReader reader = factory.createXMLEventReader(is); int listId = 0; while(true) { XMLEvent event = reader.nextEvent(); if(event instanceof EndDocument) { reader.close(); break; } if(event instanceof StartElement) { String localPart = ((StartElement) event).getName().getLocalPart(); if("Id".equals(localPart)) { listId = Integer.valueOf(AbstractList.readNextValue(reader)); map.put(listId, new ArrayList<String>()); } else if("Item".equals(localPart)) { String value = ((StartElement) event).getAttributeByName(new QName(null, "value")).getValue(); map.get(listId).add(value); } } } } catch(XMLStreamException e) { // return what was read so far } return map; }
@Test public void testEvents() { XMLEventFactory f = XMLEventFactory.newInstance(); final String contents = "test <some> text & more! [[]] --"; final String prefix = "prefix"; final String uri = "http://foo"; final String localName = "elem"; try { StartDocument sd = f.createStartDocument(); writeAsEncodedUnicode(sd); Comment c = f.createComment("some comments"); writeAsEncodedUnicode(c); StartElement se = f.createStartElement(prefix, uri, localName); ProcessingInstruction pi = f.createProcessingInstruction("target", "data"); writeAsEncodedUnicode(pi); Namespace ns = f.createNamespace(prefix, uri); writeAsEncodedUnicode(ns); Characters characters = f.createCharacters(contents); writeAsEncodedUnicode(characters); // CData Characters cdata = f.createCData(contents); writeAsEncodedUnicode(cdata); // Attribute QName attrName = new QName("http://test.com", "attr", "ns"); Attribute attr = f.createAttribute(attrName, "value"); writeAsEncodedUnicode(attr); // prefix, uri, localName EndElement ee = f.createEndElement(prefix, uri, localName); writeAsEncodedUnicode(ee); EndDocument ed = f.createEndDocument(); writeAsEncodedUnicode(ed); } catch (Exception e) { Assert.fail(e.getMessage()); } }
@Override public EndDocument createEndDocument() { return defaultImpl.createEndDocument(); }
@Override public EndDocument createEndDocument() { return null; }
public EndDocument createEndDocument() { return new EndDocumentImpl(location); }
@Override public EndDocument createEndDocument() { return delegate.createEndDocument(); }
public EndDocument createEndDocument() { return new EndDocumentImpl(fLocation); }
public void endDocument(EndDocument event) throws XMLStreamException { fEventWriter.add(event); fEventWriter.flush(); }
public void endDocument(EndDocument event) throws XMLStreamException { fStreamWriter.writeEndDocument(); fStreamWriter.flush(); }
protected void index(XMLEvent event) throws XMLStreamException { final Location location = event.getLocation(); try { if (event instanceof EndDocument) { if (null != context) trackContext(event); if (null != formatter) { formatter.end(); conveyFormatted(); } for (XMLEvent resumed : deferred) super.add(resumed); super.add(event); } else if (event instanceof EndElement && null != context && context.isEmpty()) { EndElement close = (EndElement)event; if (!((StartElement)origin).getName().equals(close.getName())) throw new IllegalStateException("Unclosed " + describeEvent(origin) + ' ' + describeLocation(origin.getLocation())); endContext(); if (null != formatter) { formatter.closeItem(event); conveyFormatted(); } deferred.add(event); } else if (null == context) { String id = null; if (null != formatter && formatter.accept(event)) { event = assignIdToElement((StartElement)event); id = ((StartElement)event).getAttributeByName(ID_ATTR_QNAME).getValue(); } deferred.add(event); if (null != id) { XMLEventFactory eventFactory = getXMLEventFactory(); Set<Attribute> attrs = Collections.singleton( eventFactory.createAttribute("name", id) ); deferred.add(eventFactory.createStartElement( DEFAULT_NS_PREFIX, NULL_NS_URI, "a", attrs.iterator(), Collections.EMPTY_SET.iterator())); deferred.add(eventFactory.createCharacters(" ")); deferred.add(eventFactory.createEndElement(DEFAULT_NS_PREFIX, NULL_NS_URI, "a")); if (null != formatter) { formatter.openItem(event, id); conveyFormatted(); } enterContext(event); } } else // null != context { trackContext(event); deferred.add(event); if (null != formatter) { formatter.addContent(event); conveyFormatted(); } } } catch (RuntimeException error) { throw new XMLStreamException(error.getLocalizedMessage(), location, error); } }
/** * Create an end-document event. */ public abstract EndDocument createEndDocument();
public void endDocument(EndDocument event) throws XMLStreamException;