Search in sources :

Example 6 with XMLWriter

use of com.sun.xml.bind.marshaller.XMLWriter in project OpenAM by OpenRock.

the class MarshallerImpl method createWriter.

public XMLWriter createWriter(Writer w, String encoding) throws JAXBException {
    // buffering improves the performance
    w = new BufferedWriter(w);
    CharacterEscapeHandler ceh = createEscapeHandler(encoding);
    XMLWriter xw;
    if (isFormattedOutput()) {
        DataWriter d = new DataWriter(w, encoding, ceh);
        d.setIndentStep(indent);
        xw = d;
    } else
        xw = new XMLWriter(w, encoding, ceh);
    xw.setXmlDecl(printXmlDeclaration);
    xw.setHeader(header);
    return xw;
}
Also used : CharacterEscapeHandler(com.sun.xml.bind.marshaller.CharacterEscapeHandler) XMLWriter(com.sun.xml.bind.marshaller.XMLWriter) BufferedWriter(java.io.BufferedWriter) DataWriter(com.sun.xml.bind.marshaller.DataWriter)

Example 7 with XMLWriter

use of com.sun.xml.bind.marshaller.XMLWriter in project OpenAM by OpenRock.

the class MarshallerImpl method marshal.

public void marshal(Object obj, Result result) throws JAXBException {
    //XMLSerializable so = Util.toXMLSerializable(obj);
    XMLSerializable so = context.getGrammarInfo().castToXMLSerializable(obj);
    if (so == null)
        throw new MarshalException(Messages.format(Messages.NOT_MARSHALLABLE));
    if (result instanceof SAXResult) {
        write(so, ((SAXResult) result).getHandler());
        return;
    }
    if (result instanceof DOMResult) {
        Node node = ((DOMResult) result).getNode();
        if (node == null) {
            try {
                DocumentBuilder db = XMLUtils.getSafeDocumentBuilder(false);
                Document doc = db.newDocument();
                ((DOMResult) result).setNode(doc);
                write(so, new SAX2DOMEx(doc));
            } catch (ParserConfigurationException pce) {
                throw new JAXBAssertionError(pce);
            }
        } else {
            write(so, new SAX2DOMEx(node));
        }
        return;
    }
    if (result instanceof StreamResult) {
        StreamResult sr = (StreamResult) result;
        XMLWriter w = null;
        if (sr.getWriter() != null)
            w = createWriter(sr.getWriter());
        else if (sr.getOutputStream() != null)
            w = createWriter(sr.getOutputStream());
        else if (sr.getSystemId() != null) {
            String fileURL = sr.getSystemId();
            if (fileURL.startsWith("file:///")) {
                if (fileURL.substring(8).indexOf(":") > 0)
                    fileURL = fileURL.substring(8);
                else
                    fileURL = fileURL.substring(7);
            }
            try {
                w = createWriter(new FileOutputStream(fileURL));
            } catch (IOException e) {
                throw new MarshalException(e);
            }
        }
        if (w == null)
            throw new IllegalArgumentException();
        write(so, w);
        return;
    }
    // unsupported parameter type
    throw new MarshalException(Messages.format(Messages.UNSUPPORTED_RESULT));
}
Also used : MarshalException(javax.xml.bind.MarshalException) DOMResult(javax.xml.transform.dom.DOMResult) StreamResult(javax.xml.transform.stream.StreamResult) Node(org.w3c.dom.Node) IOException(java.io.IOException) Document(org.w3c.dom.Document) XMLWriter(com.sun.xml.bind.marshaller.XMLWriter) SAXResult(javax.xml.transform.sax.SAXResult) JAXBAssertionError(com.sun.xml.bind.JAXBAssertionError) DocumentBuilder(javax.xml.parsers.DocumentBuilder) FileOutputStream(java.io.FileOutputStream) SAX2DOMEx(com.sun.xml.bind.marshaller.SAX2DOMEx) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 8 with XMLWriter

use of com.sun.xml.bind.marshaller.XMLWriter in project OpenAM by OpenRock.

the class MarshallerImpl method createWriter.

public XMLWriter createWriter(Writer w, String encoding) throws JAXBException {
    // buffering improves the performance
    w = new BufferedWriter(w);
    CharacterEscapeHandler ceh = createEscapeHandler(encoding);
    XMLWriter xw;
    if (isFormattedOutput()) {
        DataWriter d = new DataWriter(w, encoding, ceh);
        d.setIndentStep(indent);
        xw = d;
    } else
        xw = new XMLWriter(w, encoding, ceh);
    xw.setXmlDecl(printXmlDeclaration);
    xw.setHeader(header);
    return xw;
}
Also used : CharacterEscapeHandler(com.sun.xml.bind.marshaller.CharacterEscapeHandler) XMLWriter(com.sun.xml.bind.marshaller.XMLWriter) BufferedWriter(java.io.BufferedWriter) DataWriter(com.sun.xml.bind.marshaller.DataWriter)

Aggregations

XMLWriter (com.sun.xml.bind.marshaller.XMLWriter)8 JAXBAssertionError (com.sun.xml.bind.JAXBAssertionError)4 CharacterEscapeHandler (com.sun.xml.bind.marshaller.CharacterEscapeHandler)4 DataWriter (com.sun.xml.bind.marshaller.DataWriter)4 SAX2DOMEx (com.sun.xml.bind.marshaller.SAX2DOMEx)4 BufferedWriter (java.io.BufferedWriter)4 FileOutputStream (java.io.FileOutputStream)4 IOException (java.io.IOException)4 MarshalException (javax.xml.bind.MarshalException)4 DocumentBuilder (javax.xml.parsers.DocumentBuilder)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 DOMResult (javax.xml.transform.dom.DOMResult)4 SAXResult (javax.xml.transform.sax.SAXResult)4 StreamResult (javax.xml.transform.stream.StreamResult)4 Document (org.w3c.dom.Document)4 Node (org.w3c.dom.Node)4