Esempio n. 1
0
  /**
   * Creates and returns a new Image object that is a rendering of this Image object. The input
   * object defines the rendering parameters of the new Image object. Imagemagick's convert program
   * is used to create a scaled bounding box of this Image with the given dimensions.
   *
   * @param {Object} input A JavaScript object specifying the rendering parameters. For example,
   *     <code> {maxWidth:200, maxHeight:100} </code>
   * @returns {Image} The rendered Image object
   * @throws Exception
   */
  public ImageObject jsFunction_render(Object input) throws Exception {
    if (input == null || !(input instanceof Scriptable)) {
      throw new RuntimeException("The first argument to render() must be a scriptable object.");
    }

    Scriptable s = (Scriptable) input;
    int maxWidth = toInt(s.get("maxWidth", s));
    int maxHeight = toInt(s.get("maxHeight", s));

    int cropWidth = toInt(s.get("cropWidth", s));
    int cropHeight = toInt(s.get("cropHeight", s));
    int cropXOffset = toInt(s.get("cropXOffset", s));
    int cropYOffset = toInt(s.get("cropYOffset", s));

    int currentWidth = (int) node.getInteger(WIDTH);
    int currentHeight = (int) node.getInteger(HEIGHT);

    String aname = null;
    if (maxWidth > 0 && maxHeight > 0 && currentWidth > 0 && currentHeight > 0) {
      int[] dims = this.computeResizedDimensions(maxWidth, maxHeight, currentWidth, currentHeight);
      aname = dims[0] + "x" + dims[1];
      maxWidth = dims[0];
      maxHeight = dims[1];
    } else if (cropWidth > 0 && cropHeight > 0) {
      aname = cropWidth + "x" + cropHeight + "_" + cropXOffset + "x" + cropYOffset;
    } else {
      throw new RuntimeException("render(), invalid parameter set.");
    }

    Object o = this.jsFunction_get(aname);
    if (o instanceof ImageObject) {
      return (ImageObject) o;
    }

    try {
      synchronized (this) {
        while (this.convertOps.contains(aname)) {
          this.wait();
        }
        this.convertOps.add(aname);
      }

      o = ((axiom.objectmodel.db.Node) this.node).getChildElement(aname, true);
      if (o instanceof Node) {
        return (ImageObject) Context.toObject(o, this.core.global);
      }

      ImageObject computedImg = null;
      String[] paths = getPaths();
      String imgPath = paths[0];
      String tmpPath = paths[1];
      String fileName = node.getString(FileObject.FILE_NAME);

      try {
        File tmpFile = new File(tmpPath);
        int[] dims = null;
        if (maxWidth > 0 && maxHeight > 0) {
          dims =
              this.resize(
                  maxWidth,
                  maxHeight,
                  (int) this.node.getInteger(WIDTH),
                  (int) this.node.getInteger(HEIGHT),
                  imgPath,
                  tmpPath,
                  true);
        } else {
          dims = this.crop(cropWidth, cropHeight, cropXOffset, cropYOffset, imgPath, tmpPath);
        }

        if (dims == null) {
          throw new Exception("ImageObject.render(), resizing the image failed.");
        }

        final String protoname = "Image";
        INode node =
            new axiom.objectmodel.db.Node(protoname, protoname, core.app.getWrappedNodeManager());
        computedImg = new ImageObject("Image", core, node, core.getPrototype(protoname), true);

        node.setString(FileObject.FILE_NAME, fileName);
        node.setString(FileObject.ACCESSNAME, FileObjectCtor.generateAccessName(fileName));
        node.setString(FileObject.CONTENT_TYPE, this.node.getString(FileObject.CONTENT_TYPE));
        node.setJavaObject(FileObject.SELF, computedImg);
        node.setInteger(ImageObject.WIDTH, dims[0]);
        node.setInteger(ImageObject.HEIGHT, dims[1]);
        node.setString(FileObject.RENDERED_CONTENT, "true");

        node.setInteger(FileObject.FILE_SIZE, tmpFile.length());
        computedImg.tmpPath = tmpPath;
      } catch (Exception ex) {
        throw new RuntimeException(
            "ImageObject.jsfunction_bound(): Could not write the image to temporary storage, "
                + ex.getMessage());
      }

      if (computedImg != null) {
        this.jsFunction_addThumbnail(computedImg, null);
        return computedImg;
      }
    } finally {
      synchronized (this) {
        this.convertOps.remove(aname);
        this.notifyAll();
      }
    }

    return null;
  }
Esempio n. 2
0
  /**
   * Evaluates the RhinoFile result. If the result is an instance of javax.javascript.Scriptable,
   * assume it is a JavaScript Array of TestCase objects, as described in RhinoDrv.java. For each
   * test case in the array, add an element to the RhinoFile's test case vector. If all test cases
   * passed, set the RhinoFile's passed value to true; else set its passed value to false.
   *
   * <p>If the result is not a Scriptable object, the test failed. Set the the RhinoFile's exception
   * property to the string value of the result. However, negative tests, which should have a
   * "-n.js" extension, are expected to fail.
   */
  public boolean parseResult() {
    FlattenedObject fo = null;

    if (result instanceof Scriptable) {
      fo = new FlattenedObject((Scriptable) result);

      try {
        file.totalCases = ((Number) fo.getProperty("length")).intValue();
        for (int i = 0; i < file.totalCases; i++) {
          Scriptable tc = (Scriptable) ((Scriptable) result).get(i, (Scriptable) result);

          TestCase rt =
              new TestCase(
                  getString(tc.get("passed", tc)),
                  getString(tc.get("name", tc)),
                  getString(tc.get("description", tc)),
                  getString(tc.get("expect", tc)),
                  getString(tc.get("actual", tc)),
                  getString(tc.get("reason", tc)));

          file.bugnumber =
              (getString(tc.get("bugnumber", tc))).startsWith("com.netscape.javascript")
                  ? file.bugnumber
                  : getString(tc.get("bugnumber", tc));

          file.caseVector.addElement(rt);
          if (rt.passed.equals("false")) {
            this.file.passed = false;
            this.suite.passed = false;
          }
        }

        if (file.totalCases == 0) {
          if (file.name.endsWith("-n.js")) {
            this.file.passed = true;
          } else {
            this.file.reason = "File contains no testcases. " + this.file.reason;
            this.file.passed = false;
            this.suite.passed = false;
          }
        }

      } catch (Exception e) {
        this.file.exception =
            "Got a Scriptable result, but failed "
                + "parsing its arguments.  Exception: "
                + e.toString()
                + " Flattened Object is: "
                + fo.toString();

        this.file.passed = false;
        this.suite.passed = false;

        return false;
      }
    } else {
      // if it's not a scriptable object, test failed.  set the file's
      // exception to the string value of whatever result we did get.
      this.file.exception = result.toString();

      // if the file's name ends in "-n", the test expected an error.

      if (file.name.endsWith("-n.js")) {
        this.file.passed = true;
      } else {
        this.file.passed = false;
        this.suite.passed = false;
        return false;
      }
    }

    return true;
  }