KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > shared > wg > TestInputStreamFactory


1
2 /*
3  * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
4  * See end of file.
5  */

6 package com.hp.hpl.jena.shared.wg;
7
8 import java.io.FileInputStream JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.net.URL JavaDoc;
12 import java.net.MalformedURLException JavaDoc;
13 import java.io.*;
14 import java.util.zip.*;
15
16 import com.hp.hpl.jena.shared.*;
17
18 /**
19  * This class provides input streams that:
20  * 1: can be from a URL or from a zip
21  * 2: do not actually open until the first read
22  * @author Jeremy Carroll
23  *
24  *
25  */

26 public class TestInputStreamFactory {
27
28     final private URI base;
29     final private URI mapBase;
30     final private ZipFile zip;
31     final private String JavaDoc property;
32     private String JavaDoc createMe = "error";
33
34     /** @param baseDir A prefix of all URLs accessed through this factory.
35      * @param getBaseDir Replace the baseDir into getBaseDir before opening any URL.
36      */

37     public TestInputStreamFactory(URI baseDir, URI getBaseDir) {
38         base = baseDir;
39         mapBase = getBaseDir;
40         zip = null;
41         property = null;
42     }
43     /** @param baseDir A prefix of all URLs accessed through this factory.
44      * @param zip To open a URL remove the baseDir from the URL and get the named file from the zip.
45      */

46     public TestInputStreamFactory(URI baseDir, ZipFile zip) {
47         base = baseDir;
48         mapBase = null;
49         this.zip = zip;
50         property = null;
51     }
52
53     /** @param baseDir A prefix of all URLs accessed through this factory.
54      * @param zip To open a URL remove the baseDir from the URL and get the named file from the zip.
55      */

56     public TestInputStreamFactory(URI baseDir, String JavaDoc propDir) {
57         createMe = "new TestInputStreamFactory(URI.create(\""
58         +baseDir.toString()
59         +"\"),\""+propDir+"\")";
60         base = baseDir;
61         mapBase = null;
62         this.zip = null;
63         property = propDir.endsWith("/") ? propDir : propDir + "/";
64     }
65
66     public URI getBase() {
67         return base;
68     }
69     /**
70      * A lazy open. The I/O only starts, and resources
71      * are only allocated on first read.
72      * @param str The URI to open
73      */

74     public InputStream JavaDoc open(String JavaDoc str) {
75         return open(URI.create(str));
76     }
77     /**
78      * opens the file, and really does it - not a delayed
79      * lazy opening.
80      * @param str the URI to open
81      * @return null on some failures
82      * @throws IOException
83      */

84     public InputStream JavaDoc fullyOpen(String JavaDoc str) throws IOException JavaDoc {
85         InputStream JavaDoc in = open(URI.create(str));
86         if (in instanceof LazyInputStream
87                         && !((LazyInputStream) in).connect())
88                         return null;
89         return in;
90     }
91     /**
92      * A lazy open. The I/O only starts, and resources
93      * are only allocated on first read.
94      * @param uri to be opened.
95      * @return the opened stream
96      */

97     public InputStream JavaDoc open(URI uri) {
98         return (InputStream JavaDoc) open(uri, true);
99
100     }
101     public boolean savable() {
102         return mapBase != null && mapBase.getScheme().equalsIgnoreCase("file");
103
104     }
105     public OutputStream openOutput(String JavaDoc str) {
106         OutputStream foo = (OutputStream) open(URI.create(str), false);
107     // System.out.println(foo.toString());
108
return foo;
109     }
110
111     public String JavaDoc getCreationJava() {
112         return createMe;
113     }
114     private Object JavaDoc open(URI uri, boolean in) {
115         URI relative = uri.isAbsolute() ? base.relativize(uri) : uri;
116         
117         if (relative.isAbsolute())
118             throw new IllegalArgumentException JavaDoc(
119                 "This TestInputStreamFactory only knows about '" + base + "'.");
120         
121         String JavaDoc relPath = relative.toString();
122         if ( relPath.length() - relPath.lastIndexOf('.') > 5 ) {
123             relPath = relPath + ".rdf";
124             relative = URI.create(relPath);
125         }
126         
127         if (mapBase != null) {
128             //System.out.println("LazyURL: " + relative + " " + mapBase);
129
try {
130                 URL JavaDoc url = mapBase.resolve(relative).toURL();
131                 if (!in) {
132                     if (url.getProtocol().equalsIgnoreCase("file"))
133                         return new FileOutputStream(url.getFile());
134                     throw new IllegalArgumentException JavaDoc("Can only save to file: scheme");
135                 }
136                 return new LazyURLInputStream(url);
137             } catch (MalformedURLException JavaDoc e) {
138                 throw new JenaException( e );
139             } catch (IOException JavaDoc e) {
140                 e.printStackTrace();
141                 throw new JenaException( e );
142             }
143         }
144         if (!in)
145             throw new IllegalArgumentException JavaDoc("Can only save to URLs");
146
147
148         if (zip != null)
149             return new LazyZipEntryInputStream(zip,relPath );
150         else
151             return TestInputStreamFactory.getInputStream(property + relPath );
152
153     }
154
155     private static InputStream JavaDoc getInputStream(String JavaDoc prop) {
156         // System.err.println(prop);
157
ClassLoader JavaDoc loader = TestInputStreamFactory.class.getClassLoader();
158         if (loader == null)
159             throw new SecurityException JavaDoc("Cannot access class loader");
160         InputStream JavaDoc in =
161             // loader.getResourceAsStream("com/hp/hpl/jena/rdf/arp/test/data/" + prop);
162
loader.getResourceAsStream("testing/" + prop);
163         // System.out.println(prop);
164
if (in == null) {
165             try {
166                 in = new FileInputStream JavaDoc("testing/" + prop);
167             } catch (IOException JavaDoc e) {
168             }
169             if (in == null)
170                 throw new IllegalArgumentException JavaDoc(
171                     "Resource: " + prop + " not found on class path.");
172         }
173     
174         return in;
175     }
176
177 }
178
179 /*
180  * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
181  * All rights reserved.
182  *
183  * Redistribution and use in source and binary forms, with or without
184  * modification, are permitted provided that the following conditions
185  * are met:
186  * 1. Redistributions of source code must retain the above copyright
187  * notice, this list of conditions and the following disclaimer.
188  * 2. Redistributions in binary form must reproduce the above copyright
189  * notice, this list of conditions and the following disclaimer in the
190  * documentation and/or other materials provided with the distribution.
191  * 3. The name of the author may not be used to endorse or promote products
192  * derived from this software without specific prior written permission.
193
194  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
195  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
196  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
197  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
198  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
199  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
200  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
201  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
202  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
203  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
204  */

205
Popular Tags