1 57 58 package org.apache.wsif.compiler.util; 59 60 import java.io.File ; 61 import java.io.FileNotFoundException ; 62 import java.io.FileOutputStream ; 63 import java.io.IOException ; 64 import java.io.InputStream ; 65 import java.io.OutputStream ; 66 import java.net.URL ; 67 import java.net.MalformedURLException ; 68 69 import org.apache.wsif.WSIFException; 70 import org.apache.wsif.compiler.schema.tools.Conventions; 71 72 75 public class StreamFactory { 76 77 public OutputStream getOutputStream( 78 String root, 79 String name, 80 boolean overwrite) 81 throws WSIFException { 82 boolean verbose = Conventions.getVerbose(); 83 84 if (root != null) { 85 File directory = new File (root); 86 87 if (!directory.exists()) { 88 if (!directory.mkdirs()) { 89 throw new WSIFException("Failed to create directory '" + root + "'."); 90 } else if (verbose) { 91 System.out.println("Created directory '" + directory.getAbsolutePath() + "'."); 92 } 93 } 94 } 95 96 File file = new File (root, name); 97 String absolutePath = file.getAbsolutePath(); 98 99 if (file.exists()) { 100 if (!overwrite) { 101 throw new WSIFException( 102 "File '" 103 + absolutePath 104 + "' already exists. Please remove it or " 105 + "enable the overwrite option."); 106 } else { 107 file.delete(); 108 109 if (verbose) { 110 System.out.println("Deleted file '" + absolutePath + "'."); 111 } 112 } 113 } 114 115 if (verbose) { 116 System.out.println("Created file '" + absolutePath + "'."); 117 } 118 119 try { 120 return new FileOutputStream (absolutePath); 121 } catch (FileNotFoundException e) { 122 throw new WSIFException("Problem getting output stream.", e); 123 } 124 } 125 126 public InputStream getInputStream(String root, String name) 127 throws IOException { 128 String fileName = (root != null) ? root + File.separatorChar + name : name; 129 URL url = null; 130 Object content = null; 131 132 try { 133 url = getURL(null, fileName, 1); 134 content = url.getContent(); 135 } catch (SecurityException e) { 136 throw new IOException ( 137 "Your JVM's security manager has disallowed " 138 + "access to '" 139 + fileName 140 + "'."); 141 } catch (IOException e) { 142 throw new IOException ("The resource at '" + fileName + "' was not found."); 143 } 144 145 if (content == null) { 146 throw new IllegalArgumentException ("No content at '" + fileName + "'."); 147 } else if (content instanceof InputStream ) { 148 return (InputStream ) content; 149 } else { 150 throw new IOException ("The content of '" + fileName + "' is not a stream."); 151 } 152 } 153 154 private static URL getURL(URL contextURL, String spec, int recursiveDepth) 155 throws MalformedURLException { 156 URL url = null; 157 158 try { 159 url = new URL (contextURL, spec); 160 161 try { 162 url.openStream(); 163 } catch (IOException ioe1) { 164 throw new MalformedURLException ("This file was not found: " + url); 165 } 166 } catch (MalformedURLException e1) { 167 url = new URL ("file", "", spec); 168 169 try { 170 url.openStream(); 171 } catch (IOException ioe2) { 172 if (contextURL != null) { 173 String contextFileName = contextURL.getFile(); 174 String parentName = new File (contextFileName).getParent(); 175 176 if (parentName != null && recursiveDepth < 3) { 177 return getURL( 178 new URL ("file", "", parentName + '/'), 179 spec, 180 recursiveDepth + 1); 181 } 182 } 183 184 throw new MalformedURLException ("This file was not found: " + url); 185 } 186 } 187 188 return url; 189 } 190 } | Popular Tags |