Example #1
0
  /** create and configure a S2XTransformer */
  static S2XTransformer createS2X(String name, String version, Configuration config)
      throws IllegalArgumentException, TransformerException {
    if (name == null) name = config.getParameter("defaultS2X");
    if (name == null)
      throw new TransformerException("Configuration error: " + "no defaultS2X defined");
    String clname = typeToClass.getProperty(name);
    if (clname == null) clname = name;
    if (clname == null)
      throw new IllegalArgumentException("No default S2XTransformer " + "class specified");

    Class s2xclass = null;
    try {
      s2xclass = Class.forName(clname);
    } catch (ClassNotFoundException e) {
      throw new IllegalArgumentException(clname + ": class not found");
    }
    if (!S2XTransformer.class.isAssignableFrom(s2xclass)) {
      throw new IllegalArgumentException(clname + ": not an S2XTransformer class");
    }

    S2XTransformer out = null;
    try {
      out = (S2XTransformer) Class.forName(clname).newInstance();
    } catch (Exception e) {
      throw new IllegalArgumentException(clname + ": not instantiable (" + e.getMessage() + ")");
    }

    int dot = name.indexOf(".");
    while (dot >= 0) {
      name = name.substring(dot + 1);
      dot = name.indexOf(".");
    }

    Configuration blck = findConfigSection(config, name, version);
    if (blck != null) {
      out.init(blck);
    } else {
      if (version.equals(config.getParameter("version"))) out.init(config);
      else
        throw new IllegalArgumentException(
            "Unable to find configuration " + "data for version " + version);
    }

    return out;
  }
Example #2
0
  public static void main(String[] args) {
    if (args.length == 0) {
      System.out.println("ConvertADQL: convert between ADQL/x (xml) " + "and ADQL/s (SQL)");
      printUsage(new PrintWriter(new OutputStreamWriter(System.out)));
      System.exit(0);
    }

    CmdLine cl = new CmdLine("Xx:Ss:o:t:c:v:", (CmdLine.RELAX | CmdLine.USRWARN));

    // parse the command line
    try {
      cl.setCmdLine(args);
    } catch (CmdLine.UnrecognizedOptionException ex) {
    }

    // check for option sanity
    if ((cl.isSet('x') || cl.isSet('X')) && (cl.isSet('s') || cl.isSet('S'))) {
      System.err.print("ConvertADQL: can't specify both XML (-x|-X) ");
      System.err.println("and string (-s|-S) input!\n");
      printUsage(null);
      System.exit(1);
    }
    if (!(cl.isSet('x') || cl.isSet('X') || (cl.isSet('s') || cl.isSet('S')))) {
      System.err.print("ConvertADQL: no input type specified ");
      System.err.println("(-x|-X|-s|-S)\n");
      printUsage(null);
      System.exit(1);
    }

    String version = DEFAULT_VERSION;
    if (cl.isSet('v')) version = cl.getValue('v');

    try {
      // get the configuration
      Configuration config = null;
      if (cl.isSet('c')) {
        File cfile = new File(cl.getValue('c'));
        config = new Configuration(cfile.getAbsolutePath(), null);
      }
      if (config == null) {
        config = new Configuration("conf/ConvertADQL.xml", (new ConvertADQL()).getClass());
      }
      if (config == null) {
        System.err.print("ConvertADQL: Can't find configuration ");
        System.err.println("file");
        System.exit(2);
      }

      // set output stream
      PrintWriter out = null;
      if (cl.isSet('o')) {
        File ofile = new File(cl.getValue('o'));
        if (ofile.exists() && !ofile.canWrite()) {
          System.err.println("ConvertADQL: " + ofile + ": permission denied");
          System.exit(3);
        }
        out = new PrintWriter(new FileWriter(ofile));
      }
      if (out == null) out = new PrintWriter(new OutputStreamWriter(System.out));

      // converting ADQL/x to ADQL/s
      if (cl.isSet('x') || cl.isSet('X')) {
        Reader in = null;

        if (cl.isSet('x')) {
          in = new FileReader(cl.getValue('x'));
        } else {
          in = new InputStreamReader(System.in);
        }

        X2STransformer cvt = createX2S(cl.getValue('t'), version, config);
        String adqls = cvt.transform(new StreamSource(in));
        out.println(adqls);
        out.flush();
      }

      // converting ADQL/s to ADQL/x
      else if (cl.isSet('s') || cl.isSet('S')) {
        String adqls = null;
        if (cl.isSet('s')) {

          // get input string from a file
          adqls = slurp(new FileReader(cl.getValue('s')));

        } else if (cl.getNumArgs() > 0) {

          // get input string from command line
          StringBuffer sb = new StringBuffer();
          for (Enumeration e = cl.arguments(); e.hasMoreElements(); ) {
            sb.append(e.nextElement());
            if (e.hasMoreElements()) sb.append(' ');
          }
          adqls = sb.toString();

        } else {

          // get input string from standard input
          adqls = slurp(new InputStreamReader(System.in));
        }

        S2XTransformer cvt = createS2X(cl.getValue('t'), version, config);
        cvt.transform(adqls, new StreamResult(out));
        out.flush();
      }
    } catch (FileNotFoundException e) {
      System.err.println("ConvertADQL: " + e.getMessage() + ": file not found");
      System.exit(5);
    } catch (SAXException e) {
      System.err.println("ConvertADQL: error reading configuration " + "file: " + e.getMessage());
      System.exit(6);
    } catch (IOException e) {
      System.err.println("ConvertADQL: I/O error: " + e.getMessage());
      System.exit(7);
    } catch (TransformerException e) {
      System.err.println(
          "ConvertADQL: conversion failed (ADQL syntax " + "error?): " + e.getMessage());
      System.exit(8);
    } catch (IllegalArgumentException e) {
      System.err.println("ConvertADQL: problem engaging transformer: " + e.getMessage());
      System.exit(8);
    }
  }