001: /*
002: * $RCSfile: JavaBuilderPersister.java,v $
003: * @modification $Date: 2001/09/28 19:41:42 $
004: * @version $Id: JavaBuilderPersister.java,v 1.1 2001/09/28 19:41:42 hfalk Exp $
005: *
006: */
007:
008: package com.memoire.vainstall.builder.util;
009:
010: import com.memoire.vainstall.builder.*;
011:
012: import java.awt.Rectangle;
013: import java.io.File;
014: import java.io.IOException;
015: import java.util.*;
016:
017: import javax.xml.parsers.*;
018: import org.xml.sax.*;
019: import org.xml.sax.helpers.*;
020: import org.w3c.dom.*;
021:
022: import javax.xml.transform.*;
023: import javax.xml.transform.dom.*;
024: import javax.xml.transform.stream.*;
025:
026: /**
027: * This class performs the persistance of the VAInstall Builder
028: *
029: * @see com.memoire.vainstall.VAIBuilderModel
030: * @see com.memoire.vainstall.util.BuilderPersisterInterface
031: *
032: * @author Henrik Falk
033: * @version $Id: JavaBuilderPersister.java,v 1.1 2001/09/28 19:41:42 hfalk Exp $
034: */
035: public class JavaBuilderPersister implements BuilderPersisterInterface {
036:
037: /**
038: * The Builder data model
039: */
040: VAIBuilderModel model = null;
041:
042: /**
043: * A not so elegant way to find out where the VAInstall shared
044: * directory is located.
045: * Used for saving the vaibuilder.xml file which contains data
046: * from the builder data model.
047: */
048: String vai_home = XmlUtil.findVAISharedDir().getAbsolutePath();
049:
050: /**
051: * Default constructor
052: */
053: public JavaBuilderPersister() {
054: super ();
055: }
056:
057: /**
058: * Initializes this class in order to know which datamodel to save
059: * and load.
060: * @param model The builder model
061: */
062: public void initialize(VAIBuilderModel model) {
063: this .model = model;
064: }
065:
066: /**
067: * Loads the data from datastore
068: */
069: public void load() {
070:
071: // check if we are initialized
072: if (model == null) {
073: return;
074: }
075:
076: // Check if vaibuilder.xml exists
077: String vaibuilder = vai_home + java.io.File.separator
078: + "vaibuilder.xml";
079: if (new File(vaibuilder).exists() == false) {
080: // this is the first time we run the builder
081: return;
082: }
083:
084: // Parse the XML file at the moment non-validating
085: Document doc = null;
086: try {
087: doc = XmlUtil.parse(vaibuilder, null);
088: } catch (XmlParseException exc) {
089: // System.out.println("Could not load preferences.");
090: return;
091: }
092:
093: // extract data from the Document to the data model
094: importData(doc);
095: }
096:
097: /**
098: * Saves the data in datastore
099: */
100: public void save() {
101:
102: // check if we are initialized
103: if (model == null) {
104: return;
105: }
106:
107: // create a new Document
108: Document doc = null;
109: try {
110: DocumentBuilder builder = DocumentBuilderFactory
111: .newInstance().newDocumentBuilder();
112: doc = builder.newDocument();
113:
114: } catch (ParserConfigurationException exc) {
115: return;
116: }
117:
118: // Extract data from the model to the Document
119: exportData(doc);
120:
121: // Create a Transformer which enables us to export the Document
122: Transformer transformer = null;
123: try {
124: transformer = TransformerFactory.newInstance()
125: .newTransformer();
126: transformer.setOutputProperty("indent", "yes");
127: } catch (TransformerConfigurationException exc) {
128: exc.printStackTrace();
129: return;
130: }
131:
132: // Export the Document
133: String vaibuilder = vai_home + java.io.File.separator
134: + "vaibuilder.xml";
135: try {
136: transformer.transform(new DOMSource(doc), new StreamResult(
137: new File(vaibuilder)));
138: } catch (TransformerException exc) {
139: exc.printStackTrace();
140: }
141:
142: }
143:
144: /**
145: * Extract data from a Document and updates the data model
146: * @param doc a Document
147: */
148: private void importData(Document doc) {
149:
150: NodeList nodelist;
151:
152: Element rootElement = doc.getDocumentElement();
153:
154: // get saved window positions
155: nodelist = rootElement.getElementsByTagName("window");
156: for (int i = 0; i < nodelist.getLength(); i++) {
157: Node window = (Node) nodelist.item(i);
158:
159: NamedNodeMap map = window.getAttributes();
160:
161: Rectangle rect = new Rectangle(XmlUtil.getAttributeAsInt(
162: map, "x"), XmlUtil.getAttributeAsInt(map, "y"),
163: XmlUtil.getAttributeAsInt(map, "width"), XmlUtil
164: .getAttributeAsInt(map, "height"));
165: model.getWindowList().put(
166: XmlUtil.getAttribute(map, "name"), rect);
167: }
168:
169: // get saved last opened projects
170: nodelist = rootElement.getElementsByTagName("lastfile");
171:
172: LinkedList tmpList = new LinkedList();
173: for (int i = 0; i < nodelist.getLength(); i++) {
174: tmpList.add("");
175: }
176: for (int i = 0; i < nodelist.getLength(); i++) {
177: int number = new Integer(XmlUtil.getAttribute(nodelist
178: .item(i), "number")).intValue();
179: String lastproject = XmlUtil.getAttribute(nodelist.item(i),
180: "file");
181: tmpList.set(number, lastproject);
182: }
183:
184: for (int i = tmpList.size() - 1; i >= 0; i--) {
185: model.addLastOpenedProject((String) tmpList.get(i));
186: }
187:
188: // get builder properties
189: nodelist = rootElement.getElementsByTagName("property");
190:
191: for (int i = 0; i < nodelist.getLength(); i++) {
192: String name = XmlUtil
193: .getAttribute(nodelist.item(i), "name");
194: String value = XmlUtil.getAttribute(nodelist.item(i),
195: "value");
196: model.getPropertyList().put(name, value);
197: }
198: }
199:
200: /**
201: * Extract data from the data model and updates a Document
202: * @param doc a Document
203: */
204: private void exportData(Document doc) {
205:
206: // create root element
207: Element root = doc.createElement("vaibuilder");
208: doc.appendChild(root);
209:
210: // create <window> elements
211: Hashtable windowList = model.getWindowList();
212:
213: Enumeration keys = windowList.keys();
214: while (keys.hasMoreElements() == true) {
215: String key = (String) keys.nextElement();
216: Rectangle rect = (Rectangle) windowList.get(key);
217:
218: Element element = doc.createElement("window");
219: element.setAttribute("name", key);
220: element.setAttribute("x", String.valueOf(rect.x));
221: element.setAttribute("y", String.valueOf(rect.y));
222: element.setAttribute("width", String.valueOf(rect.width));
223: element.setAttribute("height", String.valueOf(rect.height));
224:
225: root.appendChild(element);
226: }
227:
228: // create <lastproject> elements
229: LinkedList lastproject = model.getLastOpenedProjectList();
230:
231: for (int i = 0; i < lastproject.size(); i++) {
232:
233: Element element = doc.createElement("lastfile");
234: element.setAttribute("number", String.valueOf(i));
235: element.setAttribute("file", String.valueOf(lastproject
236: .get(i)));
237:
238: root.appendChild(element);
239: }
240:
241: // create <property> elements
242: Hashtable propertyList = model.getPropertyList();
243:
244: Enumeration propertyKeys = propertyList.keys();
245: while (propertyKeys.hasMoreElements() == true) {
246: String name = (String) propertyKeys.nextElement();
247: String value = (String) propertyList.get(name);
248:
249: Element element = doc.createElement("property");
250: element.setAttribute("name", name);
251: element.setAttribute("value", value);
252:
253: root.appendChild(element);
254: }
255:
256: }
257: }
|