001: /*
002: * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * See end of file.
004: */
005: package com.hp.hpl.jena.shared.wg;
006:
007: import java.io.FileInputStream;
008: import java.io.FileOutputStream;
009: import java.io.IOException;
010: import java.io.InputStream;
011: import java.io.OutputStream;
012: import java.net.MalformedURLException;
013: import java.net.URL;
014: import java.util.zip.ZipFile;
015:
016: import com.hp.hpl.jena.iri.IRI;
017: import com.hp.hpl.jena.iri.IRIFactory;
018: import com.hp.hpl.jena.shared.JenaException;
019:
020: /**
021: * This class provides input streams that:
022: * 1: can be from a URL or from a zip
023: * 2: do not actually open until the first read
024: * @author Jeremy Carroll
025: *
026: *
027: */
028: public class TestInputStreamFactory {
029:
030: final IRIFactory iriFactory = IRIFactory.jenaImplementation();
031:
032: final private IRI base;
033: final private IRI mapBase;
034: final private ZipFile zip;
035: final private String property;
036: private String createMe = "error";
037:
038: /** @param baseDir A prefix of all URLs accessed through this factory.
039: * @param getBaseDir Replace the baseDir into getBaseDir before opening any URL.
040: */
041: public TestInputStreamFactory(IRI baseDir, IRI getBaseDir) {
042: base = baseDir;
043: mapBase = getBaseDir;
044: zip = null;
045: property = null;
046: }
047:
048: /** @param baseDir A prefix of all URLs accessed through this factory.
049: * @param zip To open a URL remove the baseDir from the URL and get the named file from the zip.
050: */
051: public TestInputStreamFactory(IRI baseDir, ZipFile zip) {
052: base = baseDir;
053: mapBase = null;
054: this .zip = zip;
055: property = null;
056: }
057:
058: /** @param baseDir A prefix of all URLs accessed through this factory.
059: * @param zip To open a URL remove the baseDir from the URL and get the named file from the zip.
060: */
061: public TestInputStreamFactory(IRI baseDir, String propDir) {
062: createMe = "new TestInputStreamFactory(URI.create(\""
063: + baseDir.toString() + "\"),\"" + propDir + "\")";
064: base = baseDir;
065: mapBase = null;
066: this .zip = null;
067: property = propDir.endsWith("/") ? propDir : propDir + "/";
068: }
069:
070: public IRI getBase() {
071: return base;
072: }
073:
074: /**
075: * A lazy open. The I/O only starts, and resources
076: * are only allocated on first read.
077: * @param str The URI to open
078: */
079: public InputStream open(String str) {
080: return open(iriFactory.create(str));
081: }
082:
083: /**
084: * opens the file, and really does it - not a delayed
085: * lazy opening.
086: * @param str the URI to open
087: * @return null on some failures
088: * @throws IOException
089: */
090: public InputStream fullyOpen(String str) throws IOException {
091: InputStream in = open(iriFactory.create(str));
092: if (in instanceof LazyInputStream
093: && !((LazyInputStream) in).connect())
094: return null;
095: return in;
096: }
097:
098: /**
099: * A lazy open. The I/O only starts, and resources
100: * are only allocated on first read.
101: * @param uri to be opened.
102: * @return the opened stream
103: */
104: public InputStream open(IRI uri) {
105: return (InputStream) open(uri, true);
106:
107: }
108:
109: public boolean savable() {
110: return mapBase != null
111: && mapBase.getScheme().equalsIgnoreCase("file");
112:
113: }
114:
115: public OutputStream openOutput(String str) {
116: OutputStream foo = (OutputStream) open(iriFactory.create(str),
117: false);
118: // System.out.println(foo.toString());
119: return foo;
120: }
121:
122: public String getCreationJava() {
123: return createMe;
124: }
125:
126: private Object open(IRI uri, boolean in) {
127:
128: IRI relative = uri.isAbsolute() ? base.relativize(uri,
129: IRI.CHILD) : uri;
130:
131: if (relative.isAbsolute())
132: throw new IllegalArgumentException(
133: "This TestInputStreamFactory only knows about '"
134: + base + "'.");
135:
136: String relPath = relative.toString();
137: if (relPath.length() - relPath.lastIndexOf('.') > 5) {
138: relPath = relPath + ".rdf";
139: relative = iriFactory.create(relPath);
140: }
141:
142: if (mapBase != null) {
143: //System.out.println("LazyURL: " + relative + " " + mapBase);
144: try {
145: URL url = mapBase.create(relative).toURL();
146: if (!in) {
147: if (url.getProtocol().equalsIgnoreCase("file"))
148: return new FileOutputStream(url.getFile());
149: throw new IllegalArgumentException(
150: "Can only save to file: scheme");
151: }
152: return new LazyURLInputStream(url);
153: } catch (MalformedURLException e) {
154: throw new JenaException(e);
155: } catch (IOException e) {
156: e.printStackTrace();
157: throw new JenaException(e);
158: }
159: }
160: if (!in)
161: throw new IllegalArgumentException("Can only save to URLs");
162:
163: if (zip != null)
164: return new LazyZipEntryInputStream(zip, relPath);
165: else
166: return TestInputStreamFactory.getInputStream(property
167: + relPath);
168:
169: }
170:
171: private static InputStream getInputStream(String prop) {
172: // System.err.println(prop);
173: ClassLoader loader = TestInputStreamFactory.class
174: .getClassLoader();
175: if (loader == null)
176: throw new SecurityException("Cannot access class loader");
177: InputStream in =
178: // loader.getResourceAsStream("com/hp/hpl/jena/rdf/arp/test/data/" + prop);
179: loader.getResourceAsStream("testing/" + prop);
180: // System.out.println(prop);
181: if (in == null) {
182: try {
183: in = new FileInputStream("testing/" + prop);
184: } catch (IOException e) {
185: }
186: if (in == null)
187: throw new IllegalArgumentException("Resource: " + prop
188: + " not found on class path.");
189: }
190:
191: return in;
192: }
193:
194: public IRI getMapBase() {
195: return mapBase;
196: }
197:
198: }
199:
200: /*
201: * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
202: * All rights reserved.
203: *
204: * Redistribution and use in source and binary forms, with or without
205: * modification, are permitted provided that the following conditions
206: * are met:
207: * 1. Redistributions of source code must retain the above copyright
208: * notice, this list of conditions and the following disclaimer.
209: * 2. Redistributions in binary form must reproduce the above copyright
210: * notice, this list of conditions and the following disclaimer in the
211: * documentation and/or other materials provided with the distribution.
212: * 3. The name of the author may not be used to endorse or promote products
213: * derived from this software without specific prior written permission.
214:
215: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
216: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
217: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
218: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
219: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
220: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
221: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
222: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
223: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
224: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
225: */
|