Source Code Cross Referenced for Resource.java in  » Portal » Open-Portal » com » sun » portal » rewriter » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Portal » Open Portal » com.sun.portal.rewriter.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.