You are viewing a plain text version of this content. The canonical link for it is here.
Posted to axis-cvs@ws.apache.org by di...@apache.org on 2005/11/18 19:28:29 UTC

svn commit: r345535 - in /webservices/axis2/trunk/java/modules: codegen/maven.xml codegen/src/org/apache/axis2/wsdl/codegen/writer/ClassWriter.java common/src/org/apache/axis2/util/Loader.java

Author: dims
Date: Fri Nov 18 10:28:24 2005
New Revision: 345535

URL: http://svn.apache.org/viewcvs?rev=345535&view=rev
Log:
Fix for AXIS2-230 - I wish the codegen tool generated code was indented nicely

No hard dependency....Drop jalopy.jar in maven/lib if you want indent code for the test harness. At runtime if the jalopy jar is in classpath then automatically the code is indented. 



Added:
    webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/Loader.java
Modified:
    webservices/axis2/trunk/java/modules/codegen/maven.xml
    webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/writer/ClassWriter.java

Modified: webservices/axis2/trunk/java/modules/codegen/maven.xml
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/codegen/maven.xml?rev=345535&r1=345534&r2=345535&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/codegen/maven.xml (original)
+++ webservices/axis2/trunk/java/modules/codegen/maven.xml Fri Nov 18 10:28:24 2005
@@ -6,8 +6,21 @@
     xmlns:j="jelly:core"
     xmlns:u="jelly:util"
     xmlns:maven="jelly:maven"
+    xmlns:util="jelly:util"
     xmlns:deploy="deploy"
     xmlns:ant="jelly:ant">
+
+    <preGoal name="java:compile">
+      <util:file var="file" name="${maven.home}/lib/jalopy.jar"/>
+      <j:if test="${file.exists()}">
+        <ant:path id="jalopy.library">
+          <ant:fileset dir="${maven.home}/lib">
+            <ant:include name="jalopy.jar"/>
+          </ant:fileset>
+        </ant:path>
+        <maven:addPath id="maven.dependency.classpath" refid="jalopy.library"/>
+      </j:if>
+    </preGoal>
 
     <preGoal name="itest:compile">
         <u:file var="file" name="${maven.itest.src}"/>

Modified: webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/writer/ClassWriter.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/writer/ClassWriter.java?rev=345535&r1=345534&r2=345535&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/writer/ClassWriter.java (original)
+++ webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/writer/ClassWriter.java Fri Nov 18 10:28:24 2005
@@ -16,21 +16,26 @@
 
 package org.apache.axis2.wsdl.codegen.writer;
 
-import org.apache.axis2.wsdl.codegen.CodeGenerationException;
-import org.apache.axis2.wsdl.util.ConfigPropertyFileLoader;
 import org.apache.axis2.util.FileWriter;
+import org.apache.axis2.util.Loader;
 import org.apache.axis2.util.XSLTTemplateProcessor;
