Ejemplo n.º 1
0
 @Override
 public Value lazyArrayChunk(final Key key) {
   final Key arykey = ValueArray.getArrayKey(key); // From the base file key
   final long off = (_iceRoot != null) ? 0 : ValueArray.getChunkOffset(key); // The offset
   final Path p =
       (_iceRoot != null)
           ? new Path(_iceRoot, getIceName(key, (byte) 'V'))
           : new Path(arykey.toString());
   final Size sz = new Size();
   run(
       new Callable() {
         @Override
         public Object call() throws Exception {
           FileSystem fs = FileSystem.get(p.toUri(), CONF);
           long rem = fs.getFileStatus(p).getLen() - off;
           sz._value = (rem > ValueArray.CHUNK_SZ * 2) ? (int) ValueArray.CHUNK_SZ : (int) rem;
           return null;
         }
       },
       true,
       0);
   Value val = new Value(key, sz._value, Value.HDFS);
   val.setdsk(); // But its already on disk.
   return val;
 }
Ejemplo n.º 2
0
  /**
   * Test sending of HTTP GET requests.
   *
   * @throws Exception exception
   */
  @Test
  public void postGet() throws Exception {
    // GET1 - just send a GET request
    try (final QueryProcessor qp =
        new QueryProcessor(
            _HTTP_SEND_REQUEST.args("<http:request method='get' href='" + REST_ROOT + "'/>"),
            ctx)) {
      final Value v = qp.value();
      checkResponse(v, 2, HttpURLConnection.HTTP_OK);

      assertEquals(NodeType.DOC, v.itemAt(1).type);
    }

    // GET2 - with override-media-type='text/plain'
    try (final QueryProcessor qp =
        new QueryProcessor(
            _HTTP_SEND_REQUEST.args(
                "<http:request method='get' override-media-type='text/plain'/>", REST_ROOT),
            ctx)) {
      final Value v = qp.value();
      checkResponse(v, 2, HttpURLConnection.HTTP_OK);

      assertEquals(AtomType.STR, v.itemAt(1).type);
    }

    // Get3 - with status-only='true'
    try (final QueryProcessor qp =
        new QueryProcessor(
            _HTTP_SEND_REQUEST.args("<http:request method='get' status-only='true'/>", REST_ROOT),
            ctx)) {
      checkResponse(qp.value(), 1, HttpURLConnection.HTTP_OK);
    }
  }
Ejemplo n.º 3
0
  public static void main(String[] args) {

    // create a graph with type inferencing
    SimpleSesameGraph g = new SimpleSesameGraph(true);

    //        //load the film schema and the example data
    //        g.addFile("../roadstead_ontologies/film-ontology.owl", SimpleSesameGraph.RDFXML);

    // try loading the geopolitical ontology
    g.addURI("http://aims.fao.org/aos/geopolitical.owl");

    //        List<HashMap<Object, Value>> solutions = g.runSPARQL("SELECT ?who WHERE  { " +
    //                "?who <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
    // <http://roadstead.org/film#Person> ." +
    //                "}");

    /*
    Here we are looking for the names of all the things with a name
     */
    List<HashMap<Object, Value>> solutions =
        g.runSPARQL(
            "SELECT ?name WHERE  { "
                + "?what <http://aims.fao.org/aos/geopolitical.owl#territory> ?name ."
                + "?what <http://aims.fao.org/aos/geopolitical.owl#nameListEN> ?name ."
                + "}");
    for (HashMap<Object, Value> object : solutions) {
      for (Value value : object.values()) {
        System.out.println("country: " + value.stringValue());
      }
    }
  }
Ejemplo n.º 4
0
  @Override
  public List<FeedValue> getFeed(final int count, final String relationshipEntityKey)
      throws NimbitsException {

    final User loggedInUser = getUser();
    final User feedUser = getFeedUser(relationshipEntityKey, loggedInUser);

    if (feedUser != null) {

      final Point point = getFeedPoint(feedUser);
      if (point == null) {
        return new ArrayList<FeedValue>(0);
      } else {
        final List<Value> values =
            RecordedValueServiceFactory.getInstance().getTopDataSeries(point, count, new Date());
        final List<FeedValue> retObj = new ArrayList<FeedValue>(values.size());

        for (final Value v : values) {
          if (!Utils.isEmptyString(v.getData())) {
            try {
              retObj.add(GsonFactory.getInstance().fromJson(v.getData(), FeedValueModel.class));
            } catch (JsonSyntaxException ignored) {

            }
          }
        }
        return retObj;
      }
    } else {
      return new ArrayList<FeedValue>(0);
    }
  }
Ejemplo n.º 5
0
  public void exceptionEvent(ExceptionEvent event) {
    ObjectReference or = event.exception();
    ReferenceType rt = or.referenceType();
    String exceptionName = rt.name();
    // Field messageField = Throwable.class.getField("detailMessage");
    Field messageField = rt.fieldByName("detailMessage");
    //    System.out.println("field " + messageField);
    Value messageValue = or.getValue(messageField);
    //    System.out.println("mess val " + messageValue);

    // "java.lang.ArrayIndexOutOfBoundsException"
    int last = exceptionName.lastIndexOf('.');
    String message = exceptionName.substring(last + 1);
    if (messageValue != null) {
      String messageStr = messageValue.toString();
      if (messageStr.startsWith("\"")) {
        messageStr = messageStr.substring(1, messageStr.length() - 1);
      }
      message += ": " + messageStr;
    }
    //    System.out.println("mess type " + messageValue.type());
    // StringReference messageReference = (StringReference) messageValue.type();

    // First just report the exception and its placement
    reportException(message, or, event.thread());
    // Then try to pretty it up with a better message
    handleCommonErrors(exceptionName, message, listener);

    if (editor != null) {
      editor.deactivateRun();
    }
  }
