KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > io > InputSourceOps


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: InputSourceOps.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.io;
25
26 import java.io.BufferedReader JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.InputStreamReader JavaDoc;
31 import java.io.Reader JavaDoc;
32 import java.net.URL JavaDoc;
33
34 import org.xml.sax.InputSource JavaDoc;
35
36 /**
37  * Various operations on InputSources.
38  */

39 public final class InputSourceOps {
40     /**
41      * XML document header prefix.
42      */

43     private static String JavaDoc XML_DOCUMENT_HEADER_PREFIX = "<?xml";
44
45     /**
46      * Disallow instantiation.
47      */

48     private InputSourceOps() {
49     }
50
51     /**
52      * Does a systemId have a URI scheme?
53      */

54     private static boolean hasScheme(String JavaDoc systemId) {
55         // Check for scheme, which must be before `/'. Also handle names with
56
// DOS drive letters (`D:', so 1-character schemes are not allows.
57
int colonIdx = systemId.indexOf(':');
58         return ((colonIdx >= 2) && (colonIdx < systemId.indexOf('/')));
59     }
60
61     /**
62      * Open a bytestream given a system id.
63      */

64    public static InputStream JavaDoc openSystemId(String JavaDoc systemId) throws IOException JavaDoc {
65         if (systemId == null) {
66             throw new IllegalArgumentException JavaDoc("no open stream or system id specified in InputSource");
67         }
68
69         if (hasScheme(systemId)) {
70             return new URL JavaDoc(systemId).openStream();
71         } else {
72             return new FileInputStream JavaDoc(systemId);
73         }
74     }
75
76     /**
77      * Open a character input stream for an input source if one is not
78      * already open.
79      *
80      * @param inputSource Specification of the document to open.
81      * @return A pointer to the character stream.
82      */

83     public static Reader JavaDoc open(InputSource JavaDoc inputSource) throws IOException JavaDoc {
84         if (inputSource.getCharacterStream() != null) {
85             return inputSource.getCharacterStream();
86         }
87         
88         InputStream JavaDoc in = inputSource.getByteStream();
89         if (in == null) {
90             in = openSystemId(inputSource.getSystemId());
91         }
92         String JavaDoc encoding = inputSource.getEncoding();
93         if (encoding != null) {
94             return new BufferedReader JavaDoc(new InputStreamReader JavaDoc(in, encoding));
95         } else {
96             return new BufferedReader JavaDoc(new InputStreamReader JavaDoc(in));
97         }
98     }
99
100     /**
101      * Determine if an input source has an open byte or character stream.
102      */

103     public static boolean isOpen(InputSource JavaDoc input) {
104         return (input.getCharacterStream() != null)
105             || (input.getByteStream() != null);
106     }
107
108     /**
109      * Close an InputSource, if open.
110      */

111     public static void close(InputSource JavaDoc input) throws IOException JavaDoc {
112         if (input.getCharacterStream() != null) {
113             input.getCharacterStream().close();
114         }
115         if (input.getByteStream() != null) {
116             input.getByteStream().close();
117         }
118     }
119
120     /**
121      * Close a byte stream returned by <code>open()</code>, only if it was
122      * actually opened by open.
123      *
124      * @param inputSource Specification of the document that was opened.
125      * @param reader The character stream returned by open.
126      */

127     public static void closeIfOpened(InputSource JavaDoc inputSource,
128                                      Reader JavaDoc reader) throws IOException JavaDoc {
129         if ((inputSource.getCharacterStream() == null)
130             && (inputSource.getByteStream() == null)) {
131             reader.close();
132         }
133     }
134
135     /**
136      * Get a description of an input source.
137      */

138     public static String JavaDoc getName(InputSource JavaDoc input) {
139         if (input == null) {
140             return "<null input source>";
141         }
142
143         String JavaDoc name = null;
144         if (input.getSystemId() != null) {
145             name = input.getSystemId();
146         } else if (input.getPublicId() != null) {
147             name = input.getPublicId();
148         }
149
150         // If byte or character stream is open, indicate that with the name.
151
if (input.getByteStream() != null) {
152             if (name == null) {
153                 return "<unnamed byte stream>";
154             } else {
155                 return name +"<byte stream>";
156             }
157         }
158         if (input.getCharacterStream() != null) {
159             if (name == null) {
160                 return "<unnamed character stream>";
161             } else {
162                 return name +"<character stream>";
163             }
164         }
165         if (name != null) {
166             return name;
167         } else {
168             return "<empty input source>";
169         }
170     }
171
172     /**
173      * Determine an document reader is attached to an XML document
174      * by checking the first few bytes. Reader must be positioned
175      * at the start.
176      *
177      * @param reader Character stream.
178      */

179     public static boolean isXMLDocument(Reader JavaDoc reader) throws IOException JavaDoc {
180         char[] buf = new char[XML_DOCUMENT_HEADER_PREFIX.length()];
181         reader.mark(XML_DOCUMENT_HEADER_PREFIX.length()+1);
182         if (reader.read(buf) != buf.length) {
183             return false;
184         }
185         return String.copyValueOf(buf).equals(XML_DOCUMENT_HEADER_PREFIX);
186     }
187
188     /**
189      * Determine an input source points to an XML document.
190      *
191      * @param inputSource Specification of the document to examine.
192      */

193     public static boolean isXMLDocument(InputSource JavaDoc inputSource) throws IOException JavaDoc {
194         Reader JavaDoc reader = open(inputSource);
195         try {
196             return isXMLDocument(reader);
197         } finally {
198             closeIfOpened(inputSource, reader);
199         }
200     }
201 }
202
Popular Tags