+import org.apache.axis2.wsdl.codegen.CodeGenerationException;
+import org.apache.axis2.wsdl.util.ConfigPropertyFileLoader;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.w3c.dom.Document;
 
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.InputStream;
+import java.lang.reflect.Method;
 import java.util.Iterator;
 import java.util.Map;
 
 public abstract class ClassWriter {
 
     protected File outputFileLocation = null;
+    protected File outputFile = null;
     protected FileOutputStream stream = null;
     protected InputStream xsltStream = null;
     protected String language = ConfigPropertyFileLoader.getDefaultLanguage(); //default would java
@@ -41,10 +46,6 @@
 
     protected boolean fileExists = false;//a flag saying the file is existing
 
-    public FileOutputStream getStream() {
-        return stream;
-    }
-
     /**
      * Sets the language
      *
@@ -113,7 +114,7 @@
      * @throws Exception
      */
     public void createOutFile(String packageName, String fileName) throws Exception {
-        File outputFile = FileWriter.createClassFile(outputFileLocation,
+        outputFile = FileWriter.createClassFile(outputFileLocation,
                 packageName,
                 fileName,
                 getFileExtensionForLanguage(language));
@@ -127,7 +128,7 @@
     /**
      * Find the file name extension
      * @param language
-     * @return
+     * @return extension
      */
     protected String getFileExtensionForLanguage(String language){
         Map languageSpecificPropertyMap = (Map)ConfigPropertyFileLoader.getLanguageSpecificPropertiesMap().get(this.language);
@@ -150,7 +151,7 @@
     /**
      * Writes the output file
      *
-     * @param documentStream
+     * @param doc
      * @throws Exception
      */
     public void parse(Document doc) throws Exception {
@@ -160,9 +161,9 @@
                     this.xsltStream);
             this.stream.flush();
             this.stream.close();
+            if ("java".equals(language) && outputFile != null) {
+                PrettyPrinter.prettify(outputFile);
+            }
         }
-
     }
-
-
 }

Added: webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/Loader.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/Loader.java?rev=345535&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/Loader.java (added)
+++ webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/Loader.java Fri Nov 18 10:28:24 2005
@@ -0,0 +1,164 @@
+/*
+ * Copyright  2003-2004 The Apache Software Foundation.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+
+package org.apache.axis2.util;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.lang.reflect.InvocationTargetException;
+import java.net.URL;
+
+/**
+ * Load resources (or images) from various sources.
+ * <p/>
+ *
+ * @author Davanum Srinivas (dims@yahoo.com).
+ */
+public class Loader {
+    private static Log log = LogFactory.getLog(Loader.class.getName());
+
+    /**
+     * This method will search for <code>resource</code> in different
+     * places. The rearch order is as follows:
+     * <ol>
+     * <p><li>Search for <code>resource</code> using the thread context
+     * class loader under Java2. If that fails, search for
+     * <code>resource</code> using the class loader that loaded this
+     * class (<code>Loader</code>).
+     * <p><li>Try one last time with
+     * <code>ClassLoader.getSystemResource(resource)</code>, that is is
+     * using the system class loader in JDK 1.2 and virtual machine's
+     * built-in class loader in JDK 1.1.
+     * </ol>
+     * <p/>
+     *
+     * @param resource
+     * @return TODO
+     */
+    static public URL getResource(String resource) {
+        ClassLoader classLoader = null;
+        URL url = null;
+        try {
+            // We could not find resource. Ler us now try with the
+            // classloader that loaded this class.
+            classLoader = getTCL();
+            if (classLoader != null) {
+                log.debug("Trying to find [" + resource + "] using " + classLoader + " class loader.");
+                url = classLoader.getResource(resource);
+                if (url != null) {
+                    return url;
+                }
+            }
+        } catch (Throwable t) {
+            log.warn("Caught Exception while in Loader.getResource. This may be innocuous.", t);
+        }
+    
+        // Last ditch attempt: get the resource from the class path. It
+        // may be the case that clazz was loaded by the Extentsion class
+        // loader which the parent of the system class loader. Hence the
+        // code below.
+        log.debug("Trying to find [" + resource + "] using ClassLoader.getSystemResource().");
+        return ClassLoader.getSystemResource(resource);
+    }
+    
+
+    /**
+     * Try to get the resource with the specified class loader
+     * <p/>
+     *
+     * @param loader
+     * @param resource
+     * @return Class
+     * @throws ClassNotFoundException
+     */
+    static public URL getResource(ClassLoader loader, String resource) throws ClassNotFoundException {
+        URL url = null;
+        try {
+            if (loader != null) {
+                log.debug("Trying to find [" + resource + "] using " + loader + " class loader.");
+                url = loader.getResource(resource);
+                if (url != null) {
+                    return url;
+                }
+            }
+        } catch (Throwable t) {
+            log.warn("Caught Exception while in Loader.getResource. This may be innocuous.", t);
+        }
+        return getResource(resource);
+    }
+
+    /**
+     * Get the Thread context class loader.
+     * <p/>
+     *
+     * @return
+     * @throws IllegalAccessException
+     * @throws InvocationTargetException
+     */
+    static public ClassLoader getTCL() throws IllegalAccessException, InvocationTargetException {
+        return Thread.currentThread().getContextClassLoader();
+    }
+
+    /**
+     * Try the specified classloader and then fall back to the loadClass
+     * <p/>
+     *
+     * @param loader
+     * @param clazz
+     * @return Class
+     * @throws ClassNotFoundException
+     */
+    static public Class loadClass(ClassLoader loader, String clazz) throws ClassNotFoundException {
+        try {
+            if(loader != null) {
+                Class c = loader.loadClass(clazz);
+                if (c != null)
+                    return c;
+            }
+        } catch (Throwable e) {
+        }
+        return loadClass(clazz);
+    }
+
+    /**
+     * If running under JDK 1.2 load the specified class using the
+     * <code>Thread</code> <code>contextClassLoader</code> if that
+     * fails try Class.forname.
+     * <p/>
+     *
+     * @param clazz
+     * @return TODO
+     * @throws ClassNotFoundException
+     */
+    static public Class loadClass(String clazz) throws ClassNotFoundException {
+        try {
+            ClassLoader tcl = getTCL();	
+            
+            if(tcl != null) {
+                Class c = tcl.loadClass(clazz);
+                if (c != null)
+                    return c;
+            }
+        } catch (Throwable e) {
+        }
+        // we reached here because tcl was null or because of a
+        // security exception, or because clazz could not be loaded...
+        // In any case we now try one more time
+        return Class.forName(clazz);
+    }
+}