Exemplo n.º 1
0
  public FWSDL(File wsdlFile) throws MalformedURLException, Exception {
    log.info("Reading WSDL definition from " + wsdlFile);

    Definition wsdlDef = new WSDLReaderImpl().readWSDL(wsdlFile.getAbsolutePath());
    Map<?, ?> wsdlNamespaces = wsdlDef.getNamespaces();

    if (wsdlDef.getTypes() != null) {
      List<?> exEls = wsdlDef.getTypes().getExtensibilityElements();
      for (int i = 0; i < exEls.size(); i++) {
        Object a = exEls.get(i);

        /* Ignore Types != an XML Schema */
        if (javax.wsdl.extensions.schema.Schema.class.isAssignableFrom(a.getClass())) {
          javax.wsdl.extensions.schema.Schema containedSchema =
              (javax.wsdl.extensions.schema.Schema) a;

          log.debug("Found XMLSchema in WSDL: " + containedSchema);

          /* This is the root node of the XML Schema tree */
          Element schemaElement = containedSchema.getElement();

          /*
           * Go through all parent namespace declarations and copy
           * them into the tree of the XMLSchema.
           * (probably a little hacky)
           */
          for (Object key : wsdlNamespaces.keySet()) {
            String nsShort = (String) key;
            String nsLong = (String) wsdlNamespaces.get(key);

            if (schemaElement.getAttribute("xmlns:" + nsShort).length() == 0) {
              schemaElement.setAttribute("xmlns:" + nsShort, nsLong);
            }
          }

          /* Fall back on global defininiton */
          if (schemaElement.getAttribute("targetNamespace").length() == 0) {
            schemaElement.setAttribute("targetNamespace", wsdlDef.getTargetNamespace());
          }

          /*
           * TODO: more things to take care of when cutting out the
           * schema?
           */
          Schema schemaDocument = SchemaDocument.Factory.parse(schemaElement).getSchema();
          log.debug("Adding schema to FSchema: " + schemaDocument);
          schema.addSchema(schemaDocument, wsdlFile.toURI());
        }

        // log.debug("Ignoring unknown ExtensibilityElement in WSDL (type: " + a.getClass() + ") -->
        // " + a);
      }
    }
  }
Exemplo n.º 2
0
  public Main(String[] args) {

    // Create the command line parser
    CommandLineParser parser = new PosixParser();
    Options options = new Options();
    options.addOption("x", "xsd", true, "The XML Schema file to load");
    options.addOption("w", "wsdl", true, "The WSDL file to load");
    options.addOption("p", "properties", true, "The properties file to configure the modules");
    options.addOption("m", "modules", true, "Comma-separated list of modules to run");
    options.addOption("v", "verbose", false, "Verbose logging output");
    options.addOption("h", "help", false, "Help output");
    options.addOption("o", "output", true, "The code output directory");

    // Load all modules
    try {
      registerModules();
    } catch (Exception e) {
      LOGGER.error("", e);
      System.exit(1);
    }

    // Parse the command line
    try {
      CommandLine line = parser.parse(options, args);

      // Check if verbose output should be used
      if (line.hasOption('v')) {
        Logger.getRootLogger().setLevel(Level.DEBUG);
      } else {
        Logger.getRootLogger().setLevel(Level.INFO);
      }

      // Output help and exit
      if (line.hasOption('h')) {
        usage(options, registry);
        System.exit(0);
      }

      // Load properties from file
      if (line.hasOption('p')) {
        String propertiesFile = line.getOptionValue('p');
        LOGGER.debug("Loading properties from '" + propertiesFile + "'.");
        properties.load(new FileReader(new File(propertiesFile)));
      }

      // Set code output directory
      if (line.hasOption('o')) {
        String oValue = line.getOptionValue('o');

        // Ensure that last character is the file.separator
        if (!oValue.endsWith(System.getProperty("file.separator"))) {
          oValue += System.getProperty("file.separator");
        }

        File fileOutput = new File(oValue);

        if (!fileOutput.exists()) {
          LOGGER.error("The desired output directory '" + oValue + "' does not exist.");
          System.exit(0);
        } else if (!fileOutput.isDirectory()) {
          LOGGER.error("The desired output directory '" + oValue + "' is a file.");
          System.exit(0);
        } else {
          LOGGER.debug("Setting output directory to '" + oValue + "'.");
          properties.setProperty(
              "fabric.output_directory", oValue); // Add code output directory to properties
        }
      }

      workspace = new Workspace(properties);

      // Create module instances
      if (line.hasOption('m')) {
        for (String moduleName : line.getOptionValue('m').split(",")) {
          moduleName = moduleName.trim();
          LOGGER.debug("Creating instance of module {}", moduleName);

          ArrayList<FItemHandlerBase> handlers =
              this.registry.get(moduleName).getHandlers(this.workspace);
          for (FItemHandlerBase handler : handlers) {
            // Add an XML Schema handler
            if (handler instanceof FSchemaTreeItemHandler) {
              FSchemaTreeItemHandler schemaHandler = (FSchemaTreeItemHandler) handler;
              this.schemaHandlers.add(schemaHandler);
            }
            // Add a WSDL handler
            else if (handler instanceof FWSDLItemHandler) {
              FWSDLItemHandler wsdlHandler = (FWSDLItemHandler) handler;
              this.wsdlHandlers.add(wsdlHandler);
            }
            // Catch unknown handler
            else {
              throw new IllegalStateException(
                  String.format(
                      "Module provided unknown handler type '%s'.",
                      handler.getClass().getSimpleName()));
            }
          }
        }
      }

      if (line.hasOption('x') && line.hasOption('w')) {
        throw new Exception("Only one of -x or -w is allowed");
      } else if (line.hasOption('x')) {
        schemaFile = new File(line.getOptionValue('x'));
        properties.setProperty(
            "fabric.xsd", line.getOptionValue('x')); // Add path of XSD file to properties
      } else if (line.hasOption('w')) {
        wsdlFile = new File(line.getOptionValue('w'));
        properties.setProperty(
            "fabric.wsdl", line.getOptionValue('w')); // Add path of WSDL file to properties
      } else {
        throw new Exception("Supply one of -x or -w");
      }

    } catch (Exception e) {
      LOGGER.error("Invalid command line: " + e, e);
      usage(options, registry);
      System.exit(1);
    }

    // Handle the different file types
    try {
      FSchema schema = null;
      FWSDL wsdl = null;

      // Create FSchema object
      if (null != this.schemaFile) {
        schema = new FSchema(this.schemaFile);
      }
      // Create FWSDL object
      else if (null != this.wsdlFile) {
        wsdl = new FWSDL(this.wsdlFile);

        // Extract inline XML Schema (if any)
        if (null != wsdl.getSchema()) {
          schema = wsdl.getSchema();
        }
      }

      // Walk XML Schema tree (if any)
      if (null != schema) {
        LOGGER.debug("Walking XML Schema tree.");
        System.out.println(schema.toString());

        FSchemaTreeWalker treeWalker = new FSchemaTreeWalker();
        for (FSchemaTreeItemHandler schemaHandler : this.schemaHandlers) {
          treeWalker.walk(schema, schemaHandler);
        }
      }

      // Process WSDL elements (if any)
      if (null != wsdl) {
        LOGGER.debug("Handling WSDL elements.");
        System.out.println(wsdl.toString());

        FWSDLProcessor processor = new FWSDLProcessor();
        for (FWSDLItemHandler wsdlHandler : this.wsdlHandlers) {
          processor.process(wsdl, wsdlHandler);
        }
      }

      // Generate code from workspace
      this.workspace.generate();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }