001: /*
002: * $Id: JGraphEditorResources.java,v 1.2 2005/08/06 10:21:12 gaudenz Exp $
003: * Copyright (c) 2001-2005, Gaudenz Alder
004: *
005: * All rights reserved.
006: *
007: * See LICENSE file for license details. If you are unable to locate
008: * this file please contact info (at) jgraph (dot) com.
009: */
010: package com.jgraph.editor;
011:
012: import java.io.BufferedInputStream;
013: import java.io.BufferedOutputStream;
014: import java.io.ByteArrayOutputStream;
015: import java.io.FileInputStream;
016: import java.io.FileNotFoundException;
017: import java.io.FileOutputStream;
018: import java.io.IOException;
019: import java.io.InputStream;
020: import java.io.OutputStream;
021: import java.net.MalformedURLException;
022: import java.net.URL;
023: import java.util.Iterator;
024: import java.util.LinkedList;
025: import java.util.MissingResourceException;
026: import java.util.PropertyResourceBundle;
027: import java.util.ResourceBundle;
028:
029: import javax.imageio.ImageIO;
030: import javax.swing.ImageIcon;
031:
032: import com.jgraph.JGraphEditor;
033:
034: /**
035: * Manages a set of resource bundles to retrieve keys and reads images and
036: * streams from the classpath. In order to find a key, the class searches all
037: * bundles in inverse insertion order (last inserted first).
038: */
039: public class JGraphEditorResources {
040:
041: /**
042: * Ordered list of the inserted resource bundles.
043: */
044: protected static LinkedList bundles = new LinkedList();
045:
046: /**
047: * Adds a resource bundle.
048: *
049: * @param basename
050: * The basename of the resource bundle to add.
051: */
052: public static void addBundle(String basename) {
053: bundles.addFirst(PropertyResourceBundle.getBundle(basename));
054: }
055:
056: /**
057: * Adds an array of resource bundles using {@link #addBundle(String)}.
058: *
059: * @param basenames
060: * The array of basenames to add.
061: */
062: public static void addBundles(String[] basenames) {
063: if (basenames != null)
064: for (int i = 0; i < basenames.length; i++)
065: addBundle(basenames[i]);
066: }
067:
068: /**
069: * Returns the value for <code>key</code> by searching the resource
070: * bundles in inverse order or <code>null</code> if no value can be found
071: * for <code>key</code>.
072: *
073: * @param key
074: * The key to be searched for.
075: * @return Returns the value for <code>key</code> or <code>null</code>.
076: *
077: * @see ResourceBundle#getString(java.lang.String)
078: */
079: public static String getString(String key) {
080: Iterator it = bundles.iterator();
081: while (it.hasNext()) {
082: try {
083: return ((PropertyResourceBundle) it.next())
084: .getString(key);
085: } catch (MissingResourceException mrex) {
086: // continue
087: }
088: }
089: return null;
090: }
091:
092: /**
093: * Returns the value for <code>key</code> replacing every occurrence of
094: * <code>{0}</code> with <code>param</code>. This is a shortcut method
095: * for values with only one placeholder.
096: *
097: * @return Returns the parametrized value for <code>key</code>.
098: *
099: * @see #getString(String, Object[])
100: */
101: public static String getString(String key, Object param) {
102: return getString(key, new Object[] { param });
103: }
104:
105: /**
106: * Returns the value for <code>key</code> replacing every occurrence of
107: * <code>{i}</code> with <code>params[i]</code> where i is an integer (i =
108: * 0, 1, ..., n).
109: *
110: * @return Returns the parametrized value for <code>key</code>.
111: */
112: public static String getString(String key, Object[] params) {
113: String base = getString(key);
114: if (base != null && params != null) {
115:
116: // Allocates space for the result string
117: int len = base.length();
118: for (int i = 0; i < params.length; i++)
119: len += String.valueOf(params[i]).length();
120: StringBuffer ret = new StringBuffer(len);
121:
122: // Parses the resource string and replaces placeholders
123: StringBuffer indexString = null;
124: for (int i = 0; i < base.length(); i++) {
125: char c = base.charAt(i);
126:
127: // Starts reading the value index
128: if (c == '{') {
129:
130: // Assumes an average index length of 1
131: indexString = new StringBuffer(1);
132:
133: } else if (indexString != null && c == '}') {
134:
135: // Finishes reading and appends the value
136: // Issues a warning if the index is wrong
137: // and warnings are switched on.
138: int index = Integer
139: .parseInt(indexString.toString());
140: if (index >= 0 && index < params.length)
141: ret.append(params[index]);
142: indexString = null;
143:
144: } else if (indexString != null) {
145:
146: // Reads the value index
147: indexString.append(c);
148:
149: } else {
150: ret.append(c);
151: }
152: }
153: return ret.toString();
154: }
155: return base;
156: }
157:
158: /**
159: * Returns the specified file as an image or <code>null</code> if there
160: * was an exception. Exceptions are silently ignored by this method. This
161: * implementation first tries to load the specified filename from the
162: * classpath. If the file cannot be found, it tries loading it as external
163: * file or URL.
164: *
165: * @param uri
166: * The URI to load the image from.
167: * @return Returns the image for <code>filename</code>.
168: *
169: * @see ImageIO#read(java.net.URL)
170: * @see Class#getResource(java.lang.String)
171: */
172: public static ImageIcon getImage(String uri) {
173: try {
174: return new ImageIcon(ImageIO
175: .read(JGraphEditorResources.class.getResource(uri)));
176: } catch (Exception e) {
177: try {
178: return new ImageIcon(ImageIO
179: .read(new BufferedInputStream(JGraphEditor
180: .isURL(uri) ? new URL(uri).openStream()
181: : new FileInputStream(uri))));
182: } catch (Exception e1) {
183: // ignore
184: }
185: }
186: return null;
187: }
188:
189: /**
190: * Returns the specified file as a buffered input stream or
191: * <code>null</code> if there was an exception. Exceptions are silently
192: * ignored by this method. This implementation first tries to load the
193: * specified filename from the classpath. If the file cannot be found, it
194: * tries loading it as external file or URL.
195: *
196: * @param uri
197: * The URI to return the input stream for.
198: * @throws IOException
199: * If the URI can not be read.
200: * @throws FileNotFoundException
201: * If the URI can not be found.
202: * @throws MalformedURLException
203: * If the URI is an invalid URL.
204: * @return Returns the input stream for <code>filename</code>.
205: *
206: * @see Class#getResource(java.lang.String)
207: * @see URL#openStream()
208: * @see BufferedInputStream
209: */
210: public static InputStream getInputStream(String uri)
211: throws MalformedURLException, FileNotFoundException,
212: IOException {
213: URL url = JGraphEditorResources.class.getResource(uri);
214: try {
215: return new BufferedInputStream(url.openStream());
216: } catch (Exception e) {
217: return new BufferedInputStream(
218: JGraphEditor.isURL(uri) ? new URL(uri).openStream()
219: : new FileInputStream(uri));
220: }
221: }
222:
223: /**
224: * Returns a buffered output stream for the specified URI or null if there
225: * was an exception.
226: *
227: * @param uri
228: * The URI to return the output stream for.
229: * @return Returns an output stream for the specified URI.
230: *
231: * @throws FileNotFoundException
232: * If the specified URI can not be found.
233: */
234: public static OutputStream getOutputStream(String uri)
235: throws FileNotFoundException {
236: OutputStream out = null;
237: if (JGraphEditor.isURL(uri))
238: out = new ByteArrayOutputStream();
239: else
240: out = new BufferedOutputStream(new FileOutputStream(uri));
241: return out;
242: }
243:
244: /**
245: * Returns the bundles.
246: *
247: * @return Returns the bundles.
248: */
249: public static LinkedList getBundles() {
250: return bundles;
251: }
252:
253: /**
254: * Sets the bundles.
255: *
256: * @param bundles
257: * The bundles to set.
258: */
259: public static void setBundles(LinkedList bundles) {
260: JGraphEditorResources.bundles = bundles;
261: }
262:
263: }
|