List of usage examples for java.beans XMLDecoder readObject
public Object readObject()
From source file:Main.java
/** * Creates from the given xml string an java object. * // w w w. j av a2 s . c o m * @param <T> * the generic type * @param xmlString * the xml string to transform to an java object. * @return the xml string */ @SuppressWarnings("unchecked") public static <T> T toObjectWithXMLDecoder(final String xmlString) { XMLDecoder dec = null; T obj = null; try { final InputStream is = new ByteArrayInputStream(xmlString.getBytes()); dec = new XMLDecoder(is); obj = (T) dec.readObject(); } finally { if (dec != null) { dec.close(); } } return obj; }
From source file:org.clickframes.testframes.TestResultsParser.java
public static ProjectTestResults parseTestResults(File zip) throws IOException { // tmp directory File tmpDirectory = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString()); tmpDirectory.mkdirs();/*from w w w . jav a 2s . c o m*/ ZipInputStream zis = new ZipInputStream(new FileInputStream(zip)); ZipEntry entry; int BUFFER = 4096; while ((entry = zis.getNextEntry()) != null) { int count; byte data[] = new byte[BUFFER]; // write the files to the disk File destinationFile = new File(tmpDirectory, entry.getName()); destinationFile.getParentFile().mkdirs(); FileOutputStream fos = new FileOutputStream(destinationFile); BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER); while ((count = zis.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, count); } dest.flush(); dest.close(); } File firefoxDir = new File(tmpDirectory, "firefox"); if (!firefoxDir.exists()) { throw new RuntimeException("Invalid test results zip file uploaded. firefox directory does not exist."); } File testResultsXmlFile = new File(tmpDirectory, "testResults.xml"); XMLDecoder d = new XMLDecoder(new BufferedInputStream(new FileInputStream(testResultsXmlFile))); ProjectTestResults projectTestResults = (ProjectTestResults) d.readObject(); d.close(); return projectTestResults; }
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. */// w ww .j a va 2s . 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. *//* w ww . java2 s .co 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:com.icesoft.icefaces.tutorial.component.autocomplete.AutoCompleteDictionary.java
private static void init() { // Raw list of xml cities. List cityList = null;/*from w w w . j a va2 s.c o m*/ // load the city dictionary from the compressed xml file. // get the path of the compressed file HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true); String basePath = session.getServletContext().getRealPath("/WEB-INF/resources"); basePath += "/city.xml.zip"; // extract the file ZipEntry zipEntry; ZipFile zipFile; try { zipFile = new ZipFile(basePath); zipEntry = zipFile.getEntry("city.xml"); } catch (Exception e) { log.error("Error retrieving records", e); return; } // get the xml stream and decode it. if (zipFile.size() > 0 && zipEntry != null) { try { BufferedInputStream dictionaryStream = new BufferedInputStream(zipFile.getInputStream(zipEntry)); XMLDecoder xDecoder = new XMLDecoder(dictionaryStream); // get the city list. cityList = (List) xDecoder.readObject(); dictionaryStream.close(); zipFile.close(); xDecoder.close(); } catch (ArrayIndexOutOfBoundsException e) { log.error("Error getting city list, not city objects", e); return; } catch (IOException e) { log.error("Error getting city list", e); return; } } // Finally load the object from the xml file. if (cityList != null) { dictionary = 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) { dictionary.add(new SelectItem(tmpCity, tmpCity.getCity())); } } cityList.clear(); // finally sort the list Collections.sort(dictionary, LABEL_COMPARATOR); } }
From source file:Main.java
public static Object decodeObject(byte[] byteArray, final boolean noisy) { ByteArrayInputStream inputStream = new ByteArrayInputStream(byteArray); XMLDecoder decoder = new XMLDecoder(inputStream); decoder.setExceptionListener(new ExceptionListener() { public void exceptionThrown(Exception ex) { if (noisy) ex.printStackTrace();/* w ww.ja v a 2s . c om*/ } }); Object copy = decoder.readObject(); decoder.close(); return copy; }
From source file:net.sourceforge.fenixedu.util.tests.ResponseExternalization.java
private static Response getResponse(String xmlResponse) { XMLDecoder decoder = null; try {// w w w.j av a 2 s. c om decoder = new XMLDecoder(new ByteArrayInputStream(xmlResponse.getBytes(CharEncoding.UTF_8))); } catch (UnsupportedEncodingException e1) { logger.error(e1.getMessage(), e1); } Response response = null; try { response = (Response) decoder.readObject(); } catch (ArrayIndexOutOfBoundsException e) { logger.error(e.getMessage(), e); } decoder.close(); return response; }
From source file:org.scantegrity.IRVTally.IRVTally.java
/** * Load a contest from file. /* ww w . j av a 2 s.c o 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; }
From source file:resources.ResourceManager.java
public static int loadLayoutTable(File file, LayoutTableModel tableModel) { int size = 0; try {// w ww . j av a 2s. c o m 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:com.clustercontrol.plugin.impl.AsyncTask.java
/** * ??XMLSerializable???// w ww .j av a2s .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; }