KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > wsif > compiler > util > StreamFactory


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "WSIF" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, 2002, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.apache.wsif.compiler.util;
59
60 import java.io.File JavaDoc;
61 import java.io.FileNotFoundException JavaDoc;
62 import java.io.FileOutputStream JavaDoc;
63 import java.io.IOException JavaDoc;
64 import java.io.InputStream JavaDoc;
65 import java.io.OutputStream JavaDoc;
66 import java.net.URL JavaDoc;
67 import java.net.MalformedURLException JavaDoc;
68
69 import org.apache.wsif.WSIFException;
70 import org.apache.wsif.compiler.schema.tools.Conventions;
71
72 /**
73  * @author Matthew J. Duftler
74  */

75 public class StreamFactory {
76     
77     public OutputStream JavaDoc getOutputStream(
78         String JavaDoc root,
79         String JavaDoc name,
80         boolean overwrite)
81         throws WSIFException {
82         boolean verbose = Conventions.getVerbose();
83
84         if (root != null) {
85             File JavaDoc directory = new File JavaDoc(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 JavaDoc file = new File JavaDoc(root, name);
97         String JavaDoc 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 JavaDoc(absolutePath);
121         } catch (FileNotFoundException JavaDoc e) {
122             throw new WSIFException("Problem getting output stream.", e);
123         }
124     }
125
126     public InputStream JavaDoc getInputStream(String JavaDoc root, String JavaDoc name)
127         throws IOException JavaDoc {
128         String JavaDoc fileName = (root != null) ? root + File.separatorChar + name : name;
129         URL JavaDoc url = null;
130         Object JavaDoc content = null;
131
132         try {
133             url = getURL(null, fileName, 1);
134             content = url.getContent();
135         } catch (SecurityException JavaDoc e) {
136             throw new IOException JavaDoc(
137                 "Your JVM's security manager has disallowed "
138                     + "access to '"
139                     + fileName
140                     + "'.");
141         } catch (IOException JavaDoc e) {
142             throw new IOException JavaDoc("The resource at '" + fileName + "' was not found.");
143         }
144
145         if (content == null) {
146             throw new IllegalArgumentException JavaDoc("No content at '" + fileName + "'.");
147         } else if (content instanceof InputStream JavaDoc) {
148             return (InputStream JavaDoc) content;
149         } else {
150             throw new IOException JavaDoc("The content of '" + fileName + "' is not a stream.");
151         }
152     }
153
154     private static URL JavaDoc getURL(URL JavaDoc contextURL, String JavaDoc spec, int recursiveDepth)
155         throws MalformedURLException JavaDoc {
156         URL JavaDoc url = null;
157     
158         try {
159             url = new URL JavaDoc(contextURL, spec);
160     
161             try {
162                 url.openStream();
163             } catch (IOException JavaDoc ioe1) {
164                 throw new MalformedURLException JavaDoc("This file was not found: " + url);
165             }
166         } catch (MalformedURLException JavaDoc e1) {
167             url = new URL JavaDoc("file", "", spec);
168     
169             try {
170                 url.openStream();
171             } catch (IOException JavaDoc ioe2) {
172                 if (contextURL != null) {
173                     String JavaDoc contextFileName = contextURL.getFile();
174                     String JavaDoc parentName = new File JavaDoc(contextFileName).getParent();
175     
176                     if (parentName != null && recursiveDepth < 3) {
177                         return getURL(
178                             new URL JavaDoc("file", "", parentName + '/'),
179                             spec,
180                             recursiveDepth + 1);
181                     }
182                 }
183     
184                 throw new MalformedURLException JavaDoc("This file was not found: " + url);
185             }
186         }
187     
188         return url;
189     }
190 }
Popular Tags