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);
}
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);
}
}
}
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();
}
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;
}
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();
}
Aggregations