1 5 package org.hibernate.eclipse.console.wizards; 6 7 import java.io.File ; 8 import java.io.IOException ; 9 import java.net.MalformedURLException ; 10 import java.net.URL ; 11 12 import javax.xml.parsers.DocumentBuilder ; 13 import javax.xml.parsers.DocumentBuilderFactory ; 14 import javax.xml.parsers.ParserConfigurationException ; 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 ; 25 import org.w3c.dom.Element ; 26 import org.w3c.dom.NodeList ; 27 import org.xml.sax.SAXException ; 28 29 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 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 51 public IPath getCfgFile() { 52 return cfgFile; 53 } 54 55 58 public IPath getPropertyFilename() { 59 return propertyFilename; 60 } 61 62 65 public IPath[] getMappings() { 66 return mappings; 67 } 68 69 72 public IPath[] getCustomClasspath() { 73 return customClasspath; 74 } 75 76 protected EclipseConsoleConfigurationPreferences() { 77 78 } 79 80 public URL [] getCustomClassPathURLS() { 81 try { 82 return ClassLoaderHelper.getRawLocationsURLForResources(customClasspath); 83 } catch (MalformedURLException mue) { 84 throw new HibernateConsoleRuntimeException("Could not resolve classpaths", mue); 85 } 86 } 87 88 public File [] getMappingFiles() { 89 File [] files = new File [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 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 pathToFile(String 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 getConfigXMLFile() { 117 return pathToFile(cfgFile); 118 } 119 120 public File getPropertyFile() { 121 return pathToFile(propertyFilename); 122 } 123 124 public void writeStateTo(Element node) { 125 writeStateTo(node, getName(), cfgFile, propertyFilename, mappings, customClasspath); 126 } 127 128 protected void setConfigFile(String cfgFile) { 129 this.cfgFile = cfgFile==null?null:new Path(cfgFile); 130 } 131 132 protected void setPropertyFile(String cfgFile) { 133 this.propertyFilename = cfgFile==null?null:new Path(cfgFile); 134 } 135 protected void setMappings(String [] mappings) { 136 this.mappings = new IPath[mappings.length]; 137 for (int i = 0; i < mappings.length; i++) { 138 String str = mappings[i]; 139 this.mappings[i] = new Path(mappings[i]); 140 } 141 } 142 143 protected void setCustomClassPath(String [] mappings) { 144 this.customClasspath = new IPath[mappings.length]; 145 for (int i = 0; i < mappings.length; i++) { 146 String str = mappings[i]; 147 this.customClasspath[i] = new Path(mappings[i]); 148 } 149 } 150 151 public static EclipseConsoleConfigurationPreferences[] readStateFrom(File f) { 152 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 153 DocumentBuilder parser; 154 try { 155 parser = factory.newDocumentBuilder(); 156 157 Document doc = parser.parse(f); 158 159 Element root = doc.getDocumentElement(); 160 161 NodeList 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 )elementsByTagName.item(i)); 167 } 168 return result; 169 } catch(SAXException sa) { 170 throw new HibernateConsoleRuntimeException("Errors while parsing " + f,sa); 171 } catch (ParserConfigurationException e) { 172 throw new HibernateConsoleRuntimeException("Errors while parsing " + f,e); 173 } catch (IOException e) { 174 throw new HibernateConsoleRuntimeException("Errors while parsing " + f,e); 175 } 176 } 177 178 } 179 | Popular Tags |