Ejemplo n.º 6
0
 private void printVar(OutputSink out, LocalVariable var, StackFrame frame) {
   out.print("  " + var.name());
   if (var.isVisible(frame)) {
     Value val = frame.getValue(var);
     out.println(" = " + val.toString());
   } else {
     out.println(" is not in scope");
   }
 }
Ejemplo n.º 7
0
 /**
  * Checks the response to an HTTP request.
  *
  * @param v query result
  * @param itemsCount expected number of items
  * @param expStatus expected status
  */
 private static void checkResponse(final Value v, final int itemsCount, final int expStatus) {
   assertEquals(itemsCount, v.size());
   assertTrue(v.itemAt(0) instanceof FElem);
   final FElem response = (FElem) v.itemAt(0);
   assertNotNull(response.attributes());
   if (!eq(response.attribute(STATUS), token(expStatus))) {
     fail("Expected: " + expStatus + "\nFound: " + response);
   }
 }
Ejemplo n.º 8
0
 private static void addFolder(FileSystem fs, Path p, JsonArray succeeded, JsonArray failed) {
   try {
     if (fs == null) return;
     for (FileStatus file : fs.listStatus(p)) {
       Path pfs = file.getPath();
       if (file.isDir()) {
         addFolder(fs, pfs, succeeded, failed);
       } else {
         Key k = Key.make(pfs.toString());
         long size = file.getLen();
         Value val = null;
         if (pfs.getName().endsWith(Extensions.JSON)) {
           JsonParser parser = new JsonParser();
           JsonObject json = parser.parse(new InputStreamReader(fs.open(pfs))).getAsJsonObject();
           JsonElement v = json.get(Constants.VERSION);
           if (v == null) throw new RuntimeException("Missing version");
           JsonElement type = json.get(Constants.TYPE);
           if (type == null) throw new RuntimeException("Missing type");
           Class c = Class.forName(type.getAsString());
           OldModel model = (OldModel) c.newInstance();
           model.fromJson(json);
         } else if (pfs.getName().endsWith(Extensions.HEX)) { // Hex file?
           FSDataInputStream s = fs.open(pfs);
           int sz = (int) Math.min(1L << 20, size); // Read up to the 1st meg
           byte[] mem = MemoryManager.malloc1(sz);
           s.readFully(mem);
           // Convert to a ValueArray (hope it fits in 1Meg!)
           ValueArray ary = new ValueArray(k, 0).read(new AutoBuffer(mem));
           val = new Value(k, ary, Value.HDFS);
         } else if (size >= 2 * ValueArray.CHUNK_SZ) {
           val =
               new Value(
                   k,
                   new ValueArray(k, size),
                   Value.HDFS); // ValueArray byte wrapper over a large file
         } else {
           val = new Value(k, (int) size, Value.HDFS); // Plain Value
           val.setdsk();
         }
         DKV.put(k, val);
         Log.info("PersistHdfs: DKV.put(" + k + ")");
         JsonObject o = new JsonObject();
         o.addProperty(Constants.KEY, k.toString());
         o.addProperty(Constants.FILE, pfs.toString());
         o.addProperty(Constants.VALUE_SIZE, file.getLen());
         succeeded.add(o);
       }
     }
   } catch (Exception e) {
     Log.err(e);
     JsonObject o = new JsonObject();
     o.addProperty(Constants.FILE, p.toString());
     o.addProperty(Constants.ERROR, e.getMessage());
     failed.add(o);
   }
 }
Ejemplo n.º 9
0
 @Override
 public void store(Value v) {
   // Should be used only if ice goes to HDFS
   assert this == getIce();
   assert !v.isPersisted();
   byte[] m = v.memOrLoad();
   assert (m == null || m.length == v._max); // Assert not saving partial files
   store(new Path(_iceRoot, getIceName(v)), m);
   v.setdsk(); // Set as write-complete to disk
 }
Ejemplo n.º 10
0
 /* Returns a negative integer, zero, or a positive integer as v1
 is less than, equal to, or greater v2.
 NOTE: assume values are ints.
 */
 public static int compareIntValues(Value val1, Value val2) {
   try {
     int vval1 = new Integer(val1.toString()).intValue();
     int vval2 = new Integer(val2.toString()).intValue();
     if (vval1 > vval2) return 1;
     else if (vval2 > vval1) return -1;
     return 0;
   } catch (Exception e) {
     Utility.Dprint("  Error in Variable.compareValues(): Vals are not ints." + e, 0);
   }
   return 0;
 }
