This question has been asked several times:
How can I generate a web.xml out of *.gwt.xml module files.
Here is a simple class that solved the problem for us:

package com.ifbag.gwtbase.base;

import java.io.*;

import com.google.gwt.core.ext.TreeLogger;
import com.google.gwt.core.ext.UnableToCompleteException;
import com.google.gwt.dev.cfg.ModuleDef;
import com.google.gwt.dev.cfg.ModuleDefLoader;
import com.google.gwt.dev.util.log.PrintWriterTreeLogger;

@SuppressWarnings("nls")
public class GWTModule2WebXml {
        private String moduleFile;
        private String webXml;
        private BufferedWriter os;

        public GWTModule2WebXml() {
                super();
        }

        public String getModuleFile() {
                return moduleFile;
        }
        public void setModuleFile(String aModule) {
                this.moduleFile = aModule;
        }
        public void setWebXml(String aFilename) {
                this.webXml = aFilename;
        }
        public String getWebXml() {
                return webXml;
        }
        public void setOs(BufferedWriter aOs) {
                this.os = aOs;
        }
        public BufferedWriter getOs() {
                return os;
        }
        /**
         *
         * @param args
         */
        public static void main(String[] args) {
                try {

                        GWTModule2WebXml g = new GWTModule2WebXml();
                        g.setWebXml("web.xml");
                        g.setModuleFile("com.ifbag.gwttest.base.GWTTest");
                        g.execute();
                }
                catch (Exception e) {

                        e.printStackTrace();
                }
        }

        public void execute() throws Exception {
                try {
                        // GWT lädt die Klasse über den ContextClassLoader !
                        // in ant ist dieser aber nicht gesetzt
                        ClassLoader classLoader = getClass().getClassLoader();
                        
Thread.currentThread().setContextClassLoader(classLoader);

                        FileOutputStream fos = new 
FileOutputStream(getWebXml());
                        BufferedWriter bw = new BufferedWriter(new 
PrintWriter(fos));
                        setOs(bw);
                        ModuleDef module = readModule();
                        generateWebXml(module);
                        bw.close();
                }
                catch (Exception e) {
                        e.printStackTrace();
                        throw e;
                }
        }

        private ModuleDef readModule() throws FileNotFoundException,
UnableToCompleteException {
                PrintWriter out = new PrintWriter(new
FileOutputStream("module.log"));
                TreeLogger logger = new PrintWriterTreeLogger(out);

                try {
                        ModuleDef module = 
ModuleDefLoader.loadFromClassPath(logger,
getModuleFile());
                        return module;
                }
                finally {
                        out.close();
                }

        }
        private void generateWebXml(ModuleDef module) throws IOException {

                writeHead();

                writeBody(module);

                writeFooter();
        }

        private void writeBody(ModuleDef module) throws IOException {

                String[] servletPaths = module.getServletPaths();
                for (int i = 0; i < servletPaths.length; i++) {
                        String path = servletPaths[i];
                        String servlet = module.findServletForPath(path);

                        writeServlet(path, servlet);
                }

                for (int i = 0; i < servletPaths.length; i++) {
                        String path = servletPaths[i];
                        String servlet = module.findServletForPath(path);

                        writeServletMapping(path, servlet);
                }
                writeln("");
        }

        private void writeServlet(String path, String servlet) throws
IOException {
                writeln("\t<servlet>");
                writeln("\t\t<servlet-name>" + path + "</servlet-name>");
                writeln("\t\t<servlet-class>");
                writeln("\t\t\t" + servlet);
                writeln("\t\t</servlet-class>");
                writeln("\t</servlet>");
        }
        private void writeServletMapping(String path, String servlet) throws
IOException {
                writeln("\t<servlet-mapping>");
                writeln("\t\t<servlet-name>" + path + "</servlet-name>");
                writeln("\t\t<url-pattern>");
                writeln("\t\t\t/" + getModuleFile() + path);
                writeln("\t\t</url-pattern>");
                writeln("\t</servlet-mapping>");
        }
        private void writeFooter() throws IOException {
                writeln("\t<welcome-file-list>");
                String jspFile = getModuleFile() + "/" + getModuleName();
                writeln("\t\t<welcome-file>" + jspFile + ".jsp</welcome-file>");
                writeln("\t</welcome-file-list>");
                writeln("");
                writeln("</web-app>");
        }

        private String getModuleName() {
                String moduleFile2 = getModuleFile();
                int lastIndexOf = moduleFile2.lastIndexOf('.');
                return moduleFile2.substring(lastIndexOf + 1);
        }

        private void writeln(String line) throws IOException {
                getOs().write(line + "\n");
        }

        private void writeHead() throws IOException {
                writeln("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

                writeln("<web-app id=\"WebApp_ID\" version=\"2.4\"");
                writeln("\t\txmlns=\"http://java.sun.com/xml/ns/j2ee\"";);
                
writeln("\t\txmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance
\"");
                
writeln("\t\txsi:schemaLocation=\"http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd\";>");
                //                      <display-name>GWTTest</display-name>
                writeln("");
        }

}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to