/**
   * Return the input stream that results from applying the given transformer URL to the provided
   * input stream.
   *
   * @param inputStream the stream to transform
   * @param bundle the resource representing the transformer
   * @return the transformed stream
   */
  protected InputStream getInputStream(InputStream inputStream, Bundle bundle, String path) {
    String namespace = bundle.getSymbolicName();

    String[] transformTypes = templates.getTransformTypes();
    if (transformTypes.length == 0) {
      return null;
    }
    for (int i = 0; i < transformTypes.length; i++) {
      StreamTransformer transformer = transformers.getTransformer(transformTypes[i]);
      if (transformer == null) {
        continue;
      }
      TransformTuple[] transformTuples = templates.getTransformsFor(transformTypes[i]);
      if (transformTuples == null) {
        continue;
      }
      for (int j = 0; j < transformTuples.length; j++) {
        TransformTuple transformTuple = transformTuples[j];
        if (match(transformTuple.bundlePattern, namespace)
            && match(transformTuple.pathPattern, path)) {
          try {
            return transformer.getInputStream(inputStream, transformTuple.transformerUrl);
          } catch (IOException e) {
            TransformerHook.log(
                FrameworkLogEntry.ERROR,
                "Problem obtaining transformed stream from transformer : " //$NON-NLS-1$
                    + transformer.getClass().getName(),
                e);
          }
        }
      }
    }

    return null;
  }
Пример #2
0
  /**
   * Parse the given url as a CSV file containing transform tuples. The tuples have the form:
   *
   * <pre>
   * bundleRegex,pathRegex,transformerResource
   * </pre>
   *
   * @param transformMapURL the map url
   * @return an array of tuples derived from the contents of the file
   * @throws IOException thrown if there are issues parsing the file
   */
  public static TransformTuple[] parse(URL transformMapURL) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(transformMapURL.openStream()));
    String currentLine = null;
    List<TransformTuple> list = new ArrayList<TransformTuple>();
    while ((currentLine = reader.readLine()) != null) {
      if (currentLine.startsWith("#")) { // $NON-NLS-1$
        continue;
      }
      currentLine = currentLine.trim();
      if (currentLine.length() == 0) {
        continue;
      }
      StringTokenizer toker = new StringTokenizer(currentLine, ","); // $NON-NLS-1$
      try {
        String bundlePatternString = toker.nextToken().trim();
        String pathPatternString = toker.nextToken().trim();
        String transformPath = toker.nextToken().trim();
        try {
          Pattern bundlePattern = Pattern.compile(bundlePatternString);
          Pattern pathPattern = Pattern.compile(pathPatternString);
          URL transformerURL = new URL(transformMapURL, transformPath);
          try {
            try {
              transformerURL.openStream();
            } catch (FileNotFoundException ex) {
              if (!transformPath.endsWith(".class")) { // $NON-NLS-1$
                throw ex;
              }
              // Development class paths are in the /bin directory
              String binPath = "/bin" + transformPath; // $NON-NLS-1$
              URL binURL = new URL(transformMapURL, binPath);
              binURL.openStream();
              transformPath = binPath;
              transformerURL = binURL;
            }
            if (transformPath.endsWith(".class")
                && transformerURL.getProtocol().equals("bundleentry")) { // $NON-NLS-1$ $NON-NLS-2$
              addTuplesForClass(
                  list, bundlePatternString, pathPatternString, transformerURL, transformPath);
            } else {
              TransformTuple tuple = new TransformTuple();
              tuple.bundlePattern = bundlePattern;
              tuple.pathPattern = pathPattern;
              tuple.transformerUrl = transformerURL;
              list.add(tuple);
            }
          } catch (IOException e) {
            TransformerHook.log(
                FrameworkLogEntry.ERROR,
                "Could not add transform :" + transformerURL.toString(),
                e); //$NON-NLS-1$
          }
        } catch (PatternSyntaxException e) {
          TransformerHook.log(
              FrameworkLogEntry.ERROR,
              "Could not add compile transform matching regular expression",
              e); //$NON-NLS-1$
        }

      } catch (NoSuchElementException e) {
        TransformerHook.log(
            FrameworkLogEntry.ERROR,
            "Could not parse transform file record :" + currentLine,
            e); //$NON-NLS-1$
      }
    }
    return list.toArray(new TransformTuple[list.size()]);
  }