Example usage for javax.xml.stream XMLEventFactory createNamespace

List of usage examples for javax.xml.stream XMLEventFactory createNamespace

Introduction

In this page you can find the example usage for javax.xml.stream XMLEventFactory createNamespace.

Prototype

public abstract Namespace createNamespace(String prefix, String namespaceUri);

Source Link

Document

Create a new Namespace

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(System.out);

    Namespace ns1 = eventFactory.createNamespace("ns1", "http://www.e.com/ns1");
    Namespace ns2 = eventFactory.createNamespace("ns2", "http://www.e.com/ns2");
    List<Namespace> namespaceList = new ArrayList<Namespace>();
    namespaceList.add(ns1);/* ww  w  . jav  a2 s  .  c o  m*/
    namespaceList.add(ns2);

    Attribute attribute = eventFactory.createAttribute(ns2.getPrefix(), ns2.getNamespaceURI(), "attribute",
            "true");

    writer.add(eventFactory.createStartElement(ns1.getPrefix(), ns1.getNamespaceURI(), "sample",
            Collections.singletonList(attribute).iterator(), namespaceList.iterator()));
    writer.add(eventFactory.createEndDocument());
    writer.flush();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(System.out);

    writer.add(eventFactory.createStartElement("ns1", "http://www.e.com/ns1", "sample", null, null));
    writer.add(eventFactory.createNamespace("ns1", "http://www.e.com/ns1"));
    writer.add(eventFactory.createNamespace("ns2", "http://www.e.com/ns2"));
    writer.add(eventFactory.createAttribute("ns2", "http://www.e.com/ns2", "attribute", "true"));
    writer.add(eventFactory.createEndDocument());
    writer.flush();/*  ww  w .  j a v a  2  s  .c  om*/
}

From source file:com.msopentech.odatajclient.testservice.utils.XMLUtilities.java

private InputStream writeFromStartToEndElement(final StartElement element, final XMLEventReader reader,
        final boolean document) throws XMLStreamException {
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    final XMLOutputFactory xof = XMLOutputFactory.newInstance();
    final XMLEventWriter writer = xof.createXMLEventWriter(bos);

    final QName name = element.getName();

    if (document) {
        final XMLEventFactory eventFactory = XMLEventFactory.newInstance();
        writer.add(eventFactory.createStartDocument("UTF-8", "1.0"));
        writer.add(element);/* w  w w.ja  va 2 s  . c o m*/

        if (element.getAttributeByName(new QName(ATOM_DATASERVICE_NS)) == null) {
            writer.add(eventFactory.createNamespace(ATOM_PROPERTY_PREFIX.substring(0, 1), DATASERVICES_NS));
        }
        if (element.getAttributeByName(new QName(ATOM_METADATA_NS)) == null) {
            writer.add(eventFactory.createNamespace(ATOM_METADATA_PREFIX.substring(0, 1), METADATA_NS));
        }
    } else {
        writer.add(element);
    }

    XMLEvent event = element;

    while (reader.hasNext() && !(event.isEndElement() && name.equals(event.asEndElement().getName()))) {
        event = reader.nextEvent();
        writer.add(event);
    }

    writer.flush();
    writer.close();

    return new ByteArrayInputStream(bos.toByteArray());
}

From source file:org.apache.olingo.fit.utils.AbstractXMLUtilities.java

@Override
public InputStream getProperty(final String entitySetName, final String entityId, final List<String> path,
        final String edmType) throws Exception {
    final List<String> pathElements = new ArrayList<String>();

    for (String element : path) {
        pathElements.add(ATOM_PROPERTY_PREFIX + element);
    }//from w  ww .  jav a 2s .  c  om

    final InputStream src = fsManager.readFile(Commons.getEntityBasePath(entitySetName, entityId) + ENTITY,
            Accept.XML);

    final XMLEventReader reader = getEventReader(src);
    final XmlElement property = extractElement(reader, null, pathElements, 0, 3, 4).getValue();

    reader.close();
    IOUtils.closeQuietly(src);

    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    final XMLEventWriter writer = getEventWriter(bos);

    final XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    writer.add(eventFactory.createStartDocument("UTF-8", "1.0"));
    writer.add(property.getStart());

    if (property.getStart().getAttributeByName(new QName(ATOM_DATASERVICE_NS)) == null) {
        writer.add(eventFactory.createNamespace(ATOM_PROPERTY_PREFIX.substring(0, 1), DATASERVICES_NS));
    }
    if (property.getStart().getAttributeByName(new QName(ATOM_METADATA_NS)) == null) {
        writer.add(eventFactory.createNamespace(ATOM_METADATA_PREFIX.substring(0, 1), METADATA_NS));
    }

    writer.add(property.getContentReader());
    writer.add(property.getEnd());

    writer.flush();
    writer.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();//w  w w .j  a  va2 s  . com
            } 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.emonocot.job.io.StaxEventItemWriter.java

