Search in sources :

Example 1 with DomDriver

use of com.thoughtworks.xstream.io.xml.DomDriver in project GDSC-SMLM by aherbert.

the class CreateData method createXStream.

private XStream createXStream() {
    if (xs == null) {
        xs = new XStream(new DomDriver());
        xs.autodetectAnnotations(true);
        xs.alias("Compound", Compound.class);
        xs.alias("Atom", Atom.class);
    }
    return xs;
}
Also used : DomDriver(com.thoughtworks.xstream.io.xml.DomDriver) XStream(com.thoughtworks.xstream.XStream)

Example 2 with DomDriver

use of com.thoughtworks.xstream.io.xml.DomDriver in project jmeter by apache.

the class TemplateManager method initXStream.

private XStream initXStream() {
    XStream xstream = new XStream(new DomDriver() {

        /**
             * Create the DocumentBuilderFactory instance.
             * See https://blog.compass-security.com/2012/08/secure-xml-parser-configuration/
             * See https://github.com/x-stream/xstream/issues/25
             * @return the new instance
             */
        @Override
        protected DocumentBuilderFactory createDocumentBuilderFactory() {
            final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            try {
                factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
                factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
            } catch (ParserConfigurationException e) {
                throw new StreamException(e);
            }
            factory.setExpandEntityReferences(false);
            return factory;
        }
    });
    xstream.alias("template", Template.class);
    xstream.alias("templates", Templates.class);
    xstream.useAttributeFor(Template.class, "isTestPlan");
    // templates i
    xstream.addImplicitMap(Templates.class, // $NON-NLS-1$
    "templates", Template.class, // $NON-NLS-1$
    "name");
    return xstream;
}
Also used : DomDriver(com.thoughtworks.xstream.io.xml.DomDriver) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XStream(com.thoughtworks.xstream.XStream) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) StreamException(com.thoughtworks.xstream.io.StreamException)

Example 3 with DomDriver

use of com.thoughtworks.xstream.io.xml.DomDriver in project GDSC-SMLM by aherbert.

the class PCPALMAnalysis method loadResults.

/**
	 * Load all the results from a directory. File must have the XML suffix
	 * 
	 * @return DONE
	 */
private int loadResults() {
    if (getDirectory()) {
        File[] fileList = (new File(resultsDirectory)).listFiles(new FilenameFilter() {

            public boolean accept(File arg0, String arg1) {
                return arg1.endsWith("xml");
            }
        });
        if (fileList == null)
            return DONE;
        int count = 0;
        for (int i = 0; i < fileList.length; i++) {
            XStream xs = new XStream(new DomDriver());
            if (fileList[i].isFile())
                if (loadResult(xs, fileList[i].getPath()))
                    count++;
        }
        if (count > 0)
            Collections.sort(results);
        log("Loaded %d results", count);
    }
    return DONE;
}
Also used : FilenameFilter(java.io.FilenameFilter) DomDriver(com.thoughtworks.xstream.io.xml.DomDriver) XStream(com.thoughtworks.xstream.XStream) File(java.io.File)

Example 4 with DomDriver

use of com.thoughtworks.xstream.io.xml.DomDriver in project drools by kiegroup.

the class XmlBifParser method loadBif.

public static Bif loadBif(Resource resource, KnowledgeBuilderErrors errors) {
    InputStream is = null;
    try {
        is = resource.getInputStream();
    } catch (IOException e) {
        errors.add(new ParserError(resource, "Exception opening Stream:\n" + e.toString(), 0, 0));
        return null;
    }
    try {
        String encoding = resource instanceof InternalResource ? ((InternalResource) resource).getEncoding() : null;
        XStream xstream = encoding != null ? createTrustingXStream(new DomDriver(encoding)) : createTrustingXStream();
        initXStream(xstream);
        Bif bif = (Bif) xstream.fromXML(is);
        return bif;
    } catch (Exception e) {
        errors.add(new BayesNetworkAssemblerError(resource, "Unable to parse opening Stream:\n" + e.toString()));
        return null;
    }
}
Also used : InternalResource(org.drools.core.io.internal.InternalResource) DomDriver(com.thoughtworks.xstream.io.xml.DomDriver) ParserError(org.drools.compiler.compiler.ParserError) InputStream(java.io.InputStream) XStream(com.thoughtworks.xstream.XStream) XStreamUtils.createTrustingXStream(org.kie.soup.commons.xstream.XStreamUtils.createTrustingXStream) BayesNetworkAssemblerError(org.drools.beliefs.bayes.assembler.BayesNetworkAssemblerError) IOException(java.io.IOException) IOException(java.io.IOException)

