axis-java-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aj...@apache.org
Subject svn commit: r411720 - in /webservices/axis2/trunk/java/modules: codegen/src/org/apache/axis2/wsdl/codegen/ codegen/src/org/apache/axis2/wsdl/codegen/extension/ codegen/src/org/apache/axis2/wsdl/codegen/writer/ codegen/src/org/apache/axis2/wsdl/util/ co...
Date Mon, 05 Jun 2006 09:04:49 GMT
Author: ajith
Date: Mon Jun  5 02:04:48 2006
New Revision: 411720

URL: http://svn.apache.org/viewvc?rev=411720&view=rev
Log:
1. Modified the code generator to have post extensions that can do tasks such as formatting
2. Added an XML pretty printer that works on JTidy (http://sourceforge.net/projects/jtidy).
It is done just like the java pretty printer where the classes are loaded by reflection. However
the JTidy jar needs to be in classpath for this to work.
3. Added three new extensions
  I. JavaPrettyPrinterExtension which calls the Jalopy pretty printer internally and prettifies
java source files.
  II XMLPrettyPrinterExtension which prettifies XMLs
  II WSDLPrettyPrinterExtension which prettifies WSDL files
4. removed the call to PrettyPrinter from the ClassWriter

Added:
    webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/AbstractPrettyPrinterExtension.java
    webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/JavaPrettyPrinterExtension.java
    webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/WSDLPrettyPrinterExtension.java
    webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/XMLPrettyPrinterExtension.java
    webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/XMLPrettyPrinter.java
    webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/prettifiers/
Modified:
    webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/CodeGenerationEngine.java
    webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/codegen-config.properties
    webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/writer/ClassWriter.java
    webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/util/ConfigPropertyFileLoader.java
    webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/PrettyPrinter.java

Modified: webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/CodeGenerationEngine.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/CodeGenerationEngine.java?rev=411720&r1=411719&r2=411720&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/CodeGenerationEngine.java
(original)
+++ webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/CodeGenerationEngine.java
Mon Jun  5 02:04:48 2006
@@ -48,7 +48,8 @@
 public class CodeGenerationEngine {
 	private static final Log log = LogFactory.getLog(CodeGenerationEngine.class);
 
-    private List extensions = new ArrayList();
+    private List preExtensions = new ArrayList();
+    private List postExtensions = new ArrayList();
 
 
     private CodeGenConfiguration configuration;
@@ -105,16 +106,22 @@
     }
 
     /**
-     * Loads the relevant extensions
+     * Loads the relevant preExtensions
      *
      * @throws CodeGenerationException
      */
     private void loadExtensions() throws CodeGenerationException {
-
+        //load pre extensions
         String[] extensions = ConfigPropertyFileLoader.getExtensionClassNames();
         for (int i = 0; i < extensions.length; i++) {
             //load the Extension class
-            addExtension((CodeGenExtension) getObjectFromClassName(extensions[i]));
+            addPreExtension((CodeGenExtension) getObjectFromClassName(extensions[i]));
+        }
+        //load post extensions
+        String[] postExtensions = ConfigPropertyFileLoader.getPostExtensionClassNames();
+        for (int i = 0; i < postExtensions.length; i++) {
+            //load the Extension class
+            addPostExtension((CodeGenExtension) getObjectFromClassName(postExtensions[i]));
         }
 
     }
@@ -123,20 +130,31 @@
      * Adds a given extension to the list
      * @param ext
      */
-    private void addExtension(CodeGenExtension ext) {
+    private void addPreExtension(CodeGenExtension ext) {
         if(ext != null) {
-           extensions.add(ext);
+           preExtensions.add(ext);
+        }
+    }
+
+     /**
+     * Adds a given extension to the list
+     * @param ext
+     */
+    private void addPostExtension(CodeGenExtension ext) {
+        if(ext != null) {
+           postExtensions.add(ext);
         }
     }
 
     /**
-     * Generate a WSDL
+     * Generate the code!!
      * @throws CodeGenerationException
      */
     public void generate() throws CodeGenerationException {
         try {
-            for (int i = 0; i < extensions.size(); i++) {
-                ((CodeGenExtension) extensions.get(i)).engage(configuration);
+            //engage the pre-extensions
+            for (int i = 0; i < preExtensions.size(); i++) {
+                ((CodeGenExtension) preExtensions.get(i)).engage(configuration);
             }
 
             Emitter emitter;
@@ -151,6 +169,7 @@
                 throw new CodeGenerationException(CodegenMessages.getMessage("engine.noProperDatabindingException"));
             }
 
+            //Find and invoke the emitter by reflection
             Map emitterMap = ConfigPropertyFileLoader.getLanguageEmitterMap();
             String className = (String)emitterMap.get(configuration.getOutputLanguage());
             if (className != null) {
@@ -162,12 +181,11 @@
             }
 
 
-
+            //invoke the necessary methods in the emitter
             if (configuration.isServerSide()) {
                 emitter.emitSkeleton();
                 // if the users want both client and server, it would be in the
                 // generate all option
-
                 if (configuration.isGenerateAll()) {
                     emitter.emitStub();
                 }
@@ -175,11 +193,13 @@
                 emitter.emitStub();
             }
 
-
+            //engage the post-extensions
+            for (int i = 0; i < postExtensions.size(); i++) {
+                ((CodeGenExtension) postExtensions.get(i)).engage(configuration);
+            }
 
         } catch (ClassCastException e) {
             throw new CodeGenerationException(CodegenMessages.getMessage("engine.wrongEmitter"),
e);
-
         } catch (Exception e) {
             throw new CodeGenerationException(e);
         }

Modified: webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/codegen-config.properties
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/codegen-config.properties?rev=411720&r1=411719&r2=411720&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/codegen-config.properties
(original)
+++ webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/codegen-config.properties
Mon Jun  5 02:04:48 2006
@@ -1,11 +1,15 @@
 #######################################################################################################################
 #####################   code generator property file  #################################################################
 #######################################################################################################################
-# Extensions - The extensions are comma separated
+# Extensions - The extension class names are comma separated
+# and these extensions will run before the main emitter
 # these are loaded in their lexical order
 # Note the last extension - It includes a check to figure out whether proper databinding
has taken place
 # This extension should appear AFTER all the databinding extensions inorder to function properly
 codegen.extension=org.apache.axis2.wsdl.codegen.extension.JaxMeExtension,org.apache.axis2.wsdl.codegen.extension.XMLBeansExtension,org.apache.axis2.wsdl.codegen.extension.SimpleDBExtension,org.apache.axis2.wsdl.codegen.extension.JiBXExtension,org.apache.axis2.wsdl.codegen.extension.JAXBRIExtension,org.apache.axis2.wsdl.codegen.extension.TypeMapperExtension,org.apache.axis2.wsdl.codegen.extension.DefaultDatabindingExtension
+#extensions that work after the main emitter. These will include functionality such as
+#formatters
+post.codegen.extension=org.apache.axis2.wsdl.codegen.extension.JavaPrettyPrinterExtension,org.apache.axis2.wsdl.codegen.extension.XMLPrettyPrinterExtension,org.apache.axis2.wsdl.codegen.extension.WSDLPrettyPrinterExtension
 #codegen.extension=org.apache.axis2.wsdl.codegen.extension.AxisBindingBuilder,org.apache.axis2.wsdl.codegen.extension.WSDLValidatorExtension,org.apache.axis2.wsdl.codegen.extension.PackageFinder,org.apache.axis2.wsdl.codegen.extension.SimpleDBExtension
 # The third party schemas to be loaded. e.g. The Xmime extension
 # Note - these will be loaded from the org.apache.axis2.wsdl.codegen.schema package.
@@ -95,7 +99,6 @@
 # Note this section is specific to the emitter logic. One can have anything here!
 c.interface.header.template=org.apache.axis2.wsdl.codegen.writer.CHeaderWriter,/org/apache/axis2/wsdl/template/c/StubHeaderTemplate.xsl
 c.interface.impl.template=org.apache.axis2.wsdl.codegen.writer.CSourceWriter,/org/apache/axis2/wsdl/template/c/StubSourceTemplate.xsl
-
 # file extension for generated source files from this language
 c.filename.extension=c
 

Added: webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/AbstractPrettyPrinterExtension.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/AbstractPrettyPrinterExtension.java?rev=411720&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/AbstractPrettyPrinterExtension.java
(added)
+++ webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/AbstractPrettyPrinterExtension.java
Mon Jun  5 02:04:48 2006
@@ -0,0 +1,64 @@
+package org.apache.axis2.wsdl.codegen.extension;
+
+import org.apache.axis2.wsdl.codegen.CodeGenConfiguration;
+import org.apache.axis2.wsdl.codegen.CodeGenerationException;
+
+import java.io.File;
+/*
+ * Copyright 2004,2005 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.
+ */
+
+public abstract class AbstractPrettyPrinterExtension extends AbstractCodeGenerationExtension{
+     /**
+     * If the extension for property file changes it might effect this as
+     * well !!!
+     */
+    protected String fileExtension = "";
+
+    public void engage(CodeGenConfiguration configuration) throws CodeGenerationException
{
+
+        //recurse through the output files and prettify them
+        File outputFolder = configuration.getOutputLocation();
+        prettify(outputFolder);
+
+
+    }
+
+    /**
+     * Recursive procedure to prettify the files
+     * @param file
+     */
+    protected void prettify(File file){
+        if (file.isFile() &&
+                file.exists() &&
+                file.getName().toLowerCase().endsWith(fileExtension)){
+            prettifyFile(file);
+        }else if (file.isDirectory()){
+            File[] childFiles = file.listFiles();
+            for (int i = 0; i < childFiles.length; i++) {
+                prettify(childFiles[i]);
+            }
+        }
+
+    }
+
+    /**
+     * Implement this to call the proper pretty printers
+     * @param file
+     */
+    protected abstract void prettifyFile(File file);
+
+
+}

Added: webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/JavaPrettyPrinterExtension.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/JavaPrettyPrinterExtension.java?rev=411720&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/JavaPrettyPrinterExtension.java
(added)
+++ webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/JavaPrettyPrinterExtension.java
Mon Jun  5 02:04:48 2006
@@ -0,0 +1,40 @@
+package org.apache.axis2.wsdl.codegen.extension;
+
+import org.apache.axis2.util.PrettyPrinter;
+
+import java.io.File;
+/*
+ * Copyright 2004,2005 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.
+ */
+
+public class JavaPrettyPrinterExtension extends AbstractPrettyPrinterExtension{
+
+
+    public JavaPrettyPrinterExtension() {
+        /*
+        * If the extension for property file changes it might effect this as
+        * well !!!
+        */
+        fileExtension = ".java";
+    }
+
+    /**
+     * Overridden to call the java pretty printer
+     * @param file
+     */
+    protected void prettifyFile(File file) {
+        PrettyPrinter.prettify(file);
+    }
+}

Added: webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/WSDLPrettyPrinterExtension.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/WSDLPrettyPrinterExtension.java?rev=411720&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/WSDLPrettyPrinterExtension.java
(added)
+++ webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/WSDLPrettyPrinterExtension.java
Mon Jun  5 02:04:48 2006
@@ -0,0 +1,31 @@
+package org.apache.axis2.wsdl.codegen.extension;
+
+import org.apache.axis2.util.XMLPrettyPrinter;
+
+import java.io.File;
+/*
+ * Copyright 2004,2005 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.
+ */
+
+public class WSDLPrettyPrinterExtension extends AbstractPrettyPrinterExtension{
+
+    public WSDLPrettyPrinterExtension() {
+        fileExtension = ".wsdl";
+    }
+
+    protected void prettifyFile(File file) {
+        XMLPrettyPrinter.prettify(file);
+    }
+}

Added: webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/XMLPrettyPrinterExtension.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/XMLPrettyPrinterExtension.java?rev=411720&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/XMLPrettyPrinterExtension.java
(added)
+++ webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/XMLPrettyPrinterExtension.java
Mon Jun  5 02:04:48 2006
@@ -0,0 +1,37 @@
+package org.apache.axis2.wsdl.codegen.extension;
+
+import org.apache.axis2.util.XMLPrettyPrinter;
+
+import java.io.File;
+/*
+ * Copyright 2004,2005 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.
+ */
+
+public class XMLPrettyPrinterExtension extends AbstractPrettyPrinterExtension{
+
+
+    public XMLPrettyPrinterExtension() {
+        fileExtension = ".xml";
+    }
+
+    /**
+     * calls the xml pretty printers
+     * @param file
+     */
+    protected void prettifyFile(File file) {
+        XMLPrettyPrinter.prettify(file);
+    }
+
+}

Modified: webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/writer/ClassWriter.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/writer/ClassWriter.java?rev=411720&r1=411719&r2=411720&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
Mon Jun  5 02:04:48 2006
@@ -19,7 +19,6 @@
 import org.apache.axis2.i18n.Messages;
 import org.apache.axis2.util.FileWriter;
 import org.apache.axis2.util.XSLTTemplateProcessor;
-import org.apache.axis2.util.PrettyPrinter;
 import org.apache.axis2.wsdl.codegen.CodeGenerationException;
 import org.apache.axis2.wsdl.i18n.CodegenMessages;
 import org.apache.axis2.wsdl.util.ConfigPropertyFileLoader;
@@ -169,9 +168,6 @@
                     resolver);
             this.stream.flush();
             this.stream.close();
-            if ("java".equals(language) && outputFile != null) {
-                PrettyPrinter.prettify(outputFile);
-            }
         }
     }
 }

