Example usage for javax.xml.stream XMLStreamWriter writeStartDocument

List of usage examples for javax.xml.stream XMLStreamWriter writeStartDocument

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamWriter writeStartDocument.

Prototype

public void writeStartDocument() throws XMLStreamException;

Source Link

Document

Write the XML Declaration.

Usage

From source file:com.fiorano.openesb.application.application.Application.java

/**
 * Writes manageable properties file with the specified label
 * @param applicationFolderName event process folder
 * @param label environment label//  w w w  .  j  a  v  a 2 s .  com
 * @throws FioranoException FioranoException
 * @throws XMLStreamException XMLStreamException
 */
public void writeManageableProperties(File applicationFolderName, Label label)
        throws FioranoException, XMLStreamException {
    File manageablePropertiesFile = getManageablePropertiesFile(applicationFolderName, label);
    if (!manageablePropertiesFile.exists())
        manageablePropertiesFile.getParentFile().mkdirs();

    XMLOutputFactory outputFactory = XMLUtils.getStaxOutputFactory();
    //        System.out.println(".....:"+outputFactory);
    //outputFactory.setProperty(XMLOutputFactory.INDENTATION, "/t");
    XMLStreamWriter writer = null;
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(manageablePropertiesFile);
        writer = outputFactory.createXMLStreamWriter(fos);
        writer.writeStartDocument();
        {

            writer.writeStartElement(ELEM_TARGET, ELEM_TARGET, Namespaces.URI_TARGET);
            writer.writeAttribute(XMLNS_TARGET, Namespaces.URI_TARGET);
            writer.writeAttribute(XMLNS_XSI, Namespaces.URI_XSI);
            writer.writeAttribute(XSI_LOCATION, Namespaces.URI_ENV_XSD);
            {
                for (ServiceInstance instance : getServiceInstances()) {
                    instance.writeManageableProperties(writer);
                }

            }
            writer.writeEndElement();
        }

        writer.writeEndDocument();

        writer.flush();
    } catch (XMLStreamException e) {
        throw new FioranoException(e);
    } catch (IOException e) {
        throw new FioranoException(e);
    } finally {
        try {
            if (writer != null)
                writer.close();
        } catch (XMLStreamException e) {
            // Ignore
        }

        try {
            if (fos != null)
                fos.close();
        } catch (IOException e) {
            // Ignore
        }
    }

}

From source file:com.microsoft.tfs.core.ws.runtime.types.StaxAnyContentType.java

@Override
public void readFromElement(final XMLStreamReader reader) throws XMLStreamException {
    FastTempOutputStream ftos = null;//from  www  . j  av a  2  s. c o  m
    XMLStreamWriter writer = null;

    /*
     * When this method is called, the writer is positioned at the element
     * that contains the "any" content. Process the child elements until the
     * container is done.
     */

    /*
     * Advance one event to get the first child.
     */
    int event = reader.next();

    do {
        if (event == XMLStreamConstants.START_ELEMENT) {
            /*
             * Parse the child element into its own temp file. The copier
             * will read the child's end element.
             */
            try {
                /*
                 * Get a new fast temp output stream.
                 */
                ftos = new FastTempOutputStream(heapStorageLimitBytes, initialHeapStorageSizeBytes);

                tempOutputStreams.add(ftos);

                /*
                 * Create a writer.
                 */
                writer = StaxFactoryProvider.getXMLOutputFactory().createXMLStreamWriter(ftos,
                        SOAPRequestEntity.SOAP_ENCODING);
                writer.writeStartDocument();

                StaxUtils.copyCurrentElement(reader, writer);

                /*
                 * Make sure to finish off the document.
                 */
                writer.writeEndDocument();
            } finally {
                if (writer != null) {
                    writer.close();
                }

                /*
                 * Closing writers does not close the underlying stream, so
                 * close that manually. This is required so the temp stream
                 * can be read from.
                 */
                if (ftos != null) {
                    try {
                        ftos.close();
                    } catch (final IOException e) {
                        log.error(e);
                    }
                }

            }
        }
    } while ((event = reader.next()) != XMLStreamConstants.END_ELEMENT);
}

From source file:com.liferay.portal.util.LocalizationImpl.java

