Example usage for java.beans XMLDecoder XMLDecoder

List of usage examples for java.beans XMLDecoder XMLDecoder

Introduction

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

Prototype

public XMLDecoder(InputSource is) 

Source Link

Document

Creates a new decoder to parse XML archives created by the XMLEncoder class.

Usage

From source file:org.cesecore.util.HashMapTest.java

@SuppressWarnings("rawtypes")
@Test/* ww  w  .  j av  a 2 s  .  c  o  m*/
public void testHashMapStrangeCharsSafe() throws Exception {
    HashMap<String, Comparable> h = new HashMap<String, Comparable>();
    h.put("foo0", Boolean.FALSE);
    h.put("foo1", "\0001\0002fooString");
    h.put("foo2", Integer.valueOf(2));
    h.put("foo3", Boolean.TRUE);
    h.put("foo4", "");
    HashMap<Object, Object> a = new Base64PutHashMap();
    a.putAll(h);

    // Write to XML
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLEncoder encoder = new XMLEncoder(baos);
    encoder.writeObject(a);
    encoder.close();
    String data = baos.toString("UTF8");
    //log.error(data);

    try {
        XMLDecoder decoder = new XMLDecoder(new ByteArrayInputStream(data.getBytes("UTF8")));
        HashMap<?, ?> b = (HashMap<?, ?>) decoder.readObject();
        decoder.close();
        @SuppressWarnings("unchecked")
        HashMap<Object, Object> c = new Base64GetHashMap(b);
        assertEquals(((Boolean) c.get("foo0")).booleanValue(), false);
        assertEquals(((Boolean) c.get("foo3")).booleanValue(), true);
        assertEquals(((String) c.get("foo1")), "\0001\0002fooString");
        assertEquals(((String) c.get("foo4")), "");
        assertEquals(((Integer) c.get("foo2")).intValue(), 2);

    } catch (ClassCastException e) {
        assertTrue(false);
    }
}

From source file:com.clustercontrol.plugin.impl.AsyncTask.java

/**
 * ??XMLSerializable???//from   ww w . j  a  v a  2s . c o m
 * @param xml ??XML
 * @return Serializable
 * @throws IOException
 */
public static Serializable decodeXML(String xml) throws IOException {
    ByteArrayInputStream bais = null;
    XMLDecoder dec = null;
    Serializable obj = null;

    try {
        bais = new ByteArrayInputStream(xml.getBytes("UTF-8"));
        dec = new XMLDecoder(bais);
        obj = (Serializable) dec.readObject();
    } catch (UnsupportedEncodingException e) {
        log.warn(e);
    } finally {
        if (dec != null) {
            dec.close();
        }
        if (bais != null) {
            bais.close();
        }
    }

    return obj;
}

From source file:org.icefaces.sample.location.CityDictionary.java

/**
 * Reads the zipped xml city cityDictionary and loads it into memory.
 *
 * @throws java.io.IOException If anything goes wrong acquiring or readying the zip file of city data.
 *///from  ww  w  .  j  a  v a  2  s. c  o  m
private static void init() throws IOException {

    if (!initialized) {

        initialized = true;

        // Loading of the resource must be done the "JSF way" so that
        // it is agnostic about it's environment (portlet vs servlet).
        // First we get the resource as an InputStream
        FacesContext fc = FacesContext.getCurrentInstance();
        ExternalContext ec = fc.getExternalContext();
        InputStream is = ec.getResourceAsStream(DATA_RESOURCE_PATH);

        //is a zip file.
        ZipInputStream zipStream = new ZipInputStream(is);

        //Prime the stream by reading the first entry.  The way
        //we have it currently configured, there should only be
        //one.
        ZipEntry firstEntry = zipStream.getNextEntry();

        //Pass the ZipInputStream to the XMLDecoder so that it
        //can read in the list of cities and associated data.
        XMLDecoder xDecoder = new XMLDecoder(zipStream);
        List cityList = (List) xDecoder.readObject();

        //Close the decoder and the stream.
        xDecoder.close();
        zipStream.close();

        if (cityList == null) {
            throw new IOException();
        }

        cityDictionary = new ArrayList(cityList.size());
        City tmpCity = null;
        for (int i = 0, max = cityList.size(); i < max; i++) {
            tmpCity = (City) cityList.get(i);
            if (tmpCity != null && tmpCity.getCity() != null) {
                cityDictionary.add(new SelectItem(tmpCity, tmpCity.getCity()));
            }
        }
        cityList.clear();
        Collections.sort(cityDictionary, LABEL_COMPARATOR);
    }

}

From source file:org.icefaces.application.showcase.view.bean.examples.component.selectInputText.CityDictionary.java

/**
 * Reads the zipped xml city cityDictionary and loads it into memory.
 *//*from   w w  w .  java 2 s. c o  m*/
