Search in sources :

Example 6 with JAXBAssertionError

use of com.sun.xml.bind.JAXBAssertionError in project Payara by payara.

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 {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                dbf.setNamespaceAware(true);
                DocumentBuilder db = dbf.newDocumentBuilder();
                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) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) 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 7 with JAXBAssertionError

use of com.sun.xml.bind.JAXBAssertionError 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 JAXBAssertionError

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

the class MSVValidator method childAsElementBody.

private void childAsElementBody(Object o, ValidatableObject vo) throws SAXException {
    String intfName = vo.getPrimaryInterface().getName();
    intfName = intfName.replace('$', '.');
    // if the object implements the RIElement interface,
    // add a marker attribute to the dummy element.
    //
    // For example, if the object is org.acme.impl.FooImpl,
    // the dummy element will look like
    // <{DUMMY_ELEMENT_NS}org.acme.Foo
    //          {<URI of this element>}:<local name of this element>="" />
    // 
    // This extra attribute is used to validate wildcards.
    //        AttributesImpl atts;
    //        if(o instanceof RIElement) {
    //            RIElement rie = (RIElement)o;
    //            atts = new AttributesImpl();
    //            atts.addAttribute(
    //                rie.____jaxb_ri____getNamespaceURI(),
    //                rie.____jaxb_ri____getLocalName(),
    //                rie.____jaxb_ri____getLocalName(),  // use local name as qname
    //                "CDATA",
    //                "");    // we don't care about the attribute value
    //        } else
    //            atts = emptyAttributes;
    // feed a dummy element to the acceptor.
    StartTagInfo sti = new StartTagInfo(DUMMY_ELEMENT_NS, intfName, intfName, /*just pass the local name as QName.*/
    emptyAttributes, this);
    Acceptor child = acceptor.createChildAcceptor(sti, null);
    if (child == null) {
        // some required elements were missing. report errors
        StringRef ref = new StringRef();
        child = acceptor.createChildAcceptor(sti, ref);
        context.reportEvent(target, ref.str);
    }
    if (o instanceof RIElement) {
        RIElement rie = (RIElement) o;
        if (!child.onAttribute2(rie.____jaxb_ri____getNamespaceURI(), rie.____jaxb_ri____getLocalName(), rie.____jaxb_ri____getLocalName(), "", null, null, null))
            // this object is not a valid member of the wildcard
            context.reportEvent(target, Messages.format(Messages.INCORRECT_CHILD_FOR_WILDCARD, rie.____jaxb_ri____getNamespaceURI(), rie.____jaxb_ri____getLocalName()));
    }
    child.onEndAttributes(sti, null);
    if (!acceptor.stepForward(child, null)) {
        // generated by XJC.
        throw new JAXBAssertionError();
    }
    // we need a separate validator instance to validate a child object
    context.validate(vo);
}
Also used : Acceptor(com.sun.msv.verifier.Acceptor) JAXBAssertionError(com.sun.xml.bind.JAXBAssertionError) RIElement(com.sun.xml.bind.RIElement) StartTagInfo(com.sun.msv.util.StartTagInfo) StringRef(com.sun.msv.util.StringRef)

Example 9 with JAXBAssertionError

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

the class MSVValidator method childAsElementBody.