Ejemplo n.º 11
0
  /**
   * @ast method
   * @aspect Expressions
   * @declaredat
   *     /Users/eric/Documents/workspaces/clara-soot/JastAddExtensions/JimpleBackend/Expressions.jrag:84
   */
  public soot.Value eval(Body b) {
    TypeDecl dest = getDest().type();
    TypeDecl source = getSource().type();
    if (dest.isString()) {

      Value lvalue = getDest().eval(b);

      Value v = asImmediate(b, lvalue);

      // new StringBuffer(left)
      Local local =
          b.newTemp(b.newNewExpr(lookupType("java.lang", "StringBuffer").sootRef(), this));
      b.setLine(this);
      b.add(
          b.newInvokeStmt(
              b.newSpecialInvokeExpr(
                  local,
                  Scene.v()
                      .getMethod("<java.lang.StringBuffer: void <init>(java.lang.String)>")
                      .makeRef(),
                  v,
                  this),
              this));

      // append right
      Local rightResult =
          b.newTemp(
              b.newVirtualInvokeExpr(
                  local,
                  lookupType("java.lang", "StringBuffer")
                      .methodWithArgs("append", new TypeDecl[] {source.stringPromotion()})
                      .sootRef(),
                  asImmediate(b, getSource().eval(b)),
                  this));

      // toString
      Local result =
          b.newTemp(
              b.newVirtualInvokeExpr(
                  rightResult,
                  Scene.v()
                      .getMethod("<java.lang.StringBuffer: java.lang.String toString()>")
                      .makeRef(),
                  this));

      Value v2 = lvalue instanceof Local ? lvalue : (Value) lvalue.clone();
      getDest().emitStore(b, v2, result, this);
      return result;
    } else {
      return super.eval(b);
    }
  }
Ejemplo n.º 12
0
  static Value lazyArrayChunk(Key key) {
    Key arykey = ValueArray.getArrayKey(key); // From the base file key
    long off = ValueArray.getChunkOffset(key); // The offset
    long size = getFileForKey(arykey).length();
    long rem = size - off;

    // the last chunk can be fat, so it got packed into the earlier chunk
    if (rem < ValueArray.CHUNK_SZ && off > 0) return null;
    int sz = (rem >= ValueArray.CHUNK_SZ * 2) ? (int) ValueArray.CHUNK_SZ : (int) rem;
    Value val = new Value(key, sz, Value.NFS);
    val.setdsk(); // But its already on disk.
    return val;
  }
Ejemplo n.º 13
0
 /**
  * Returns a parameter.
  *
  * @param value value
  * @param name name
  * @param declared variable declaration flags
  * @return parameter
  * @throws QueryException HTTP exception
  */
 private RestXqParam param(final Value value, final QNm name, final boolean[] declared)
     throws QueryException {
   // [CG] RESTXQ: allow identical field names?
   final long vs = value.size();
   if (vs < 2) error(ANN_PARAMS, "%", name.string(), 2);
   // name of parameter
   final String key = toString(value.itemAt(0), name);
   // variable template
   final QNm qnm = checkVariable(toString(value.itemAt(1), name), declared);
   // default value
   final ValueBuilder vb = new ValueBuilder();
   for (int v = 2; v < vs; v++) vb.add(value.itemAt(v));
   return new RestXqParam(qnm, key, vb.value());
 }
Ejemplo n.º 14
0
  /**
   * Builds the thesaurus.
   *
   * @param value input nodes
   * @throws QueryException query exception
   */
  private void build(final Value value) throws QueryException {
    final Value synonyms = nodes("*:synonym", value);
    if (synonyms.isEmpty()) return;

    final ThesNode term = node(text("*:term", value));
    for (final Item synonym : synonyms) {
      final ThesNode sterm = node(text("*:term", synonym));
      final byte[] rs = text("*:relationship", synonym);
      term.add(sterm, rs);

      final byte[] srs = RSHIPS.get(rs);
      if (srs != null) sterm.add(term, srs);
      build(synonyms);
    }
  }
Ejemplo n.º 15
0
 // Read up to 'len' bytes of Value. Value should already be persisted to
 // disk.  A racing delete can trigger a failure where we get a null return,
 // but no crash (although one could argue that a racing load&delete is a bug
 // no matter what).
 @Override
 public byte[] load(Value v) {
   long skip = 0;
   Key k = v._key;
   // Convert an arraylet chunk into a long-offset from the base file.
   if (k._kb[0] == Key.ARRAYLET_CHUNK) {
     skip = ValueArray.getChunkOffset(k); // The offset
     k = ValueArray.getArrayKey(k); // From the base file key
   }
   if (k._kb[0] == Key.DVEC) {
     skip = water.fvec.NFSFileVec.chunkOffset(k); // The offset
   }
   try {
     FileInputStream s = null;
     try {
       s = new FileInputStream(getFileForKey(k));
       FileChannel fc = s.getChannel();
       fc.position(skip);
       AutoBuffer ab = new AutoBuffer(fc, true, Value.NFS);
       byte[] b = ab.getA1(v._max);
       ab.close();
       assert v.isPersisted();
       return b;
     } finally {
       if (s != null) s.close();
     }
   } catch (IOException e) { // Broken disk / short-file???
     H2O.ignore(e);
     return null;
   }
 }
