コード例 #1
0
  /**
   * Executes the specified {@code scriptResource}.
   *
   * <p>The output is either a GraphNode or a response with the media type specified by the
   * producedType property of the associated script.
   *
   * <p>GraphNodes can be rendered by Renderlets registered for the type of the GraphNode.
   *
   * @param scriptResource The resource (URI).
   * @param bindings Name/Value pairs of Java {@code Object}s made accessible to script
   * @return The value returned from the execution of the script. Either a GraphNode or a Response.
   * @throws ScriptException If an error occurrs in the script.
   * @throws NoEngineException If no engine can be found
   * @see org.apache.clerezza.rdf.ontologies.SCRIPT#Script
   * @see org.apache.clerezza.rdf.ontologies.SCRIPT#producedType
   * @see org.apache.clerezza.platform.typerendering.Renderlet
   */
  public Object execute(NonLiteral scriptResource, Bindings bindings)
      throws ScriptException, NoEngineException {

    MGraph contentGraph = cgProvider.getContentGraph();

    String scriptString = null;
    String scriptLanguage = null;
    String scriptLanguageVersion = null;
    String scriptProducedType = "text/plain";

    GraphNode scriptNode = new GraphNode(scriptResource, contentGraph);
    Iterator<Resource> it = scriptNode.getObjects(SCRIPT.scriptLanguage);
    scriptLanguage =
        LiteralFactory.getInstance().createObject(String.class, (TypedLiteral) it.next());

    it = scriptNode.getObjects(SCRIPT.scriptLanguageVersion);
    scriptLanguageVersion =
        LiteralFactory.getInstance().createObject(String.class, (TypedLiteral) it.next());

    it = scriptNode.getObjects(SCRIPT.producedType);
    if (it.hasNext()) {
      scriptProducedType =
          LiteralFactory.getInstance().createObject(String.class, (TypedLiteral) it.next());
    }

    scriptString = new String(contentHandler.getData((UriRef) scriptResource));

    Object result = execute(scriptString, bindings, scriptLanguage, scriptLanguageVersion);
    if (result instanceof GraphNode) {
      return result;
    }
    return Response.ok(result, MediaType.valueOf(scriptProducedType)).build();
  }