001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s): Alexandre Iline.
025: *
026: * The Original Software is the Jemmy library.
027: * The Initial Developer of the Original Software is Alexandre Iline.
028: * All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: *
041: *
042: *
043: * $Id$ $Revision$ $Date$
044: *
045: */
046:
047: package org.netbeans.jemmy;
048:
049: import java.io.IOException;
050: import java.io.FileNotFoundException;
051: import java.io.PrintStream;
052: import java.io.PrintWriter;
053:
054: import java.util.Enumeration;
055: import java.util.Hashtable;
056: import java.util.Properties;
057:
058: import java.util.zip.ZipException;
059:
060: /**
061: *
062: * Provides functionality to work with a bunch of resource files. <BR>
063: *
064: * @see org.netbeans.jemmy.Bundle
065: *
066: * @author Alexandre Iline (alexandre.iline@sun.com)
067: */
068:
069: public class BundleManager extends Object {
070:
071: private Hashtable bundles;
072:
073: /**
074: * Bundle manager constructor.
075: */
076: public BundleManager() {
077: bundles = new Hashtable();
078: try {
079: load();
080: } catch (IOException e) {
081: }
082: }
083:
084: /**
085: * Adds a Bundle to the managed collection of resource files.
086: * @param bundle Bundle object
087: * @param ID Symbolic bundle id
088: * @return First parameter or null if bundle with ID already exists.
089: * @see org.netbeans.jemmy.Bundle
090: */
091: public Bundle addBundle(Bundle bundle, String ID) {
092: if (getBundle(ID) != null) {
093: return (null);
094: } else {
095: bundles.put(ID, bundle);
096: return (bundle);
097: }
098: }
099:
100: /**
101: * Removes a Bundle from the managed collection of resource files.
102: * @param ID Symbolic bundle id
103: * @return Removed bundle or null if no bundle ID is.
104: */
105: public Bundle removeBundle(String ID) {
106: Bundle value = getBundle(ID);
107: bundles.remove(ID);
108: return (value);
109: }
110:
111: /**
112: * Returns a Bundle given it's symbolic ID.
113: * @param ID Symbolic bundle ID
114: * @return the Bundle. A null reference is returned if
115: * no bundle with the symbolic ID was found.
116: */
117: public Bundle getBundle(String ID) {
118: return ((Bundle) bundles.get(ID));
119: }
120:
121: /**
122: * Create a new Bundle, load resources from a simple text file,
123: * and add the bundle.
124: * Load resources from a text file to a new Bundle object. The new
125: * Bundle is added to the collection of objects managed by this
126: * <code>BundleManager</code>.
127: *
128: * @param fileName Name of a file to load resources from.
129: * @param ID Symbolic bundle ID used to identify the new bundle used
130: * to manage the resources from the file.
131: * @return a newly created bundle.
132: * @exception IOException
133: * @exception FileNotFoundException
134: */
135: public Bundle loadBundleFromFile(String fileName, String ID)
136: throws IOException, FileNotFoundException {
137: if (getBundle(ID) != null) {
138: return (null);
139: }
140: Bundle bundle = new Bundle();
141: bundle.loadFromFile(fileName);
142: return (addBundle(bundle, ID));
143: }
144:
145: /**
146: * Loads resources from simple text file pointed by jemmy.resources system property.
147: * The resources are loaded into the Bundle with ID "".
148: * Does not do anything if jemmy.resources has not been set or is empty.
149: *
150: * @return a newly created bundle.
151: * @exception IOException
152: * @exception FileNotFoundException
153: */
154: public Bundle load() throws IOException, FileNotFoundException {
155: if (System.getProperty("jemmy.resources") != null
156: && !System.getProperty("jemmy.resources").equals("")) {
157: return (loadBundleFromFile(System
158: .getProperty("jemmy.resources"), ""));
159: }
160: return (null);
161: }
162:
163: /**
164: * Loads resources from file in jar archive into new Bundle object and adds it.
165: *
166: * @param fileName Name of jar file.
167: * @param entryName ?enryName? Name of file to load resources from.
168: * @param ID Symbolic bundle id
169: * @return a newly created bundle.
170: * @exception IOException
171: * @exception FileNotFoundException
172: */
173: public Bundle loadBundleFromJar(String fileName, String entryName,
174: String ID) throws IOException, FileNotFoundException {
175: if (getBundle(ID) != null) {
176: return (null);
177: }
178: Bundle bundle = new Bundle();
179: bundle.loadFromJar(fileName, entryName);
180: return (addBundle(bundle, ID));
181: }
182:
183: /**
184: * Loads resources from file in zip archive into new Bundle object and adds it.
185: *
186: * @param fileName Name of jar file.
187: * @param entryName ?enryName? Name of file to load resources from.
188: * @param ID Symbolic bundle id
189: * @return a newly created bundle.
190: * @exception ZipException
191: * @exception IOException
192: * @exception FileNotFoundException
193: */
194: public Bundle loadBundleFromZip(String fileName, String entryName,
195: String ID) throws IOException, FileNotFoundException,
196: ZipException {
197: if (getBundle(ID) != null) {
198: return (null);
199: }
200: Bundle bundle = new Bundle();
201: bundle.loadFromZip(fileName, entryName);
202: return (addBundle(bundle, ID));
203: }
204:
205: /**
206: * Prints bundles contents.
207: * @param writer Writer to print data in.
208: */
209: public void print(PrintWriter writer) {
210: Enumeration keys = bundles.keys();
211: Bundle bundle;
212: String key;
213: while (keys.hasMoreElements()) {
214: key = (String) keys.nextElement();
215: writer.println("\"" + key + "\" bundle contents");
216: bundle = getBundle(key);
217: bundle.print(writer);
218: }
219: }
220:
221: /**
222: * Prints bundles contents.
223: * @param stream Stream to print data in.
224: */
225: public void print(PrintStream stream) {
226: print(new PrintWriter(stream));
227: }
228:
229: /**
230: * Returns resource from ID bundle.
231: * @param bundleID Bundle ID.
232: * @param key Resource key.
233: * @return the resource value. If the bundle ID does not exist
234: * if the resource with the given key cannot be found, a null
235: * reference is returned.
236: */
237: public String getResource(String bundleID, String key) {
238: Bundle bundle = getBundle(bundleID);
239: if (bundle != null) {
240: return (bundle.getResource(key));
241: }
242: return (null);
243: }
244:
245: /**
246: * Searches for a resource in all the managed Bundles.
247: * @param key Resource key.
248: * @return first resource value found that is indexed by the given key.
249: * If no resource is found, return a null reference.
250: */
251: public String getResource(String key) {
252: Enumeration data = bundles.elements();
253: String value;
254: while (data.hasMoreElements()) {
255: value = ((Bundle) data.nextElement()).getResource(key);
256: if (value != null) {
257: return (value);
258: }
259: }
260: return (null);
261: }
262:
263: /**
264: * Counts the number of resource occurences in all the managed Bundles.
265: * @param key Resource key
266: * @return the number of resource occurences with the given key among
267: * all the Bundles managed by this BundleManager.
268: */
269: public int calculateResources(String key) {
270: Enumeration data = bundles.elements();
271: int count = 0;
272: while (data.hasMoreElements()) {
273: if (((Bundle) data.nextElement()).getResource(key) != null) {
274: count++;
275: ;
276: }
277: }
278: return (count);
279: }
280:
281: /**
282: * Creates a shallow copy of this BundleManager.
283: * Does not copy bundles, only their references.
284: * @return a copy of this BundleManager.
285: */
286: public BundleManager cloneThis() {
287: BundleManager result = new BundleManager();
288: Enumeration keys = bundles.keys();
289: Enumeration elements = bundles.elements();
290: while (keys.hasMoreElements()) {
291: result.bundles.put(keys.nextElement(), elements
292: .nextElement());
293: }
294: return (result);
295: }
296: }
|