Ejemplo n.º 16
0
  /**
   * Parses the S-Expression from the lexer output. The lexer should be positioned on the first
   * symbol after the opening parenthesis.
   *
   * @return the parse tree of the input
   * @throws IOException if a read error occurs in the lexer
   * @throws ParsingException if the input cannot be parsed successfully
   */
  private Expression parseSymbolicExpression() throws IOException, ParsingException {
    Expression expr = new Expression(lexer.sval);

    int t = lexer.nextToken();
    while (t != StreamTokenizer.TT_EOF) {
      switch (t) {
        case ')':
          if (stack.empty()) return expr;
          stack.peek().addOperand(expr);
          expr = stack.pop();
          break;
        case '(': // descend into a sub-expression
          stack.push(expr);
          if (lexer.nextToken() != StreamTokenizer.TT_WORD) {
            throw new ParsingException("Expected symbol. Got: " + lexer.ttype);
          }
          expr = new Expression(lexer.sval);
          break;
        case StreamTokenizer.TT_WORD:
          try {
            // test for a number
            expr.addOperand(Value.newInt(Integer.parseInt(lexer.sval)));
          } catch (NumberFormatException ignored) {
            // fall back on a symbol
            expr.addOperand(lexer.sval);
          }
          break;
        default:
          throw new ParsingException("Unknown token type: " + lexer.ttype);
      }
      t = lexer.nextToken();
    }

    throw new ParsingException("Expected end of input.  Got: " + lexer.ttype);
  }
Ejemplo n.º 17
0
 /**
  * Populates a DataValue from the supplied array of tokens.
  *
  * @param tokens The tokens to use when building a DataValue.
  * @param destValue That this populator is filling with content.
  */
 @Override
 void populate(final String[] tokens, final Value destValue) {
   // BugzID:722 - Only populate the value if we have one from the file
   if (tokens.length > DATA_INDEX) {
     destValue.set(stripEscChars(tokens[DATA_INDEX]));
   }
 }
Ejemplo n.º 18
0
 private void commandPrint(StringTokenizer t, boolean dumpObject) throws NoSessionException {
   if (!t.hasMoreTokens()) {
     // ### Probably confused if expresion contains whitespace.
     env.error("No expression specified.");
     return;
   }
   ThreadReference current = context.getCurrentThread();
   if (current == null) {
     env.failure("No default thread specified: " + "use the \"thread\" command first.");
     return;
   }
   StackFrame frame;
   try {
     frame = context.getCurrentFrame(current);
     if (frame == null) {
       env.failure("Thread has not yet created any stack frames.");
       return;
     }
   } catch (VMNotInterruptedException e) {
     env.failure("Target VM must be in interrupted state.");
     return;
   }
   while (t.hasMoreTokens()) {
     String expr = t.nextToken("");
     Value val = null;
     try {
       val = runtime.evaluate(frame, expr);
     } catch (Exception e) {
       env.error("Exception: " + e);
       // ### Fix this!
     }
     if (val == null) {
       return; // Error message already printed
     }
     OutputSink out = env.getOutputSink();
     if (dumpObject && (val instanceof ObjectReference) && !(val instanceof StringReference)) {
       ObjectReference obj = (ObjectReference) val;
       ReferenceType refType = obj.referenceType();
       out.println(expr + " = " + val.toString() + " {");
       dump(out, obj, refType, refType);
       out.println("}");
     } else {
       out.println(expr + " = " + val.toString());
     }
     out.show();
   }
 }
Ejemplo n.º 19
0
 // Store Value v to disk.
 static void fileStore(Value v) {
   // Only the home node does persistence on NFS
   if (!v._key.home()) return;
   // A perhaps useless cutout: the upper layers should test this first.
   if (v.isPersisted()) return;
   // Never store arraylets on NFS, instead we'll store the entire array.
   assert !v.isArray();
   try {
     File f = getFileForKey(v._key);
     f.mkdirs();
     FileOutputStream s = new FileOutputStream(f);
     try {
       byte[] m = v.memOrLoad();
       assert (m == null || m.length == v._max); // Assert not saving partial files
       if (m != null) new AutoBuffer(s.getChannel(), false, Value.NFS).putA1(m, m.length).close();
       v.setdsk(); // Set as write-complete to disk
     } finally {
       s.close();
     }
   } catch (IOException e) {
     H2O.ignore(e);
   }
 }
Ejemplo n.º 20
0
 @Override
 public void postToFeed(
     final User user,
     final Entity entity,
     final Point originalPoint,
     final Value value,
     final FeedType type)
     throws NimbitsException {
   final Point point = getFeedPoint(user);
   if (point != null) {
     final FeedValue feedValue =
         new FeedValueModel(valueToHtml(entity, originalPoint, value), value.getData(), type);
     final String json = GsonFactory.getSimpleInstance().toJson(feedValue);
     final Value v = ValueModelFactory.createValueModel(value, json);
     RecordedValueServiceFactory.getInstance().recordValue(user, point, v, false);
   }
 }
