Example usage for javax.xml.stream XMLStreamWriter close

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

Introduction

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

Prototype

public void close() throws XMLStreamException;

Source Link

Document

Close this writer and free any resources associated with the writer.

Usage

From source file:org.ops4j.pax.exam.karaf.container.internal.DependenciesDeployer.java

private static void close(XMLStreamWriter sw) {
    if (sw != null) {
        try {/*  ww  w.  jav  a2  s .c o  m*/
            sw.close();
        } catch (XMLStreamException e) {
        }
    }
}

From source file:org.osaf.cosmo.dav.impl.StandardDavResponse.java

public void sendDavError(DavException e) throws IOException {
    setStatus(e.getErrorCode());/*from   ww w.j  a  v a 2s.  c o m*/
    if (!e.hasContent())
        return;

    XMLStreamWriter writer = null;

    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(out);
        writer.writeStartDocument();
        e.writeTo(writer);
        writer.writeEndDocument();

        setContentType("text/xml; charset=UTF-8");
        byte[] bytes = out.toByteArray();
        setContentLength(bytes.length);
        getOutputStream().write(bytes);
    } catch (Throwable e2) {
        log.error("Error writing XML", e2);
        log.error("Original exception", e);
        setStatus(500);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (XMLStreamException e2) {
                log.warn("Unable to close XML writer", e2);
            }
        }
    }
}

From source file:org.osaf.cosmo.mc.CollectionService.java

public void writeTo(OutputStream out) throws IOException, XMLStreamException {
    XMLStreamWriter writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(out);
    writer.setPrefix(PRE_XML, NS_XML);/*w  w  w.j  av a  2s. co  m*/
    writer.setDefaultNamespace(NS_MC);

    try {
        writer.writeStartDocument();
        writer.writeStartElement(EL_MC_SERVICE);
        writer.writeDefaultNamespace(NS_MC);
        writer.writeAttribute(NS_XML, EL_XML_BASE, locator.getMorseCodeBase());

        for (CollectionItem collection : collections) {
            writer.writeStartElement(EL_MC_COLLECTION);
            writer.writeAttribute(ATTR_MC_UUID, collection.getUid());
            writer.writeAttribute(ATTR_MC_HREF, href(collection));

            writer.writeStartElement(EL_MC_NAME);
            writer.writeCharacters(collection.getDisplayName());
            writer.writeEndElement();

            for (Ticket ticket : visibleTickets(collection)) {
                writer.writeStartElement(EL_MC_TICKET);
                writer.writeAttribute(ATTR_MC_TYPE, ticket.getType().toString());
                writer.writeCharacters(ticket.getKey());
                writer.writeEndElement();
            }

            writer.writeEndElement();
        }

        writer.writeEndElement();
        writer.writeEndDocument();
    } finally {
        writer.close();
    }
}

From source file:org.osaf.cosmo.mc.MorseCodeServlet.java

private void handleGeneralException(MorseCodeException e, HttpServletResponse resp) throws IOException {
    if (e.getCode() >= 500)
        log.error("Unknown Morse Code exception", e);
    else if (e.getCode() >= 400)
        log.info("Client error (" + e.getCode() + "): " + e.getMessage());

    resp.setStatus(e.getCode());/*  www.ja v  a  2s .co  m*/
    if (!e.hasContent())
        return;

    XMLStreamWriter writer = null;

    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(out);
        writer.writeStartDocument();
        e.writeTo(writer);
        writer.writeEndDocument();

        resp.setContentType("application/xml");
        byte[] bytes = out.toByteArray();
        resp.setContentLength(bytes.length);
        resp.getOutputStream().write(bytes);
    } catch (Throwable e2) {
        log.error("Error writing XML", e2);
        log.error("Original exception", e);
        resp.setStatus(500);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (XMLStreamException e2) {
                log.warn("Unable to close XML writer", e2);
            }
        }
    }
}

From source file:org.osaf.cosmo.model.text.XhtmlCollectionFormat.java

public String format(CollectionItem collection) {
    try {/*from w  ww. ja v  a  2 s .  c o  m*/
        StringWriter sw = new StringWriter();
        XMLStreamWriter writer = createXmlWriter(sw);

        writer.writeStartElement("div");
        writer.writeAttribute("class", "collection");

        writer.writeCharacters("Collection: ");

        if (collection.getDisplayName() != null) {
            writer.writeStartElement("span");
            writer.writeAttribute("class", "name");
            writer.writeCharacters(collection.getDisplayName());
            writer.writeEndElement();
        }

        if (collection.getUid() != null) {
            writer.writeCharacters(" (uuid ");
            writer.writeStartElement("span");
            writer.writeAttribute("class", "uuid");
            writer.writeCharacters(collection.getUid());
            writer.writeEndElement();
            writer.writeCharacters(")");
        }

        writer.writeEndElement();
        writer.close();

        return sw.toString();
    } catch (XMLStreamException e) {
        throw new RuntimeException("Error formatting XML", e);
    }
}

From source file:org.osaf.cosmo.model.text.XhtmlPreferenceFormat.java