/**
 * Writes simple XML header containing://from ww  w . ja  v  a2s  .co  m
 * <ul>
 * <li>xml declaration - defines encoding and XML version</li>
 * <li>opening tag of the root element and its attributes</li>
 * </ul>
 * If this is not sufficient for you, simply override this method. Encoding,
 * version and root tag name can be retrieved with corresponding getters.
 *
 * @param writer
 *            XML event writer
 * @throws XMLStreamException if there is a problem starting the document
 */
protected final void startDocument(final XMLEventWriter writer) throws XMLStreamException {

    XMLEventFactory factory = XMLEventFactory.newInstance();

    // write start document
    writer.add(factory.createStartDocument(getEncoding(), getVersion()));

    // write root tag
    writer.add(
            factory.createStartElement(getRootTagNamespacePrefix(), getRootTagNamespace(), getRootTagName()));
    if (StringUtils.hasText(getRootTagNamespace())) {
        if (StringUtils.hasText(getRootTagNamespacePrefix())) {
            writer.add(factory.createNamespace(getRootTagNamespacePrefix(), getRootTagNamespace()));
        } else {
            writer.add(factory.createNamespace(getRootTagNamespace()));
        }
    }

    // write root tag attributes
    if (!CollectionUtils.isEmpty(getRootElementAttributes())) {

        for (Map.Entry<String, String> entry : getRootElementAttributes().entrySet()) {
            String key = entry.getKey();
            if (key.startsWith("xmlns")) {
                String prefix = "";
                if (key.contains(":")) {
                    prefix = key.substring(key.indexOf(":") + 1);
                }
                writer.add(factory.createNamespace(prefix, entry.getValue()));
            } else {
                writer.add(factory.createAttribute(key, entry.getValue()));
            }
        }

    }

    /*
     * This forces the flush to write the end of the root element and avoids
     * an off-by-one error on restart.
     */
    writer.add(factory.createIgnorableSpace(""));
    writer.flush();

}

From source file:org.springframework.batch.item.xml.StaxEventItemWriter.java

/**
 * Writes simple XML header containing:/*  www  . j  ava 2  s  .c  o m*/
 * <ul>
 * <li>xml declaration - defines encoding and XML version</li>
 * <li>opening tag of the root element and its attributes</li>
 * </ul>
 * If this is not sufficient for you, simply override this method. Encoding,
 * version and root tag name can be retrieved with corresponding getters.
 * 
 * @param writer XML event writer
 * @throws XMLStreamException
 */
protected void startDocument(XMLEventWriter writer) throws XMLStreamException {

    XMLEventFactory factory = createXmlEventFactory();

    // write start document
    writer.add(factory.createStartDocument(getEncoding(), getVersion()));

    // write root tag
    writer.add(
            factory.createStartElement(getRootTagNamespacePrefix(), getRootTagNamespace(), getRootTagName()));
    if (StringUtils.hasText(getRootTagNamespace())) {
        if (StringUtils.hasText(getRootTagNamespacePrefix())) {
            writer.add(factory.createNamespace(getRootTagNamespacePrefix(), getRootTagNamespace()));
        } else {
            writer.add(factory.createNamespace(getRootTagNamespace()));
        }
    }

    // write root tag attributes
    if (!CollectionUtils.isEmpty(getRootElementAttributes())) {

        for (Map.Entry<String, String> entry : getRootElementAttributes().entrySet()) {
            String key = entry.getKey();
            if (key.startsWith("xmlns")) {
                String prefix = "";
                if (key.contains(":")) {
                    prefix = key.substring(key.indexOf(":") + 1);
                }
                writer.add(factory.createNamespace(prefix, entry.getValue()));
            } else {
                writer.add(factory.createAttribute(key, entry.getValue()));
            }
        }

    }

    /*
     * This forces the flush to write the end of the root element and avoids
     * an off-by-one error on restart.
     */
    writer.add(factory.createIgnorableSpace(""));
    writer.flush();

}