001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.rewriter.util;
006:
007: import com.sun.portal.rewriter.util.xml.Document;
008:
009: import java.io.BufferedReader;
010: import java.io.ByteArrayOutputStream;
011: import java.io.FileInputStream;
012: import java.io.FileNotFoundException;
013: import java.io.FileOutputStream;
014: import java.io.IOException;
015: import java.io.InputStream;
016: import java.io.InputStreamReader;
017: import java.io.OutputStream;
018: import java.io.Reader;
019: import java.io.UnsupportedEncodingException;
020: import java.net.URL;
021:
022: /**
023: * Utilitity class to read resource from a filesystem or from jar file
024: * (useses java classloader to achive this).
025: *
026: * @version 1.0 12/15/2001
027: * @author Raja Nagendra Kumar, Nagendra.Raja@sun.com
028: */
029: public final class Resource {
030: private static final String[] messages = { "Unable to Read inFull\n", //0
031: };
032:
033: public static String read(final String aFileName) {
034: return read(aFileName, (Class) null);
035: }//read()
036:
037: public static String read(final String aFileName, final Class aClass) {
038: return read(aFileName, aClass, null);
039: }//read()
040:
041: public static String read(final String aFileName,
042: final String aCharSetID) {
043: return read(aFileName, null, aCharSetID);
044: }//read()
045:
046: public static String read(final String aFileName,
047: final Class aClass, final String aCharSetID) {
048: try {
049: String data = "";
050: InputStream in = getInputStream(aClass, aFileName);
051:
052: if (aCharSetID == null) {
053: data = Resource.read(new InputStreamReader(in));
054: } else {
055: data = Resource.read(new InputStreamReader(in,
056: aCharSetID));
057: }
058:
059: in.close();
060: return data;
061: } catch (Exception e) {
062: return "";
063: }//try/catch
064: }//read()
065:
066: public static String read(final Reader aReader) {
067: String lResult = "";
068: try {
069: BufferedReader buffReader;
070: if (aReader instanceof BufferedReader) {
071: buffReader = (BufferedReader) aReader;
072: } else {
073: buffReader = new BufferedReader(aReader);
074: }
075:
076: lResult = read(buffReader);
077: aReader.close();
078: } catch (IOException e) {
079: Debug.warning(messages[0], e);
080: }//try/catch
081: return lResult;
082: }//read()
083:
084: private static final String read(final BufferedReader aReader)
085: throws IOException {
086: StringBuffer sb = new StringBuffer();
087: char[] data = new char[2048];
088: int count = 0;
089:
090: while ((count = aReader.read(data)) != -1) {
091: sb.append(data, 0, count);
092: }//while loop
093:
094: aReader.close();
095: return sb.toString();
096: }//readRest()
097:
098: public static boolean save(final String aFileName,
099: final byte[] aContentArray) {
100: try {
101: OutputStream fOut = new FileOutputStream(aFileName);
102: fOut.write(aContentArray);
103: fOut.flush();
104: fOut.close();
105:
106: return true;
107: } catch (Exception e) {
108: //Debug.warning( "Unable to Save File: " + aFileName, e );
109: Debug.warning("PSRW_CSPR_0029", e);
110: return false;
111: }//try/catch
112: }//save()
113:
114: public static boolean save(final String aFileName,
115: final String aContent) {
116: return save(aFileName, aContent.getBytes());
117: }//save()
118:
119: public static boolean saveXML(final String aFileName,
120: final String aContent) {
121: try {
122: return save(aFileName, aContent.getBytes(Document
123: .parseEncoding(aContent)));
124: } catch (Exception e) {
125: //Debug.warning( "Unable to Save XML File: " + aFileName, e );
126: Debug.warning("PSRW_CSPR_0030", e);
127: return false;
128: }
129: }//saveXML()
130:
131: public static String readXML(final String aResourceLocation) {
132: return readXML(aResourceLocation, null);
133: }//readXML()
134:
135: /**
136: * i18n way - Create the Dom Object and Get the Reader from the dom
137: */
138: public static String readXML(final String aResourceLocation,
139: final Class aClass) {
140: final byte[] byteData = readBytes(aResourceLocation, aClass);
141: if (byteData.length != 0) {
142: try {
143: return new String(byteData, Document
144: .parseEncoding(byteData));
145: } catch (UnsupportedEncodingException e) {
146: //Debug.warning( "encoding issue..", e );
147: Debug.warning("PSRW_CSPR_0031", e);
148: }
149: }
150:
151: return new String(byteData);
152: }//readXML()
153:
154: public static byte[] readBytes(final String aResourceLocation) {
155: return readBytes(aResourceLocation, null);
156: }//readBytes()
157:
158: public static byte[] readBytes(final String aResourceLocation,
159: final Class aClass) {
160: try {
161: InputStream in = getInputStream(aClass, aResourceLocation);
162: byte[] data = new byte[2048];
163:
164: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
165:
166: int count = 0;
167: while ((count = in.read(data)) != -1) {
168: bOut.write(data, 0, count);
169: }//while loop
170:
171: data = bOut.toByteArray();
172: bOut.close();
173: in.close();
174: return data;
175: } catch (Exception e) {
176: return new byte[0];
177: }
178:
179: }//readBytes()
180:
181: private static InputStream getInputStream(Class aClass,
182: final String aResourceLocation) throws IOException {
183: if (aClass == null) {
184: aClass = Resource.class;
185: }
186:
187: InputStream in = aClass.getResourceAsStream(aResourceLocation);
188:
189: //may be absoulte file path is given
190: if (in == null) {
191: try {
192: //works well if the user has given absolute path
193: in = new FileInputStream(aResourceLocation);
194: } catch (FileNotFoundException e) {
195: //works well if the user has given the relative path to the
196: //location of class file
197: String directoryURL = aClass.getProtectionDomain()
198: .getCodeSource().getLocation().toString();
199: String fileURL = directoryURL + aResourceLocation;
200: URL url = new URL(fileURL);
201: in = url.openStream();
202: }
203: }//if
204:
205: if (in != null) {
206: return in;
207: } else {
208: /*Debug.warning( "Unable to read the resource : " +
209: aResourceLocation + " \nUsing ClassLoader: " +
210: aClass.getName() );*/
211: Object[] param0 = { aResourceLocation };
212: Object[] param1 = { aClass.getName() };
213: Debug.warning("PSRW_CSPR_0032", param0);
214: Debug.warning("PSRW_CSPR_0033", param1);
215: throw new FileNotFoundException("Resource Name: "
216: + aResourceLocation);
217: }
218: }//getInputStream()
219:
220: public static void main(String[] args) {
221: System.out.println(Resource.read(args[0]));
222: }//main()
223: }//class Read
|