Search in sources :

Example 6 with IBindingFactory

use of org.jibx.runtime.IBindingFactory in project RESTdoclet by IG-Group.

the class JiBXUtils method unmarshallService.

/**
    * Static method for unmarshalling a service.
    * 
    * @param input the input-stream the service has to be unmarshalled from.
    * @return the unmarshalled service.
    * @throws JiBXException if JiBX fails.
    */
public static Service unmarshallService(final InputStream input) throws JiBXException {
    final IBindingFactory factory = BindingDirectory.getFactory(Service.class);
    final IUnmarshallingContext context = factory.createUnmarshallingContext();
    return (Service) context.unmarshalDocument(input, ENCODING);
}
Also used : IUnmarshallingContext(org.jibx.runtime.IUnmarshallingContext) IBindingFactory(org.jibx.runtime.IBindingFactory) Service(com.iggroup.oss.restdoclet.doclet.type.Service)

Example 7 with IBindingFactory

use of org.jibx.runtime.IBindingFactory in project kernel by exoplatform.

the class ConfigurationUnmarshaller method unmarshall.

public Configuration unmarshall(final URL url) throws Exception {
    if (PropertyManager.isDevelopping()) {
        boolean valid = isValid(url);
        if (!valid) {
            LOG.info("The configuration file " + url + " was not found valid according to its XSD");
        }
    }
    // 
    DocumentBuilderFactory factory = null;
    try {
        // With Java 6, it's safer to precise the builder factory class name as it may result:
        // java.lang.AbstractMethodError: org.apache.xerces.dom.DeferredDocumentImpl.getXmlStandalone()Z
        // at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.setDocumentInfo(Unknown Source)
        Method dbfniMethod = DocumentBuilderFactory.class.getMethod("newInstance", String.class, ClassLoader.class);
        factory = (DocumentBuilderFactory) dbfniMethod.invoke(null, "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl", Thread.currentThread().getContextClassLoader());
    } catch (InvocationTargetException e) {
        Throwable cause = e.getCause();
        if (cause instanceof FactoryConfigurationError) {
            // do nothing and let try to instantiate later
            LOG.debug("Was not able to find document builder factory class in Java > 5, will use default", cause);
        } else {
            // Rethrow
            throw e;
        }
    } catch (NoSuchMethodException e) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("An exception occurred: " + e.getMessage());
        }
    }
    // 
    if (factory == null) {
        factory = DocumentBuilderFactory.newInstance();
    }
    // 
    factory.setNamespaceAware(true);
    final DocumentBuilderFactory builderFactory = factory;
    try {
        return SecurityHelper.doPrivilegedExceptionAction(new PrivilegedExceptionAction<Configuration>() {

            public Configuration run() throws Exception {
                DocumentBuilder builder = builderFactory.newDocumentBuilder();
                Document doc = builder.parse(url.openStream());
                // Filter DOM
                ProfileDOMFilter filter = new ProfileDOMFilter(profiles);
                filter.process(doc.getDocumentElement());
                // SAX event stream -> String
                StringWriter buffer = new StringWriter();
                SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
                TransformerHandler hd = tf.newTransformerHandler();
                StreamResult result = new StreamResult(buffer);
                hd.setResult(result);
                Transformer serializer = tf.newTransformer();
                serializer.setOutputProperty(OutputKeys.ENCODING, "UTF8");
                serializer.setOutputProperty(OutputKeys.INDENT, "yes");
                // Transform -> SAX event stream
                SAXResult saxResult = new SAXResult(new NoKernelNamespaceSAXFilter(hd));
                // DOM -> Transform
                serializer.transform(new DOMSource(doc), saxResult);
                // Reuse the parsed document
                String document = buffer.toString();
                // Debug
                if (LOG.isTraceEnabled())
                    LOG.trace("About to parse configuration file " + document);
                // 
                IBindingFactory bfact = BindingDirectory.getFactory(Configuration.class);
                IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
                return (Configuration) uctx.unmarshalDocument(new StringReader(document), null);
            }
        });
    } catch (PrivilegedActionException pae) {
        Throwable cause = pae.getCause();
        if (cause instanceof JiBXException) {
            throw (JiBXException) cause;
        } else if (cause instanceof ParserConfigurationException) {
            throw (ParserConfigurationException) cause;
        } else if (cause instanceof IOException) {
            throw (IOException) cause;
        } else if (cause instanceof SAXException) {
            throw (SAXException) cause;
        } else if (cause instanceof IllegalArgumentException) {
            throw (IllegalArgumentException) cause;
        } else if (cause instanceof TransformerException) {
            throw (TransformerException) cause;
        } else if (cause instanceof TransformerConfigurationException) {
            throw (TransformerConfigurationException) cause;
        } else if (cause instanceof TransformerFactoryConfigurationError) {
            throw (TransformerFactoryConfigurationError) cause;
        } else if (cause instanceof RuntimeException) {
            throw (RuntimeException) cause;
        } else {
            throw new RuntimeException(cause);
        }
    }
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Transformer(javax.xml.transform.Transformer) Configuration(org.exoplatform.container.xml.Configuration) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IBindingFactory(org.jibx.runtime.IBindingFactory) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) FactoryConfigurationError(javax.xml.parsers.FactoryConfigurationError) TransformerException(javax.xml.transform.TransformerException) TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) StreamResult(javax.xml.transform.stream.StreamResult) PrivilegedActionException(java.security.PrivilegedActionException) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) JiBXException(org.jibx.runtime.JiBXException) Method(java.lang.reflect.Method) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) TransformerException(javax.xml.transform.TransformerException) JiBXException(org.jibx.runtime.JiBXException) PrivilegedActionException(java.security.PrivilegedActionException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) IUnmarshallingContext(org.jibx.runtime.IUnmarshallingContext) SAXResult(javax.xml.transform.sax.SAXResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder)