Example 5 with DomDriver

use of com.thoughtworks.xstream.io.xml.DomDriver in project universa by UniversaBlockchain.

the class CLIMain method exportFields.

/**
 * Export fields from specified contract.
 *
 * @param contract   - contract to export.
 * @param fieldNames - list of field names to export.
 * @param fileName   - name of file to export to.
 * @param format     - format of file to export to. Can be xml or json.
 */
private static void exportFields(Contract contract, List<String> fieldNames, String fileName, String format, Boolean jsonPretty) throws IOException {
    format = format.toLowerCase();
    report("export format: " + format);
    if (fileName == null) {
        if (testMode && testRootPath != null) {
            fileName = testRootPath + "Universa_fields_" + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(contract.getCreatedAt());
        } else {
            fileName = "Universa_fields_" + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(contract.getCreatedAt());
        }
    }
    Binder hm = new Binder();
    try {
        for (String fieldName : fieldNames) {
            report("export field: " + fieldName + " -> " + contract.get(fieldName));
            hm.put(fieldName, contract.get(fieldName));
        }
        Binder binder = DefaultBiMapper.getInstance().newSerializer().serialize(hm);
        byte[] data;
        if ("xml".equals(format)) {
            XStream xstream = new XStream(new DomDriver());
            xstream.registerConverter(new MapEntryConverter());
            xstream.alias("fields", Binder.class);
            data = xstream.toXML(binder).getBytes();
        } else if ("yaml".equals(format) || "yml".equals(format)) {
            Yaml yaml = new Yaml();
            data = yaml.dumpAsMap(binder).getBytes();
        } else {
            Gson gson;
            if (jsonPretty) {
                gson = new GsonBuilder().setPrettyPrinting().create();
            } else {
                gson = new GsonBuilder().create();
            }
            String jsonString = gson.toJson(binder);
            data = jsonString.getBytes();
        }
        try (FileOutputStream fs = new FileOutputStream(fileName)) {
            fs.write(data);
            fs.close();
        }
        report("export fields as " + format + " ok");
    } catch (IllegalArgumentException e) {
        report("export fields error: " + e.getMessage());
    }
}
Also used : Binder(net.sergeych.tools.Binder) DomDriver(com.thoughtworks.xstream.io.xml.DomDriver) GsonBuilder(com.google.gson.GsonBuilder) XStream(com.thoughtworks.xstream.XStream) Gson(com.google.gson.Gson) Yaml(org.yaml.snakeyaml.Yaml)

Aggregations

XStream (com.thoughtworks.xstream.XStream)32 DomDriver (com.thoughtworks.xstream.io.xml.DomDriver)32 IOException (java.io.IOException)8 Gson (com.google.gson.Gson)5 File (java.io.File)5 InputStream (java.io.InputStream)5 ObjectInputStream (java.io.ObjectInputStream)5 GsonBuilder (com.google.gson.GsonBuilder)4 Binder (net.sergeych.tools.Binder)4 Yaml (org.yaml.snakeyaml.Yaml)4 HttpClient (org.apache.commons.httpclient.HttpClient)3 HttpException (org.apache.commons.httpclient.HttpException)3 HttpMethod (org.apache.commons.httpclient.HttpMethod)3 GetMethod (org.apache.commons.httpclient.methods.GetMethod)3 XStreamException (com.thoughtworks.xstream.XStreamException)2 FileInputStream (java.io.FileInputStream)2 ArrayList (java.util.ArrayList)2 BackingStoreException (java.util.prefs.BackingStoreException)2 OptionException (joptsimple.OptionException)2 BiDeserializer (net.sergeych.biserializer.BiDeserializer)2