Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
4.8 kB
3
Indexable
Never
package META-INF.versions.9.com.sun.tools.jxc;

import com.sun.tools.jxc.Messages;
import com.sun.tools.jxc.SecureLoader;
import com.sun.tools.jxc.ap.Options;
import com.sun.tools.xjc.BadCommandLineException;
import com.sun.xml.bind.util.Which;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;

public class SchemaGenerator {
  private static final Logger LOGGER = Logger.getLogger(com.sun.tools.jxc.SchemaGenerator.class.getName());
  
  public static void main(String[] args) throws Exception {
    System.exit(run(args));
  }
  
  public static int run(String[] args) throws Exception {
    try {
      ClassLoader cl = SecureLoader.getClassClassLoader(com.sun.tools.jxc.SchemaGenerator.class);
      if (cl == null)
        cl = SecureLoader.getSystemClassLoader(); 
      return run(args, cl);
    } catch (Exception e) {
      LOGGER.log(Level.SEVERE, e.getMessage(), e);
      return -1;
    } 
  }
  
  public static int run(String[] args, ClassLoader classLoader) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    Options options = new Options();
    if (args.length == 0) {
      usage();
      return -1;
    } 
    for (String arg : args) {
      if (arg.equals("-help")) {
        usage();
        return -1;
      } 
      if (arg.equals("-version")) {
        System.out.println(Messages.VERSION.format(new Object[0]));
        return -1;
      } 
      if (arg.equals("-fullversion")) {
        System.out.println(Messages.FULLVERSION.format(new Object[0]));
        return -1;
      } 
    } 
    try {
      options.parseArguments(args);
    } catch (BadCommandLineException e) {
      System.out.println(e.getMessage());
      System.out.println();
      usage();
      return -1;
    } 
    Class<?> schemagenRunner = classLoader.loadClass(Runner.class.getName());
    Method compileMethod = schemagenRunner.getDeclaredMethod("compile", new Class[] { String[].class, File.class });
    List<String> aptargs = new ArrayList<>();
    if (options.encoding != null) {
      aptargs.add("-encoding");
      aptargs.add(options.encoding);
    } 
    aptargs.add("-cp");
    aptargs.add(setClasspath(options.classpath));
    aptargs.add("--add-modules");
    aptargs.add("java.xml.bind");
    if (options.targetDir != null) {
      aptargs.add("-d");
      aptargs.add(options.targetDir.getPath());
    } 
    aptargs.addAll(options.arguments);
    String[] argsarray = aptargs.<String>toArray(new String[aptargs.size()]);
    return ((Boolean)compileMethod.invoke(null, new Object[] { argsarray, options.episodeFile })).booleanValue() ? 0 : 1;
  }
  
  private static String setClasspath(String givenClasspath) {
    StringBuilder cp = new StringBuilder();
    appendPath(cp, givenClasspath);
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    while (cl != null) {
      if (cl instanceof URLClassLoader)
        for (URL url : ((URLClassLoader)cl).getURLs()) {
          try {
            appendPath(cp, (new File(url.toURI())).getPath());
          } catch (URISyntaxException ex) {
            LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
          } 
        }  
      cl = cl.getParent();
    } 
    appendPath(cp, findJaxbApiJar());
    return cp.toString();
  }
  
  private static void appendPath(StringBuilder cp, String url) {
    if (url == null || url.trim().isEmpty())
      return; 
    if (cp.length() != 0)
      cp.append(File.pathSeparatorChar); 
    cp.append(url);
  }
  
  private static String findJaxbApiJar() {
    String url = Which.which(JAXBContext.class);
    if (url == null)
      return null; 
    if (!url.startsWith("jar:") || url.lastIndexOf('!') == -1)
      return null; 
    String jarFileUrl = url.substring(4, url.lastIndexOf('!'));
    if (!jarFileUrl.startsWith("file:"))
      return null; 
    try {
      File f = new File((new URL(jarFileUrl)).toURI());
      if (f.exists() && f.getName().endsWith(".jar"))
        return f.getPath(); 
      f = new File((new URL(jarFileUrl)).getFile());
      if (f.exists() && f.getName().endsWith(".jar"))
        return f.getPath(); 
    } catch (URISyntaxException ex) {
      LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
    } catch (MalformedURLException ex) {
      LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
    } 
    return null;
  }
  
  private static void usage() {
    System.out.println(Messages.USAGE.format(new Object[0]));
  }
}