Example usage for java.beans XMLDecoder readObject

List of usage examples for java.beans XMLDecoder readObject

Introduction

In this page you can find the example usage for java.beans XMLDecoder readObject.

Prototype

public Object readObject() 

Source Link

Document

Reads the next object from the underlying input stream.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
        XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(new FileInputStream("Bean.xml")));
        Bean bean = (Bean) decoder.readObject();
        decoder.close();//from  w  ww.j a  va2  s .  c  o m
        System.out.println("ID = " + bean.getId());
    }

From source file:Main.java

public static void main(String[] argv) throws Exception {

    XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(new FileInputStream("infilename.xml")));

    MyClass o = (MyClass) decoder.readObject();
    decoder.close();//from   w w w  .  ja va  2s.  c  om

    int prop = o.getProp(); // 1
    int[] props = o.getProps(); // [1, 2, 3]

}

From source file:psidev.psi.mi.filemakers.xmlFlattener.XmlFlattener.java

public static void main(String[] args) throws Exception {

    System.setProperty("java.awt.headless", "true");
    XmlFlattener f = new XmlFlattener();
    String mappingFileName = "";
    String schema = null;//w ww. j  a va  2  s. co m
    String flatFile = null;
    String xmlDocument = null;

    Options options = new Options();

    Option option = new Option("mapping", true, "Mapping file");
    option.setRequired(true);
    options.addOption(option);

    option = new Option("xmlDocument", true, "XML document to parse");
    option.setRequired(false);
    options.addOption(option);

    option = new Option("schema", true, "Xsd schema, for instance data/MIF25.xsd");
    option.setRequired(false);
    options.addOption(option);

    option = new Option("o", true, "output tab delimited file");
    option.setRequired(true);
    options.addOption(option);

    option = new Option("validate", false,
            "validate the XML document (required to retrieved XML ids, e.g. with PSI-MI XML 1.0)");
    option.setRequired(false);
    options.addOption(option);

    CommandLineParser parser = new PosixParser();
    CommandLine cmd = null;

    try {
        // parse the command line arguments
        cmd = parser.parse(options, args, true);
    } catch (ParseException exp) {
        // Oops, something went wrong

        displayUsage(options);
        System.exit(1);
    }

    mappingFileName = cmd.getOptionValue("mapping");
    xmlDocument = cmd.getOptionValue("xmlDocument");
    schema = cmd.getOptionValue("schema");
    flatFile = cmd.getOptionValue("o");

    log.info("mapping : " + mappingFileName + ", output : " + flatFile);

    FileInputStream fin = new FileInputStream(mappingFileName);

    // Create XML decoder.
    XMLDecoder xdec = new XMLDecoder(fin);
    TreeMapping treeMapping = (TreeMapping) xdec.readObject();

    if (xmlDocument != null) {
        xmlDocument = xmlDocument.replaceAll("'", "");
        treeMapping.setDocumentURL(xmlDocument);
        log.info("xmlDocument : " + xmlDocument);
    }

    if (schema != null) {
        treeMapping.setSchemaURL(schema.replaceAll("'", ""));
        log.info("Xsd schema : " + schema);
    }

    if (cmd.hasOption("validate")) {
        log.info("XML document will be validated");
        f.xsdTree.setValidateDocument(true);
    }

    f.xsdTree.loadMapping(treeMapping);

    if (log.isErrorEnabled()) {
        log.error("Messages from XML Parsing:");
        for (String error : f.xsdTree.xmlErrorHandler.getErrors()) {
            log.error(error);
        }
    }

    PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(flatFile)));

    f.xsdTree.write(writer);
    writer.flush();
    writer.close();
    log.info("Flat file successfully created");

}

From source file:psidev.psi.mi.filemakers.xmlMaker.XmlMaker.java

