Beispiel #1
0
  public static MiningResult importFile(InputStream input) throws IOException {
    try {
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      Document doc;
      // NodeList netNodes;
      dbf.setValidating(false);
      dbf.setIgnoringComments(true);
      dbf.setIgnoringElementContentWhitespace(true);
      // dbf.setExpandEntityReferences(false);
      // dbf.setNamespaceAware(false);

      DocumentBuilder db = dbf.newDocumentBuilder();

      db.setEntityResolver(
          new EntityResolver() {
            public InputSource resolveEntity(String publicId, String systemId) {
              if (systemId.indexOf("ARIS-Export") != -1) {
                return new InputSource("file:" + About.EXTLIBLOCATION() + "ARIS-Export101.dtd");
              } else {
                return null;
              }
            }
          });

      InputSource inpStream = new InputSource(input);
      inpStream.setSystemId("file:" + System.getProperty("user.dir", ""));
      doc = db.parse(inpStream);

      // check if root element is a aml tag
      Message.add("parsing done" + doc, Message.DEBUG);
      if (!(doc.getDocumentElement().getNodeName().equals("AML"))) {
        Message.add("aml tag not found", Message.ERROR);
        throw new Exception("aml tag not found");
      } else {
        Message.add("aml root element found");
      }

      EPCResult result = new EPCResult(null, (EPC) null);
      HashMap ObjDef_LinkId = new HashMap();
      HashMap modelid_net = new HashMap();
      HashMap ObjDef_Name = new HashMap();
      HashMap function_LinkId = new HashMap();
      HashMap ModelId_ModelType = new HashMap();
      traverseAMLforObjectNames(
          ObjDef_Name, doc.getDocumentElement(), ObjDef_LinkId, ModelId_ModelType);
      Iterator findLinkToEpc = ObjDef_LinkId.keySet().iterator();
      while (findLinkToEpc.hasNext()) {
        String currentObjDef = (String) findLinkToEpc.next();
        String Links = (String) ObjDef_LinkId.get(currentObjDef);
        StringTokenizer linkSet = new StringTokenizer(Links);
        String realEpcLink = "";
        while (linkSet.hasMoreTokens()) {
          String currentLink = linkSet.nextToken();
          if (ModelId_ModelType.get(currentLink).equals("MT_EEPC")) {
            realEpcLink = currentLink;
            break;
          }
        }
        if (realEpcLink.equals(" ")) {
          ObjDef_LinkId.remove(currentObjDef);
        } else {
          ObjDef_LinkId.put(currentObjDef, realEpcLink);
        }
      }
      result =
          traverseAML(
              result,
              doc.getDocumentElement(),
              null,
              ObjDef_Name,
              ObjDef_LinkId,
              modelid_net,
              function_LinkId);
      Iterator hierarchicalFunctions = function_LinkId.keySet().iterator();
      while (hierarchicalFunctions.hasNext()) {
        EPCSubstFunction f = (EPCSubstFunction) hierarchicalFunctions.next();
        f.setSubstitutedEPC((EPC) modelid_net.get(function_LinkId.get(f)));
        // Message.add(f.getSubstitutedEPC().getName());
      }

      return result;

    } catch (Throwable x) {
      Message.add(x.toString());
      throw new IOException(x.getMessage());
    }
  }
  /** This method generates the output given a context and output stream */
  public boolean generate(XPathContext context, ProgramWriter out) {
    try {
      String collectionName = Strings.firstUpper(collection.getName());

      if (ejb) {
        out.print(" \n\t/**\n\t * @ejb:interface-method\n\t */");
      }
      out.print(" \n\tpublic java.util.Collection get");
      out.print(collectionName);
      out.print("() {");
      if (ejb) {
        out.print(
            " \n\t\tboolean cmtActivated = false;\n\t\tif (!org.openxava.hibernate.XHibernate.isCmt()) {\n\t\t\torg.openxava.hibernate.XHibernate.setCmt(true);\n\t\t\tcmtActivated = true;\n\t\t}");
      }
      out.print(" \n\t\ttry {");

      MetaCalculator calculator = collection.getMetaCalculator();
      String calculatorClass = calculator.getClassName();

      out.print(" \t\t\n\t\t\t");
      out.print(calculatorClass);
      out.print(" ");
      out.print(collection.getName());
      out.print("Calculator= (");
      out.print(calculatorClass);
      out.print(")\n\t\t\t\tgetMetaModel().getMetaCollection(\"");
      out.print(collection.getName());
      out.print("\").getMetaCalculator().createCalculator();");

      Iterator itSets = calculator.getMetaSetsWithoutValue().iterator();
      while (itSets.hasNext()) {
        MetaSet set = (MetaSet) itSets.next();
        String propertyNameInCalculator = Strings.firstUpper(set.getPropertyName());
        String propertyNameFrom = set.getPropertyNameFrom();
        MetaProperty p = metaModel.getMetaProperty(propertyNameFrom);
        if (propertyNameFrom.indexOf('.') >= 0) {
          if (p.isKey() || p.getMetaModel() instanceof MetaAggregate) {
            propertyNameFrom = Strings.firstUpper(Strings.change(propertyNameFrom, ".", "_"));
          } else {
            StringTokenizer st = new StringTokenizer(propertyNameFrom, ".");
            String ref = st.nextToken();
            String pro = st.nextToken();
            propertyNameFrom = Strings.firstUpper(ref) + "().get" + Strings.firstUpper(pro);
          }
        } else {
          propertyNameFrom = Strings.firstUpper(propertyNameFrom);
        }
        String getPropertyFrom = "boolean".equals(p.getTypeName()) ? "is" : "get";
        String value = set.getValue();
        if (set.hasValue()) {

          out.print(" \n\t\t\t");
          out.print(collection.getName());
          out.print("Calculator.set");
          out.print(propertyNameInCalculator);
          out.print("(\"");
          out.print(value);
          out.print("\");");

        } else {

          out.print("  \t\n\t\t\t");
          out.print(collection.getName());
          out.print("Calculator.set");
          out.print(propertyNameInCalculator);
          out.print("(");
          out.print(getPropertyFrom);
          out.print(propertyNameFrom);
          out.print("());");
        }
      } // else/sets
      if (IModelCalculator.class.isAssignableFrom(Class.forName(calculatorClass))) {

        out.print(" \n\t\t\t\t");
        out.print(collection.getName());
        out.print("Calculator.setModel(this);");
      }
      if (IEntityCalculator.class.isAssignableFrom(Class.forName(calculatorClass))) {

        out.print(" \n\t\t\t\t");
        out.print(collection.getName());
        out.print("Calculator.setEntity(this);");
      }
      if (IJDBCCalculator.class.isAssignableFrom(Class.forName(calculatorClass))) {

        out.print(" \n\t\t\t\t");
        out.print(collection.getName());
        out.print("Calculator.setConnectionProvider(getPortableContext());");
      }
      String calculateValueSentence = collection.getName() + "Calculator.calculate()";

      out.print(" \n\t\t\treturn ");
      out.print(Generators.generateCast("java.util.Collection", calculateValueSentence));
      out.print(
          ";\n\t\t}\n\t\tcatch (Exception ex) {\n\t\t\tex.printStackTrace();\n\t\t\tthrow new ");
      out.print(getException());
      out.print("(XavaResources.getString(\"generator.calculate_value_error\", \"");
      out.print(collection.getName());
      out.print("\", \"");
      out.print(metaModel.getName());
      out.print("\", ex.getLocalizedMessage()));\n\t\t}");
      if (ejb) {
        out.print(
            " \n\t\tfinally {\n\t\t\tif (cmtActivated) {\n\t\t\t\torg.openxava.hibernate.XHibernate.setCmt(false);\n\t\t\t}\n\t\t}");
      }
      out.print(" \t\t\t\t\n\t}");

    } catch (Exception e) {
      System.out.println("Exception: " + e.getMessage());
      e.printStackTrace();
      return false;
    }
    return true;
  }
  private boolean resourceExists(String location) {
    String bundleJar = null;
    IPath path = new Path(location);
    if ("platform:".equals(path.getDevice()) && path.segmentCount() > 2) { // $NON-NLS-1$
      if ("plugin".equals(path.segment(0))) { // $NON-NLS-1$
        String id = path.segment(1);
        IMonitorModelBase model = MonitorRegistry.findModel(id);
        if (model != null && model.isEnabled()) {
          path = path.setDevice(null).removeFirstSegments(2);
          String bundleLocation = model.getInstallLocation();
          if (bundleLocation.endsWith(".jar")) { // $NON-NLS-1$
            bundleJar = bundleLocation;
          } else {
            path = new Path(model.getInstallLocation()).append(path);
          }
          location = path.toString();
        }
      }
    } else if (path.getDevice() == null
        && path.segmentCount() > 3
        && "platform:".equals(path.segment(0))) { // $NON-NLS-1$
      if ("plugin".equals(path.segment(1))) { // $NON-NLS-1$
        String id = path.segment(2);
        IMonitorModelBase model = MonitorRegistry.findModel(id);
        if (model != null && model.isEnabled()) {
          path = path.removeFirstSegments(3);
          String bundleLocation = model.getInstallLocation();
          if (bundleLocation.endsWith(".jar")) { // $NON-NLS-1$
            bundleJar = bundleLocation;
          } else {
            path = new Path(model.getInstallLocation()).append(path);
          }
          location = path.toString();
        }
      }
    }

    ArrayList paths = new ArrayList();
    if (location.indexOf("$nl$") != -1) { // $NON-NLS-1$
      StringTokenizer tokenizer = new StringTokenizer(TargetPlatform.getNL(), "_"); // $NON-NLS-1$
      String language = tokenizer.hasMoreTokens() ? tokenizer.nextToken() : null;
      String country = tokenizer.hasMoreTokens() ? tokenizer.nextToken() : null;
      if (language != null && country != null)
        paths.add(
            location.replaceAll(
                "\\$nl\\$",
                "nl"
                    + IPath.SEPARATOR
                    + language
                    + IPath.SEPARATOR
                    + country)); //$NON-NLS-1$ //$NON-NLS-2$
      if (language != null)
        paths.add(
            location.replaceAll(
                "\\$nl\\$", "nl" + IPath.SEPARATOR + language)); // $NON-NLS-1$ //$NON-NLS-2$
      paths.add(location.replaceAll("\\$nl\\$", "")); // $NON-NLS-1$ //$NON-NLS-2$
    } else {
      paths.add(location);
    }

    for (int i = 0; i < paths.size(); i++) {
      if (bundleJar == null) {
        IPath currPath = new Path(paths.get(i).toString());
        if (currPath.isAbsolute() && currPath.toFile().exists()) return true;
        if (PDEProject.getBundleRoot(fFile.getProject()).findMember(currPath) != null) return true;
        if (fBuildModel != null
            && fBuildModel.getEntry("source." + paths.get(i)) != null) // $NON-NLS-1$
        return true;
      } else {
        if (CoreUtility.jarContainsResource(new File(bundleJar), paths.get(i).toString(), false))
          return true;
      }
    }

    return false;
  }