KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > ExtendedInputSource


1 package com.icl.saxon;
2
3 import java.io.*;
4 import java.net.URL JavaDoc;
5 import java.net.MalformedURLException JavaDoc;
6 import org.xml.sax.*;
7 //import org.xml.sax.ext.LexicalHandler;
8
//import org.xml.sax.helpers.ParserAdapter;
9
//import org.w3c.dom.Document;
10

11 /**
12   * <p>This class allows a SAXON application to encapsulate information
13   * about an input source in a single object, which may include
14   * a public identifier, a system identifier, a byte stream (possibly
15   * with a specified encoding), a character stream, or a file.</p>
16   *
17   * <p>Most of the functionality is inherited directly from the SAX
18   * InputSource class; the additional functionality offered by
19   * ExtendedInputSource is to allow the input source to be specified as
20   * a File object.</p>
21   *
22   * @author Michael Kay (mhkay@iclway.co.uk)
23   */

24   
25 public class ExtendedInputSource extends org.xml.sax.InputSource JavaDoc {
26
27     private int estimatedLength = -1;
28
29     /**
30     * Default constructor
31     */

32
33     public ExtendedInputSource() {
34         super();
35     }
36
37     /**
38     * Create a new input source from a System ID
39     */

40
41     public ExtendedInputSource(String JavaDoc systemId) {
42         super(systemId);
43     }
44
45     /**
46     * Create a new input source from a character stream
47     */

48
49     public ExtendedInputSource(Reader reader) {
50         super(reader);
51     }
52
53     /**
54     * Create a new input source from a byte stream
55     */

56
57     public ExtendedInputSource(InputStream stream) {
58         super(stream);
59     }
60
61     /**
62     * Create a new input source from a File. Note that the directory
63     * in which the file occurs will be used as the base for resolving any
64     * system identifiers encountered within the XML document
65     *
66     * <p>Example of use:<BR>
67     * parser.parse(new ExtendedInputSource(new File("test.xml")))</p>
68     * @param file A File object identifying the XML input file
69     *
70     */

71
72     public ExtendedInputSource (File file) {
73         setFile(file);
74     }
75
76     /**
77     * Create an ExtendedInputSource from an existing InputSource
78     */

79
80     public ExtendedInputSource (InputSource in) {
81         setSystemId(in.getSystemId());
82         setPublicId(in.getPublicId());
83         setByteStream(in.getByteStream());
84         setEncoding(in.getEncoding());
85         setCharacterStream(in.getCharacterStream());
86     }
87
88     /**
89     * Specify that input is to come from a given File.
90     */

91
92     public void setFile(File file) {
93         super.setSystemId(createURL(file));
94     }
95
96     /**
97     * Set the estimated length of the file (advisory only)
98     */

99
100     public void setEstimatedLength(int length) {
101         estimatedLength = length;
102     }
103
104     /**
105     * Get the estimated length of the file (advisory only; -1 if not known)
106     */

107
108     public int getEstimatedLength() {
109         return estimatedLength;
110     }
111
112     /**
113     * Create a URL that refers to a given File
114     */

115
116   public static String JavaDoc createURL(File file)
117   {
118     String JavaDoc path = file.getAbsolutePath();
119     while (true) {
120         int special = path.indexOf('#');
121         if (special >= 0) {
122             path = path.substring(0, special) + "%23" + path.substring(special+1);
123         } else {
124             break;
125         }
126     }
127         
128         
129     // Following code is cribbed from MSXML
130
URL JavaDoc url = null;
131     try
132         {
133             url = new URL JavaDoc(path);
134         }
135     catch (MalformedURLException JavaDoc ex)
136         {
137             try
138             {
139                 // This is a bunch of weird code that is required to
140
// make a valid URL on the Windows platform, due
141
// to inconsistencies in what getAbsolutePath returns.
142
String JavaDoc fs = System.getProperty("file.separator");
143                 if (fs.length() == 1)
144                 {
145                     char sep = fs.charAt(0);
146                     if (sep != '/')
147                         path = path.replace(sep, '/');
148                     if (path.charAt(0) != '/')
149                         path = '/' + path;
150                 }
151                 path = "file://" + path;
152                 url = new URL JavaDoc(path);
153             }
154             catch (MalformedURLException JavaDoc e)
155             {
156                 return null;
157             }
158         }
159      return( url.toString() );
160   }
161
162
163 }
164
165 //
166
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
167
// you may not use this file except in compliance with the License. You may obtain a copy of the
168
// License at http://www.mozilla.org/MPL/
169
//
170
// Software distributed under the License is distributed on an "AS IS" basis,
171
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
172
// See the License for the specific language governing rights and limitations under the License.
173
//
174
// The Original Code is: all this file.
175
//
176
// The Initial Developer of the Original Code is
177
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
178
//
179
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
180
//
181
// Contributor(s): none.
182
//
183
Popular Tags