コード例 #1
0
ファイル: IO.java プロジェクト: reflectionalist/rascal
 private IValue read(Type resultType, ISourceLocation loc, IMap options, IEvaluatorContext ctx) {
   setOptions(options);
   Reader reader = null;
   try {
     reader = ctx.getResolverRegistry().getCharacterReader(loc.getURI());
     List<Record> records = loadRecords(reader);
     if (resultType == null) {
       resultType = inferType(records, ctx);
       ctx.getStdOut().println("readCSV inferred the relation type: " + resultType);
       ctx.getStdOut().flush();
     } else if (header) {
       records.remove(0);
     }
     return buildCollection(resultType, records, ctx);
   } catch (IOException e) {
     throw RuntimeExceptionFactory.io(
         values.string(e.getMessage()), ctx.getCurrentAST(), ctx.getStackTrace());
   } finally {
     if (reader != null) {
       try {
         reader.close();
       } catch (IOException e) {
         throw RuntimeExceptionFactory.io(
             values.string(e.getMessage()), ctx.getCurrentAST(), ctx.getStackTrace());
       }
     }
   }
 }
コード例 #2
0
ファイル: IO.java プロジェクト: jeroenpeeters/rascal
 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);
   }
 }
コード例 #3
0
ファイル: IO.java プロジェクト: sharwell/rascal
 public void writeTextJSonFile(ISourceLocation loc, IValue value, IEvaluatorContext ctx) {
   OutputStream out = null;
   try {
     out = ctx.getResolverRegistry().getOutputStream(loc.getURI(), false);
     new JSonWriter().write(value, new OutputStreamWriter(out, "UTF8"));
   } catch (IOException e) {
     throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null);
   } finally {
     if (out != null) {
       try {
         out.close();
       } catch (IOException ioex) {
         throw RuntimeExceptionFactory.io(values.string(ioex.getMessage()), null, null);
       }
     }
   }
 }
コード例 #4
0
ファイル: IO.java プロジェクト: sharwell/rascal
 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);
       }
     }
   }
 }
コード例 #5
0
ファイル: IO.java プロジェクト: jeroenpeeters/rascal
  public IValue readTextATermFile(IValue type, ISourceLocation loc, IEvaluatorContext ctx) {
    TypeStore store = new TypeStore();
    Type start = new TypeReifier(ctx.getValueFactory()).valueToType((IConstructor) type, store);

    InputStream in = null;
    try {
      in = ctx.getResolverRegistry().getInputStream(loc.getURI());
      return new ATermReader().read(values, store, start, in);
    } catch (IOException e) {
      throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null);
    } finally {
      if (in != null) {
        try {
          in.close();
        } catch (IOException ioex) {
          throw RuntimeExceptionFactory.io(values.string(ioex.getMessage()), null, null);
        }
      }
    }
  }
コード例 #6
0
  public IString prompt(final IString prompt) {
    final String[] value = new String[1];

    PlatformUI.getWorkbench()
        .getDisplay()
        .syncExec(
            new Runnable() {
              public void run() {
                Shell activeShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
                final Shell shell = new Shell(activeShell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
                shell.setText(prompt.getValue());

                GridLayout gridLayout = new GridLayout();
                shell.setLayout(gridLayout);
                Label label = new Label(shell, SWT.WRAP);
                label.setText(prompt.getValue());

                GridData data = new GridData();
                Monitor monitor = activeShell.getMonitor();
                int maxWidth = monitor.getBounds().width * 2 / 3;
                int width = label.computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
                data.widthHint = Math.min(width, maxWidth);
                data.horizontalAlignment = GridData.FILL;
                data.grabExcessHorizontalSpace = true;
                label.setLayoutData(data);

                final Text valueText = new Text(shell, SWT.BORDER);

                Listener enterListener =
                    new Listener() {
                      public void handleEvent(Event event) {
                        value[0] = valueText.getText();
                        shell.close();
                      }
                    };

                if (value[0] != null) valueText.setText(value[0]);
                data = new GridData();
                width = valueText.computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
                if (width > maxWidth) data.widthHint = maxWidth;
                data.horizontalAlignment = GridData.FILL;
                data.grabExcessHorizontalSpace = true;
                valueText.setLayoutData(data);
                valueText.addListener(SWT.DefaultSelection, enterListener);

                Composite composite = new Composite(shell, SWT.NONE);
                data = new GridData();
                data.horizontalAlignment = GridData.CENTER;
                composite.setLayoutData(data);
                composite.setLayout(new GridLayout(2, true));

                Button button = new Button(composite, SWT.PUSH);
                button.setText("OK");
                button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

                button.addListener(SWT.Selection, enterListener);

                shell.pack();
                shell.open();

                Display display = activeShell.getDisplay();
                while (!shell.isDisposed()) {
                  if (!display.readAndDispatch()) display.sleep();
                }
              }
            });

    if (value[0] != null) {
      return vf.string(value[0]);
    }

    throw RuntimeExceptionFactory.io(vf.string("no input"), null, null);
  }
コード例 #7
0
ファイル: IO.java プロジェクト: reflectionalist/rascal
  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);
        }
      }
    }
  }