Exemplo n.º 1
0
 @Override
 public int write(TupleIterator iter) throws TrippiException {
   try {
     m_out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
     doEntities();
     m_out.println("<sparql xmlns=\"http://www.w3.org/2001/sw/DataAccess/rf1/result\">");
     String[] names = iter.names();
     m_out.println("  <head>");
     for (int i = 0; i < names.length; i++) {
       m_out.println("    <variable name=\"" + names[i] + "\"/>");
     }
     m_out.println("  </head>");
     m_out.println("  <results>");
     int count = 0;
     while (iter.hasNext()) {
       m_out.println("    <result>");
       Map<String, Node> result = iter.next();
       for (int i = 0; i < names.length; i++) {
         m_out.print("      <" + names[i]);
         Node n = result.get(names[i]);
         if (n == null) {
           m_out.println(" bound=\"false\"/>");
         } else if (n instanceof URIReference) {
           String uriString = ((URIReference) n).getURI().toString();
           m_out.println(" uri=\"" + getURI(uriString) + "\"/>");
         } else if (n instanceof BlankNode) {
           String id = "blank" + n.hashCode();
           m_out.println(" bnodeid=\"" + id + "\"/>");
         } else if (n instanceof Literal) {
           Literal lit = (Literal) n;
           URI dType = lit.getDatatypeURI();
           if (dType != null) {
             m_out.print(" datatype=\"" + getURI(dType.toString()) + "\"");
           }
           String lang = lit.getLanguage();
           if (lang != null) {
             m_out.print(" xml:lang=\"" + lang + "\"");
           }
           m_out.println(">" + enc(lit.getLexicalForm()) + "</" + names[i] + ">");
         } else {
           throw new TrippiException("Unrecognized node type: " + n.getClass().getName());
         }
       }
       m_out.println("    </result>");
       m_out.flush();
       count++;
     }
     m_out.println("  </results>");
     m_out.println("</sparql>");
     m_out.flush();
     iter.close();
     return count;
   } catch (IOException e) {
     throw new TrippiException("Error writing", e);
   }
 }
Exemplo n.º 2
0
 /**
  * Determines if the object is a Literal or Resource and returns an iTQL representation.
  *
  * <p>Throws an Exception if the ObjectNode is a BlankNode
  *
  * @param node ObjectNode
  * @return String
  */
 protected String asString(Node node) {
   if (node == null) {
     throw new IllegalArgumentException("Node is null");
   }
   // determine type and convert to String
   if (node instanceof URIReference) {
     return " <" + ((URIReference) node).getURI() + ">";
   } else if (node instanceof Literal) {
     return " '" + escape(((Literal) node).getLexicalForm()) + "'";
   } else {
     // probably a BlankNode
     throw new IllegalArgumentException("Unsupported Node type: " + node.getClass().getName());
   }
 }
Exemplo n.º 3
0
  /**
   * Resolve a constraint against an RDF/XML document.
   *
   * <p>Resolution is by filtration of a URL stream, and thus very slow.
   */
  public Resolution resolve(Constraint constraint) throws QueryException {
    if (logger.isDebugEnabled()) {
      logger.debug("Resolve " + constraint);
    }

    // Validate "constraint" parameter
    if (constraint == null) {
      throw new IllegalArgumentException("Null \"constraint\" parameter");
    }

    if (!(constraint.getModel() instanceof LocalNode)) {
      if (logger.isDebugEnabled()) logger.debug("Ignoring solutions for " + constraint);
      return new EmptyResolution(constraint, false);
    }

    if (!(constraint.getElement(1) instanceof LocalNode)
        || !(constraint.getElement(2) instanceof LocalNode)) {
      throw new QueryException(
          "Prefix resolver can only be used for fixed prefixes: " + constraint);
    }

    try {

      long property = ((LocalNode) constraint.getElement(1)).getValue();
      LocalNode object = (LocalNode) constraint.getElement(2);
      Node prefixNode = resolverSession.globalize(object.getValue());

      // check the constraint for consistency
      if ((property != mulgaraPrefix && property != mulgaraStringPrefix)
          || !(prefixNode instanceof Literal || prefixNode instanceof URIReference)) {
        logger.debug("property = " + property + ", mulgaraPrefix = " + mulgaraPrefix);
        logger.debug("element(2): " + prefixNode + " [" + prefixNode.getClass().getName() + "]");
        throw new QueryException(
            "Prefix resolver can only be used for prefix constraints: " + constraint);
      }

      if (logger.isDebugEnabled()) {
        logger.debug(
            "Evaluating " + constraint.getElement(0) + " has prefix " + constraint.getElement(2));
      }

      ConstraintElement node = constraint.getElement(0);
      assert node != null;

      Tuples tuples;

      if (node instanceof Variable) {

        // convert the prefix into a string pool object
        SPObjectFactory spoFact = SPObjectFactoryImpl.getInstance();
        SPObject startPrefixObj = getStartObject(spoFact, prefixNode, property);
        SPObject endPrefixObj = getEndObject(spoFact, prefixNode, property);

        // get the extents of the prefix from the string pool
        tuples = resolverSession.findStringPoolRange(startPrefixObj, true, endPrefixObj, false);
        assert tuples != null;
        // rename variables away from subject, predicate and object
        tuples.renameVariables(constraint);

        long resultSize;
        try {
          // Get the size of the final result.
          resultSize = tuples.getRowCount();
        } catch (TuplesException e) {
          throw new QueryException("Unable to build result", e);
        }

        if (logger.isDebugEnabled()) {
          try {
            logger.debug(
                "tuples size = " + tuples.getRowCount() + " (should be " + resultSize + ")");
          } catch (TuplesException e) {
            logger.debug("Error getting the length of the tuples object");
          }
        }

        return new TuplesWrapperResolution(tuples, constraint);

      } else { // if (node instanceof Variable)

        // node must therefore be an instanceof LocalNode
        // we can shortcut the process here
        assert node instanceof LocalNode;
        LocalNode n = (LocalNode) node;

        // get the node out of the string pool
        SPObject spo = resolverSession.findStringPoolObject(n.getValue());

        // check that the node exists
        if (spo == null) {
          tuples = TuplesOperations.empty();
        } else {

          // see if the node starts with the required prefix

          String prefix;
          // extract the string from the literal
          if (prefixNode instanceof Literal) {
            prefix = ((Literal) prefixNode).getLexicalForm();
          } else {
            prefix = ((URIReference) prefixNode).getURI().toString();
          }
          if (spo.getLexicalForm().startsWith(prefix)) {
            tuples = TuplesOperations.unconstrained();
          } else {
            tuples = TuplesOperations.empty();
          }
        }
      }

      // convert the tuples to a resolution
      return new TuplesWrapperResolution(tuples, constraint);

    } catch (GlobalizeException ge) {
      throw new QueryException("Couldn't convert internal data into a string", ge);
    } catch (StringPoolException e) {
      throw new QueryException("Couldn't query constraint", e);
    }
  }