KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > event > StandardOutputResolver


1 package net.sf.saxon.event;
2 import net.sf.saxon.OutputURIResolver;
3 import net.sf.saxon.trans.DynamicError;
4 import net.sf.saxon.trans.XPathException;
5
6 import javax.xml.transform.Result JavaDoc;
7 import javax.xml.transform.stream.StreamResult JavaDoc;
8 import java.io.File JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.io.OutputStream JavaDoc;
11 import java.net.*;
12
13
14 /**
15 * This class defines the default OutputURIResolver. This is a counterpart to the JAXP
16 * URIResolver, but is used to map the URI of a secondary result document to a Result object
17 * which acts as the destination for the new document.
18 * @author Michael H. Kay
19 */

20
21 public class StandardOutputResolver implements OutputURIResolver {
22
23     private static StandardOutputResolver theInstance = new StandardOutputResolver();
24
25     /**
26     * Get a singular instance
27     */

28
29     public static StandardOutputResolver getInstance() {
30         return theInstance;
31     }
32
33     /**
34     * Resolve an output URI
35     * @param href The relative URI of the output document. This corresponds to the
36     * href attribute of the xsl:result-document instruction.
37     * @param base The base URI that should be used. This is the base output URI,
38     * normally the URI of the principal output file.
39     * @return a Result object representing the destination for the XML document
40     */

41
42     public Result JavaDoc resolve(String JavaDoc href, String JavaDoc base) throws XPathException {
43
44         // System.err.println("Output URI Resolver (href='" + href + "', base='" + base + "')");
45

46         try {
47             URI absoluteURI;
48             if (href.equals("")) {
49                 if (base==null) {
50                     throw new DynamicError("The system identifier of the principal output file is unknown");
51                 }
52                 absoluteURI= new URI(base);
53             } else {
54                 absoluteURI= new URI(href);
55             }
56             if (!absoluteURI.isAbsolute()) {
57                 if (base==null) {
58                     throw new DynamicError("The system identifier of the principal output file is unknown");
59                 }
60                 URI baseURI = new URI(base);
61                 absoluteURI = baseURI.resolve(href);
62             }
63
64             if (absoluteURI.getScheme().equals("file")) {
65                 File JavaDoc newFile = new File JavaDoc(absoluteURI);
66                 try {
67                     if (!newFile.exists()) {
68                         String JavaDoc parent = newFile.getParent();
69                         if (parent!=null) {
70                             File JavaDoc parentPath = new File JavaDoc(parent);
71                             if (parentPath != null && !parentPath.exists()) {
72                                 parentPath.mkdirs();
73                             }
74                             newFile.createNewFile();
75                         }
76                     }
77
78                     //StreamResult result = new StreamResult(new FileOutputStream(newFile));
79
StreamResult JavaDoc result = new StreamResult JavaDoc(newFile.toURI().toASCIIString());
80                             // The call new StreamResult(newFile) does file-to-URI conversion incorrectly
81
//result.setSystemId(newFile);
82
return result;
83
84                 } catch (java.io.IOException JavaDoc err) {
85                     throw new DynamicError("Failed to create output file " + absoluteURI, err);
86                 }
87
88             } else {
89
90                 // See if the Java VM can conjure up a writable URL connection for us.
91
// This is optimistic: I have yet to discover a URL scheme that it can handle.
92

93                 URLConnection connection = absoluteURI.toURL().openConnection();
94                 connection.setDoInput(false);
95                 connection.setDoOutput(true);
96                 connection.connect();
97                 OutputStream JavaDoc stream = connection.getOutputStream();
98                 StreamResult JavaDoc result = new StreamResult JavaDoc(stream);
99                 result.setSystemId(absoluteURI.toASCIIString());
100                 return result;
101             }
102         } catch (URISyntaxException err) {
103             throw new DynamicError("Invalid syntax for base URI", err);
104         } catch (IllegalArgumentException JavaDoc err2) {
105             throw new DynamicError("Invalid URI syntax", err2);
106         } catch (MalformedURLException err3) {
107             throw new DynamicError("Resolved URL is malformed", err3);
108         } catch (UnknownServiceException err5) {
109             throw new DynamicError("Specified protocol does not allow output", err5);
110         } catch (IOException JavaDoc err4) {
111             throw new DynamicError("Cannot open connection to specified URL", err4);
112         }
113     }
114
115     /**
116     * Signal completion of the result document. This method is called by the system
117     * when the result document has been successfully written. It allows the resolver
118     * to perform tidy-up actions such as closing output streams, or firing off
119     * processes that take this result tree as input. Note that the OutputURIResolver
120     * is stateless, so the original href is supplied to identify the document
121     * that has been completed.
122     */

123
124     public void close(Result JavaDoc result) throws XPathException {
125         if (result instanceof StreamResult JavaDoc) {
126             OutputStream JavaDoc stream = ((StreamResult JavaDoc)result).getOutputStream();
127             if (stream != null) {
128                 try {
129                     stream.close();
130                 } catch (java.io.IOException JavaDoc err) {
131                     throw new DynamicError("Failed while closing output file", err);
132                 }
133             }
134         }
135     }
136
137 // public static void main(String[] args) {
138
// System.err.println("supplied base file: " + args[0]);
139
// System.err.println("relative URI: " + args[1]);
140
// System.err.println("base URI: " + new File(args[0]).toURI().toString());
141
// System.err.println("resolved URI: " + new File(args[0]).toURI().resolve(args[1]).toString());
142
// }
143

144 }
145
146
147
148
149
150 //
151
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
152
// you may not use this file except in compliance with the License. You may obtain a copy of the
153
// License at http://www.mozilla.org/MPL/
154
//
155
// Software distributed under the License is distributed on an "AS IS" basis,
156
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
157
// See the License for the specific language governing rights and limitations under the License.
158
//
159
// The Original Code is: all this file.
160
//
161
// The Initial Developer of the Original Code is Michael H. Kay
162
//
163
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
164
//
165
// Contributor(s): none.
166
//
167
Popular Tags