Beispiel #1
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();
  }
Beispiel #2
0
 public IValue readTextJSonFile(IValue type, ISourceLocation loc, IEvaluatorContext ctx) {
   // TypeStore store = new TypeStore();
   TypeStore store = ctx.getCurrentEnvt().getStore();
   Type start = new TypeReifier(ctx.getValueFactory()).valueToType((IConstructor) type, store);
   Reader read = null;
   try {
     read = ctx.getResolverRegistry().getCharacterReader(loc.getURI());
     return new JSonReader().read(values, store, start, read);
   } catch (IOException e) {
     throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null);
   } finally {
     if (read != null) {
       try {
         read.close();
       } catch (IOException ioex) {
         throw RuntimeExceptionFactory.io(values.string(ioex.getMessage()), null, null);
       }
     }
   }
 }
 public Object createRootEnvironment() {
   return ctx.getCurrentEnvt();
 }
Beispiel #4
0
  public void write(IValue rel, ISourceLocation loc, IEvaluatorContext ctx) {

    OutputStream out = null;

    Type paramType = ctx.getCurrentEnvt().getTypeBindings().get(types.parameterType("T"));
    if (!paramType.isRelation() && !paramType.isListRelation()) {
      throw RuntimeExceptionFactory.illegalTypeArgument(
          "A relation type is required instead of " + paramType,
          ctx.getCurrentAST(),
          ctx.getStackTrace());
    }

    try {
      boolean isListRel = rel instanceof IList;
      out = ctx.getResolverRegistry().getOutputStream(loc.getURI(), false);
      ISet irel = null;
      IList lrel = null;
      if (isListRel) {
        lrel = (IList) rel;
      } else {
        irel = (ISet) rel;
      }

      int nfields = isListRel ? lrel.asRelation().arity() : irel.asRelation().arity();
      if (header) {
        for (int i = 0; i < nfields; i++) {
          if (i > 0) out.write(separator);
          String label = paramType.getFieldName(i);
          if (label == null || label.isEmpty()) label = "field" + i;
          writeString(out, label);
        }
        out.write('\n');
      }
      String separatorAsString = new String(Character.toChars(separator));
      for (IValue v : (isListRel ? lrel : irel)) {
        ITuple tup = (ITuple) v;
        int sep = 0;
        for (IValue w : tup) {
          if (sep == 0) sep = separator;
          else out.write(sep);
          if (w.getType().isString()) {
            String s = ((IString) w).getValue();

            if (s.contains(separatorAsString)
                || s.contains("\n")
                || s.contains("\r")
                || s.contains("\"")) {
              s = s.replaceAll("\"", "\"\"");
              out.write('"');
              writeString(out, s);
              out.write('"');
            } else writeString(out, s);
          } else {
            writeString(out, w.toString());
          }
        }
        out.write('\n');
      }
      out.flush();
      out.close();
    } catch (IOException e) {
      throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null);
    } finally {
      if (out != null) {
        try {
          out.flush();
          out.close();
        } catch (IOException ioex) {
          throw RuntimeExceptionFactory.io(values.string(ioex.getMessage()), null, null);
        }
      }
    }
  }