public String format(Preference pref) {
    try {/* w w w  .  j a  v a  2s. c o  m*/
        StringWriter sw = new StringWriter();
        XMLStreamWriter writer = createXmlWriter(sw);

        writer.writeStartElement("div");
        writer.writeAttribute("class", "preference");

        writer.writeCharacters("Preference: ");

        if (pref.getKey() != null) {
            writer.writeStartElement("span");
            writer.writeAttribute("class", "key");
            writer.writeCharacters(pref.getKey());
            writer.writeEndElement();
        }

        writer.writeCharacters(" = ");

        if (pref.getValue() != null) {
            writer.writeStartElement("span");
            writer.writeAttribute("class", "value");
            writer.writeCharacters(pref.getValue());
            writer.writeEndElement();
        }

        writer.writeEndElement();
        writer.close();

        return sw.toString();
    } catch (XMLStreamException e) {
        throw new RuntimeException("Error formatting XML", e);
    }
}

From source file:org.osaf.cosmo.model.text.XhtmlSubscriptionFormat.java

private String format(CollectionSubscription sub, boolean isCollectionProvided, CollectionItem collection,
        boolean isTicketProvided, Ticket ticket) {
    try {//from   w  w  w.j a v  a2s.c o m
        StringWriter sw = new StringWriter();
        XMLStreamWriter writer = createXmlWriter(sw);

        writer.writeStartElement("div");
        writer.writeAttribute("class", "local-subscription");

        if (sub.getDisplayName() != null) {
            writer.writeCharacters("Subscription: ");
            writer.writeStartElement("span");
            writer.writeAttribute("class", "name");
            writer.writeCharacters(sub.getDisplayName());
            writer.writeEndElement();
        }

        if (sub.getCollectionUid() != null) {
            writer.writeStartElement("div");
            writer.writeAttribute("class", "collection");
            writer.writeCharacters("Collection: ");
            writer.writeStartElement("span");
            writer.writeAttribute("class", "uuid");
            writer.writeCharacters(sub.getCollectionUid());
            writer.writeEndElement();
            if (isCollectionProvided) {
                writer.writeCharacters(" Exists? ");
                writer.writeStartElement("span");
                writer.writeAttribute("class", "exists");
                writer.writeCharacters(Boolean.valueOf(collection != null).toString());
                writer.writeEndElement();
            }
            writer.writeEndElement();
        }

        if (sub.getTicketKey() != null) {
            writer.writeStartElement("div");
            writer.writeAttribute("class", "ticket");
            writer.writeCharacters("Ticket: ");
            writer.writeStartElement("span");
            writer.writeAttribute("class", "key");
            writer.writeCharacters(sub.getTicketKey());
            writer.writeEndElement();
            if (isTicketProvided) {
                writer.writeCharacters(" Exists? ");
                writer.writeStartElement("span");
                writer.writeAttribute("class", "exists");
                writer.writeCharacters(Boolean.valueOf(ticket != null).toString());
                writer.writeEndElement();
            }
            writer.writeEndElement();
        }

        writer.writeEndElement();
        writer.close();

        return sw.toString();
    } catch (XMLStreamException e) {
        throw new RuntimeException("Error formatting XML", e);
    }
}

From source file:org.osaf.cosmo.model.text.XhtmlTicketFormat.java

public String format(Ticket ticket) {
    try {/*from   w  w w  . j  a  va  2  s . co  m*/
        StringWriter sw = new StringWriter();
        XMLStreamWriter writer = createXmlWriter(sw);

        writer.writeStartElement("div");
        writer.writeAttribute("class", "ticket");

        if (ticket.getKey() != null) {
            writer.writeCharacters("Key: ");
            writer.writeStartElement("span");
            writer.writeAttribute("class", "key");
            writer.writeCharacters(ticket.getKey());
            writer.writeEndElement();
        }

        if (ticket.getType() != null) {
            writer.writeCharacters("Type: ");
            writer.writeStartElement("span");
            writer.writeAttribute("class", "type");
            writer.writeAttribute("title", ticket.getType().toString());
            writer.writeCharacters(ticket.getType().toString());
            writer.writeEndElement();
        }

        if (ticket.getTimeout() != null) {
            writer.writeCharacters("Timeout: ");
            writer.writeStartElement("span");
            writer.writeAttribute("class", "timeout");
            writer.writeAttribute("title", ticket.getTimeout().toString());
            writer.writeCharacters(ticket.getTimeout().toString());
            writer.writeEndElement();
        }

        writer.writeEndElement();
        writer.close();

        return sw.toString();
    } catch (XMLStreamException e) {
        throw new RuntimeException("Error formatting XML", e);
    }
}

From source file:org.osaf.cosmo.xml.DomWriter.java

public static String write(Node n) throws XMLStreamException, IOException {
    XMLStreamWriter writer = null;
    try {//from ww  w  .  j a  va  2  s  .c om
        StringWriter out = new StringWriter();
        writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(out);
        writeNode(n, writer);
        writer.close();
        return out.toString();
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (XMLStreamException e2) {
                log.warn("Unable to close XML writer", e2);
            }
        }
    }
}

From source file:org.owasp.csrfguard.util.CsrfGuardUtils.java

/**
 * close a writer quietly//from w w  w .  java2  s. c o  m
 * @param writer
 */
public static void closeQuietly(XMLStreamWriter writer) {
    if (writer != null) {
        try {
            writer.close();
        } catch (XMLStreamException e) {
            //swallow, its ok
        }
    }
}