public String updateLocalization(String xml, String key, String value, String requestedLanguageId,
        String defaultLanguageId, boolean cdata, boolean localized) {

    xml = _sanitizeXML(xml);/*w  w  w .  ja va2s .c  o  m*/

    XMLStreamReader xmlStreamReader = null;
    XMLStreamWriter xmlStreamWriter = null;

    ClassLoader portalClassLoader = PortalClassLoaderUtil.getClassLoader();

    Thread currentThread = Thread.currentThread();

    ClassLoader contextClassLoader = currentThread.getContextClassLoader();

    try {
        if (contextClassLoader != portalClassLoader) {
            currentThread.setContextClassLoader(portalClassLoader);
        }

        XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();

        xmlStreamReader = xmlInputFactory.createXMLStreamReader(new UnsyncStringReader(xml));

        String availableLocales = StringPool.BLANK;

        // Read root node

        if (xmlStreamReader.hasNext()) {
            xmlStreamReader.nextTag();

            availableLocales = xmlStreamReader.getAttributeValue(null, _AVAILABLE_LOCALES);

            if (Validator.isNull(availableLocales)) {
                availableLocales = defaultLanguageId;
            }

            if (availableLocales.indexOf(requestedLanguageId) == -1) {
                availableLocales = StringUtil.add(availableLocales, requestedLanguageId, StringPool.COMMA);
            }
        }

        UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();

        XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();

        xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(unsyncStringWriter);

        xmlStreamWriter.writeStartDocument();
        xmlStreamWriter.writeStartElement(_ROOT);

        if (localized) {
            xmlStreamWriter.writeAttribute(_AVAILABLE_LOCALES, availableLocales);
            xmlStreamWriter.writeAttribute(_DEFAULT_LOCALE, defaultLanguageId);
        }

        _copyNonExempt(xmlStreamReader, xmlStreamWriter, requestedLanguageId, defaultLanguageId, cdata);

        xmlStreamWriter.writeStartElement(key);

        if (localized) {
            xmlStreamWriter.writeAttribute(_LANGUAGE_ID, requestedLanguageId);
        }

        if (cdata) {
            xmlStreamWriter.writeCData(value);
        } else {
            xmlStreamWriter.writeCharacters(value);
        }

        xmlStreamWriter.writeEndElement();
        xmlStreamWriter.writeEndElement();
        xmlStreamWriter.writeEndDocument();

        xmlStreamWriter.close();
        xmlStreamWriter = null;

        xml = unsyncStringWriter.toString();
    } catch (Exception e) {
        if (_log.isWarnEnabled()) {
            _log.warn(e, e);
        }
    } finally {
        if (contextClassLoader != portalClassLoader) {
            currentThread.setContextClassLoader(contextClassLoader);
        }

        if (xmlStreamReader != null) {
            try {
                xmlStreamReader.close();
            } catch (Exception e) {
            }
        }

        if (xmlStreamWriter != null) {
            try {
                xmlStreamWriter.close();
            } catch (Exception e) {
            }
        }
    }

    return xml;
}

From source file:com.liferay.portal.util.LocalizationImpl.java

public String removeLocalization(String xml, String key, String requestedLanguageId, boolean cdata,
        boolean localized) {

    if (Validator.isNull(xml)) {
        return StringPool.BLANK;
    }/*w ww.j  a v a2  s  .  c o m*/

    xml = _sanitizeXML(xml);

    String systemDefaultLanguageId = LocaleUtil.toLanguageId(LocaleUtil.getDefault());

    XMLStreamReader xmlStreamReader = null;
    XMLStreamWriter xmlStreamWriter = null;

    ClassLoader portalClassLoader = PortalClassLoaderUtil.getClassLoader();

    Thread currentThread = Thread.currentThread();

    ClassLoader contextClassLoader = currentThread.getContextClassLoader();

    try {
        if (contextClassLoader != portalClassLoader) {
            currentThread.setContextClassLoader(portalClassLoader);
        }

        XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();

        xmlStreamReader = xmlInputFactory.createXMLStreamReader(new UnsyncStringReader(xml));

        String availableLocales = StringPool.BLANK;
        String defaultLanguageId = StringPool.BLANK;

        // Read root node

        if (xmlStreamReader.hasNext()) {
            xmlStreamReader.nextTag();

            availableLocales = xmlStreamReader.getAttributeValue(null, _AVAILABLE_LOCALES);
            defaultLanguageId = xmlStreamReader.getAttributeValue(null, _DEFAULT_LOCALE);

            if (Validator.isNull(defaultLanguageId)) {
                defaultLanguageId = systemDefaultLanguageId;
            }
        }

        if ((availableLocales != null) && (availableLocales.indexOf(requestedLanguageId) != -1)) {

            availableLocales = StringUtil.remove(availableLocales, requestedLanguageId, StringPool.COMMA);

            UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();

            XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();

            xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(unsyncStringWriter);

            xmlStreamWriter.writeStartDocument();
            xmlStreamWriter.writeStartElement(_ROOT);

            if (localized) {
                xmlStreamWriter.writeAttribute(_AVAILABLE_LOCALES, availableLocales);
                xmlStreamWriter.writeAttribute(_DEFAULT_LOCALE, defaultLanguageId);
            }

            _copyNonExempt(xmlStreamReader, xmlStreamWriter, requestedLanguageId, defaultLanguageId, cdata);

            xmlStreamWriter.writeEndElement();
            xmlStreamWriter.writeEndDocument();

            xmlStreamWriter.close();
            xmlStreamWriter = null;

            xml = unsyncStringWriter.toString();
        }
    } catch (Exception e) {
        if (_log.isWarnEnabled()) {
            _log.warn(e, e);
        }
    } finally {
        if (contextClassLoader != portalClassLoader) {
            currentThread.setContextClassLoader(contextClassLoader);
        }

        if (xmlStreamReader != null) {
            try {
                xmlStreamReader.close();
            } catch (Exception e) {
            }
        }

        if (xmlStreamWriter != null) {
            try {
                xmlStreamWriter.close();
            } catch (Exception e) {
            }
        }
    }

    return xml;
}

