< prev index next >

src/jdk.xml.bind/share/classes/com/sun/tools/internal/jxc/SchemaGenerator.java

Print this page


   1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  40 import java.io.File;
  41 import java.lang.reflect.InvocationTargetException;
  42 import java.lang.reflect.Method;
  43 import java.net.MalformedURLException;
  44 import java.net.URISyntaxException;
  45 import java.net.URL;
  46 import java.net.URLClassLoader;
  47 import java.util.ArrayList;
  48 import java.util.Collection;
  49 import java.util.Collections;
  50 import java.util.List;
  51 import java.util.logging.Level;
  52 import java.util.logging.Logger;
  53 
  54 /**
  55  * CLI entry-point to the schema generator.
  56  *
  57  * @author Bhakti Mehta
  58  */
  59 public class SchemaGenerator {



  60     /**
  61      * Runs the schema generator.
  62      */
  63     public static void main(String[] args) throws Exception {
  64         System.exit(run(args));
  65     }
  66 
  67     public static int run(String[] args) throws Exception {
  68         try {
  69             ClassLoader cl = SecureLoader.getClassClassLoader(SchemaGenerator.class);
  70             if (cl==null) {
  71                 cl = SecureLoader.getSystemClassLoader();
  72             }
  73             return run(args, cl);
  74         } catch(Exception e) {
  75             System.err.println(e.getMessage());
  76             return -1;
  77         }
  78     }
  79 
  80     /**
  81      * Runs the schema generator.
  82      *
  83      * @param classLoader
  84      *      the schema generator will run in this classLoader.
  85      *      It needs to be able to load annotation processing and JAXB RI classes. Note that
  86      *      JAXB RI classes refer to annotation processing classes. Must not be null.
  87      *
  88      * @return
  89      *      exit code. 0 if success.
  90      *
  91      */
  92     public static int run(String[] args, ClassLoader classLoader) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
  93         final Options options = new Options();
  94         if (args.length ==0) {
  95             usage();


 189         if(url==null)       return null;    // impossible, but hey, let's be defensive
 190 
 191         if(!url.startsWith("jar:") || url.lastIndexOf('!')==-1)
 192             // no jar file
 193             return null;
 194 
 195         String jarFileUrl = url.substring(4,url.lastIndexOf('!'));
 196         if(!jarFileUrl.startsWith("file:"))
 197             return null;    // not from file system
 198 
 199         try {
 200             File f = new File(new URL(jarFileUrl).toURI());
 201             if (f.exists() && f.getName().endsWith(".jar")) { // see 6510966
 202                 return f.getPath();
 203             }
 204             f = new File(new URL(jarFileUrl).getFile());
 205             if (f.exists() && f.getName().endsWith(".jar")) { // this is here for potential backw. compatibility issues
 206                 return f.getPath();
 207             }
 208         } catch (URISyntaxException ex) {
 209             Logger.getLogger(SchemaGenerator.class.getName()).log(Level.SEVERE, null, ex);
 210         } catch (MalformedURLException ex) {
 211             Logger.getLogger(SchemaGenerator.class.getName()).log(Level.SEVERE, null, ex);
 212         }
 213         return null;
 214     }
 215 
 216     private static void usage( ) {
 217         System.out.println(Messages.USAGE.format());
 218     }
 219 
 220     public static final class Runner {
 221         public static boolean compile(String[] args, File episode) throws Exception {
 222 
 223             JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
 224             DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
 225             StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
 226             JavacOptions options = JavacOptions.parse(compiler, fileManager, args);
 227             List<String> unrecognizedOptions = options.getUnrecognizedOptions();
 228             if (!unrecognizedOptions.isEmpty())
 229                 Logger.getLogger(SchemaGenerator.class.getName()).log(Level.WARNING, "Unrecognized options found: {0}", unrecognizedOptions);

 230             Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(options.getFiles());
 231             JavaCompiler.CompilationTask task = compiler.getTask(
 232                     null,
 233                     fileManager,
 234                     diagnostics,
 235                     options.getRecognizedOptions(),
 236                     options.getClassNames(),
 237                     compilationUnits);
 238             com.sun.tools.internal.jxc.ap.SchemaGenerator r = new com.sun.tools.internal.jxc.ap.SchemaGenerator();
 239             if (episode != null)
 240                 r.setEpisodeFile(episode);
 241             task.setProcessors(Collections.singleton(r));
 242             return task.call();
 243         }
 244     }
 245 
 246     /**
 247           *  @author Peter von der Ahe
 248           */
 249     private static final class JavacOptions {


   1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  40 import java.io.File;
  41 import java.lang.reflect.InvocationTargetException;
  42 import java.lang.reflect.Method;
  43 import java.net.MalformedURLException;
  44 import java.net.URISyntaxException;
  45 import java.net.URL;
  46 import java.net.URLClassLoader;
  47 import java.util.ArrayList;
  48 import java.util.Collection;
  49 import java.util.Collections;
  50 import java.util.List;
  51 import java.util.logging.Level;
  52 import java.util.logging.Logger;
  53 
  54 /**
  55  * CLI entry-point to the schema generator.
  56  *
  57  * @author Bhakti Mehta
  58  */
  59 public class SchemaGenerator {
  60 
  61     private static final Logger LOGGER = Logger.getLogger(SchemaGenerator.class.getName());
  62 
  63     /**
  64      * Runs the schema generator.
  65      */
  66     public static void main(String[] args) throws Exception {
  67         System.exit(run(args));
  68     }
  69 
  70     public static int run(String[] args) throws Exception {
  71         try {
  72             ClassLoader cl = SecureLoader.getClassClassLoader(SchemaGenerator.class);
  73             if (cl==null) {
  74                 cl = SecureLoader.getSystemClassLoader();
  75             }
  76             return run(args, cl);
  77         } catch(Exception e) {
  78             LOGGER.log(Level.SEVERE, e.getMessage(), e);
  79             return -1;
  80         }
  81     }
  82 
  83     /**
  84      * Runs the schema generator.
  85      *
  86      * @param classLoader
  87      *      the schema generator will run in this classLoader.
  88      *      It needs to be able to load annotation processing and JAXB RI classes. Note that
  89      *      JAXB RI classes refer to annotation processing classes. Must not be null.
  90      *
  91      * @return
  92      *      exit code. 0 if success.
  93      *
  94      */
  95     public static int run(String[] args, ClassLoader classLoader) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
  96         final Options options = new Options();
  97         if (args.length ==0) {
  98             usage();


 192         if(url==null)       return null;    // impossible, but hey, let's be defensive
 193 
 194         if(!url.startsWith("jar:") || url.lastIndexOf('!')==-1)
 195             // no jar file
 196             return null;
 197 
 198         String jarFileUrl = url.substring(4,url.lastIndexOf('!'));
 199         if(!jarFileUrl.startsWith("file:"))
 200             return null;    // not from file system
 201 
 202         try {
 203             File f = new File(new URL(jarFileUrl).toURI());
 204             if (f.exists() && f.getName().endsWith(".jar")) { // see 6510966
 205                 return f.getPath();
 206             }
 207             f = new File(new URL(jarFileUrl).getFile());
 208             if (f.exists() && f.getName().endsWith(".jar")) { // this is here for potential backw. compatibility issues
 209                 return f.getPath();
 210             }
 211         } catch (URISyntaxException ex) {
 212             LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
 213         } catch (MalformedURLException ex) {
 214             LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
 215         }
 216         return null;
 217     }
 218 
 219     private static void usage( ) {
 220         System.out.println(Messages.USAGE.format());
 221     }
 222 
 223     public static final class Runner {
 224         public static boolean compile(String[] args, File episode) throws Exception {
 225 
 226             JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
 227             DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
 228             StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
 229             JavacOptions options = JavacOptions.parse(compiler, fileManager, args);
 230             List<String> unrecognizedOptions = options.getUnrecognizedOptions();
 231             if (!unrecognizedOptions.isEmpty()) {
 232                 LOGGER.log(Level.WARNING, "Unrecognized options found: {0}", unrecognizedOptions);
 233             }
 234             Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(options.getFiles());
 235             JavaCompiler.CompilationTask task = compiler.getTask(
 236                     null,
 237                     fileManager,
 238                     diagnostics,
 239                     options.getRecognizedOptions(),
 240                     options.getClassNames(),
 241                     compilationUnits);
 242             com.sun.tools.internal.jxc.ap.SchemaGenerator r = new com.sun.tools.internal.jxc.ap.SchemaGenerator();
 243             if (episode != null)
 244                 r.setEpisodeFile(episode);
 245             task.setProcessors(Collections.singleton(r));
 246             return task.call();
 247         }
 248     }
 249 
 250     /**
 251           *  @author Peter von der Ahe
 252           */
 253     private static final class JavacOptions {


< prev index next >