List of usage examples for javax.xml.stream XMLEventWriter flush
public void flush() throws XMLStreamException;
From source file:com.msopentech.odatajclient.testservice.utils.XMLUtilities.java
private InputStream addAtomEditLink(final InputStream content, final String title, final String href) throws Exception { final XMLOutputFactory xof = XMLOutputFactory.newInstance(); final ByteArrayOutputStream copy = new ByteArrayOutputStream(); IOUtils.copy(content, copy);/* w ww . ja v a 2 s . co m*/ IOUtils.closeQuietly(content); XMLEventReader reader = getEventReader(new ByteArrayInputStream(copy.toByteArray())); ByteArrayOutputStream bos = new ByteArrayOutputStream(); XMLEventWriter writer = xof.createXMLEventWriter(bos); try { // check edit link existence final XmlElement editLink = getAtomElement(reader, writer, LINK, Collections.<Map.Entry<String, String>>singletonList( new AbstractMap.SimpleEntry<String, String>("rel", "edit"))); writer.add(editLink.getStart()); writer.add(editLink.getContentReader()); writer.add(editLink.getEnd()); writer.add(reader); } catch (Exception e) { reader.close(); reader = getEventReader(new ByteArrayInputStream(copy.toByteArray())); bos = new ByteArrayOutputStream(); writer = xof.createXMLEventWriter(bos); final XmlElement entryElement = getAtomElement(reader, writer, "entry"); writer.add(entryElement.getStart()); addAtomElement(IOUtils.toInputStream( String.format("<link rel=\"edit\" title=\"%s\" href=\"%s\" />", title, href)), writer); writer.add(entryElement.getContentReader()); writer.add(entryElement.getEnd()); writer.add(reader); writer.flush(); writer.close(); } finally { reader.close(); } return new ByteArrayInputStream(bos.toByteArray()); }
From source file:com.msopentech.odatajclient.testservice.utils.XMLUtilities.java
/** * {@inheritDoc }/*ww w. j ava2 s . c o m*/ */ @Override protected InputStream addLinks(final String entitySetName, final String entitykey, final InputStream is, final Set<String> links) throws Exception { // ----------------------------------------- // 0. Build reader and writer // ----------------------------------------- final XMLEventReader reader = getEventReader(is); final XMLEventFactory eventFactory = XMLEventFactory.newInstance(); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final XMLOutputFactory xof = XMLOutputFactory.newInstance(); final XMLEventWriter writer = xof.createXMLEventWriter(bos); // ----------------------------------------- final XmlElement entry = getAtomElement(reader, writer, "entry"); writer.add(entry.getStart()); // add for links for (String link : links) { final Set<Attribute> attributes = new HashSet<Attribute>(); attributes.add(eventFactory.createAttribute(new QName("title"), link)); attributes.add(eventFactory.createAttribute(new QName("href"), Commons.getLinksURI(version, entitySetName, entitykey, link))); attributes.add(eventFactory.createAttribute(new QName("rel"), Constants.ATOM_LINK_REL + link)); attributes.add(eventFactory.createAttribute(new QName("type"), Commons.linkInfo.get(version).isFeed(entitySetName, link) ? Constants.ATOM_LINK_FEED : Constants.ATOM_LINK_ENTRY)); writer.add(eventFactory.createStartElement(new QName(LINK), attributes.iterator(), null)); writer.add(eventFactory.createEndElement(new QName(LINK), null)); } writer.add(entry.getContentReader()); writer.add(entry.getEnd()); writer.add(reader); IOUtils.closeQuietly(is); writer.flush(); writer.close(); reader.close(); return new ByteArrayInputStream(bos.toByteArray()); }
From source file:com.msopentech.odatajclient.testservice.utils.XMLUtilities.java
public InputStream addAtomContent(final InputStream content, final String title, final String href) throws Exception { final XMLOutputFactory xof = XMLOutputFactory.newInstance(); final ByteArrayOutputStream copy = new ByteArrayOutputStream(); IOUtils.copy(content, copy);/*from w w w .j ava 2 s .c o m*/ IOUtils.closeQuietly(content); XMLEventReader reader = getEventReader(new ByteArrayInputStream(copy.toByteArray())); ByteArrayOutputStream bos = new ByteArrayOutputStream(); XMLEventWriter writer = xof.createXMLEventWriter(bos); try { // check edit link existence XmlElement contentElement = getAtomElement(reader, writer, "content"); writer.add(contentElement.getStart()); writer.add(contentElement.getContentReader()); writer.add(contentElement.getEnd()); writer.add(reader); } catch (Exception e) { reader.close(); reader = getEventReader(new ByteArrayInputStream(copy.toByteArray())); bos = new ByteArrayOutputStream(); writer = xof.createXMLEventWriter(bos); if (isMediaContent(title)) { final XmlElement entryElement = getAtomElement(reader, writer, "entry"); writer.add(entryElement.getStart()); writer.add(entryElement.getContentReader()); addAtomElement( IOUtils.toInputStream(String.format("<content type=\"*/*\" src=\"%s/$value\" />", href)), writer); writer.add(entryElement.getEnd()); } else { try { final XmlElement entryElement = getAtomElement(reader, writer, PROPERTIES); addAtomElement(IOUtils.toInputStream("<content type=\"application/xml\">"), writer); writer.add(entryElement.getStart()); writer.add(entryElement.getContentReader()); writer.add(entryElement.getEnd()); addAtomElement(IOUtils.toInputStream("</content>"), writer); } catch (Exception nf) { reader.close(); reader = getEventReader(new ByteArrayInputStream(copy.toByteArray())); bos = new ByteArrayOutputStream(); writer = xof.createXMLEventWriter(bos); final XmlElement entryElement = getAtomElement(reader, writer, "entry"); writer.add(entryElement.getStart()); writer.add(entryElement.getContentReader()); addAtomElement(IOUtils.toInputStream("<content type=\"application/xml\"/>"), writer); writer.add(entryElement.getEnd()); } } writer.add(reader); writer.flush(); writer.close(); } finally { reader.close(); } return new ByteArrayInputStream(bos.toByteArray()); }
From source file:com.msopentech.odatajclient.testservice.utils.XMLUtilities.java
@Override public InputStream selectEntity(final InputStream entity, final String[] propertyNames) throws Exception { final XMLEventReader reader = getEventReader(entity); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final XMLOutputFactory xof = XMLOutputFactory.newInstance(); final XMLEventWriter writer = xof.createXMLEventWriter(bos); final List<String> found = new ArrayList<String>(Arrays.asList(propertyNames)); boolean inProperties = false; boolean writeCurrent = true; Boolean writeNext = null;//from w ww. jav a 2 s. com String currentName = null; final List<String> fieldToBeSaved = new ArrayList<String>(Arrays.asList(propertyNames)); while (reader.hasNext()) { final XMLEvent event = reader.nextEvent(); if (event.getEventType() == XMLStreamConstants.START_ELEMENT && LINK.equals(event.asStartElement().getName().getLocalPart()) && !fieldToBeSaved .contains(event.asStartElement().getAttributeByName(new QName("title")).getValue()) && !"edit".equals(event.asStartElement().getAttributeByName(new QName("rel")).getValue())) { writeCurrent = false; } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT && LINK.equals(event.asEndElement().getName().getLocalPart())) { writeNext = true; } else if (event.getEventType() == XMLStreamConstants.START_ELEMENT && (PROPERTIES).equals(event.asStartElement().getName().getLocalPart())) { writeCurrent = true; writeNext = false; inProperties = true; } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT && (PROPERTIES).equals(event.asEndElement().getName().getLocalPart())) { writeCurrent = true; } else if (inProperties) { if (event.getEventType() == XMLStreamConstants.START_ELEMENT) { final String elementName = event.asStartElement().getName().getLocalPart(); for (String propertyName : propertyNames) { if ((ATOM_PROPERTY_PREFIX + propertyName.trim()).equals(elementName)) { writeCurrent = true; found.remove(propertyName); currentName = propertyName; } } } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT && StringUtils.isNotBlank(currentName) && (ATOM_PROPERTY_PREFIX + currentName.trim()) .equals(event.asEndElement().getName().getLocalPart())) { writeNext = false; currentName = null; } } if (writeCurrent) { writer.add(event); } if (writeNext != null) { writeCurrent = writeNext; writeNext = null; } } writer.flush(); writer.close(); reader.close(); IOUtils.closeQuietly(entity); // Do not raise any exception in order to support FC properties as well // if (!found.isEmpty()) { // throw new Exception(String.format("Could not find a properties '%s'", found)); // } return new ByteArrayInputStream(bos.toByteArray()); }
From source file:eu.delving.sip.xml.SourceConverter.java
public void parse(InputStream inputStream, OutputStream outputStream, Map<String, String> namespaces) throws StorageException { progressListener.prepareFor(totalRecords); // anonymousRecords = Integer.parseInt(System.getProperty(ANONYMOUS_RECORDS_PROPERTY, "0")); Path path = Path.create(); try {/*w w w.ja va 2 s .c om*/ XMLEventReader in = inputFactory.createXMLEventReader(new StreamSource(inputStream, "UTF-8")); XMLEventWriter out = outputFactory.createXMLEventWriter(new OutputStreamWriter(outputStream, "UTF-8")); processEvents: while (!finished) { XMLEvent event = in.nextEvent(); switch (event.getEventType()) { case XMLEvent.START_DOCUMENT: out.add(eventFactory.createStartDocument()); out.add(eventFactory.createCharacters("\n")); List<Namespace> nslist = new ArrayList<Namespace>(); for (Map.Entry<String, String> entry : namespaces.entrySet()) { if (entry.getKey().isEmpty() || entry.getValue().trim().isEmpty()) continue; if (XSI_SCHEMA.equals(entry.getValue())) continue; nslist.add(eventFactory.createNamespace(entry.getKey(), entry.getValue())); } out.add(eventFactory.createStartElement("", "", ENVELOPE_TAG, null, nslist.iterator())); out.add(eventFactory.createCharacters("\n")); break; case XMLEvent.START_ELEMENT: boolean followsStart = start != null; start = event.asStartElement(); if (linesAvailable()) { eventBuffer.add(eventFactory.createCharacters("\n")); eventBuffer.add(eventFactory.createStartElement("", "", TEXT_TAG, null, null)); Iterator<String> walk = lines.iterator(); while (walk.hasNext()) { eventBuffer.add(eventFactory.createCharacters(walk.next())); if (walk.hasNext()) { eventBuffer.add(eventFactory.createEndElement("", "", TEXT_TAG)); eventBuffer.add(eventFactory.createCharacters("\n")); eventBuffer.add(eventFactory.createStartElement("", "", TEXT_TAG, null, null)); } } eventBuffer.add(eventFactory.createEndElement("", "", TEXT_TAG)); lines.clear(); } path = path.child(Tag.element(start.getName())); handleStartElement(path, followsStart); progressListener.setProgress(recordCount); break; case XMLEvent.END_ELEMENT: EndElement end = event.asEndElement(); if (recordEvents) { if (path.equals(recordRootPath)) { if (unique != null) { outputRecord(out); recordCount++; } else { clearEvents(); } } else { if (!uniqueElementPath.peek().isAttribute() && path.equals(uniqueElementPath)) { unique = StringUtils.join(lines, ""); } boolean addEndTag = true; if (linesAvailable()) { Iterator<String> walk = lines.iterator(); while (walk.hasNext()) { eventBuffer.add(eventFactory.createCharacters(walk.next())); if (walk.hasNext()) { eventBuffer.add(end); eventBuffer.add(eventFactory.createCharacters("\n")); handleStartElement(path, false); } } } else { if (eventBuffer.get(eventBuffer.size() - 1).isStartElement()) { eventBuffer.remove(eventBuffer.size() - 1); // remove the start event addEndTag = false; } } lines.clear(); if (addEndTag) { eventBuffer.add(end); eventBuffer.add(eventFactory.createCharacters("\n")); } } } start = null; path = path.parent(); break; case XMLEvent.END_DOCUMENT: out.add(eventFactory.createEndElement("", "", ENVELOPE_TAG)); out.add(eventFactory.createCharacters("\n")); out.add(eventFactory.createEndDocument()); out.flush(); break processEvents; case XMLEvent.CHARACTERS: case XMLEvent.CDATA: if (recordEvents) extractLines(event.asCharacters().getData()); break; } } } catch (CancelException e) { throw new StorageException("Cancelled", e); } catch (StorageException e) { throw e; } catch (Exception e) { throw new StorageException("Storage problem", e); } finally { if (uniqueRepeatCount > 0) { progressListener.getFeedback().alert(String.format("Uniqueness violations : " + uniqueRepeatCount)); } IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(outputStream); } }
From source file:org.alex73.osm.converters.bel.Convert.java
public static void main(String[] args) throws Exception { loadStreetNamesForHouses();//from ww w .j a v a 2s. c o m InputStream in = new BZip2CompressorInputStream( new BufferedInputStream(new FileInputStream("tmp/belarus-latest.osm.bz2"), BUFFER_SIZE)); // create xml event reader for input stream XMLEventFactory eventFactory = XMLEventFactory.newInstance(); XMLEvent newLine = eventFactory.createCharacters("\n"); XMLInputFactory xif = XMLInputFactory.newInstance(); XMLOutputFactory xof = XMLOutputFactory.newInstance(); XMLEventReader reader = xif.createXMLEventReader(in); XMLEventWriter wrCyr = xof.createXMLEventWriter( new BufferedOutputStream(new FileOutputStream("tmp/belarus-bel.osm"), BUFFER_SIZE)); XMLEventWriter wrInt = xof.createXMLEventWriter( new BufferedOutputStream(new FileOutputStream("tmp/belarus-intl.osm"), BUFFER_SIZE)); // initialize jaxb JAXBContext jaxbCtx = JAXBContext.newInstance(Node.class, Way.class, Relation.class); Unmarshaller um = jaxbCtx.createUnmarshaller(); Marshaller m = jaxbCtx.createMarshaller(); m.setProperty(Marshaller.JAXB_FRAGMENT, true); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); XMLEvent e = null; while ((e = reader.peek()) != null) { boolean processed = false; if (e.isStartElement()) { StartElement se = (StartElement) e; switch (se.getName().getLocalPart()) { case "way": Way way = um.unmarshal(reader, Way.class).getValue(); if (way.getId() == 25439425) { System.out.println(); } fixBel(way.getTag(), "name:be", "name"); String nameBeHouse = houseStreetBe.get(way.getId()); if (nameBeHouse != null) { setTag(way.getTag(), "addr:street", nameBeHouse); } m.marshal(way, wrCyr); fixInt(way.getTag()); m.marshal(way, wrInt); wrCyr.add(newLine); wrInt.add(newLine); processed = true; break; case "node": Node node = um.unmarshal(reader, Node.class).getValue(); fixBel(node.getTag(), "name:be", "name"); // fixBel(node.getTag(),"addr:street:be","addr:street"); m.marshal(node, wrCyr); fixInt(node.getTag()); m.marshal(node, wrInt); wrCyr.add(newLine); wrInt.add(newLine); processed = true; break; case "relation": Relation relation = um.unmarshal(reader, Relation.class).getValue(); fixBel(relation.getTag(), "name:be", "name"); // fixBel(relation.getTag(),"addr:street:be","addr:street"); m.marshal(relation, wrCyr); fixInt(relation.getTag()); m.marshal(relation, wrInt); wrCyr.add(newLine); wrInt.add(newLine); processed = true; break; } } if (!processed) { wrCyr.add(e); wrInt.add(e); } reader.next(); } wrCyr.flush(); wrCyr.close(); wrInt.flush(); wrInt.close(); System.out.println("UniqueTranslatedTags: " + uniqueTranslatedTags); }
From source file:org.apache.olingo.fit.utils.AbstractXMLUtilities.java
/** * {@inheritDoc }// w w w . j a v a 2s .c o m */ @Override protected InputStream addLinks(final String entitySetName, final String entitykey, final InputStream is, final Set<String> links) throws Exception { // ----------------------------------------- // 0. Build reader and writer // ----------------------------------------- final XMLEventReader reader = getEventReader(is); final XMLEventFactory eventFactory = XMLEventFactory.newInstance(); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final XMLEventWriter writer = getEventWriter(bos); // ----------------------------------------- final Map.Entry<Integer, XmlElement> entry = extractElement(reader, writer, Collections.singletonList("entry"), 0, 1, 1); writer.add(entry.getValue().getStart()); // add for links for (String link : links) { final Set<Attribute> attributes = new HashSet<Attribute>(); attributes.add(eventFactory.createAttribute(new QName("title"), link)); attributes.add(eventFactory.createAttribute(new QName("href"), Commons.getLinksURI(version, entitySetName, entitykey, link))); attributes.add(eventFactory.createAttribute(new QName("rel"), Constants.ATOM_LINK_REL + link)); attributes.add(eventFactory.createAttribute(new QName("type"), Commons.linkInfo.get(version).isFeed(entitySetName, link) ? Constants.ATOM_LINK_FEED : Constants.ATOM_LINK_ENTRY)); writer.add(eventFactory.createStartElement(new QName(LINK), attributes.iterator(), null)); writer.add(eventFactory.createEndElement(new QName(LINK), null)); } writer.add(entry.getValue().getContentReader()); writer.add(entry.getValue().getEnd()); writer.add(reader); IOUtils.closeQuietly(is); writer.flush(); writer.close(); reader.close(); return new ByteArrayInputStream(bos.toByteArray()); }
From source file:org.apache.olingo.fit.utils.AbstractXMLUtilities.java
@Override public InputStream addEditLink(final InputStream content, final String title, final String href) throws Exception { final ByteArrayOutputStream copy = new ByteArrayOutputStream(); IOUtils.copy(content, copy);/*from w w w . jav a 2s . com*/ IOUtils.closeQuietly(content); XMLEventReader reader = getEventReader(new ByteArrayInputStream(copy.toByteArray())); ByteArrayOutputStream bos = new ByteArrayOutputStream(); XMLEventWriter writer = getEventWriter(bos); final String editLinkElement = String.format("<link rel=\"edit\" title=\"%s\" href=\"%s\" />", title, href); try { // check edit link existence extractElement(reader, writer, Collections.<String>singletonList(LINK), Collections.<Map.Entry<String, String>>singletonList( new AbstractMap.SimpleEntry<String, String>("rel", "edit")), false, 0, -1, -1); addAtomElement(IOUtils.toInputStream(editLinkElement), writer); writer.add(reader); } catch (Exception e) { reader.close(); reader = getEventReader(new ByteArrayInputStream(copy.toByteArray())); bos = new ByteArrayOutputStream(); writer = getEventWriter(bos); final XmlElement entryElement = extractElement(reader, writer, Collections.<String>singletonList("entry"), 0, 1, 1).getValue(); writer.add(entryElement.getStart()); addAtomElement(IOUtils.toInputStream(editLinkElement), writer); writer.add(entryElement.getContentReader()); writer.add(entryElement.getEnd()); writer.add(reader); writer.flush(); writer.close(); } finally { reader.close(); } return new ByteArrayInputStream(bos.toByteArray()); }
From source file:org.apache.olingo.fit.utils.AbstractXMLUtilities.java
public InputStream addAtomContent(final InputStream content, final String title, final String href) throws Exception { final ByteArrayOutputStream copy = new ByteArrayOutputStream(); IOUtils.copy(content, copy);//from w w w .j a va 2 s . co m IOUtils.closeQuietly(content); XMLEventReader reader = getEventReader(new ByteArrayInputStream(copy.toByteArray())); ByteArrayOutputStream bos = new ByteArrayOutputStream(); XMLEventWriter writer = getEventWriter(bos); try { // check edit link existence XmlElement contentElement = extractElement(reader, writer, Collections.<String>singletonList("content"), 0, 2, 2).getValue(); writer.add(contentElement.getStart()); writer.add(contentElement.getContentReader()); writer.add(contentElement.getEnd()); writer.add(reader); } catch (Exception e) { reader.close(); reader = getEventReader(new ByteArrayInputStream(copy.toByteArray())); bos = new ByteArrayOutputStream(); writer = getEventWriter(bos); if (isMediaContent(title)) { final XmlElement entryElement = extractElement(reader, writer, Collections.<String>singletonList("entry"), 0, 1, 1).getValue(); writer.add(entryElement.getStart()); writer.add(entryElement.getContentReader()); addAtomElement( IOUtils.toInputStream(String.format("<content type=\"*/*\" src=\"%s/$value\" />", href)), writer); writer.add(entryElement.getEnd()); } else { try { final XmlElement entryElement = extractElement(reader, writer, Collections.<String>singletonList(PROPERTIES), 0, 2, 3).getValue(); addAtomElement(IOUtils.toInputStream("<content type=\"application/xml\">"), writer); writer.add(entryElement.getStart()); writer.add(entryElement.getContentReader()); writer.add(entryElement.getEnd()); addAtomElement(IOUtils.toInputStream("</content>"), writer); } catch (Exception nf) { reader.close(); reader = getEventReader(new ByteArrayInputStream(copy.toByteArray())); bos = new ByteArrayOutputStream(); writer = getEventWriter(bos); final XmlElement entryElement = extractElement(reader, writer, Collections.<String>singletonList("entry"), 0, 1, 1).getValue(); writer.add(entryElement.getStart()); writer.add(entryElement.getContentReader()); addAtomElement(IOUtils.toInputStream("<content type=\"application/xml\"/>"), writer); writer.add(entryElement.getEnd()); } } writer.add(reader); writer.flush(); writer.close(); } finally { reader.close(); } return new ByteArrayInputStream(bos.toByteArray()); }
From source file:org.apache.olingo.fit.utils.AbstractXMLUtilities.java
public InputStream addAtomInlinecount(final InputStream feed, final int count, final Accept accept) throws Exception { final XMLEventReader reader = getEventReader(feed); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final XMLEventWriter writer = getEventWriter(bos); try {/*from www . j a v a 2 s . c om*/ final XmlElement feedElement = extractElement(reader, writer, Collections.<String>singletonList("feed"), 0, 1, 1).getValue(); writer.add(feedElement.getStart()); addAtomElement(IOUtils.toInputStream(String.format("<m:count>%d</m:count>", count)), writer); writer.add(feedElement.getContentReader()); writer.add(feedElement.getEnd()); while (reader.hasNext()) { writer.add(reader.nextEvent()); } } finally { writer.flush(); writer.close(); reader.close(); IOUtils.closeQuietly(feed); } return new ByteArrayInputStream(bos.toByteArray()); }