private void childAsElementBody(Object o, ValidatableObject vo) throws SAXException {
    String intfName = vo.getPrimaryInterface().getName();
    intfName = intfName.replace('$', '.');
    // if the object implements the RIElement interface,
    // add a marker attribute to the dummy element.
    //
    // For example, if the object is org.acme.impl.FooImpl,
    // the dummy element will look like
    // <{DUMMY_ELEMENT_NS}org.acme.Foo
    //          {<URI of this element>}:<local name of this element>="" />
    // 
    // This extra attribute is used to validate wildcards.
    //        AttributesImpl atts;
    //        if(o instanceof RIElement) {
    //            RIElement rie = (RIElement)o;
    //            atts = new AttributesImpl();
    //            atts.addAttribute(
    //                rie.____jaxb_ri____getNamespaceURI(),
    //                rie.____jaxb_ri____getLocalName(),
    //                rie.____jaxb_ri____getLocalName(),  // use local name as qname
    //                "CDATA",
    //                "");    // we don't care about the attribute value
    //        } else
    //            atts = emptyAttributes;
    // feed a dummy element to the acceptor.
    StartTagInfo sti = new StartTagInfo(DUMMY_ELEMENT_NS, intfName, intfName, /*just pass the local name as QName.*/
    emptyAttributes, this);
    Acceptor child = acceptor.createChildAcceptor(sti, null);
    if (child == null) {
        // some required elements were missing. report errors
        StringRef ref = new StringRef();
        child = acceptor.createChildAcceptor(sti, ref);
        context.reportEvent(target, ref.str);
    }
    if (o instanceof RIElement) {
        RIElement rie = (RIElement) o;
        if (!child.onAttribute2(rie.____jaxb_ri____getNamespaceURI(), rie.____jaxb_ri____getLocalName(), rie.____jaxb_ri____getLocalName(), "", null, null, null))
            // this object is not a valid member of the wildcard
            context.reportEvent(target, Messages.format(Messages.INCORRECT_CHILD_FOR_WILDCARD, rie.____jaxb_ri____getNamespaceURI(), rie.____jaxb_ri____getLocalName()));
    }
    child.onEndAttributes(sti, null);
    if (!acceptor.stepForward(child, null)) {
        // generated by XJC.
        throw new JAXBAssertionError();
    }
    // we need a separate validator instance to validate a child object
    context.validate(vo);
}
Also used : Acceptor(com.sun.msv.verifier.Acceptor) JAXBAssertionError(com.sun.xml.bind.JAXBAssertionError) RIElement(com.sun.xml.bind.RIElement) StartTagInfo(com.sun.msv.util.StartTagInfo) StringRef(com.sun.msv.util.StringRef)

Example 10 with JAXBAssertionError

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

the class SAXUnmarshallerHandlerImpl method handleEvent.

public void handleEvent(ValidationEvent event, boolean canRecover) throws SAXException {
    ValidationEventHandler eventHandler;
    try {
        eventHandler = parent.getEventHandler();
    } catch (JAXBException e) {
        // impossible.
        throw new JAXBAssertionError();
    }
    boolean recover = eventHandler.handleEvent(event);
    // from the unmarshaller.getResult()
    if (!recover)
        aborted = true;
    if (!canRecover || !recover)
        throw new SAXException(new UnmarshalException(event.getMessage(), event.getLinkedException()));
}
Also used : ValidationEventHandler(javax.xml.bind.ValidationEventHandler) JAXBAssertionError(com.sun.xml.bind.JAXBAssertionError) UnmarshalException(javax.xml.bind.UnmarshalException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException)

Aggregations

JAXBAssertionError (com.sun.xml.bind.JAXBAssertionError)15 StartTagInfo (com.sun.msv.util.StartTagInfo)5 StringRef (com.sun.msv.util.StringRef)5 Acceptor (com.sun.msv.verifier.Acceptor)5 RIElement (com.sun.xml.bind.RIElement)5 SAX2DOMEx (com.sun.xml.bind.marshaller.SAX2DOMEx)5 XMLWriter (com.sun.xml.bind.marshaller.XMLWriter)5 FileOutputStream (java.io.FileOutputStream)5 IOException (java.io.IOException)5 JAXBException (javax.xml.bind.JAXBException)5 MarshalException (javax.xml.bind.MarshalException)5 UnmarshalException (javax.xml.bind.UnmarshalException)5 ValidationEventHandler (javax.xml.bind.ValidationEventHandler)5 DocumentBuilder (javax.xml.parsers.DocumentBuilder)5 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)5 DOMResult (javax.xml.transform.dom.DOMResult)5 SAXResult (javax.xml.transform.sax.SAXResult)5 StreamResult (javax.xml.transform.stream.StreamResult)5 Document (org.w3c.dom.Document)5 Node (org.w3c.dom.Node)5