Ejemplo n.º 21
0
    /**
     * Populates a DataValue from the supplied array of tokens.
     *
     * @param tokens The tokens to use when building a DataValue.
     * @param destValue That this populator is filling with content.
     */
    @Override
    void populate(final String[] tokens, final Value destValue) {
      // BugzID:722 - Only populate the value if we have one from the file
      if (tokens.length > DATA_INDEX) {
        String text = "";

        for (int i = DATA_INDEX; i < tokens.length; i++) {
          text = text.concat(tokens[i]);

          if (i < (tokens.length - 1)) {
            text = text.concat(",");
          }
        }

        destValue.set(stripEscChars(text));
      }
    }
Ejemplo n.º 22
0
  private String valueToHtml(final Entity entity, final Entity point, final Value value) {
    final StringBuilder sb = new StringBuilder(SIZE);
    if (!(Double.compare(value.getDoubleValue(), Const.CONST_IGNORED_NUMBER_VALUE) == 0)) {
      sb.append("<img style=\"float:left\" src=\"")
          .append(ServerInfoImpl.getFullServerURL(this.getThreadLocalRequest()));

      switch (value.getAlertState()) {
        case LowAlert:
          sb.append("/resources/images/point_low.png\">");
          break;
        case HighAlert:
          sb.append("/resources/images/point_high.png\">");
          break;
        case IdleAlert:
          sb.append("/resources/images/point_idle.png\">");
          break;
        case OK:
          sb.append("/resources/images/point_ok.png\">");
          break;
      }
    }

    if (entity != null && point != null) {

      sb.append("&nbsp;");

      if (!(Double.compare(value.getDoubleValue(), Const.CONST_IGNORED_NUMBER_VALUE) == 0)) {
        sb.append("Alert&nbsp;Status:").append(value.getAlertState().name());
        sb.append("<br>Value:").append(value.getDoubleValue());
      }

      if (!Utils.isEmptyString(value.getNote())) {
        sb.append("<br>Note:").append(value.getNote());
      }

      sb.append("<a href=\"#\" onclick=\"window.open('report.html?uuid=")
          .append(point.getKey())
          .append("', 'Report',")
          .append("'height=800,width=800,toolbar=0,status=0,location=0' );\" >")
          .append("&nbsp;[more]</a>");
    }

    return sb.toString();
  }
Ejemplo n.º 23
0
 @Override
 public void delete(final Value v) {
   assert this == getIce();
   assert !v.isPersisted(); // Upper layers already cleared out
   run(
       new Callable() {
         @Override
         public Object call() throws Exception {
           Path p = new Path(_iceRoot, getIceName(v));
           FileSystem fs = FileSystem.get(p.toUri(), CONF);
           fs.delete(p, true);
           if (v.isArray()) { // Also nuke directory if the top-level ValueArray dies
             p = new Path(_iceRoot, getIceDirectory(v._key));
             fs = FileSystem.get(p.toUri(), CONF);
             fs.delete(p, true);
           }
           return null;
         }
       },
       false,
       0);
 }