Modified: webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/util/ConfigPropertyFileLoader.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/util/ConfigPropertyFileLoader.java?rev=411720&r1=411719&r2=411720&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/util/ConfigPropertyFileLoader.java
(original)
+++ webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/util/ConfigPropertyFileLoader.java
Mon Jun  5 02:04:48 2006
@@ -23,6 +23,7 @@
     private static Map dbSupporterTemplateNameMap;
     private static String testObjectTemplateName;
     private static String[] extensionClassNames;
+    private static String[] postExtensionClassNames;
     private static String[] thirdPartySchemaNames;
     private static String[] languageTypes;
     private static String[] databindingFrameworkNames;
@@ -35,6 +36,7 @@
 
 
     private static final String CODE_GEN_KEY_PREFIX = "codegen.extension";
+    private static final String POST_CODE_GEN_KEY_PREFIX = "post.codegen.extension";
     private static final String THIRD_PARTY_SCHEMA_KEY_PREFIX = "codegen.thirdparty.schema";
     private static final String LANGUAGE_TYPE_KEY_PREFIX = "codegen.languages";
     private static final String DEFAULT_LANGUAGE_TYPE_KEY = "codegen.languages.default";
@@ -124,6 +126,12 @@
 
             }
 
+            //load the post extension class names
+            tempString = props.getProperty(POST_CODE_GEN_KEY_PREFIX);
+            if (tempString != null) {
+                postExtensionClassNames = tempString.split(SEPARATOR_CHAR);
+
+            }
             //load the data binding framework names
             tempString = props.getProperty(DATA_BINDING_FRAMEWORK_NAME_KEY);
             if (tempString != null) {
@@ -251,13 +259,19 @@
     }
     /**
      * Gets the extension class names.
-     *
      * @return Returns String[].
      */
     public static String[] getExtensionClassNames() {
         return extensionClassNames;
     }
 
