KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > eclipse > console > wizards > EclipseConsoleConfigurationPreferences


1 /*
2  * Created on 2004-11-01 by max
3  *
4  */

5 package org.hibernate.eclipse.console.wizards;
6
7 import java.io.File JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.net.MalformedURLException JavaDoc;
10 import java.net.URL JavaDoc;
11
12 import javax.xml.parsers.DocumentBuilder JavaDoc;
13 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
14 import javax.xml.parsers.ParserConfigurationException JavaDoc;
15
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.resources.ResourcesPlugin;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.core.runtime.Path;
20 import org.hibernate.console.AbstractConsoleConfigurationPreferences;
21 import org.hibernate.console.ConsoleConfigurationPreferences;
22 import org.hibernate.console.HibernateConsoleRuntimeException;
23 import org.hibernate.eclipse.console.utils.ClassLoaderHelper;
24 import org.w3c.dom.Document JavaDoc;
25 import org.w3c.dom.Element JavaDoc;
26 import org.w3c.dom.NodeList JavaDoc;
27 import org.xml.sax.SAXException JavaDoc;
28
29 /**
30  * @author max
31  *
32  */

33 public class EclipseConsoleConfigurationPreferences extends AbstractConsoleConfigurationPreferences {
34
35     private IPath cfgFile;
36     private IPath propertyFilename;
37     private IPath[] mappings;
38     private IPath[] customClasspath;
39
40     public EclipseConsoleConfigurationPreferences(String JavaDoc configName, IPath cfgFile, IPath propertyFilename, IPath[] mappings, IPath[] classpaths) {
41         super(configName);
42         this.cfgFile = cfgFile;
43         this.propertyFilename = propertyFilename;
44         this.mappings = mappings;
45         this.customClasspath = classpaths;
46     }
47
48     /**
49      * @return Returns the cfgFile.
50      */

51     public IPath getCfgFile() {
52         return cfgFile;
53     }
54     
55     /**
56      * @return Returns the propertyFilename.
57      */

58     public IPath getPropertyFilename() {
59         return propertyFilename;
60     }
61     
62     /**
63      * @return Returns the mappings.
64      */

65     public IPath[] getMappings() {
66         return mappings;
67     }
68     
69     /**
70      * @return Returns the customClasspath.
71      */

72     public IPath[] getCustomClasspath() {
73         return customClasspath;
74     }
75     
76     protected EclipseConsoleConfigurationPreferences() {
77         
78     }
79
80     public URL JavaDoc[] getCustomClassPathURLS() {
81         try {
82             return ClassLoaderHelper.getRawLocationsURLForResources(customClasspath);
83         } catch (MalformedURLException JavaDoc mue) {
84             throw new HibernateConsoleRuntimeException("Could not resolve classpaths", mue);
85         }
86     }
87
88     public File JavaDoc[] getMappingFiles() {
89         File JavaDoc[] files = new File JavaDoc[mappings.length];
90         for (int i = 0; i < mappings.length; i++) {
91             IPath path = mappings[i];
92             files[i] = pathToFile(path);
93             
94         }
95         return files;
96     }
97
98     private File JavaDoc pathToFile(IPath path) {
99         if(path==null) return null;
100         IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
101         
102         return pathToFile(path.toString(), resource);
103     }
104
105     private File JavaDoc pathToFile(String JavaDoc path, IResource resource) {
106         if(resource != null) {
107             IPath rawLocation = resource.getRawLocation();
108             if(rawLocation !=null) {
109                 return rawLocation.toFile();
110             }
111         }
112
113         throw new HibernateConsoleRuntimeException("Could not resolve " + path + " to a file");
114     }
115
116     public File JavaDoc getConfigXMLFile() {
117         return pathToFile(cfgFile);
118     }
119
120     public File JavaDoc getPropertyFile() {
121         return pathToFile(propertyFilename);
122     }
123
124     public void writeStateTo(Element JavaDoc node) {
125         writeStateTo(node, getName(), cfgFile, propertyFilename, mappings, customClasspath);
126     }
127
128     protected void setConfigFile(String JavaDoc cfgFile) {
129         this.cfgFile = cfgFile==null?null:new Path(cfgFile);
130     }
131
132     protected void setPropertyFile(String JavaDoc cfgFile) {
133         this.propertyFilename = cfgFile==null?null:new Path(cfgFile);
134     }
135     protected void setMappings(String JavaDoc[] mappings) {
136         this.mappings = new IPath[mappings.length];
137         for (int i = 0; i < mappings.length; i++) {
138             String JavaDoc str = mappings[i];
139             this.mappings[i] = new Path(mappings[i]);
140         }
141     }
142
143     protected void setCustomClassPath(String JavaDoc[] mappings) {
144         this.customClasspath = new IPath[mappings.length];
145         for (int i = 0; i < mappings.length; i++) {
146             String JavaDoc str = mappings[i];
147             this.customClasspath[i] = new Path(mappings[i]);
148         }
149     }
150     
151     public static EclipseConsoleConfigurationPreferences[] readStateFrom(File JavaDoc f) {
152         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
153         DocumentBuilder JavaDoc parser;
154         try {
155             parser = factory.newDocumentBuilder();
156             
157             Document JavaDoc doc = parser.parse(f);
158             
159             Element JavaDoc root = doc.getDocumentElement();
160             
161             NodeList JavaDoc elementsByTagName = root.getElementsByTagName(CONFIGURATION_TAG);
162             EclipseConsoleConfigurationPreferences[] result = new EclipseConsoleConfigurationPreferences[elementsByTagName.getLength()];
163             
164             for(int i = 0; i < elementsByTagName.getLength(); i++) {
165                 result[i] = new EclipseConsoleConfigurationPreferences();
166                 result[i].readStateFrom((Element JavaDoc)elementsByTagName.item(i));
167             }
168             return result;
169         } catch(SAXException JavaDoc sa) {
170             throw new HibernateConsoleRuntimeException("Errors while parsing " + f,sa);
171         } catch (ParserConfigurationException JavaDoc e) {
172             throw new HibernateConsoleRuntimeException("Errors while parsing " + f,e);
173         } catch (IOException JavaDoc e) {
174             throw new HibernateConsoleRuntimeException("Errors while parsing " + f,e);
175         }
176     }
177     
178 }
179
Popular Tags