From source file:org.gaul.s3proxy.S3ProxyHandler.java

private void handleContainerLocation(HttpServletResponse response, BlobStore blobStore, String containerName)
        throws IOException {
    try (Writer writer = response.getWriter()) {
        XMLStreamWriter xml = xmlOutputFactory.createXMLStreamWriter(writer);
        xml.writeStartDocument();//  w  w w.j a  va  2  s .c o  m
        // TODO: using us-standard semantics but could emit actual location
        xml.writeStartElement("LocationConstraint");
        xml.writeDefaultNamespace(AWS_XMLNS);
        xml.writeEndElement();
        xml.flush();
    } catch (XMLStreamException xse) {
        throw new IOException(xse);
    }
}

From source file:org.gaul.s3proxy.S3ProxyHandler.java

protected final void sendSimpleErrorResponse(HttpServletRequest request, HttpServletResponse response,
        S3ErrorCode code, String message, Map<String, String> elements) throws IOException {
    logger.debug("{} {}", code, elements);

    response.setStatus(code.getHttpStatusCode());

    if (request.getMethod().equals("HEAD")) {
        // The HEAD method is identical to GET except that the server MUST
        // NOT return a message-body in the response.
        return;//ww w. j  a  va 2 s .c  om
    }

    try (Writer writer = response.getWriter()) {
        XMLStreamWriter xml = xmlOutputFactory.createXMLStreamWriter(writer);
        xml.writeStartDocument();
        xml.writeStartElement("Error");

        writeSimpleElement(xml, "Code", code.getErrorCode());
        writeSimpleElement(xml, "Message", message);

        for (Map.Entry<String, String> entry : elements.entrySet()) {
            writeSimpleElement(xml, entry.getKey(), entry.getValue());
        }

        writeSimpleElement(xml, "RequestId", FAKE_REQUEST_ID);

        xml.writeEndElement();
        xml.flush();
    } catch (XMLStreamException xse) {
        throw new IOException(xse);
    }
}

From source file:org.gaul.s3proxy.S3ProxyHandler.java

private void handleMultiBlobRemove(HttpServletResponse response, InputStream is, BlobStore blobStore,
        String containerName) throws IOException {
    DeleteMultipleObjectsRequest dmor = new XmlMapper().readValue(is, DeleteMultipleObjectsRequest.class);
    Collection<String> blobNames = new ArrayList<>();
    for (DeleteMultipleObjectsRequest.S3Object s3Object : dmor.objects) {
        blobNames.add(s3Object.key);
    }/*  w  ww. j a  v  a 2  s  .c o  m*/

    blobStore.removeBlobs(containerName, blobNames);

    try (Writer writer = response.getWriter()) {
        XMLStreamWriter xml = xmlOutputFactory.createXMLStreamWriter(writer);
        xml.writeStartDocument();
        xml.writeStartElement("DeleteResult");
        xml.writeDefaultNamespace(AWS_XMLNS);

        if (!dmor.quiet) {
            for (String blobName : blobNames) {
                xml.writeStartElement("Deleted");

                writeSimpleElement(xml, "Key", blobName);

                xml.writeEndElement();
            }
        }

        // TODO: emit error stanza
        xml.writeEndElement();
        xml.flush();
    } catch (XMLStreamException xse) {
        throw new IOException(xse);
    }
}

From source file:org.gaul.s3proxy.S3ProxyHandler.java

