List of usage examples for javax.xml.stream XMLEventFactory createEndElement
public abstract EndElement createEndElement(QName name, Iterator<? extends Namespace> namespaces);
From source file:com.msopentech.odatajclient.testservice.utils.XMLUtilities.java
/** * {@inheritDoc }/* w w w. j a va 2s . c om*/ */ @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:org.apache.olingo.fit.utils.AbstractXMLUtilities.java
/** * {@inheritDoc }//from ww w . jav a 2s . co 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.XMLUtilities.java
/** * {@inheritDoc }/*from w w w . j a v a 2 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 XMLEventWriter writer = getEventWriter(bos); // ----------------------------------------- final Map.Entry<Integer, XMLElement> entry = extractElement(reader, writer, Collections.singletonList("entry"), 0, 1, 1); writer.add(entry.getValue().getStart()); final Map<String, NavigationProperty> navigationProperties = metadata .getNavigationProperties(entitySetName); // 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(entitySetName, entitykey, link))); attributes.add(eventFactory.createAttribute(new QName("rel"), Constants.get(ConstantKey.ATOM_LINK_REL) + link)); attributes.add(eventFactory.createAttribute(new QName("type"), navigationProperties.get(link).isEntitySet() ? Constants.get(ConstantKey.ATOM_LINK_FEED) : Constants.get(ConstantKey.ATOM_LINK_ENTRY))); writer.add(eventFactory.createStartElement(new QName(Constants.get(ConstantKey.LINK)), attributes.iterator(), null)); writer.add(eventFactory.createEndElement(new QName(Constants.get(ConstantKey.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.callimachusproject.behaviours.ZipArchiveSupport.java
public XMLEventReader createAtomFeedFromArchive(final String id, final String entryPattern) throws IOException { final FileObject file = this; final XMLEventFactory ef = XMLEventFactory.newInstance(); final byte[] buf = new byte[1024]; final ZipArchiveInputStream zip = new ZipArchiveInputStream(file.openInputStream()); return new XMLEventReaderBase() { private boolean started; private boolean ended; public void close() throws XMLStreamException { try { zip.close();//from w w w. jav a 2 s . co m } catch (IOException e) { throw new XMLStreamException(e); } } protected boolean more() throws XMLStreamException { try { ZipArchiveEntry entry; if (!started) { Namespace atom = ef.createNamespace(FEED.getPrefix(), FEED.getNamespaceURI()); add(ef.createStartDocument()); add(ef.createStartElement(FEED, null, Arrays.asList(atom).iterator())); add(ef.createStartElement(TITLE, null, null)); add(ef.createCharacters(file.getName())); add(ef.createEndElement(TITLE, null)); add(ef.createStartElement(ID, null, null)); add(ef.createCharacters(id)); add(ef.createEndElement(ID, null)); Attribute href = ef.createAttribute("href", file.toUri().toASCIIString()); List<Attribute> attrs = Arrays.asList(href, ef.createAttribute("type", "application/zip")); add(ef.createStartElement(LINK, attrs.iterator(), null)); add(ef.createEndElement(LINK, null)); add(ef.createStartElement(UPDATED, null, null)); add(ef.createCharacters(format(new Date(file.getLastModified())))); add(ef.createEndElement(UPDATED, null)); started = true; return true; } else if (started && !ended && (entry = zip.getNextZipEntry()) != null) { String name = entry.getName(); String link = entryPattern.replace("{entry}", PercentCodec.encode(name)); MimetypesFileTypeMap mimetypes = new javax.activation.MimetypesFileTypeMap(); String type = mimetypes.getContentType(name); if (type == null || type.length() == 0) { type = "application/octet-stream"; } add(ef.createStartElement(ENTRY, null, null)); add(ef.createStartElement(TITLE, null, null)); add(ef.createCharacters(name)); add(ef.createEndElement(TITLE, null)); Attribute href = ef.createAttribute("href", link); List<Attribute> attrs = Arrays.asList(href, ef.createAttribute("type", type)); add(ef.createStartElement(LINK, attrs.iterator(), null)); add(ef.createEndElement(LINK, null)); long size = entry.getSize(); if (size > 0) { zip.skip(size); } else { while (zip.read(buf, 0, buf.length) >= 0) ; } add(ef.createEndElement(ENTRY, null)); return true; } else if (!ended) { add(ef.createEndElement(FEED, null)); add(ef.createEndDocument()); ended = true; return true; } else { return false; } } catch (IOException e) { throw new XMLStreamException(e); } } }; }
From source file:org.omnaest.utils.xml.XMLNestedMapConverter.java
/** * @param nestedMap/* www . j a va 2 s .co m*/ * {@link Map} * @param outputStream * {@link OutputStream} * @param includeDocumentHeader * if true a xml document header is written out additionally */ private <K> void toXML(Map<K, Object> nestedMap, OutputStream outputStream, final ElementConverter<K, QName> keyElementConverter, boolean includeDocumentHeader) { // if (nestedMap != null && keyElementConverter != null && outputStream != null) { // try { // final XMLOutputFactory xmlOutputFactory = this.xmlInstanceContextFactory.newXmlOutputFactory(); final XMLEventFactory xmlEventFactory = this.xmlInstanceContextFactory.newXmlEventFactory(); Assert.isNotNull(xmlOutputFactory, "xmlOutputFactory must not be null"); Assert.isNotNull(xmlEventFactory, "xmlEventFactory must not be null"); // final XMLEventWriter xmlEventWriter = xmlOutputFactory.createXMLEventWriter(outputStream, this.encoding); final ExceptionHandler exceptionHandler = this.exceptionHandler; // try { // class Helper { /* ********************************************** Variables ********************************************** */ private List<String> namespaceStack = new ArrayList<String>(); /* ********************************************** Methods ********************************************** */ @SuppressWarnings("unchecked") public void write(Map<K, Object> map) { if (map != null) { for (K key : map.keySet()) { // final QName tagName = keyElementConverter.convert(key); final Object value = map.get(key); // if (value instanceof String) { // this.writeStartTag(tagName); // final String text = (String) value; this.writeText(text); // this.writeEndTag(tagName); } else if (value instanceof Map) { // this.writeStartTag(tagName); // final Map<K, Object> subMap = (Map<K, Object>) value; this.write(subMap); // this.writeEndTag(tagName); } else if (value instanceof List) { // final List<Object> valueList = (List<Object>) value; this.write(tagName, valueList); } } } } /** * @param tagName */ private void writeStartTag(QName tagName) { // try { // final String namespaceURI = tagName.getNamespaceURI(); // final Iterator<?> attributes = null; final Iterator<?> namespaces = StringUtils.isNotBlank(namespaceURI) && !StringUtils .equals(namespaceURI, ListUtils.lastElement(this.namespaceStack)) ? IteratorUtils.valueOf( xmlEventFactory.createNamespace(namespaceURI)) : null; StartElement startElement = xmlEventFactory.createStartElement(tagName, attributes, namespaces); xmlEventWriter.add(startElement); // this.namespaceStack.add(namespaceURI); } catch (Exception e) { exceptionHandler.handleException(e); } } /** * @param tagName */ private void writeEndTag(QName tagName) { // try { // final Iterator<?> namespaces = null; EndElement endElement = xmlEventFactory.createEndElement(tagName, namespaces); xmlEventWriter.add(endElement); // ListUtils.removeLast(this.namespaceStack); } catch (Exception e) { exceptionHandler.handleException(e); } } /** * @param text */ private void writeText(String text) { // try { // final Characters characters = xmlEventFactory.createCharacters(text); xmlEventWriter.add(characters); } catch (Exception e) { exceptionHandler.handleException(e); } } /** * @param tagName * @param valueList */ @SuppressWarnings("unchecked") private void write(QName tagName, List<Object> valueList) { if (valueList != null) { for (Object value : valueList) { // if (value != null) { // this.writeStartTag(tagName); // if (value instanceof Map) { // final Map<K, Object> map = (Map<K, Object>) value; this.write(map); } else if (value instanceof String) { // final String text = (String) value; this.writeText(text); } // this.writeEndTag(tagName); } } } } } // if (includeDocumentHeader) { xmlEventWriter.add(xmlEventFactory.createStartDocument()); } // new Helper().write(nestedMap); // xmlEventWriter.add(xmlEventFactory.createEndDocument()); } finally { xmlEventWriter.close(); outputStream.flush(); } } catch (Exception e) { if (this.exceptionHandler != null) { this.exceptionHandler.handleException(e); } } } }