示例#1
0
  /**
   * Returns a transformer for the given XSLT file. If one already exists, it will be returned and
   * no new one will be created.
   *
   * @param xsltFileName the name of the XSLT file to use
   * @return The transformer that uses the given XSLT file or null, if none could be created.
   */
  private static Transformer getTransformer(String xsltFileName) {
    String key = xsltFileName + "-" + Thread.currentThread().getId();
    if (transformers.containsKey(key)) {
      return transformers.get(key);
    }
    InputStream xsltFile = XsltRunner.class.getResourceAsStream("/" + xsltFileName);
    if (xsltFile == null) {
      Log.error(TAG, "Error could not find: " + xsltFileName);
      return null;
    }
    try {
      Source xsltSource = new StreamSource(xsltFile);
      TransformerFactory transFactory = TransformerFactory.newInstance();

      // Add a URI resolver so that the XSL docs may import other XSL docs
      // using relative paths
      transFactory.setURIResolver(
          new URIResolver() {

            public Source resolve(String href, String base) throws TransformerException {
              InputStream is = XsltRunner.class.getResourceAsStream("/" + href);
              return new StreamSource(is);
            }
          });

      Transformer transformer = transFactory.newTransformer(xsltSource);
      transformers.put(key, transformer);
      return transformer;
    } catch (TransformerConfigurationException e) {
      Log.error(TAG, "Could not create transformer for " + xsltFileName + ": " + e.getMessage());
    }
    return null;
  }
    public void run() {
      for (int i = start; i <= end; ++i) {
        XmlvmResource resource = allResources[i];
        if (resource == null) {
          continue;
        }
        Log.debug("JavaScriptOutputProcess: Processing " + resource.getName());
        OutputFile file = generateJavaScript(resource);
        file.setLocation(arguments.option_out());
        String packageName = resource.getPackageName().replace('.', '_');
        String resourceName = resource.getName();
        Log.debug("RESOURCE NAME: " + resourceName);

        String fileName = resourceName + JS_EXTENSION;
        if (!packageName.isEmpty()) {
          fileName = packageName + '_' + fileName;
        }
        file.setFileName(fileName);
        resources.addOutputFile(file);
      }
    }
  public boolean processPhase2(BundlePhase2 bundle) {
    Map<String, XmlvmResource> mappedResources = new HashMap<String, XmlvmResource>();
    for (XmlvmResource resource : bundle.getResources()) {
      mappedResources.put(resource.getFullName(), resource);
    }

    long startTime = System.currentTimeMillis();

    XmlvmResource[] allResources = mappedResources.values().toArray(new XmlvmResource[0]);
    int threadCount = Runtime.getRuntime().availableProcessors();
    int itemsPerThread = (int) Math.ceil(allResources.length / (float) threadCount);
    Log.debug(TAG, "Threads: " + threadCount);
    Log.debug(TAG, "Items per thread: " + itemsPerThread);
    JavaScriptTranslationThread[] threads = new JavaScriptTranslationThread[threadCount];

    // Divide work and start the threads.
    for (int i = 0; i < threadCount; ++i) {
      int start = i * itemsPerThread;
      int end = Math.min(start + itemsPerThread - 1, allResources.length - 1);
      threads[i] = new JavaScriptTranslationThread(allResources, start, end, bundle);
      threads[i].start();
    }

    // Wait for threads to finish.
    for (int i = 0; i < threadCount; ++i) {
      try {
        threads[i].join();
      } catch (InterruptedException e) {
        e.printStackTrace();
        return false;
      }
    }

    long endTime = System.currentTimeMillis();
    Log.debug(TAG, "JS Processing took: " + (endTime - startTime) + " ms.");
    return true;
  }
示例#4
0
  /**
   * Runs an XSLT file on the given JDOM document.
   *
   * @param xsltFileName The name of the file that contains the XSLT definition.
   * @param doc The JDOM document the XSL transformation should be applied to. The first index
   *     refers to the i-th parameter, the index will be 0 for the parameter name and 1 for the
   *     parameter value. See {@link Transformer#setParameter(String, Object)}
   * @param xsltParams Parameters that should be applied to the transformation.
   * @return The output file with the result of the transformation.
   */
  public static OutputFile runXSLT(String xsltFileName, Document doc, String[][] xsltParams) {
    StringWriter writer = new StringWriter();
    try {
      Transformer transformer = getTransformer(xsltFileName);
      transformer.reset();
      if (xsltParams != null) {
        for (int i = 0; i < xsltParams.length; i++)
          transformer.setParameter(xsltParams[i][0], xsltParams[i][1]);
      }
      DocumentWrapper docw =
          new DocumentWrapper(doc, "", ((Controller) transformer).getConfiguration());
      Result result = new StreamResult(writer);
      transformer.transform(docw, result);

      return new OutputFile(writer.toString());
    } catch (TransformerException e) {
      Log.error(TAG, e.getMessage());
    }
    return null;
  }