KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > gui > repository > lib > XMLFileStorage


1 /***
2  * FractalGUI: a graphical tool to edit Fractal component configurations.
3  * Copyright (C) 2003 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: fractal@objectweb.org
20  *
21  * Authors: Eric Bruneton, Patrice Fauvel
22  */

23
24 package org.objectweb.fractal.gui.repository.lib;
25
26 import java.io.File JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.io.FileWriter JavaDoc;
29 import java.io.InputStream JavaDoc;
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 /**
39  * Basic implementation of {@link Storage} interface, based on a single XML
40  * file. More precsiely, each storage is a single XML file, and each (name,
41  * value) pair is represented by an XML element of these files (the name is
42  * given by the 'name' attribute of an XML element).
43  */

44
45 public class XMLFileStorage implements Storage {
46
47   static String JavaDoc FS = new String JavaDoc (System.getProperty ("file.separator"));
48
49   /**
50    * Name of the currently opened storage, or <tt>null</tt>.
51    */

52
53   private String JavaDoc storage;
54
55   private Parser parser = new XMLParser();
56   
57   public void open (final String JavaDoc storage)
58     throws Exception JavaDoc
59   {
60     if (this.storage != null) {
61       throw new Exception JavaDoc("Storage already opened");
62     }
63     this.storage = storage;
64   }
65
66   public Object JavaDoc load (final String JavaDoc name) throws Exception JavaDoc {
67     if (storage == null) {
68       throw new Exception JavaDoc("Storage not opened");
69     }
70     String JavaDoc n = name.replace('.', '/') + ".fractal";
71     File JavaDoc f = new File JavaDoc(storage, n);
72     InputStream JavaDoc is = null;
73     if (f.exists()) {
74       is = new FileInputStream JavaDoc(f);
75     } else {
76       ClassLoader JavaDoc 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 JavaDoc name, final Object JavaDoc value) throws Exception JavaDoc {
93     if (storage == null) {
94       throw new Exception JavaDoc("Storage not opened");
95     }
96     String JavaDoc n = name.replace('.', '/') + ".fractal";
97     File JavaDoc f = new File JavaDoc(storage, n);
98     if (!f.getParentFile().exists()) {
99       f.getParentFile().mkdirs();
100     }
101     FileWriter JavaDoc pw = new FileWriter JavaDoc(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 JavaDoc {
110     if (storage == null) {
111       throw new Exception JavaDoc("Storage not opened");
112     }
113     storage = null;
114   }
115 }
116
Popular Tags