cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject [2/4] cxf-xjc-utils git commit: Convenience option for configuring the code generation from XSD files: Instead of having to explicitly list every single file using the '<xsd>' element, now the new '<xsdDir>' element can be used instead for specifying a d
Date Wed, 08 Jul 2015 20:42:42 GMT
Convenience option for configuring the code generation from XSD files:
Instead of having to explicitly list every single file using the '<xsd>'
element, now the new '<xsdDir>' element can be used instead for
specifying a directory. From this directory all '*.xsd' files will be
used for code generation.


Project: http://git-wip-us.apache.org/repos/asf/cxf-xjc-utils/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf-xjc-utils/commit/fb0162e3
Tree: http://git-wip-us.apache.org/repos/asf/cxf-xjc-utils/tree/fb0162e3
Diff: http://git-wip-us.apache.org/repos/asf/cxf-xjc-utils/diff/fb0162e3

Branch: refs/heads/master
Commit: fb0162e35f313bebc0d92ab089f0905ade7f2b79
Parents: 48db284
Author: Martin Dietze <mdietze@gmail.com>
Authored: Wed Jun 17 15:12:38 2015 +0200
Committer: Martin Dietze <martin.dietze@dermalog.com>
Committed: Wed Jun 17 15:12:38 2015 +0200

----------------------------------------------------------------------
 .../cxf/maven_plugin/AbstractXSDToJavaMojo.java | 187 +++++++++++--------
 .../org/apache/cxf/maven_plugin/XsdOption.java  |   7 +
 2 files changed, 118 insertions(+), 76 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf-xjc-utils/blob/fb0162e3/cxf-xjc-plugin/src/main/java/org/apache/cxf/maven_plugin/AbstractXSDToJavaMojo.java
----------------------------------------------------------------------
diff --git a/cxf-xjc-plugin/src/main/java/org/apache/cxf/maven_plugin/AbstractXSDToJavaMojo.java
b/cxf-xjc-plugin/src/main/java/org/apache/cxf/maven_plugin/AbstractXSDToJavaMojo.java
index 371bc22..dfbe3c4 100644
--- a/cxf-xjc-plugin/src/main/java/org/apache/cxf/maven_plugin/AbstractXSDToJavaMojo.java
+++ b/cxf-xjc-plugin/src/main/java/org/apache/cxf/maven_plugin/AbstractXSDToJavaMojo.java
@@ -20,6 +20,7 @@
 package org.apache.cxf.maven_plugin;
 
 import java.io.File;
+import java.io.FilenameFilter;
 import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URI;
