Exemplo n.º 1
0
  /**
   * @param options map with options may contain: - "header" (Possible values "true" or "false", as
   *     string!) - "separator" (Possible value a single character string) When map is null, the
   *     defaults are reset.
   */
  private void setOptions(IMap options) {

    IString separatorKey = values.string("separator");
    IString headerKey = values.string("header");

    IString iseparator = null;
    IString iheader = null;

    if (options != null) {
      iseparator = (IString) options.get(separatorKey);
      iheader = (IString) options.get(headerKey);
    }

    separator = (iseparator == null) ? ',' : iseparator.getValue().charAt(0);
    header = (iheader == null) ? true : iheader.toString().equals("true");
  }
Exemplo n.º 2
0
 public void registerResource(IString javaClass, IEvaluatorContext ctx) {
   try {
     String className = javaClass.getValue();
     List<ClassLoader> loaders = ctx.getEvaluator().getClassLoaders();
     Class<?> resourceClass = null;
     for (ClassLoader loader : loaders) {
       try {
         resourceClass = loader.loadClass(className);
       } catch (ClassNotFoundException e) {
         continue;
       }
       break;
     }
     if (resourceClass != null) {
       IResource handler = (IResource) resourceClass.newInstance();
       Resource.registerResourceHandler(handler);
     } else {
       throw new ClassNotFoundException(className);
     }
   } catch (ClassNotFoundException e) {
     e.printStackTrace();
   } catch (InstantiationException e) {
     e.printStackTrace();
   } catch (IllegalAccessException e) {
     e.printStackTrace();
   }
 }
Exemplo n.º 3
0
 public IValue uriDecode(IString str) {
   try {
     return vf.string(URLDecoder.decode(str.getValue(), "UTF-8"));
   } catch (UnsupportedEncodingException e) {
     throw RuntimeExceptionFactory.illegalArgument(
         str, null, null, "UTF-8 is not a valid encoding");
   }
 }
Exemplo n.º 4
0
 public IValue readATermFromFile(IString fileName) {
   // @doc{readATermFromFile -- read an ATerm from a named file}
   ATermReader atr = new ATermReader();
   try {
     FileInputStream stream = new FileInputStream(fileName.getValue());
     IValue result = atr.read(values, stream);
     stream.close();
     return result;
   } catch (FactTypeUseException e) {
     e.printStackTrace();
     throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null);
   } catch (IOException e) {
     e.printStackTrace();
     throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null);
   }
 }
  public static IConstructor loadForRascal(IValueFactory vf, IString src, ISourceLocation loc) {
    IGTD<IConstructor, IConstructor, ISourceLocation> gtd = new ClojureParser();
    IConstructor file =
        (IConstructor)
            gtd.parse(
                START_SORT,
                loc.getURI(),
                src.getValue().toCharArray(),
                new DefaultNodeFlattener<IConstructor, IConstructor, ISourceLocation>(),
                new UPTRNodeFactory());

    String path = loc.getURI().getPath();
    IConstructor[] fileRef = new IConstructor[] {file};

    // ignore the clojure result
    ISetWriter errors = vf.setWriter(Message.Message);
    new UPTRCompiler(vf, errors).loadPT(fileRef, path, new File(path).getName());

    // return the updated parse tree.
    return fileRef[0].setAnnotation("messages", errors.done());
  }
Exemplo n.º 6
0
  public void generateTypedInterfaceInternal(
      IString tag, ISourceLocation uriLoc, IEvaluatorContext ctx) {
    IResource handler = Resource.getResourceHandler(uriLoc);
    String tagStr = tag.getValue();
    Type t = handler.getResourceType(ctx, uriLoc);
    PrintWriter currentOutStream = ctx.getStdOut();

    // Declare an alias to the type of the resource
    TypeStore ts = ctx.getCurrentEnvt().getStore();
    Type alias2t = TypeFactory.getInstance().aliasType(ts, tagStr + "Type", t);

    currentOutStream.println(
        "Generated type alias " + alias2t.toString() + ": " + alias2t.getAliased().toString());
    currentOutStream.flush();

    // Declare a function that just uses the given URI. This way, if we provide
    // the complete URI up front, we don't need to keep providing it later.
    StringBuilder sb = new StringBuilder();
    sb.append("public ").append(tagStr).append("Type ").append(tagStr).append("() { ");
    sb.append(" return getTypedResource(")
        .append(uriLoc.toString())
        .append(",#")
        .append(tagStr)
        .append("Type); }");
    IConstructor declTree =
        ctx.getEvaluator()
            .parseCommand(
                ctx.getEvaluator().getMonitor(),
                sb.toString(),
                ctx.getCurrentAST().getLocation().getURI());
    Command cmd = ctx.getEvaluator().getBuilder().buildCommand(declTree);
    Environment env = ctx.getCurrentEnvt();
    ctx.setCurrentEnvt(env.getRoot());
    Result<IValue> fun0 = ctx.getEvaluator().eval(ctx.getEvaluator().getMonitor(), cmd);
    ctx.unwind(env);

    currentOutStream.println("Generated function " + fun0.toString());
    currentOutStream.flush();
  }