public static void main(String[] args) throws Exception {
    try {//from ww w. j a va 2  s.co  m
        System.setProperty("java.awt.headless", "true");
        XmlMaker f = new XmlMaker();
        // create Option objects
        Option helpOpt = new Option("help", "print this message.");

        Option mappingOpt = OptionBuilder.withArgName("mapping").hasArg()
                .withDescription("the mapping file, created by the GUI application").create("mapping");
        mappingOpt.setRequired(true);

        Option oOpt = OptionBuilder.withArgName("xml document").hasArg().withDescription("name of the log file")
                .create("o");
        oOpt.setRequired(true);

        Option logOpt = OptionBuilder.withArgName("log").hasArg()
                .withDescription("the mapping file, created by the GUI application").create("log");
        oOpt.setRequired(false);

        Option flatfilesOpt = OptionBuilder.withArgName("flat files").hasArg()
                .withDescription("names of the flat files in the right order, separated by comma")
                .create("flatfiles");
        oOpt.setRequired(false);

        Option dictionariesOpt = OptionBuilder.withArgName("dictionaries").hasArg()
                .withDescription("names of the dictionary files in the right order, separated by comma")
                .create("dictionaries");
        oOpt.setRequired(false);

        Option schemaOpt = OptionBuilder.withArgName("schema").hasArg().withDescription("the XML schema")
                .create("schema");
        schemaOpt.setRequired(false);

        Options options = new Options();

        options.addOption(helpOpt);
        options.addOption(mappingOpt);
        options.addOption(oOpt);
        options.addOption(logOpt);
        options.addOption(flatfilesOpt);
        options.addOption(dictionariesOpt);
        options.addOption(schemaOpt);

        // create the parser
        CommandLineParser parser = new BasicParser();
        CommandLine line = null;
        try {
            // parse the command line arguments
            line = parser.parse(options, args, true);
        } catch (ParseException exp) {
            // Oops, something went wrong

            displayUsage(options);

            System.err.println("Parsing failed.  Reason: " + exp.getMessage());
            System.exit(1);
        }

        if (line.hasOption("help")) {
            displayUsage(options);
            System.exit(0);
        }

        String mappingFileName = line.getOptionValue("mapping");
        String flatFiles = line.getOptionValue("flatfiles");
        String dictionaries = line.getOptionValue("dictionaries");
        String schema = line.getOptionValue("schema");
        String xmlFile = line.getOptionValue("o");
        String logFile = line.getOptionValue("log");

        if (logFile == null)
            logFile = xmlFile + ".log";

        System.out.println("mapping = " + mappingFileName + ", output = " + xmlFile);

        if (mappingFileName == null || mappingFileName.length() == 0) {
            System.out.println(
                    "usage: java -classpath classes/ -Djava.ext.dirs=libs mint.filemakers.xmlFlattener.XmlMaker -mapping mapping.xml [-flatfiles flatfile1[,flatfile2]] [-dictionaries [dictionary1[,dictionary2]] [-o xmlDocument] [-log logfile]");
            System.out.println("Available parameters:");
            System.out.println("-mapping: the mapping file, created by the GUI application");
            System.out.println("-o: name of the XML document to write -log: name of the log file");
            System.out.println("-flatfiles: names of the flat files in the right order, separated by comma");
            System.out.println(
                    "-dictionaries: names of the dictionary files in the right order, separated by comma");
            return;
        }

        FileInputStream fin = new FileInputStream(mappingFileName);
        // Create XML decoder.
        XMLDecoder xdec = new XMLDecoder(fin);
        Mapping mapping = (Mapping) xdec.readObject();

        if (flatFiles != null) {
            String[] files = flatFiles.replaceAll("'", "").split(",");
            for (int j = 0; j < files.length; j++) {
                ((FlatFileMapping) mapping.getFlatFiles().get(j)).fileURL = files[j];
                System.out.println("flat file " + j + ": " + files[j]);
            }
        }

        if (dictionaries != null) {
            String[] files = dictionaries.replaceAll("'", "").split(",");
            for (int j = 0; j < files.length; j++) {
                ((DictionaryMapping) mapping.getDictionaries().get(j)).setFileURL(files[j]);
                System.out.println("dictionary " + j + ": " + files[j]);
            }
        }

        if (schema != null) {
            mapping.getTree().setSchemaURL(schema.replaceAll("'", ""));
        }
        try {
            f.load(mapping);
        } catch (FileMakersException fme) {
            System.out.println("exit from program: unable to load the mapping");
            return;
        }
        //         MarshallingObserver observer = new MarshallingObserver();
        //         observer
        //               .setObservable(f.xsdTree.observable);
        //         (f.xsdTree).observable.addObserver(observer);
        //f.xsdTree.print(new File(xmlFile), new File(logFile));
        f.xsdTree.print2(new File(xmlFile));
        System.out.println("done");
        return;
    } catch (Exception e) {
        System.out.println(
                "usage: java -classpath classes/ -Djava.ext.dirs=libs mint.filemakers.xmlFlattener.XmlMaker -mapping mapping.xml [-flatfiles flatfile1[,flatfile2]] [-dictionaries [dictionary1[,dictionary2]] [-o xmlDocument] [-log logfile]");
        System.out.println("Available parameters:");
        System.out.println("-mapping: the mapping file, created by the GUI application");
        System.out.println("-o: name of the XML document to write -log: name of the log file");
        System.out.println("-flatfiles: names of the flat files in the right order, separated by comma");
        System.out
                .println("-dictionaries: names of the dictionary files in the right order, separated by comma");
        throw (e);
    }
}

From source file:psidev.psi.mi.filemakers.xmlFlattener.XmlFlattenerGui.java

