public void parseDataFormatXMLfile(URL url) throws MaltChainedException { if (url == null) { throw new DataFormatException("The data format specifcation file cannot be found. "); } try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Element root = db.parse(url.openStream()).getDocumentElement(); if (root.getNodeName().equals("dataformat")) { dataFormatName = root.getAttribute("name"); if (root.getAttribute("datastructure").length() > 0) { dataStructure = DataStructure.valueOf(root.getAttribute("datastructure").toUpperCase()); } else { dataStructure = DataStructure.DEPENDENCY; } } else { throw new DataFormatException( "Data format specification file must contain one 'dataformat' element. "); } NodeList cols = root.getElementsByTagName("column"); Element col; for (int i = 0, n = cols.getLength(); i < n; i++) { col = (Element) cols.item(i); DataFormatEntry entry = new DataFormatEntry( col.getAttribute("name"), col.getAttribute("category"), col.getAttribute("type"), col.getAttribute("default")); entries.put(entry.getDataFormatEntryName(), entry); } NodeList deps = root.getElementsByTagName("dependencies"); if (deps.getLength() > 0) { NodeList dep = ((Element) deps.item(0)).getElementsByTagName("dependency"); for (int i = 0, n = dep.getLength(); i < n; i++) { Element e = (Element) dep.item(i); dependencies.add( new Dependency( e.getAttribute("name"), e.getAttribute("url"), e.getAttribute("map"), e.getAttribute("urlmap"))); } } } catch (java.io.IOException e) { throw new DataFormatException("Cannot find the file " + url.toString() + ". ", e); } catch (ParserConfigurationException e) { throw new DataFormatException("Problem parsing the file " + url.toString() + ". ", e); } catch (SAXException e) { throw new DataFormatException("Problem parsing the file " + url.toString() + ". ", e); } }
public static void main(String[] args) throws IOException, ClassNotFoundException { // Create a simple object graph DataStructure ds = new DataStructure(); ds.message = "hello world"; ds.data = new int[] {1, 2, 3, 4}; ds.other = new DataStructure(); ds.other.message = "nested structure"; ds.other.data = new int[] {9, 8, 7}; // Display the original object graph System.out.println("Original data structure: " + ds); // Output it to a file File f = new File("datastructure.ser"); System.out.println("Storing to a file..."); Serializer.store(ds, f); // Read it back from the file, and display it again ds = (DataStructure) Serializer.load(f); System.out.println("Read from the file: " + ds); // Create a deep clone and display that. After making the copy // modify the original to prove that the clone is "deep". DataStructure ds2 = (DataStructure) Serializer.deepclone(ds); ds.other.message = null; ds.other.data = null; // Change original System.out.println("Deep clone: " + ds2); }
public String toString() { String s = message; for (int i = 0; i < data.length; i++) s += " " + data[i]; if (other != null) s += "\n\t" + other.toString(); return s; }