001: // This file is part of KeY - Integrated Deductive Software Design
002: // Copyright (C) 2001-2007 Universitaet Karlsruhe, Germany
003: // Universitaet Koblenz-Landau, Germany
004: // Chalmers University of Technology, Sweden
005: //
006: // The KeY system is protected by the GNU General Public License.
007: // See LICENSE.TXT for details.
008: //
009: //
010:
011: /**
012: * KeYResourceManager controls the access to the properties
013: * and resources used in the KeY system.
014: * Use the static method getManager to get the unique instance.
015: */package de.uka.ilkd.key.util;
016:
017: import java.io.*;
018: import java.net.URL;
019:
020: import javax.swing.ImageIcon;
021:
022: public class KeYResourceManager {
023:
024: /** the unique instance */
025: private static final KeYResourceManager instance = new KeYResourceManager();
026:
027: private String version = null;
028: private String sha1 = null;
029:
030: private KeYResourceManager() {
031: }
032:
033: /**
034: * Return an instance of the ResourceManager
035: */
036: public static KeYResourceManager getManager() {
037: return instance;
038: }
039:
040: /**
041: * reads a version string or returns "x.z.y" in case of failures
042: */
043: private String readVersionString(URL url) {
044: String result = "";
045: if (url != null) {
046: try {
047: final InputStream io = new BufferedInputStream(url
048: .openStream());
049: int c;
050: while ((c = io.read()) != -1) {
051: result += (char) c;
052: }
053: } catch (IOException ioe) {
054: // who cares it is just a version number
055: result = "x.z.y";
056: }
057: } else {
058: result = "x.z.y";
059: }
060: return result.trim();
061: }
062:
063: /**
064: * returns the SHA 1 git code from which this version has been
065: * derived
066: * @return returns the SHA1 hash uniquely identifying the version
067: */
068: public String getSHA1() {
069: if (sha1 != null) {
070: return sha1;
071: }
072: sha1 = readVersionString(getResourceFile(this , "sha1"));
073:
074: return sha1;
075: }
076:
077: /**
078: * returns a readable customizable versin number
079: * @return
080: */
081: public String getVersion() {
082: if (version != null) {
083: return version;
084: }
085: version = readVersionString(getResourceFile(this , "version"));
086:
087: return version;
088: }
089:
090: /**
091: * Creates an icon from an image contained in a resource.
092: * The resource is fist search using the package name of the calling Object
093: * and if it is not found there the packagename of its superclass is used
094: * recusrivly.
095: * @param o the Object reference to the calling object
096: * @param filename String the name of the file to search (only relative
097: * pathname to the path of the calling class)
098: * @return the newly created image
099: */
100: public ImageIcon createImageIcon(Object o, String filename) {
101: return createImageIcon(o.getClass(), filename);
102: }
103:
104: /**
105: * Creates an icon from an image contained in a resource.
106: * The resource is fist search using the package name of the given class
107: * and if the resource is not found the packagename of its superclass is used
108: * recursivly.
109: * @param cl the Class the resource is looked for
110: * @param filename String the name of the file to search (only relative
111: * pathname to the path of the calling class)
112: * @return the newly created image
113: */
114: public ImageIcon createImageIcon(Class cl, String filename) {
115: URL iconURL = cl.getResource(filename);
116: Debug.out("Load Resource:" + filename + " of class " + cl);
117: if (iconURL == null && cl.getSuperclass() != null) {
118: return createImageIcon(cl.getSuperclass(), filename);
119: } else if (iconURL == null && cl.getSuperclass() == null) {
120: // error message Resource not found
121: System.out.println("No image resource " + filename
122: + " found");
123: return null;
124: } else {
125: Debug.out("Done.");
126: return new ImageIcon(iconURL);
127: }
128: }
129:
130: /**
131: * Copies the specified resource to targetLocation if such a file
132: * does not exist yet.
133: * The created file is removed automatically after finishing JAVA.
134: * @param o an Object the directory from where <code>resourcename</code>
135: * is copied is determined by looking on the package where <code>o.getClass()</code>
136: * is declared
137: * @param resourcename String the name of the file to search (only relative
138: * pathname to the path of the calling class)
139: * @param targetLocation target for copying
140: * @return true if resource was copied
141: */
142: public boolean copyIfNotExists(Object o, String resourcename,
143: String targetLocation) {
144: return copyIfNotExists(o.getClass(), resourcename,
145: targetLocation);
146: }
147:
148: public boolean copyIfNotExists(Class cl, String resourcename,
149: String targetLocation) {
150: URL resourceURL = cl.getResource(resourcename);
151:
152: Debug.out("Load Resource:" + resourcename + " of class " + cl);
153:
154: if (resourceURL == null && cl.getSuperclass() != null) {
155: return copyIfNotExists(cl.getSuperclass(), resourcename,
156: targetLocation);
157: } else if (resourceURL == null && cl.getSuperclass() == null) {
158: // error message Resource not found
159: System.out
160: .println("No resource " + resourcename + " found");
161: return false;
162: }
163:
164: // copying the resource to the target if targetfile
165: // does not exist yet
166: boolean result = false;
167: try {
168: File targetFile = new File(targetLocation);
169: if (!targetFile.exists()) {
170: result = true;
171: targetFile.createNewFile();
172: targetFile.deleteOnExit();
173:
174: InputStream sourceStream = resourceURL.openStream();
175: FileOutputStream targetStream = new FileOutputStream(
176: targetFile);
177:
178: int copyItem;
179: copyItem = sourceStream.read();
180: while (copyItem > -1) {
181: targetStream.write(copyItem);
182: copyItem = sourceStream.read();
183: }
184: sourceStream.close();
185: targetStream.close();
186: }
187: } catch (Exception e) {
188: System.err.println("KeYError: " + e);
189: return false;
190: }
191: return result;
192: }
193:
194: /** loads a resource and returns its URL
195: * @param cl the Class used to determine the resource
196: * @param resourcename the String that contains the name of the resource
197: * @return the URL of the resource
198: */
199: public URL getResourceFile(Class cl, String resourcename) {
200: URL resourceURL = cl.getResource(resourcename);
201: if (resourceURL == null && cl.getSuperclass() != null) {
202: return getResourceFile(cl.getSuperclass(), resourcename);
203: } else if (resourceURL == null && cl.getSuperclass() == null) {
204: return null;
205: }
206: return resourceURL;
207: }
208:
209: /** loads a resource and returns its URL
210: * @param o the Object used to determine the resource
211: * @param resourcename the String that contains the name of the resource
212: * @return the URL of the resource
213: */
214: public URL getResourceFile(Object o, String resourcename) {
215: return getResourceFile(o.getClass(), resourcename);
216: }
217: }
|