Ejemplo n.º 24
0
  public void generate(String className, PrintWriter out)
      throws IOException, GraphUtilException, GenerationException {
    // be sure to have at least the uri constants generated
    if (stringGeneration == null && uriGeneration == null) {
      uriGeneration = GenerationSetting.createDefault(caseFormat, "", "");
      // throw new GenerationException("no generation settings present, please set explicitly");
    }
    log.trace("classname: {}", className);
    if (StringUtils.isBlank(name)) {
      name = className;
    }
    if (StringUtils.isBlank(prefix)) {
      throw new GenerationException("could not detect prefix, please set explicitly");
    } else {
      log.debug("prefix: {}", prefix);
    }

    Pattern pattern = Pattern.compile(Pattern.quote(getPrefix()) + "(.+)");
    ConcurrentMap<String, URI> splitUris = new ConcurrentHashMap<>();
    for (Resource nextSubject : model.subjects()) {
      if (nextSubject instanceof URI) {
        Matcher matcher = pattern.matcher(nextSubject.stringValue());
        if (matcher.find()) {
          String k = matcher.group(1);
          URI putIfAbsent = splitUris.putIfAbsent(k, (URI) nextSubject);
          if (putIfAbsent != null) {
            log.warn(
                "Conflicting keys found: uri={} key={} existing={}",
                nextSubject.stringValue(),
                k,
                putIfAbsent);
          }
        }
      }
    }

    // print

    // package is optional
    if (StringUtils.isNotBlank(packageName)) {
      out.printf("package %s;%n%n", getPackageName());
    }
    // imports
    out.println("import org.openrdf.model.URI;");
    out.println("import org.openrdf.model.ValueFactory;");
    out.println("import org.openrdf.model.impl.ValueFactoryImpl;");
    out.println();

    final URI pfx = new URIImpl(prefix);
    Literal oTitle =
        getFirstExistingObjectLiteral(model, pfx, getPreferredLanguage(), LABEL_PROPERTIES);
    Literal oDescr =
        getFirstExistingObjectLiteral(model, pfx, getPreferredLanguage(), COMMENT_PROPERTIES);
    Set<Value> oSeeAlso = model.filter(pfx, RDFS.SEEALSO, null).objects();

    // class JavaDoc
    out.println("/**");
    if (oTitle != null) {
      out.printf(
          " * %s.%n",
          WordUtils.wrap(oTitle.getLabel().replaceAll("\\s+", " "), 70, "\n * ", false));
      out.println(" * <p>");
    }
    if (oDescr != null) {
      out.printf(
          " * %s.%n",
          WordUtils.wrap(oDescr.getLabel().replaceAll("\\s+", " "), 70, "\n * ", false));
      out.println(" * <p>");
    }
    out.printf(" * Namespace %s.%n", name);
    out.printf(" * Prefix: {@code <%s>}%n", prefix);
    if (!oSeeAlso.isEmpty()) {
      out.println(" *");
      for (Value s : oSeeAlso) {
        if (s instanceof URI) {
          out.printf(" * @see <a href=\"%s\">%s</a>%n", s.stringValue(), s.stringValue());
        }
      }
    }
    out.println(" */");
    // class Definition
    out.printf("public class %s {%n", className);
    out.println();

    // constants
    out.printf(getIndent(1) + "/** {@code %s} **/%n", prefix);
    out.printf(getIndent(1) + "public static final String NAMESPACE = \"%s\";%n", prefix);
    out.println();
    out.printf(getIndent(1) + "/** {@code %s} **/%n", name.toLowerCase());
    out.printf(getIndent(1) + "public static final String PREFIX = \"%s\";%n", name.toLowerCase());
    out.println();

    List<String> keys = new ArrayList<>();
    keys.addAll(splitUris.keySet());
    Collections.sort(keys, String.CASE_INSENSITIVE_ORDER);
    // do the string constant generation (if set)
    if (stringGeneration != null) {
      for (String key : keys) {
        final Literal comment =
            getFirstExistingObjectLiteral(
                model, splitUris.get(key), getPreferredLanguage(), COMMENT_PROPERTIES);
        final Literal label =
            getFirstExistingObjectLiteral(
                model, splitUris.get(key), getPreferredLanguage(), LABEL_PROPERTIES);

        out.println(getIndent(1) + "/**");
        if (label != null) {
          out.printf(getIndent(1) + " * %s%n", label.getLabel());
          out.println(getIndent(1) + " * <p>");
        }
        out.printf(getIndent(1) + " * {@code %s}.%n", splitUris.get(key).stringValue());
        if (comment != null) {
          out.println(getIndent(1) + " * <p>");
          out.printf(
              getIndent(1) + " * %s%n",
              WordUtils.wrap(
                  comment.getLabel().replaceAll("\\s+", " "),
                  70,
                  "\n" + getIndent(1) + " * ",
                  false));
        }
        out.println(getIndent(1) + " *");
        out.printf(getIndent(1) + " * @see <a href=\"%s\">%s</a>%n", splitUris.get(key), key);
        out.println(getIndent(1) + " */");

        final String nextKey =
            cleanKey(
                // NOTE: CONSTANT PREFIX and constant SUFFIX are NOT part of caseFormatting
                String.format(
                    "%s%s%s",
                    StringUtils.defaultString(stringGeneration.getConstantPrefix()),
                    doCaseFormatting(key, stringGeneration.getCaseFormat()),
                    StringUtils.defaultString(stringGeneration.getConstantSuffix())));
        checkField(className, nextKey);
        out.printf(
            getIndent(1) + "public static final String %s = %s.NAMESPACE + \"%s\";%n",
            nextKey,
            className,
            key);
        out.println();
      }
    }
    // do the entire uri constant generation
    if (uriGeneration != null) {
      for (String key : keys) {
        Literal comment =
            getFirstExistingObjectLiteral(
                model, splitUris.get(key), getPreferredLanguage(), COMMENT_PROPERTIES);
        Literal label =
            getFirstExistingObjectLiteral(
                model, splitUris.get(key), getPreferredLanguage(), LABEL_PROPERTIES);

        out.println(getIndent(1) + "/**");
        if (label != null) {
          out.printf(getIndent(1) + " * %s%n", label.getLabel());
          out.println(getIndent(1) + " * <p>");
        }
        out.printf(getIndent(1) + " * {@code %s}.%n", splitUris.get(key).stringValue());
        if (comment != null) {
          out.println(getIndent(1) + " * <p>");
          out.printf(
              getIndent(1) + " * %s%n",
              WordUtils.wrap(
                  comment.getLabel().replaceAll("\\s+", " "),
                  70,
                  "\n" + getIndent(1) + " * ",
                  false));
        }
        out.println(getIndent(1) + " *");
        out.printf(getIndent(1) + " * @see <a href=\"%s\">%s</a>%n", splitUris.get(key), key);
        out.println(getIndent(1) + " */");
        final String nextKey =
            cleanKey(
                // NOTE: CONSTANT PREFIX and constant SUFFIX are NOT part of caseFormatting
                String.format(
                    "%s%s%s",
                    StringUtils.defaultString(uriGeneration.getConstantPrefix()),
                    doCaseFormatting(key, uriGeneration.getCaseFormat()),
                    StringUtils.defaultString(uriGeneration.getConstantSuffix())));

        // String nextKey = cleanKey(doCaseFormatting(key, uriGeneration.getCaseFormat()));
        checkField(className, nextKey);
        out.printf(getIndent(1) + "public static final URI %s;%n", nextKey);
        out.println();
      }
      // static init
      out.println(getIndent(1) + "static {");
      out.printf(getIndent(2) + "ValueFactory factory = ValueFactoryImpl.getInstance();%n");
      out.println();
      for (String key : keys) {
        final String nextKey =
            cleanKey(
                // NOTE: CONSTANT PREFIX and constant SUFFIX are NOT part of caseFormatting
                String.format(
                    "%s%s%s",
                    StringUtils.defaultString(uriGeneration.getConstantPrefix()),
                    doCaseFormatting(key, uriGeneration.getCaseFormat()),
                    StringUtils.defaultString(uriGeneration.getConstantSuffix())));
        out.printf(
            getIndent(2) + "%s = factory.createURI(%s.NAMESPACE, \"%s\");%n",
            nextKey,
            className,
            key);
      }
      out.println(getIndent(1) + "}");
      out.println();
    }

    // private contructor to avoid instances
    out.printf(getIndent(1) + "private %s() {%n", className);
    out.println(getIndent(2) + "//static access only");
    out.println(getIndent(1) + "}");
    out.println();

    // class end
    out.println("}");
    out.flush();
  }