+    /**
+     * get the post extension class names
+     * @return Returns String[].
+     */
+     public static String[] getPostExtensionClassNames() {
+        return postExtensionClassNames;
+    }
     /**
      * Gets the third party schema names list.
      *

Modified: webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/PrettyPrinter.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/PrettyPrinter.java?rev=411720&r1=411719&r2=411720&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/PrettyPrinter.java
(original)
+++ webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/PrettyPrinter.java
Mon Jun  5 02:04:48 2006
@@ -16,7 +16,6 @@
 
 package org.apache.axis2.util;
 
-import org.apache.axis2.util.Loader;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -26,6 +25,8 @@
 
 /**
  * Tidies up the java source code using Jalopy.
+ * This is used by both ADB and Codegen hence needs to be in the
+ * commons rather than a specific module
  */
 public class PrettyPrinter {
 	private static final Log log = LogFactory.getLog(PrettyPrinter.class);

Added: webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/XMLPrettyPrinter.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/XMLPrettyPrinter.java?rev=411720&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/XMLPrettyPrinter.java
(added)
+++ webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/XMLPrettyPrinter.java
Mon Jun  5 02:04:48 2006
@@ -0,0 +1,106 @@
+package org.apache.axis2.util;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.lang.reflect.Method;
+/*
+ * Copyright 2004,2005 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.
+ */
+/**
+ *   An XML pretty printer based on jtidy (http://sourceforge.net/projects/jtidy)
+ *   The Jtidy jar needs to be in classpath for this to work and can be found at
+ *   http://sourceforge.net/project/showfiles.php?group_id=13153
+ * 
+ */
+public class XMLPrettyPrinter {
+
+    private static final Log log = LogFactory.getLog(XMLPrettyPrinter.class);
+    private static final String PRETTIFIED_SUFFIX = ".prettyfied";
+
+
+    /**
+     * Pretty prints contents of the java source file.
+     *
+     * @param file
+     */
+    public static void prettify(File file) {
+        try{
+            // Create an instance of the Jalopy bean
+            Class clazz = Loader.loadClass("org.w3c.tidy.Tidy");
+            Object prettifier = clazz.newInstance();
+
+            //set the input to be xml
+            Method setXmlInFlagMethod = clazz.getMethod(
+                    "setXmlTags",
+                    new Class[]{boolean.class});
+            setXmlInFlagMethod.invoke(prettifier,
+                    new Object[]{Boolean.TRUE});
+
+            //set the output to be xml
+            Method setXmlOutFlagMethod = clazz.getMethod(
+                    "setXmlOut",
+                    new Class[]{boolean.class});
+            setXmlOutFlagMethod.invoke(prettifier,
+                    new Object[]{Boolean.TRUE});
+
+            //create the input stream
+            InputStream input = new FileInputStream(file);
+
+            //create a new file with "prettyfied"  attached
+            // to existing file name
+            String existingFileName = file.getAbsolutePath();
+            String tempFileName = existingFileName + PRETTIFIED_SUFFIX;
+
+            File tempFile = new File(tempFileName);
+            FileOutputStream tempFileOutputStream = new FileOutputStream(tempFile);
+            //Call the pretty printer
+            Method parsr = clazz.getMethod("parse", new Class[]{
+                    InputStream.class,
+                    OutputStream.class});
+
+            parsr.invoke(prettifier, new Object[]{input,
+                    tempFileOutputStream});
+
+            //rename and restore the pretty printed one as the original
+
+            //first close the streams. if not this may cause the
+            // files not to be renamed
+            input.close();
+            tempFileOutputStream.close();
+            //delete the original
+            file.delete();
+
+            if (!tempFile.renameTo(new File(existingFileName))){
+                throw new Exception("File renaming failed!" + existingFileName);
+            }
+            log.debug("Pretty printed file : " + file);
+        } catch (ClassNotFoundException e) {
+            log.info("Tidy not found - unable to pretty print " + file);
+        } catch (Exception e) {
+            log.warn("Exception occurred while trying to pretty print file " + file, e);
+        } catch (Throwable t) {
+            log.debug("Exception occurred while trying to pretty print file " + file, t);
+        }
+
+    }
+
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: axis-cvs-unsubscribe@ws.apache.org
For additional commands, e-mail: axis-cvs-help@ws.apache.org


Mime
View raw message