private static void init() throws IOException {

    if (!initialized) {

        initialized = true;

        // Loading of the resource must be done the "JSF way" so that
        // it is agnostic about it's environment (portlet vs servlet).
        // First we get the resource as an InputStream
        FacesContext fc = FacesContext.getCurrentInstance();
        ExternalContext ec = fc.getExternalContext();
        InputStream is = ec.getResourceAsStream(DATA_RESOURCE_PATH);

        //Wrap the InputStream as a ZipInputStream since it
        //is a zip file.
        ZipInputStream zipStream = new ZipInputStream(is);

        //Prime the stream by reading the first entry.  The way
        //we have it currently configured, there should only be
        //one.
        ZipEntry firstEntry = zipStream.getNextEntry();

        //Pass the ZipInputStream to the XMLDecoder so that it
        //can read in the list of cities and associated data.
        XMLDecoder xDecoder = new XMLDecoder(zipStream);
        List cityList = (List) xDecoder.readObject();

        //Close the decoder and the stream.
        xDecoder.close();
        zipStream.close();

        if (cityList == null) {
            throw new IOException();
        }

        cityDictionary = new ArrayList(cityList.size());
        City tmpCity;
        for (int i = 0, max = cityList.size(); i < max; i++) {
            tmpCity = (City) cityList.get(i);
            if (tmpCity != null && tmpCity.getCity() != null) {
                cityDictionary.add(new SelectItem(tmpCity, tmpCity.getCity()));
            }
        }
        cityList.clear();

        Collections.sort(cityDictionary, LABEL_COMPARATOR);
    }

}

From source file:org.cesecore.util.HashMapTest.java

@SuppressWarnings("rawtypes")
@Test/*w  ww . j  a v  a 2  s  .  co m*/
public void testHashMapNormalCharsSafe() throws Exception {
    HashMap<String, Comparable> h = new HashMap<String, Comparable>();
    h.put("foo0", Boolean.FALSE);
    h.put("foo1", "fooString");
    h.put("foo2", Integer.valueOf(2));
    h.put("foo3", Boolean.TRUE);
    h.put("foo4", "");
    HashMap<Object, Object> a = new Base64PutHashMap();
    a.putAll(h);

    // Write to XML
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLEncoder encoder = new XMLEncoder(baos);
    encoder.writeObject(a);
    encoder.close();
    String data = baos.toString("UTF8");
    //log.error(data);

    try {
        XMLDecoder decoder = new XMLDecoder(new ByteArrayInputStream(data.getBytes("UTF8")));
        HashMap<?, ?> b = (HashMap<?, ?>) decoder.readObject();
        decoder.close();
        @SuppressWarnings("unchecked")
        HashMap<Object, Object> c = new Base64GetHashMap(b);
        assertEquals(((Boolean) c.get("foo0")).booleanValue(), false);
        assertEquals(((Boolean) c.get("foo3")).booleanValue(), true);
        assertEquals(((String) c.get("foo4")), "");
        assertEquals(((String) c.get("foo1")), "fooString");
        assertEquals(((Integer) c.get("foo2")).intValue(), 2);

    } catch (ClassCastException e) {
        assertTrue(false);
    }
}

From source file:net.commerce.zocalo.experiment.config.SessionConfigurationTest.java

private Object readObjFromBytes(byte[] bytes) {
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    XMLDecoder dec = new XMLDecoder(bais);
    return dec.readObject();
}

From source file:gov.llnl.lc.smt.command.config.SmtConfig.java

public static Map<String, String> readConfigFile(String fileName) throws FileNotFoundException {
    // return null if can't be read, for any reason
    logger.info("Reading config from: " + fileName);
    Map<String, String> config = null;

    if (fileName != null) {
        // Create input stream.
        FileInputStream fis;//from ww  w .  j a  v a2 s .  c  om
        try {
            fis = new FileInputStream(fileName);
            // Create XML decoder.
            XMLDecoder xdec = new XMLDecoder(fis);

            // Read object.
            config = (HashMap<String, String>) xdec.readObject();

            // override this config file, with the actual filename (may already be correct)
            config.put(SmtProperty.SMT_READ_CONFIG.getName(), fileName);

        } catch (FileNotFoundException e) {
            logger.severe("Unable to read config from: " + fileName);
            throw (e);
        }
    }
    return config;
}

From source file:lu.lippmann.cdb.graph.GraphUtil.java

/**
 * /*from   www .j av  a 2s  . c  o  m*/
 * @param xmlString
 * @return
 */
public static Layout<CNode, CEdge> getLayoutFromXML(final String xmlString) {
    final GraphWithOperations graph = new GraphWithOperations();
    final XMLDecoder in = new XMLDecoder(new BufferedInputStream(new StringInputStream(xmlString)));
    final GraphTO gto = (GraphTO) in.readObject();
    applyOperationsToGraph(graph, new ArrayList<GraphOperation>(gto.getOperations()));
    graph.setUntitledEdgeCount(gto.getUntitledEdgeCount());
    graph.setUntitledVertexCount(gto.getUntitledVertexCount());
    graph.setVariables(gto.getVariables());
    return getLayoutFromGraphWithOperations(graph);
}

From source file:resources.ResourceManager.java

public static int loadLayoutTable(File file, LayoutTableModel tableModel) {
    int size = 0;
    try {/*from   w  w w  .  j ava  2  s  .com*/
        final XMLDecoder decoder = new XMLDecoder(new FileInputStream(file));
        final List<LayoutTableItem> listFromFile = (List<LayoutTableItem>) decoder.readObject();
        tableModel.setLayoutTableItemList(listFromFile);
        decoder.close();

        size = listFromFile.size();
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return size;
}

From source file:org.scantegrity.IRVTally.IRVTally.java

/**
 * Load a contest from file. /*ww w. java 2  s.  co  m*/
 * @param l_fname
 * @throws FileNotFoundException 
 */
@SuppressWarnings("unchecked")
public static Vector<Contest> loadContest(String p_fname) throws FileNotFoundException {
    Vector<Contest> l_res;
    XMLDecoder e;
    e = new XMLDecoder(new BufferedInputStream(new FileInputStream(p_fname)));
    l_res = (Vector<Contest>) e.readObject();
    e.close();
    return l_res;
}