Ejemplo n.º 25
0
 /**
  * Get the domain index of the given value.
  *
  * @param v a value within the domain of this variable.
  * @return domain index corresponding to the given value or -1 if the value is not within the
  *     domain.
  */
 public int indexOf(Value v) {
   for (int i = 0; i < domain.length; i++) if (v.equal(domain[i])) return i;
   return -1;
 }
Ejemplo n.º 26
0
  /**
   * Evaluates the specified query.
   *
   * @param query query
   * @return success flag
   */
  final boolean query(final String query) {
    final Performance p = new Performance();
    String error;
    if (exception != null) {
      error = Util.message(exception);
    } else {
      try {
        long hits = 0;
        final boolean run = options.get(MainOptions.RUNQUERY);
        final boolean serial = options.get(MainOptions.SERIALIZE);
        final int runs = Math.max(1, options.get(MainOptions.RUNS));
        for (int r = 0; r < runs; ++r) {
          // reuse existing processor instance
          if (r != 0) qp = null;
          qp(query, context);
          parse(p);
          if (r == 0) plan(false);

          qp.compile();
          info.compiling += p.time();
          if (r == 0) plan(true);
          if (!run) continue;

          final PrintOutput po = r == 0 && serial ? out : new NullOutput();
          try (final Serializer ser = qp.getSerializer(po)) {
            if (maxResults >= 0) {
              result = qp.cache(maxResults);
              info.evaluating += p.time();
              result.serialize(ser);
              hits = result.size();
            } else {
              hits = 0;
              final Iter ir = qp.iter();
              info.evaluating += p.time();
              for (Item it; (it = ir.next()) != null; ) {
                ser.serialize(it);
                ++hits;
                checkStop();
              }
            }
          }
          qp.close();
          info.serializing += p.time();
        }
        // dump some query info
        // out.flush();

        // remove string list if global locking is used and if query is updating
        if (soptions.get(StaticOptions.GLOBALLOCK) && qp.updating) {
          info.readLocked = null;
          info.writeLocked = null;
        }
        return info(info.toString(qp, out.size(), hits, options.get(MainOptions.QUERYINFO)));

      } catch (final QueryException | IOException ex) {
        exception = ex;
        error = Util.message(ex);
      } catch (final ProcException ex) {
        error = INTERRUPTED;
      } catch (final StackOverflowError ex) {
        Util.debug(ex);
        error = BASX_STACKOVERFLOW.desc;
      } catch (final RuntimeException ex) {
        extError("");
        Util.debug(info());
        throw ex;
      } finally {
        // close processor after exceptions
        if (qp != null) qp.close();
      }
    }
    return extError(error);
  }
Ejemplo n.º 27
0
  /*
   * Generates the sequence of instructions needed to instrument ifStmtUnit
   *
   * @param ifStmtUnit: the unit to be instrumented
   * @return A Chain of Units that represent the instrumentation of ifStmtUnit
   */
  private static Chain<Unit> generateInstrumentationUnits(Unit ifStmtUnit, Body b) {

    AbstractBinopExpr expression =
        (AbstractBinopExpr)
            ((JIfStmt) ifStmtUnit).getCondition(); // implementation of AbstractBinopExpr
    Value operand1 = expression.getOp1();
    Value operand2 = expression.getOp2();

    JimpleLocal op1Local = (JimpleLocal) operand1;

    // Local localOperand = Jimple.v().newLocal("op1", operand1.getType());
    // b.getLocals().add(localOperand);

    // We need to use these operand as Locals or constants
    /**
     * JimpleLocal test; if(operand1 instanceof soot.jimple.internal.JimpleLocal) test =
     * (JimpleLocal)operand1; else test = null;
     */
    String op = expression.getClass().toString();

    Chain<Unit> resultUnits = new HashChain<Unit>();
    Local tmpRef;
    // Add locals directely on the top of the body, java.io.printStream tmpRef
    tmpRef = Jimple.v().newLocal("tmpRef", RefType.v("java.io.PrintStream"));
    b.getLocals().addFirst(tmpRef);

    // add "tmpRef = java.lang.System.out"
    resultUnits.add(
        Jimple.v()
            .newAssignStmt(
                tmpRef,
                Jimple.v()
                    .newStaticFieldRef(
                        Scene.v()
                            .getField("<java.lang.System: java.io.PrintStream out>")
                            .makeRef())));
    {
      SootMethod toCall =
          Scene.v().getMethod("<java.io.PrintStream: void println(java.lang.String)>");
      resultUnits.add(
          Jimple.v()
              .newInvokeStmt(
                  Jimple.v()
                      .newVirtualInvokeExpr(
                          tmpRef, toCall.makeRef(), StringConstant.v("Operande 1: "))));
      resultUnits.add(
          Jimple.v()
              .newInvokeStmt(
                  Jimple.v()
                      .newVirtualInvokeExpr(
                          tmpRef,
                          toCall.makeRef(),
                          StringConstant.v(operand1.getClass().toString()))));

      resultUnits.add(
          Jimple.v()
              .newInvokeStmt(
                  Jimple.v()
                      .newVirtualInvokeExpr(
                          tmpRef, toCall.makeRef(), StringConstant.v("Operande 2:"))));

      resultUnits.add(
          Jimple.v()
              .newInvokeStmt(
                  Jimple.v()
                      .newVirtualInvokeExpr(
                          tmpRef,
                          toCall.makeRef(),
                          StringConstant.v(operand2.getClass().toString()))));

      resultUnits.add(
          Jimple.v()
              .newInvokeStmt(
                  Jimple.v()
                      .newVirtualInvokeExpr(
                          tmpRef, toCall.makeRef(), StringConstant.v("Operateur: "))));

      resultUnits.add(
          Jimple.v()
              .newInvokeStmt(
                  Jimple.v().newVirtualInvokeExpr(tmpRef, toCall.makeRef(), StringConstant.v(op))));
    }

    return resultUnits;
  }