@@ -167,97 +168,127 @@ public abstract class AbstractXSDToJavaMojo extends AbstractMojo {
     
         for (int x = 0; x < xsdOptions.length; x++) {
             ClassLoader origLoader = Thread.currentThread().getContextClassLoader();
-            try {
-                URI xsdURI = mapLocation(xsdOptions[x].getXsd());
-                URI basedir = project.getBasedir().toURI();
-                
-                String doneFileName = xsdURI.toString();
-                if (doneFileName.startsWith(basedir.toString())) {
-                    doneFileName = doneFileName.substring(basedir.toString().length());
-                }
-                
-                doneFileName = doneFileName.replace('?', '_')
-                    .replace('&', '_').replace('/', '_').replace('\\', '_')
-                    .replace(':', '_').replace('!', '_');
-                
-                // If URL to WSDL, replace ? and & since they're invalid chars for file
names
-                File doneFile =
-                    new File(markerDirectory, "." + doneFileName + ".DONE");
-                
-                long srctimestamp = 0;
-                if ("file".equals(xsdURI.getScheme())) {
-                    srctimestamp = new File(xsdURI).lastModified();
-                } else {
-                    try {
-                        srctimestamp = xsdURI.toURL().openConnection().getDate();
-                    } catch (Exception e) {
-                        //ignore
+            final String[] xsdFiles = getXsdFiles(xsdOptions[x].getXsdDir(), xsdOptions[x].getXsd());
+            for (String xsdFile : xsdFiles) {
+                try {
+                    URI xsdURI = mapLocation(xsdFile);
+                    URI basedir = project.getBasedir().toURI();
+
+                    String doneFileName = xsdURI.toString();
+                    if (doneFileName.startsWith(basedir.toString())) {
+                        doneFileName = doneFileName.substring(basedir.toString().length());
                     }
-                }
-                if (xsdOptions[x].getBindingFile() != null) { 
-                    URI bindingURI = mapLocation(xsdOptions[x].getBindingFile());
-                    if ("file".equals(bindingURI.getScheme())) {
-                        long bts = new File(bindingURI).lastModified();
-                        if (bts > srctimestamp) {
-                            srctimestamp = bts;
+
+                    doneFileName = doneFileName.replace('?', '_')
+                        .replace('&', '_').replace('/', '_').replace('\\', '_')
+                        .replace(':', '_').replace('!', '_');
+
+                    // If URL to WSDL, replace ? and & since they're invalid chars for
file names
+                    File doneFile =
+                        new File(markerDirectory, "." + doneFileName + ".DONE");
+
+                    long srctimestamp = 0;
+                    if ("file".equals(xsdURI.getScheme())) {
+                        srctimestamp = new File(xsdURI).lastModified();
+                    } else {
+                        try {
+                            srctimestamp = xsdURI.toURL().openConnection().getDate();
+                        } catch (Exception e) {
+                            //ignore
                         }
                     }
-                }
-
-                boolean doWork = false;
-                if (!doneFile.exists()) {
-                    doWork = true;
-                } else if (srctimestamp > doneFile.lastModified()) {
-                    doWork = true;
-                } else {
-                    File files[] = xsdOptions[x].getDependencies();
-                    if (files != null) {
-                        for (int z = 0; z < files.length; ++z) {
-                            if (files[z].lastModified() > doneFile.lastModified()) {
-                                doWork = true;
+                    if (xsdOptions[x].getBindingFile() != null) { 
+                        URI bindingURI = mapLocation(xsdOptions[x].getBindingFile());
+                        if ("file".equals(bindingURI.getScheme())) {
+                            long bts = new File(bindingURI).lastModified();
+                            if (bts > srctimestamp) {
+                                srctimestamp = bts;
                             }
                         }
                     }
-                }
-                
-                if (doWork) {
-                    try {
+
+                    boolean doWork = false;
+                    if (!doneFile.exists()) {
+                        doWork = true;
+                    } else if (srctimestamp > doneFile.lastModified()) {
+                        doWork = true;
+                    } else {
                         File files[] = xsdOptions[x].getDependencies();
                         if (files != null) {
                             for (int z = 0; z < files.length; ++z) {
                                 if (files[z].lastModified() > doneFile.lastModified())
{
-                                    buildContext.removeMessages(files[z]);
+                                    doWork = true;
                                 }
                             }
                         }
-                        removeMessages(xsdOptions[x].getXsd());
-                        removeMessages(xsdOptions[x].getBindingFile());
-                        int i = run(xsdOptions[x], outputDir);
-                        if (i == 0) {
-                            doneFile.delete();
-                            doneFile.createNewFile();
-                        }
-                        File dirs[] = xsdOptions[x].getDeleteDirs();
-                        if (dirs != null) {
-                            for (int idx = 0; idx < dirs.length; ++idx) {
-                                result = result && deleteDir(dirs[idx]);
+                    }
+
+                    if (doWork) {
+                        try {
+                            File files[] = xsdOptions[x].getDependencies();
+                            if (files != null) {
+                                for (int z = 0; z < files.length; ++z) {
+                                    if (files[z].lastModified() > doneFile.lastModified())
{
+                                        buildContext.removeMessages(files[z]);
+                                    }
+                                }
+                            }
+                            removeMessages(xsdFile);
+                            removeMessages(xsdOptions[x].getBindingFile());
+                            int i = run(xsdOptions[x], xsdFile, outputDir);
+                            if (i == 0) {
+                                doneFile.delete();
+                                doneFile.createNewFile();
                             }
+                            File dirs[] = xsdOptions[x].getDeleteDirs();
+                            if (dirs != null) {
+                                for (int idx = 0; idx < dirs.length; ++idx) {
+                                    result = result && deleteDir(dirs[idx]);
+                                }
+                            }
+                            buildContext.refresh(outputDirFile);
+                        } catch (Exception e) {
+                            throw new MojoExecutionException(e.getMessage(), e);
                         }
-                        buildContext.refresh(outputDirFile);
-                    } catch (Exception e) {
-                        throw new MojoExecutionException(e.getMessage(), e);
                     }
+
+                    if (!result) {
+                        throw new MojoExecutionException("Could not delete redundant dirs");
+                    }
+                } finally {
+                    Thread.currentThread().setContextClassLoader(origLoader);
                 }
-            
-                if (!result) {
-                    throw new MojoExecutionException("Could not delete redundant dirs");
-                }  
-            } finally {
-                Thread.currentThread().setContextClassLoader(origLoader);
             }
         }
     }
     
+    private String[] getXsdFiles(String xsdDir, String xsd) throws MojoExecutionException
{
+        final String[] xsdFiles;
+        if (xsdDir != null && !xsdDir.isEmpty()) {
+            File dir = new File(xsdDir);
+            if (!dir.isDirectory()) {
+                throw new MojoExecutionException("Error, xsdDir \"" + xsdDir + "\" does not
exist.");
+            }  
+            String[] fileList = dir.list(new FilenameFilter() {
+                @Override
+                public boolean accept(File dir, String name) {
+                    return name.endsWith(".xsd");
+                }
+            });
+            if (fileList == null || fileList.length == 0) {
+                throw new MojoExecutionException("Error, xsdDir \"" + xsdDir + "\" does not
contain any *.xsd files.");
+            }
+            xsdFiles = new String[fileList.length];
+            for (int i = 0; i < fileList.length; ++i) {
+                xsdFiles[i] = xsdDir + (xsdDir.endsWith(File.separator) ? "" : File.separator)
+ fileList[i];
+            }
+        } else {
+            xsdFiles = new String[1];
+            xsdFiles[0] = xsd;
+        }
+        return xsdFiles;
+    }
+    
     private List<File> resolve(String artifactDescriptor) {
         String[] s = artifactDescriptor.split(":");
 
@@ -293,12 +324,12 @@ public abstract class AbstractXSDToJavaMojo extends AbstractMojo {
         return project.getCompileClasspathElements();
     }
     
-    private int run(XsdOption option, String outputDir) throws Exception {
+    private int run(XsdOption option, String xsdFile, String outputDir) throws Exception
{
         if (!fork) {
             String[] args = getArguments(option, outputDir);
             this.getLog().debug("Args: " + Arrays.asList(args));
             XJCErrorListener listener = new XJCErrorListener(buildContext);
-            int i = new XSDToJavaRunner(args, listener, new File(option.getXsd()), getClasspathElements()).run();
+            int i = new XSDToJavaRunner(args, listener, new File(xsdFile), getClasspathElements()).run();
             if (i != 0 && listener.getFirstError() != null) {
                 throw listener.getFirstError();
             }
@@ -363,10 +394,14 @@ public abstract class AbstractXSDToJavaMojo extends AbstractMojo {
         if (getLog().isDebugEnabled()) {
             list.add("-verbose");            
         }
-        list.add("-d");
-        list.add(outputDir);
-        list.add(mapLocation(option.getXsd()).toString());
-       
+
+        String[] xsdFiles = getXsdFiles(option.getXsdDir(), option.getXsd());
+        for (String xsdFile : xsdFiles) {
+            list.add("-d");
+            list.add(outputDir);
+            list.add(mapLocation(xsdFile).toString());
+        }
+
         return list.toArray(new String[list.size()]);
         
     }

http://git-wip-us.apache.org/repos/asf/cxf-xjc-utils/blob/fb0162e3/cxf-xjc-plugin/src/main/java/org/apache/cxf/maven_plugin/XsdOption.java
----------------------------------------------------------------------
diff --git a/cxf-xjc-plugin/src/main/java/org/apache/cxf/maven_plugin/XsdOption.java b/cxf-xjc-plugin/src/main/java/org/apache/cxf/maven_plugin/XsdOption.java
index d1f1a6b..4329b39 100644
--- a/cxf-xjc-plugin/src/main/java/org/apache/cxf/maven_plugin/XsdOption.java
+++ b/cxf-xjc-plugin/src/main/java/org/apache/cxf/maven_plugin/XsdOption.java
@@ -24,6 +24,7 @@ import java.util.List;
 
 public class XsdOption {
     String xsd;
+    String xsdDir;
     String packagename;
     String bindingFile;
     File dependencies[];
@@ -44,6 +45,12 @@ public class XsdOption {
     public void setXsd(String x) {
         this.xsd = x;
     }
+    public String getXsdDir() {
+        return xsdDir;
+    }
+    public void setXsdDir(String x) {
+        this.xsdDir = x;
+    }
     public String getBindingFile() {
         return bindingFile;
     }


Mime
View raw message