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.InputStream;
051: import java.io.FileInputStream;
052: import java.io.FileNotFoundException;
053: import java.io.PrintStream;
054: import java.io.PrintWriter;
055:
056: import java.util.Enumeration;
057: import java.util.Properties;
058:
059: import java.util.jar.JarFile;
060:
061: import java.util.zip.ZipFile;
062: import java.util.zip.ZipException;
063:
064: /**
065: *
066: * Load string resources from file.
067: * Resources should be stored in <code>name=value</code> format.
068: *
069: * @see org.netbeans.jemmy.BundleManager
070: *
071: * @author Alexandre Iline (alexandre.iline@sun.com)
072: */
073:
074: public class Bundle extends Object {
075:
076: private Properties resources;
077:
078: /**
079: * Bunble constructor.
080: */
081: public Bundle() {
082: resources = new Properties();
083: }
084:
085: /**
086: * Loads resources from an input stream.
087: *
088: * @param stream Stream to load resources from.
089: * @exception IOException
090: */
091: public void load(InputStream stream) throws IOException {
092: resources.load(stream);
093: }
094:
095: /**
096: * Loads resources from a simple file.
097: *
098: * @param fileName Name of the file to load resources from.
099: * @exception IOException
100: * @exception FileNotFoundException
101: */
102: public void loadFromFile(String fileName) throws IOException,
103: FileNotFoundException {
104: load(new FileInputStream(fileName));
105: }
106:
107: /**
108: * Loads resources from a file in a jar archive.
109: *
110: * @param fileName Name of the jar archive.
111: * @param entryName ?enryName? Name of the file to load resources from.
112: * @exception IOException
113: * @exception FileNotFoundException
114: */
115: public void loadFromJar(String fileName, String entryName)
116: throws IOException, FileNotFoundException {
117: JarFile jFile = new JarFile(fileName);
118: load(jFile.getInputStream(jFile.getEntry(entryName)));
119: }
120:
121: /**
122: * Loads resources from a file in a zip archive.
123: *
124: * @param fileName Name of the zip archive.
125: * @param entryName ?enryName? Name of the file to load resources from.
126: * @exception ZipException
127: * @exception IOException
128: * @exception FileNotFoundException
129: */
130: public void loadFromZip(String fileName, String entryName)
131: throws IOException, FileNotFoundException, ZipException {
132: ZipFile zFile = new ZipFile(fileName);
133: load(zFile.getInputStream(zFile.getEntry(entryName)));
134: }
135:
136: /**
137: * Prints bundle contents.
138: * @param writer Writer to print data in.
139: */
140: public void print(PrintWriter writer) {
141: Enumeration keys = resources.keys();
142: while (keys.hasMoreElements()) {
143: String key = (String) keys.nextElement();
144: writer.println(key + "=" + getResource(key));
145: }
146: }
147:
148: /**
149: * Prints bundle contents.
150: * @param stream Stream to print data in.
151: */
152: public void print(PrintStream stream) {
153: print(new PrintWriter(stream));
154: }
155:
156: /**
157: * Gets resource by key.
158: * @param key Resource key
159: * @return Resource value or null if resource was not found.
160: */
161: public String getResource(String key) {
162: return (resources.getProperty(key));
163: }
164:
165: }
|