1/* 2 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23package org.netbeans.jemmy; 24 25import java.io.FileInputStream; 26import java.io.FileNotFoundException; 27import java.io.IOException; 28import java.io.InputStream; 29import java.io.PrintStream; 30import java.io.PrintWriter; 31import java.util.Enumeration; 32import java.util.Properties; 33import java.util.jar.JarFile; 34import java.util.zip.ZipException; 35import java.util.zip.ZipFile; 36 37/** 38 * 39 * Load string resources from file. Resources should be stored in 40 * {@code name=value} format. 41 * 42 * @see org.netbeans.jemmy.BundleManager 43 * 44 * @author Alexandre Iline (alexandre.iline@oracle.com) 45 */ 46public class Bundle extends Object { 47 48 private Properties resources; 49 50 /** 51 * Bunble constructor. 52 */ 53 public Bundle() { 54 resources = new Properties(); 55 } 56 57 /** 58 * Loads resources from an input stream. 59 * 60 * @param stream Stream to load resources from. 61 * @exception IOException 62 */ 63 public void load(InputStream stream) 64 throws IOException { 65 resources.load(stream); 66 } 67 68 /** 69 * Loads resources from a simple file. 70 * 71 * @param fileName Name of the file to load resources from. 72 * @exception IOException 73 * @exception FileNotFoundException 74 */ 75 public void loadFromFile(String fileName) 76 throws IOException, FileNotFoundException { 77 try (FileInputStream fileInputStream = new FileInputStream(fileName)) { 78 load(fileInputStream); 79 } 80 } 81 82 /** 83 * Loads resources from a file in a jar archive. 84 * 85 * @param fileName Name of the jar archive. 86 * @param entryName ?enryName? Name of the file to load resources from. 87 * @exception IOException 88 * @exception FileNotFoundException 89 */ 90 public void loadFromJar(String fileName, String entryName) 91 throws IOException, FileNotFoundException { 92 try (JarFile jFile = new JarFile(fileName); 93 InputStream inputStream = jFile.getInputStream(jFile.getEntry(entryName))) { 94 load(inputStream); 95 } 96 } 97 98 /** 99 * Loads resources from a file in a zip archive. 100 * 101 * @param fileName Name of the zip archive. 102 * @param entryName ?enryName? Name of the file to load resources from. 103 * @exception ZipException 104 * @exception IOException 105 * @exception FileNotFoundException 106 */ 107 public void loadFromZip(String fileName, String entryName) 108 throws IOException, FileNotFoundException, ZipException { 109 try (ZipFile zFile = new ZipFile(fileName); 110 InputStream inputStream = zFile.getInputStream(zFile.getEntry(entryName))) { 111 load(inputStream); 112 } 113 } 114 115 /** 116 * Prints bundle contents. 117 * 118 * @param writer Writer to print data in. 119 */ 120 public void print(PrintWriter writer) { 121 Enumeration<Object> keys = resources.keys(); 122 while (keys.hasMoreElements()) { 123 String key = (String) keys.nextElement(); 124 writer.println(key + "=" + getResource(key)); 125 } 126 } 127 128 /** 129 * Prints bundle contents. 130 * 131 * @param stream Stream to print data in. 132 */ 133 public void print(PrintStream stream) { 134 print(new PrintWriter(stream)); 135 } 136 137 /** 138 * Gets resource by key. 139 * 140 * @param key Resource key 141 * @return Resource value or null if resource was not found. 142 */ 143 public String getResource(String key) { 144 return resources.getProperty(key); 145 } 146 147} 148