Search in sources :

Example 31 with Marshaller

use of javax.xml.bind.Marshaller in project orientdb by orientechnologies.

the class OServerConfigurationLoaderXml method load.

public OServerConfiguration load() throws IOException {
    try {
        if (file != null) {
            fileLastModified = file.lastModified();
            String path = OFileUtils.getPath(file.getAbsolutePath());
            String current = OFileUtils.getPath(new File("").getAbsolutePath());
            if (path.startsWith(current))
                path = path.substring(current.length() + 1);
            OLogManager.instance().info(this, "Loading configuration from: %s...", path);
        } else {
            OLogManager.instance().info(this, "Loading configuration from input stream");
        }
        context = JAXBContext.newInstance(rootClass);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        unmarshaller.setSchema(null);
        final OServerConfiguration obj;
        if (file != null) {
            if (file.exists())
                obj = rootClass.cast(unmarshaller.unmarshal(file));
            else {
                OLogManager.instance().error(this, "Server configuration file not found: %s", file);
                return rootClass.getConstructor(OServerConfigurationLoaderXml.class).newInstance(this);
            }
            obj.location = file.getAbsolutePath();
        } else {
            obj = rootClass.cast(unmarshaller.unmarshal(inputStream));
            obj.location = "memory";
        }
        // AUTO CONFIGURE SYSTEM CONFIGURATION
        OGlobalConfiguration config;
        if (obj.properties != null)
            for (OServerEntryConfiguration prop : obj.properties) {
                try {
                    config = OGlobalConfiguration.findByKey(prop.name);
                    if (config != null) {
                        config.setValue(prop.value);
                    }
                } catch (Exception e) {
                }
            }
        return obj;
    } catch (Exception e) {
        // SYNTAX ERROR? PRINT AN EXAMPLE
        OLogManager.instance().error(this, "Invalid syntax. Below an example of how it should be:", e);
        try {
            context = JAXBContext.newInstance(rootClass);
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            Object example = rootClass.getConstructor(OServerConfigurationLoaderXml.class).newInstance(this);
            marshaller.marshal(example, System.out);
        } catch (Exception ex) {
        }
        throw new IOException(e);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) OGlobalConfiguration(com.orientechnologies.orient.core.config.OGlobalConfiguration) IOException(java.io.IOException) Unmarshaller(javax.xml.bind.Unmarshaller) File(java.io.File) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException)

Example 32 with Marshaller

use of javax.xml.bind.Marshaller in project openhab1-addons by openhab.

the class LgTvChannelSet method savetofile.

/**
     * Save Channel List to File f
     * 
     * @param f
     */
public void savetofile(String f) {
    Writer writer = null;
    JAXBContext jc;
    try {
        jc = JAXBContext.newInstance(envelope.class);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), "utf-8"));
        marshaller.marshal(envel, writer);
    } catch (PropertyException e) {
        logger.error("error in savetofile", e);
    } catch (JAXBException e) {
        logger.error("error in savetofile", e);
    } catch (IOException ex) {
        logger.error("error in savetofile", ex);
    } finally {
        try {
            writer.close();
        } catch (Exception ex) {
        }
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) PropertyException(javax.xml.bind.PropertyException) FileOutputStream(java.io.FileOutputStream) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) PropertyException(javax.xml.bind.PropertyException) JAXBException(javax.xml.bind.JAXBException) BufferedWriter(java.io.BufferedWriter)

Example 33 with Marshaller

use of javax.xml.bind.Marshaller in project openhab1-addons by openhab.

the class LgTvEventChannelChanged method readevent.

public String readevent(String s) throws JAXBException {
    JAXBContext jc;
    jc = JAXBContext.newInstance(envelope.class);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    int start = s.indexOf("<envelope>");
    int stop = s.indexOf("</envelope>") + "</envelope>".length();
    String t = s.substring(start, stop);
    // System.out.println(t);
    StringReader reader = new StringReader(t);
    envel = null;
    envel = (envelope) unmarshaller.unmarshal(reader);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    StringWriter sw = new StringWriter();
    marshaller.marshal(envel, sw);
    return new String(sw.toString());
}
Also used : Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 34 with Marshaller

use of javax.xml.bind.Marshaller in project OpenAM by OpenRock.

the class XACMLPrivilegeUtils method writeXMLToStream.

public static void writeXMLToStream(PolicySet policySet, OutputStream outputStream) throws EntitlementException {
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(XACMLConstants.XACML3_CORE_PKG);
        JAXBElement<PolicySet> policySetElement = objectFactory.createPolicySet(policySet);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(policySetElement, outputStream);
    } catch (JAXBException je) {
        PrivilegeManager.debug.error("JAXBException while mapping privilege to policy:", je);
        throw new EntitlementException(EntitlementException.UNABLE_TO_SERIALIZE_OBJECT, je);
    }
}
Also used : EntitlementException(com.sun.identity.entitlement.EntitlementException) Marshaller(javax.xml.bind.Marshaller) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) PolicySet(com.sun.identity.entitlement.xacml3.core.PolicySet)

Example 35 with Marshaller

use of javax.xml.bind.Marshaller in project OpenAM by OpenRock.

the class CreateWSFedMetaDataTemplate method createExtendedMetaTemplate.

public static String createExtendedMetaTemplate(String entityId, Map mapParams) throws JAXBException {
    JAXBContext jc = WSFederationMetaUtils.getMetaJAXBContext();
    com.sun.identity.wsfederation.jaxb.entityconfig.ObjectFactory objFactory = new com.sun.identity.wsfederation.jaxb.entityconfig.ObjectFactory();
    FederationConfigElement fedConfig = objFactory.createFederationConfigElement();
    fedConfig.setFederationID(entityId);
    fedConfig.setHosted(true);
    String idpAlias = (String) mapParams.get(MetaTemplateParameters.P_IDP);
    if (idpAlias != null) {
        buildWSFedIDPConfigTemplate(objFactory, fedConfig, mapParams);
    }
    String spAlias = (String) mapParams.get(MetaTemplateParameters.P_SP);
    if (spAlias != null) {
        buildWSFedSPConfigTemplate(objFactory, fedConfig, mapParams);
    }
    Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    StringWriter pw = new StringWriter();
    m.marshal(fedConfig, pw);
    return pw.toString();
}
Also used : Marshaller(javax.xml.bind.Marshaller) JAXBContext(javax.xml.bind.JAXBContext) StringWriter(java.io.StringWriter) FederationConfigElement(com.sun.identity.wsfederation.jaxb.entityconfig.FederationConfigElement)

Aggregations

Marshaller (javax.xml.bind.Marshaller)280 JAXBContext (javax.xml.bind.JAXBContext)162 JAXBException (javax.xml.bind.JAXBException)100 StringWriter (java.io.StringWriter)82 Test (org.junit.Test)33 JAXBElement (javax.xml.bind.JAXBElement)32 ByteArrayOutputStream (java.io.ByteArrayOutputStream)31 File (java.io.File)29 Unmarshaller (javax.xml.bind.Unmarshaller)21 IOException (java.io.IOException)20 FileOutputStream (java.io.FileOutputStream)19 ByteArrayInputStream (java.io.ByteArrayInputStream)15 QName (javax.xml.namespace.QName)14 Element (org.w3c.dom.Element)14 HashMap (java.util.HashMap)12 Writer (java.io.Writer)11 Document (org.w3c.dom.Document)11 InputStream (java.io.InputStream)7 OutputStream (java.io.OutputStream)7 ArrayList (java.util.ArrayList)7