1 2 6 package com.hp.hpl.jena.shared.wg; 7 8 import java.io.FileInputStream ; 9 import java.io.IOException ; 10 import java.io.InputStream ; 11 import java.net.URL ; 12 import java.net.MalformedURLException ; 13 import java.io.*; 14 import java.util.zip.*; 15 16 import com.hp.hpl.jena.shared.*; 17 18 26 public class TestInputStreamFactory { 27 28 final private URI base; 29 final private URI mapBase; 30 final private ZipFile zip; 31 final private String property; 32 private String createMe = "error"; 33 34 37 public TestInputStreamFactory(URI baseDir, URI getBaseDir) { 38 base = baseDir; 39 mapBase = getBaseDir; 40 zip = null; 41 property = null; 42 } 43 46 public TestInputStreamFactory(URI baseDir, ZipFile zip) { 47 base = baseDir; 48 mapBase = null; 49 this.zip = zip; 50 property = null; 51 } 52 53 56 public TestInputStreamFactory(URI baseDir, String 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 74 public InputStream open(String str) { 75 return open(URI.create(str)); 76 } 77 84 public InputStream fullyOpen(String str) throws IOException { 85 InputStream in = open(URI.create(str)); 86 if (in instanceof LazyInputStream 87 && !((LazyInputStream) in).connect()) 88 return null; 89 return in; 90 } 91 97 public InputStream open(URI uri) { 98 return (InputStream ) open(uri, true); 99 100 } 101 public boolean savable() { 102 return mapBase != null && mapBase.getScheme().equalsIgnoreCase("file"); 103 104 } 105 public OutputStream openOutput(String str) { 106 OutputStream foo = (OutputStream) open(URI.create(str), false); 107 return foo; 109 } 110 111 public String getCreationJava() { 112 return createMe; 113 } 114 private Object open(URI uri, boolean in) { 115 URI relative = uri.isAbsolute() ? base.relativize(uri) : uri; 116 117 if (relative.isAbsolute()) 118 throw new IllegalArgumentException ( 119 "This TestInputStreamFactory only knows about '" + base + "'."); 120 121 String 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 try { 130 URL url = mapBase.resolve(relative).toURL(); 131 if (!in) { 132 if (url.getProtocol().equalsIgnoreCase("file")) 133 return new FileOutputStream(url.getFile()); 134 throw new IllegalArgumentException ("Can only save to file: scheme"); 135 } 136 return new LazyURLInputStream(url); 137 } catch (MalformedURLException e) { 138 throw new JenaException( e ); 139 } catch (IOException e) { 140 e.printStackTrace(); 141 throw new JenaException( e ); 142 } 143 } 144 if (!in) 145 throw new IllegalArgumentException ("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 getInputStream(String prop) { 156 ClassLoader loader = TestInputStreamFactory.class.getClassLoader(); 158 if (loader == null) 159 throw new SecurityException ("Cannot access class loader"); 160 InputStream in = 161 loader.getResourceAsStream("testing/" + prop); 163 if (in == null) { 165 try { 166 in = new FileInputStream ("testing/" + prop); 167 } catch (IOException e) { 168 } 169 if (in == null) 170 throw new IllegalArgumentException ( 171 "Resource: " + prop + " not found on class path."); 172 } 173 174 return in; 175 } 176 177 } 178 179 205 | Popular Tags |