Exemple #1
0
 /** Considering name and address as combined inverse functional property */
 public void smush() {
   final Map<String, Set<GraphNode>> equalityMap = new HashMap<String, Set<GraphNode>>();
   final LockableMGraph dataGraph = getDataGraph();
   Lock l = dataGraph.getLock().writeLock();
   l.lock();
   try {
     final Iterator<Triple> triples = dataGraph.filter(null, RDF.type, ADDRESSES.Address);
     while (triples.hasNext()) {
       final GraphNode gn = new GraphNode(triples.next().getSubject(), dataGraph);
       final String name = gn.getLiterals(ADDRESSES.name).next().getLexicalForm();
       final String address = gn.getLiterals(ADDRESSES.address).next().getLexicalForm();
       final String key = name + address;
       final Set<GraphNode> set =
           equalityMap.containsKey(key) ? equalityMap.get(key) : new HashSet<GraphNode>();
       set.add(gn);
       equalityMap.put(key, set);
     }
   } finally {
     l.unlock();
   }
   for (Set<GraphNode> equalitySet : equalityMap.values()) {
     final Iterator<GraphNode> iter = equalitySet.iterator();
     final GraphNode first = iter.next();
     while (iter.hasNext()) {
       iter.next().replaceWith((NonLiteral) first.getNode());
     }
   }
 }
  /**
   * 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();
  }
Exemple #3
0
 public void importFile(File file) throws IOException {
   final FileInputStream fileInputStream = new FileInputStream(file);
   final InputStreamReader inputStreamReader =
       new InputStreamReader(fileInputStream, "iso-8859-1");
   // final FileReader fileReader = new FileReader(file);
   final BufferedReader in = new BufferedReader(inputStreamReader);
   final MGraph dataGraph = getDataGraph();
   // int abortCounter = 0;
   for (String line = in.readLine(); line != null; line = in.readLine()) {
     /*if (abortCounter++ > 10) {
     	break;
     }*/
     final GraphNode entry = new GraphNode(new BNode(), dataGraph);
     entry.addPropertyValue(RDFS.comment, line);
     final String[] tokens = line.replace('|', ';').split(";");
     /* Wey Andreas|Dr. med. Facharzt FMH für Allgemeinmedizin|
      * Bürenstrasse 2, 3296 Arch|[email protected]|be|allgemeinaerzte */
     entry.addPropertyValue(ADDRESSES.name, tokens[0]);
     entry.addPropertyValue(ADDRESSES.description, tokens[1]);
     entry.addPropertyValue(ADDRESSES.address, tokens[2]);
     if (!tokens[3].equals("")) entry.addPropertyValue(ADDRESSES.email, tokens[3]);
     entry.addPropertyValue(ADDRESSES.tel, tokens[4]);
     entry.addPropertyValue(ADDRESSES.fax, tokens[5]);
     entry.addPropertyValue(ADDRESSES.canton, tokens[6]);
     entry.addPropertyValue(ADDRESSES.category, tokens[7]);
     entry.addProperty(RDF.type, ADDRESSES.Address);
     addGeoPos(entry);
   }
 }
Exemple #4
0
 @GET
 public GraphNode overviewPage() {
   MGraph resultGraph = new SimpleMGraph();
   GraphNode result = new GraphNode(new BNode(), resultGraph);
   result.addProperty(RDF.type, BACKUP.BackupAdminPage);
   result.addProperty(RDF.type, PLATFORM.HeadedPage);
   return result;
 }
 @Override
 public void render(GraphNode resource, GraphNode context, String mode, OutputStream os)
     throws IOException {
   Renderer renderer = manager.createRenderer(resource, mode, mediaTypeList);
   if (renderer == null) {
     throw new RuntimeException(
         "no renderer could be created for "
             + resource
             + " (in "
             + resource.getNodeContext()
             + "), "
             + mode
             + ","
             + mediaTypeList);
   }
   renderer.render(
       resource,
       context,
       mode,
       uriInfo,
       requestHeaders,
       responseHeaders,
       sharedRenderingValue,
       os);
 }
Exemple #6
0
 static void addGeoPos(GraphNode entry) {
   /* http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=Tscharnerstrasse%2041,%20Bern*/
   final String address = entry.getLiterals(ADDRESSES.address).next().getLexicalForm();
   try {
     final String uriString =
         "http://maps.googleapis.com/maps/api/"
             + "geocode/json?sensor=false&address="
             + URLEncoder.encode(address, "utf-8");
     final URL uri = new URL(uriString);
     final InputStream in = uri.openStream();
     final JSONObject jsonObject = parseJSon(in);
     /* {"lat":46.4916335,"lng":7.557490499999999}*/
     // System.out.println(jsonObject.toString(2));
     String status = (String) jsonObject.get("status");
     // System.out.println(status);
     if (status.equals("OVER_QUERY_LIMIT")) {
       throw new RuntimeException("over query limit");
     }
     if (status.equals("OK")) {
       final JSONObject location =
           jsonObject
               .getJSONArray("results")
               .getJSONObject(0)
               .getJSONObject("geometry")
               .getJSONObject("location");
       final String lat = location.getString("lat");
       final String long_ = location.getString("lng");
       entry.addPropertyValue(WGS84_POS.lat, lat);
       entry.addPropertyValue(WGS84_POS.long_, long_);
     } else {
       throw new UnknownStatusException(status);
     }
   } catch (JSONException ex) {
     throw new RuntimeException(ex);
   } catch (IOException ex) {
     throw new RuntimeException(ex);
   }
 }
 public void generate(GraphNode node, byte[] data, MediaType mediaType) {
   final ContentItem ci =
       store.create(((UriRef) node.getNode()).getUnicodeString(), data, mediaType.toString());
   if (ci == null) {
     return;
   }
   try {
     jobManager.enhanceContent(ci);
   } catch (EngineException e) {
     // TODO: would be better wrapped as an exception accepted by the
     // MetaDataGenerator interface instead of hiding unexpected problems
     // in the logs and behave as if everything went well
     log.error(e, e);
   }
 }