public static void main(String[] args) {

    /*/*from  www  .j  a  v  a2  s.  c  o  m*/
     * Load look'n feel
     */
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e2) {
        log.warn("Cannot use System Look and Feel.");
    }

    XmlFlattenerGui f = new XmlFlattenerGui();

    Options options = new Options();

    Option option = new Option("mapping", true, "Mapping file");
    option.setRequired(false);
    options.addOption(option);

    option = new Option("xmlDocument", true, "XML document to parse");
    option.setRequired(false);
    options.addOption(option);
    option = new Option("schema", true, "Xsd schema, for instance data/MIF25.xsd");
    option.setRequired(false);
    options.addOption(option);
    option = new Option("o", true, "output tab delimited file");
    option.setRequired(false);
    options.addOption(option);
    option = new Option("validate", false,
            "validate the XML document (required to retrieved XML ids, e.g. with PSI-MI XML 1.0)");
    option.setRequired(false);
    options.addOption(option);

    CommandLineParser parser = new PosixParser();
    CommandLine cmd = null;

    try {
        // parse the command line arguments
        cmd = parser.parse(options, args, true);
    } catch (ParseException exp) {
        displayUsage(options);
        System.exit(1);
    }

    String mappingFileName = "";
    String schema = null;
    String xmlDocument = null;

    mappingFileName = cmd.getOptionValue("mapping");
    xmlDocument = cmd.getOptionValue("xmlDocument");
    schema = cmd.getOptionValue("schema");

    if (false == "".equals(mappingFileName)) {
        try {

            FileInputStream fin = new FileInputStream(mappingFileName);

            // Create XML encoder.
            XMLDecoder xdec = new XMLDecoder(fin);

            /* get mapping */
            TreeMapping treeMapping = (TreeMapping) xdec.readObject();

            /* tree */
            if (xmlDocument != null) {
                xmlDocument = xmlDocument.replaceAll("'", "");
                treeMapping.setDocumentURL(xmlDocument);
                log.info("xmlDocument: " + xmlDocument);
            }

            if (schema != null) {
                treeMapping.setSchemaURL(schema.replaceAll("'", ""));
            }

            if (cmd.hasOption("validate")) {
                log.info("XML document will be validated");
                ((XsdTreeStructImpl) f.treePanel.xsdTree).setValidateDocument(true);
            }

            ((XsdTreeStructImpl) f.treePanel.xsdTree).loadMapping(treeMapping);

            if (log.isErrorEnabled()) {
                log.error("Xml Parsing messages:");
                for (String error : ((XsdTreeStructImpl) f.treePanel.xsdTree).xmlErrorHandler.getErrors()) {
                    log.error(error);
                }
            }

            ((XsdTreeStructImpl) f.treePanel.xsdTree).loadMapping(treeMapping);
            f.treePanel.updatePreview();

            f.treePanel.setTreeSelectionListener();
            f.treePanel.setCellRenderer();
            f.treePanel.xsdTree.check();
            f.treePanel.reload();
            xdec.close();
            fin.close();
        } catch (Exception e) {

        }
    }
}

From source file:Main.java

public static Object deserializeObject(File file) {
    Object data = null;/* ww w  .  j  av a2  s . c  o  m*/
    try {
        FileInputStream os = new FileInputStream(file);
        XMLDecoder decoder = new XMLDecoder(os);
        data = decoder.readObject();
        decoder.close();
    } catch (FileNotFoundException f) {
        data = null;
        f.printStackTrace();
    }
    return data;
}

From source file:Main.java

public static Object handleXml(InputStream in) {
    XMLDecoder d = new XMLDecoder(in);
    try {/*from  w  ww . j  av a  2s . c  om*/
        Object result = d.readObject(); //Deserialization happen here
        return result;
    } finally {
        d.close();
    }
}

From source file:Main.java

public static Object objectXmlDecoder(String objSource) {
    Object obj = null;/*from   ww  w.jav  a  2  s .  co m*/
    try {
        File fin = new File(objSource);
        if (fin.exists()) {
            FileInputStream fis = new FileInputStream(fin);
            XMLDecoder decoder = new XMLDecoder(fis);
            obj = decoder.readObject();
            fis.close();
            decoder.close();
        }
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    return obj;
}

From source file:Main.java

public static Object Read(String filename) throws FileNotFoundException {
    XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(new FileInputStream(filename)));
    Object obj = decoder.readObject();
    decoder.close();/*w ww .  j a  v a 2  s  .  co  m*/
    return obj;
}

From source file:Main.java

public static Object read(String filename) throws Exception {
    XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(new FileInputStream(filename)));
    Object o = decoder.readObject();
    decoder.close();//from   w w w  . ja  va 2 s  .  co m
    return o;
}