Ejemplo n.º 28
0
  /**
   * Adds items to the specified list.
   *
   * @param value value
   * @param name name
   * @param list list to add values to
   * @throws QueryException HTTP exception
   */
  private void strings(final Value value, final QNm name, final StringList list)
      throws QueryException {

    final long vs = value.size();
    for (int v = 0; v < vs; v++) list.add(toString(value.itemAt(v), name));
  }
Ejemplo n.º 29
0
  /**
   * Checks a function for RESTFful annotations.
   *
   * @return {@code true} if module contains relevant annotations
   * @throws QueryException query exception
   */
  boolean analyze() throws QueryException {
    // parse all annotations
    final EnumSet<HTTPMethod> mth = EnumSet.noneOf(HTTPMethod.class);
    final boolean[] declared = new boolean[function.args.length];
    boolean found = false;
    final int as = function.ann.size();
    for (int a = 0; a < as; a++) {
      final QNm name = function.ann.names[a];
      final Value value = function.ann.values[a];
      final byte[] local = name.local();
      final byte[] uri = name.uri();
      final boolean rexq = eq(uri, QueryText.RESTXQURI);
      if (rexq) {
        if (eq(PATH, local)) {
          // annotation "path"
          if (path != null) error(ANN_TWICE, "%", name.string());
          path = new RestXqPath(toString(value, name));
          for (final String s : path) {
            if (s.trim().startsWith("{")) checkVariable(s, AtomType.AAT, declared);
          }
        } else if (eq(CONSUMES, local)) {
          // annotation "consumes"
          strings(value, name, consumes);
        } else if (eq(PRODUCES, local)) {
          // annotation "produces"
          strings(value, name, produces);
        } else if (eq(QUERY_PARAM, local)) {
          // annotation "query-param"
          queryParams.add(param(value, name, declared));
        } else if (eq(FORM_PARAM, local)) {
          // annotation "form-param"
          formParams.add(param(value, name, declared));
        } else if (eq(HEADER_PARAM, local)) {
          // annotation "header-param"
          headerParams.add(param(value, name, declared));
        } else if (eq(COOKIE_PARAM, local)) {
          // annotation "cookie-param"
          cookieParams.add(param(value, name, declared));
        } else {
          // method annotations
          final HTTPMethod m = HTTPMethod.get(string(local));
          if (m == null) error(ANN_UNKNOWN, "%", name.string());
          if (!value.isEmpty()) {
            // remember post/put variable
            if (requestBody != null) error(ANN_TWICE, "%", name.string());
            if (m != POST && m != PUT) error(METHOD_VALUE, m);
            requestBody = checkVariable(toString(value, name), declared);
          }
          if (mth.contains(m)) error(ANN_TWICE, "%", name.string());
          mth.add(m);
        }
      } else if (eq(uri, QueryText.OUTPUTURI)) {
        // serialization parameters
        final String key = string(local);
        final String val = toString(value, name);
        if (output.get(key) == null) error(UNKNOWN_SER, key);
        output.set(key, val);
      }
      found |= rexq;
    }
    if (!mth.isEmpty()) methods = mth;

    if (found) {
      if (path == null) error(ANN_MISSING, PATH);
      for (int i = 0; i < declared.length; i++)
        if (!declared[i]) error(VAR_UNDEFINED, function.args[i].name.string());
    }
    return found;
  }
Ejemplo n.º 30
0
 public static byte [] getFirstUnzipedBytes(Value v){
   byte [] bits = v.getFirstBytes();
   try{
     return unzipBytes(bits, guessCompressionMethod(bits));
   } catch(Exception e){return null;}
 }