JDefinitionWriter.java

00001 
00025 package org.objectweb.jonas.ws;
00026 
00027 import java.io.File;
00028 import java.io.FileOutputStream;
00029 import java.io.IOException;
00030 import java.io.OutputStreamWriter;
00031 import java.io.Writer;
00032 import java.nio.charset.Charset;
00033 import java.util.Iterator;
00034 import java.util.List;
00035 import java.util.Map;
00036 
00037 import javax.wsdl.Definition;
00038 import javax.wsdl.Import;
00039 import javax.wsdl.Types;
00040 import javax.wsdl.WSDLException;
00041 import javax.wsdl.extensions.UnknownExtensibilityElement;
00042 import javax.wsdl.factory.WSDLFactory;
00043 import javax.wsdl.xml.WSDLWriter;
00044 
00045 import org.w3c.dom.Document;
00046 
00047 import org.objectweb.jonas_lib.xml.XMLSerializer;
00048 
00049 import org.objectweb.jonas.common.Log;
00050 
00051 import org.objectweb.util.monolog.api.BasicLevel;
00052 import org.objectweb.util.monolog.api.Logger;
00053 
00058 public class JDefinitionWriter {
00059 
00061     private File base;
00062 
00064     private Charset cs;
00065 
00067     private Definition definition;
00068 
00070     private String filename;
00071 
00073     private static Logger logger = Log.getLogger(Log.JONAS_WS_PREFIX);
00074 
00083     public JDefinitionWriter(Definition def, File context, Charset cs, String filename) {
00084         if (!context.exists()) {
00085             throw new IllegalArgumentException("Context MUST exists : " + context);
00086         }
00087         this.definition = def;
00088         if (context.isDirectory()) {
00089             this.base = context;
00090         } else {
00091             this.base = context.getParentFile();
00092         }
00093         this.cs = cs;
00094         this.filename = filename;
00095     }
00096 
00102     public void write() throws IOException, WSDLException {
00103         writeDefinition(definition, base, filename);
00104         Map imports = definition.getImports();
00105         for (Iterator ns = imports.keySet().iterator(); ns.hasNext();) {
00106             // for each nsURI, we have a List of Import
00107             String uri = (String) ns.next();
00108             List importeds = (List) imports.get(uri);
00109             // for each import related to the given ns
00110             for (Iterator imp = importeds.iterator(); imp.hasNext();) {
00111                 Import imported = (Import) imp.next();
00112 
00113                 // create a resolved URL to know if the import is relative or
00114                 // not to the base
00115                 String locURI = imported.getLocationURI();
00116                 if (!locURI.startsWith("http://")) {
00117                     // locResolvedURL starts with the base URI, so it's a
00118                     // relative import
00119                     // and we should wrote it.
00120                     Definition impDef = imported.getDefinition();
00121                     if (locURI.toLowerCase().endsWith("wsdl")) {
00122                         // if we have another WSDL
00123                         JDefinitionWriter jdw = new JDefinitionWriter(impDef, new File(base, filename).getCanonicalFile(), cs, locURI);
00124                         jdw.write();
00125                     } else {
00126                         // Assume this is a types import
00127                         Types types = impDef.getTypes();
00128                         if (types != null) {
00129                             List extList = types.getExtensibilityElements();
00130                             if (!extList.isEmpty()) {
00131                                 UnknownExtensibilityElement e = (UnknownExtensibilityElement) extList.get(0);
00132                                 Document doc = e.getElement().getOwnerDocument();
00133                                 File dir = new File(base, filename).getCanonicalFile().getParentFile();
00134                                 writeDocument(doc, dir, locURI);
00135                             }
00136                         }
00137                     }
00138                 }
00139             }
00140         }
00141     }
00142 
00149     private void writeDocument(Document doc, File base, String locURI) throws IOException {
00150         XMLSerializer ser = new XMLSerializer(doc);
00151         File file = new File(base, locURI).getCanonicalFile();
00152         logger.log(BasicLevel.DEBUG, "Writing XML Document in " + file);
00153         createParentIfNeeded(file);
00154         Writer writer = new OutputStreamWriter(new FileOutputStream(file), cs);
00155         ser.serialize(writer);
00156         writer.close();
00157     }
00158 
00166     private void writeDefinition(Definition def, File base, String filename) throws WSDLException, IOException {
00167         WSDLFactory factory = WSDLFactory.newInstance();
00168         WSDLWriter writer = factory.newWSDLWriter();
00169         File wsdl = new File(base, filename).getCanonicalFile();
00170         logger.log(BasicLevel.DEBUG, "Writing WSDL Definition in " + wsdl);
00171         createParentIfNeeded(wsdl);
00172         Writer w = new OutputStreamWriter(new FileOutputStream(wsdl), cs);
00173         writer.writeWSDL(def, w);
00174         w.close();
00175     }
00176 
00182     private void createParentIfNeeded(File file) throws IOException {
00183         File parent = file.getParentFile();
00184         if (!parent.exists()) {
00185             if (!parent.mkdirs()) {
00186                 // cannot create directory
00187                 throw new IOException("Cannot create directory " + parent.getCanonicalPath());
00188             }
00189         } else if (!parent.isDirectory()) {
00190             // parent exists but is not a directory
00191             throw new IOException("Parent " + parent.getCanonicalPath() + " already exists but is not a directory.");
00192         }
00193     }
00194 }

Generated on Tue Feb 15 15:05:36 2005 for JOnAS by  doxygen 1.3.9.1