KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > ws > JDefinitionWriter


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JDefinitionWriter.java,v 1.4 2005/05/27 15:01:22 sauthieg Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas.ws;
26
27 import java.io.File JavaDoc;
28 import java.io.FileOutputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.OutputStreamWriter JavaDoc;
31 import java.io.Writer JavaDoc;
32 import java.nio.charset.Charset JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Map JavaDoc;
36 import javax.wsdl.Definition;
37 import javax.wsdl.Import;
38 import javax.wsdl.Types;
39 import javax.wsdl.WSDLException;
40 import javax.wsdl.extensions.ExtensibilityElement;
41 import javax.wsdl.extensions.UnknownExtensibilityElement;
42 import javax.wsdl.extensions.schema.Schema;
43 import javax.wsdl.factory.WSDLFactory;
44 import javax.wsdl.xml.WSDLWriter;
45 import org.w3c.dom.Document JavaDoc;
46 import org.objectweb.jonas_lib.xml.XMLSerializer;
47 import org.objectweb.jonas.common.Log;
48 import org.objectweb.util.monolog.api.BasicLevel;
49 import org.objectweb.util.monolog.api.Logger;
50
51 /**
52  * Wrote the given Definition and all imported WSDL to the given base directory.
53  * @author Guillaume Sauthier
54  */

55 public class JDefinitionWriter {
56
57     /** WSDL Directory */
58     private File JavaDoc base;
59
60     /** charset */
61     private Charset JavaDoc cs;
62
63     /** Definition to write */
64     private Definition definition;
65
66     /** base WSDL filename */
67     private String JavaDoc filename;
68
69     /** logger */
70     private static Logger logger = Log.getLogger(Log.JONAS_WS_PREFIX);
71
72     /**
73      * Constructs a new JDefinitionWriter that will wrote the given Definition
74      * and all imported WSDL to the given base directory.
75      * @param def base Definition
76      * @param context Context where file should be wrote (MUST exists)
77      * @param cs Charset to use
78      * @param filename base Definition filename
79      */

80     public JDefinitionWriter(Definition def, File JavaDoc context, Charset JavaDoc cs, String JavaDoc filename) {
81         if (!context.exists()) {
82             throw new IllegalArgumentException JavaDoc("Context MUST exists : " + context);
83         }
84         this.definition = def;
85         if (context.isDirectory()) {
86             this.base = context;
87         } else {
88             this.base = context.getParentFile();
89         }
90         this.cs = cs;
91         this.filename = filename;
92     }
93
94     /**
95      * Write the given Definition into the base directory
96      * @throws IOException if a file or directory cannot be created
97      * @throws WSDLException if WSDLWriter is not able to write its Definition
98      */

99     public void write() throws IOException JavaDoc, WSDLException {
100         writeDefinition(definition, base, filename);
101         Map JavaDoc imports = definition.getImports();
102         for (Iterator JavaDoc ns = imports.keySet().iterator(); ns.hasNext();) {
103             // for each nsURI, we have a List of Import
104
String JavaDoc uri = (String JavaDoc) ns.next();
105             List JavaDoc importeds = (List JavaDoc) imports.get(uri);
106             // for each import related to the given ns
107
for (Iterator JavaDoc imp = importeds.iterator(); imp.hasNext();) {
108                 Import imported = (Import) imp.next();
109
110                 // create a resolved URL to know if the import is relative or
111
// not to the base
112
String JavaDoc locURI = imported.getLocationURI();
113                 if (!locURI.startsWith("http://")) {
114                     // locResolvedURL starts with the base URI, so it's a
115
// relative import
116
// and we should wrote it.
117
Definition impDef = imported.getDefinition();
118                     if (locURI.toLowerCase().endsWith("wsdl")) {
119                         // if we have another WSDL
120
JDefinitionWriter jdw = new JDefinitionWriter(impDef, new File JavaDoc(base, filename).getCanonicalFile(), cs, locURI);
121                         jdw.write();
122                     } else {
123                         // Assume this is a types import
124
Types types = impDef.getTypes();
125                         if (types != null) {
126                             List JavaDoc extList = types.getExtensibilityElements();
127                             for (Iterator JavaDoc extIt = extList.iterator(); extIt.hasNext();) {
128                                 ExtensibilityElement ext = (ExtensibilityElement) extIt.next();
129                                 Document JavaDoc doc = null;
130
131                                 if (ext instanceof Schema) {
132                                     // schema handling
133
Schema schema = (Schema) ext;
134                                     doc = schema.getElement().getOwnerDocument();
135                                 } else if (ext instanceof UnknownExtensibilityElement) {
136                                     // other XML
137
UnknownExtensibilityElement unknownExtElem = (UnknownExtensibilityElement) ext;
138                                     doc = unknownExtElem.getElement().getOwnerDocument();
139                                 }
140
141                                 if (doc != null) {
142                                     // we have something to serialize
143
File JavaDoc dir = new File JavaDoc(base, filename).getCanonicalFile().getParentFile();
144                                     writeDocument(doc, dir, locURI);
145                                 }
146                             }
147                         }
148                     }
149                 }
150             }
151         }
152     }
153
154     /**
155      * @param doc XML Document to write
156      * @param base storing directory
157      * @param locURI Document filename (may include directories)
158      * @throws IOException if OutputStream cannot be created
159      */

160     private void writeDocument(Document JavaDoc doc, File JavaDoc base, String JavaDoc locURI) throws IOException JavaDoc {
161         XMLSerializer ser = new XMLSerializer(doc);
162         File JavaDoc file = new File JavaDoc(base, locURI).getCanonicalFile();
163         logger.log(BasicLevel.DEBUG, "Writing XML Document in " + file);
164         createParentIfNeeded(file);
165         Writer JavaDoc writer = new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(file), cs);
166         ser.serialize(writer);
167         writer.close();
168     }
169
170     /**
171      * @param def Definition to write (that does not write imported files)
172      * @param base storing directory
173      * @param filename Definition filename
174      * @throws WSDLException if WSDLWriter failed to wrote the Definition
175      * @throws IOException if OutputStream cannot be created
176      */

177     private void writeDefinition(Definition def, File JavaDoc base, String JavaDoc filename) throws WSDLException, IOException JavaDoc {
178         WSDLFactory factory = WSDLFactory.newInstance();
179         WSDLWriter writer = factory.newWSDLWriter();
180         File JavaDoc wsdl = new File JavaDoc(base, filename).getCanonicalFile();
181         logger.log(BasicLevel.DEBUG, "Writing WSDL Definition in " + wsdl);
182         createParentIfNeeded(wsdl);
183         Writer JavaDoc w = new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(wsdl), cs);
184         writer.writeWSDL(def, w);
185         w.close();
186     }
187
188     /**
189      * Creates the parent of the given file as a directory
190      * @param file with a parent that must be a directory
191      * @throws IOException if directory cannot be created
192      */

193     private void createParentIfNeeded(File JavaDoc file) throws IOException JavaDoc {
194         File JavaDoc parent = file.getParentFile();
195         if (!parent.exists()) {
196             if (!parent.mkdirs()) {
197                 // cannot create directory
198
throw new IOException JavaDoc("Cannot create directory " + parent.getCanonicalPath());
199             }
200         } else if (!parent.isDirectory()) {
201             // parent exists but is not a directory
202
throw new IOException JavaDoc("Parent " + parent.getCanonicalPath() + " already exists but is not a directory.");
203         }
204     }
205 }
Popular Tags