1 23 24 package org.objectweb.fractal.gui.repository.lib; 25 26 import java.io.File ; 27 import java.io.FileInputStream ; 28 import java.io.FileWriter ; 29 import java.io.InputStream ; 30 31 import org.objectweb.fractal.adl.ADLException; 32 import org.objectweb.fractal.adl.Node; 33 import org.objectweb.fractal.adl.Parser; 34 import org.objectweb.fractal.adl.xml.XMLParser; 35 import org.objectweb.fractal.adl.xml.XMLWriter; 36 import org.objectweb.fractal.gui.repository.api.Storage; 37 38 44 45 public class XMLFileStorage implements Storage { 46 47 static String FS = new String (System.getProperty ("file.separator")); 48 49 52 53 private String storage; 54 55 private Parser parser = new XMLParser(); 56 57 public void open (final String storage) 58 throws Exception 59 { 60 if (this.storage != null) { 61 throw new Exception ("Storage already opened"); 62 } 63 this.storage = storage; 64 } 65 66 public Object load (final String name) throws Exception { 67 if (storage == null) { 68 throw new Exception ("Storage not opened"); 69 } 70 String n = name.replace('.', '/') + ".fractal"; 71 File f = new File (storage, n); 72 InputStream is = null; 73 if (f.exists()) { 74 is = new FileInputStream (f); 75 } else { 76 ClassLoader cl = getClass().getClassLoader(); 77 if (cl == null) { 78 cl = ClassLoader.getSystemClassLoader(); 79 } 80 is = cl.getResourceAsStream(n); 81 } 82 if (is == null) { 83 throw new ADLException("Cannot find '" + name + "' in the storage or in the classpath", null); 84 } 85 try { 86 return parser.parse(is, f.getAbsolutePath()); 87 } finally { 88 is.close(); 89 } 90 } 91 92 public void store (final String name, final Object value) throws Exception { 93 if (storage == null) { 94 throw new Exception ("Storage not opened"); 95 } 96 String n = name.replace('.', '/') + ".fractal"; 97 File f = new File (storage, n); 98 if (!f.getParentFile().exists()) { 99 f.getParentFile().mkdirs(); 100 } 101 FileWriter pw = new FileWriter (f); 102 pw.write("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n"); 103 pw.write("<!DOCTYPE definition PUBLIC \"-//objectweb.org//DTD Fractal ADL 2.0//EN\" \"classpath://org/objectweb/fractal/adl/xml/standard.dtd\">\n\n"); 104 XMLWriter xmlw = new XMLWriter(pw); 105 xmlw.write((Node)value); 106 pw.close(); 107 } 108 109 public void close () throws Exception { 110 if (storage == null) { 111 throw new Exception ("Storage not opened"); 112 } 113 storage = null; 114 } 115 } 116 | Popular Tags |