private void handleContainerList(HttpServletResponse response, BlobStore blobStore) throws IOException {
    PageSet<? extends StorageMetadata> buckets = blobStore.list();

    try (Writer writer = response.getWriter()) {
        XMLStreamWriter xml = xmlOutputFactory.createXMLStreamWriter(writer);
        xml.writeStartDocument();/*from   w w  w  .jav  a  2 s  .  c om*/
        xml.writeStartElement("ListAllMyBucketsResult");
        xml.writeDefaultNamespace(AWS_XMLNS);

        writeOwnerStanza(xml);

        xml.writeStartElement("Buckets");
        for (StorageMetadata metadata : buckets) {
            xml.writeStartElement("Bucket");

            writeSimpleElement(xml, "Name", metadata.getName());

            Date creationDate = metadata.getCreationDate();
            if (creationDate == null) {
                // Some providers, e.g., Swift, do not provide container
                // creation date.  Emit a bogus one to satisfy clients like
                // s3cmd which require one.
                creationDate = new Date(0);
            }
            writeSimpleElement(xml, "CreationDate",
                    blobStore.getContext().utils().date().iso8601DateFormat(creationDate).trim());

            xml.writeEndElement();
        }
        xml.writeEndElement();

        xml.writeEndElement();
        xml.flush();
    } catch (XMLStreamException xse) {
        throw new IOException(xse);
    }
}

From source file:org.gaul.s3proxy.S3ProxyHandler.java

private void handleGetContainerAcl(HttpServletResponse response, BlobStore blobStore, String containerName)
        throws IOException {
    ContainerAccess access = blobStore.getContainerAccess(containerName);

    try (Writer writer = response.getWriter()) {
        XMLStreamWriter xml = xmlOutputFactory.createXMLStreamWriter(writer);
        xml.writeStartDocument();/*from   w ww.j av  a 2  s.c om*/
        xml.writeStartElement("AccessControlPolicy");
        xml.writeDefaultNamespace(AWS_XMLNS);

        writeOwnerStanza(xml);

        xml.writeStartElement("AccessControlList");

        xml.writeStartElement("Grant");

        xml.writeStartElement("Grantee");
        xml.writeNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        xml.writeAttribute("xsi:type", "CanonicalUser");

        writeSimpleElement(xml, "ID", FAKE_OWNER_ID);
        writeSimpleElement(xml, "DisplayName", FAKE_OWNER_DISPLAY_NAME);

        xml.writeEndElement();

        writeSimpleElement(xml, "Permission", "FULL_CONTROL");

        xml.writeEndElement();

        if (access == ContainerAccess.PUBLIC_READ) {
            xml.writeStartElement("Grant");

            xml.writeStartElement("Grantee");
            xml.writeNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
            xml.writeAttribute("xsi:type", "Group");

            writeSimpleElement(xml, "URI", "http://acs.amazonaws.com/groups/global/AllUsers");

            xml.writeEndElement();

            writeSimpleElement(xml, "Permission", "READ");

            xml.writeEndElement();
        }

        xml.writeEndElement();

        xml.writeEndElement();
        xml.flush();
    } catch (XMLStreamException xse) {
        throw new IOException(xse);
    }
}

From source file:org.gaul.s3proxy.S3ProxyHandler.java

private void handleGetBlobAcl(HttpServletResponse response, BlobStore blobStore, String containerName,
        String blobName) throws IOException {
    BlobAccess access = blobStore.getBlobAccess(containerName, blobName);

    try (Writer writer = response.getWriter()) {
        XMLStreamWriter xml = xmlOutputFactory.createXMLStreamWriter(writer);
        xml.writeStartDocument();//  w w  w.j  av  a  2  s .  c o  m
        xml.writeStartElement("AccessControlPolicy");
        xml.writeDefaultNamespace(AWS_XMLNS);

        writeOwnerStanza(xml);

        xml.writeStartElement("AccessControlList");

        xml.writeStartElement("Grant");

        xml.writeStartElement("Grantee");
        xml.writeNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        xml.writeAttribute("xsi:type", "CanonicalUser");

        writeSimpleElement(xml, "ID", FAKE_OWNER_ID);
        writeSimpleElement(xml, "DisplayName", FAKE_OWNER_DISPLAY_NAME);

        xml.writeEndElement();

        writeSimpleElement(xml, "Permission", "FULL_CONTROL");

        xml.writeEndElement();

        if (access == BlobAccess.PUBLIC_READ) {
            xml.writeStartElement("Grant");

            xml.writeStartElement("Grantee");
            xml.writeNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
            xml.writeAttribute("xsi:type", "Group");

            writeSimpleElement(xml, "URI", "http://acs.amazonaws.com/groups/global/AllUsers");

            xml.writeEndElement();

            writeSimpleElement(xml, "Permission", "READ");

            xml.writeEndElement();
        }

        xml.writeEndElement();

        xml.writeEndElement();
        xml.flush();
    } catch (XMLStreamException xse) {
        throw new IOException(xse);
    }
}