Example 8 with IBindingFactory

use of org.jibx.runtime.IBindingFactory in project kernel by exoplatform.

the class XMLCollection method toByteArray.

public byte[] toByteArray(String encoding) throws Exception {
    IBindingFactory bfact = XMLObject.getBindingFactoryInPriviledgedMode(XMLObject.class);
    IMarshallingContext mctx = bfact.createMarshallingContext();
    mctx.setIndent(2);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    mctx.marshalDocument(this, encoding, null, os);
    return os.toByteArray();
}
Also used : IMarshallingContext(org.jibx.runtime.IMarshallingContext) IBindingFactory(org.jibx.runtime.IBindingFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 9 with IBindingFactory

use of org.jibx.runtime.IBindingFactory in project kernel by exoplatform.

the class XMLObject method getXMLObject.

public static XMLObject getXMLObject(InputStream is) throws Exception {
    IBindingFactory bfact = getBindingFactoryInPriviledgedMode(XMLObject.class);
    IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
    XMLObject xmlobject = (XMLObject) uctx.unmarshalDocument(is, "UTF-8");
    return xmlobject;
}
Also used : IUnmarshallingContext(org.jibx.runtime.IUnmarshallingContext) IBindingFactory(org.jibx.runtime.IBindingFactory)

Example 10 with IBindingFactory

use of org.jibx.runtime.IBindingFactory in project kernel by exoplatform.

the class XMLObject method toByteArray.

public byte[] toByteArray(String encoding) throws Exception {
    IBindingFactory bfact = getBindingFactoryInPriviledgedMode(XMLObject.class);
    IMarshallingContext mctx = bfact.createMarshallingContext();
    mctx.setIndent(2);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    mctx.marshalDocument(this, encoding, null, os);
    return os.toByteArray();
}
Also used : IMarshallingContext(org.jibx.runtime.IMarshallingContext) IBindingFactory(org.jibx.runtime.IBindingFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

IBindingFactory (org.jibx.runtime.IBindingFactory)18 IUnmarshallingContext (org.jibx.runtime.IUnmarshallingContext)10 IMarshallingContext (org.jibx.runtime.IMarshallingContext)9 FileWriter (java.io.FileWriter)3 Writer (java.io.Writer)3 URL (java.net.URL)3 Configuration (org.exoplatform.container.xml.Configuration)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Component (org.exoplatform.container.xml.Component)2 ObjectParameter (org.exoplatform.container.xml.ObjectParameter)2 Controller (com.iggroup.oss.restdoclet.doclet.type.Controller)1 Service (com.iggroup.oss.restdoclet.doclet.type.Service)1 Services (com.iggroup.oss.restdoclet.doclet.type.Services)1 FileInputStream (java.io.FileInputStream)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1