/** {@inheritDoc} */
 protected void addLiteral(Literal literal) throws IOException {
   s.append(i(4)).append("<literal");
   if (literal.getLanguage() != null)
     s.append(" xml:lang=\"").append(literal.getLanguage()).append("\"");
   else if (literal.getDatatype() != null)
     s.append(" datatype=\"").append(literal.getDatatype().toString()).append("\"");
   s.append(">").append(StringUtil.quoteAV(literal.getLexicalForm())).append("</literal>");
 }
Beispiel #2
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);
   }
 }
Beispiel #3
0
  /**
   * Used to write Resources for a Subject. Resources will use "ObjectNode" method.
   *
   * @param predicate PredicateNode
   * @param object Literal
   * @param writer PrintWriter
   * @throws GraphException
   */
  protected void writeStatement(
      Graph graph, SubjectNode subject, PredicateNode predicate, Literal object, PrintWriter writer)
      throws GraphException {

    // determine if the Literal has a datatype
    URI datatype = object.getDatatypeURI();

    // Get the lexical form of the literal
    String literalObject = object.getLexicalForm();

    // Create the StringBuffer to hold the resultant string
    StringBuffer buffer = new StringBuffer();

    // Escape the XML string
    StringUtil.quoteAV(literalObject, buffer);

    if (datatype != null) {

      // write as:  <predicateURI rdf:datatype="datatype">"Literal value"
      //           </predicateURI>
      writer.println(
          "    <"
              + this.getURI(predicate)
              + " "
              + RDF_PREFIX
              + ":datatype=\""
              + datatype
              + "\">"
              + buffer.toString()
              + "</"
              + this.getURI(predicate)
              + ">");
    } else {

      // write as:  <predicateURI>"Literal value"</predicateURI>
      writer.println(
          "    <"
              + this.getURI(predicate)
              + ">"
              + buffer.toString()
              + "</"
              + this.getURI(predicate)
              + ">");
    }
  }
Beispiel #4
0
  /**
   * Convert a Mulgara Value to a Jena graph node.
   *
   * @param obj The Mulgara value to convert.
   * @return A new Jena graph node.
   */
  static Node o2n(org.jrdf.graph.Node obj) {
    if (obj == null) return Node.ANY;

    // testing for org.jrdf.graph.URIReference
    if (obj.isURIReference()) {
      URIReference uRef = (URIReference) obj;
      if (skolemizedBlankNodes) {
        String x = uRef.getURI().toString();
        if (x.startsWith(bNodeScheme)) {
          x = x.substring(bNodeScheme.length());
          Node n = Node.createAnon(new AnonId(x));
          return n;
        }
      }
      return Node.createURI(uRef.getURI().toString());
    }

    // testing for org.jrdf.graph.Literal
    if (obj.isLiteral()) {
      Literal literal = (Literal) obj;
      if (literal.getDatatypeURI() != null) {
        RDFDatatype type =
            TypeMapper.getInstance().getSafeTypeByName(literal.getDatatypeURI().toString());
        return Node.createLiteral(literal.getLexicalForm(), null, type);
      }

      return Node.createLiteral(literal.getLexicalForm(), literal.getLanguage(), null);
    }

    if (obj.isBlankNode()) {
      BlankNodeImpl b = (BlankNodeImpl) obj;
      // check if this was a Jena-allocated node
      Node jenaNode = valuesToNodes.get(b);
      // if not known, then create a Jena node from the Mulgara ID
      return jenaNode != null ? jenaNode : Node.createAnon(new AnonId(b.getID()));
    }
    throw new RuntimeException("Can't convert to Jena Node : " + obj);
  }
Beispiel #5
0
  /** Insert or delete RDF statements in a model at a URL. */
  public void modifyModel(long model, Statements statements, boolean occurs)
      throws ResolverException {
    if (logger.isDebugEnabled()) {
      logger.debug("Modify URL model " + model);
    }

    try {
      FullTextStringIndex stringIndex = getFullTextStringIndex(model);

      statements.beforeFirst();
      while (statements.next()) {
        Node subjectNode = resolverSession.globalize(statements.getSubject());

        // Do not insert the triple if it contains a blank node in subject.
        if (subjectNode instanceof BlankNode) {
          if (logger.isInfoEnabled()) {
            logger.info(statements.getSubject() + " is blank node; ignoring Lucene insert.");
          }

          continue;
        }

        Node predicateNode = resolverSession.globalize(statements.getPredicate());
        Node objectNode = resolverSession.globalize(statements.getObject());

        // Get the subject's string value.
        String subject = ((URIReference) subjectNode).getURI().toString();

        // Predicates can only ever be URIReferences.
        String predicate = ((URIReference) predicateNode).getURI().toString();

        if (objectNode instanceof URIReference) {
          URIReference objectURI = (URIReference) objectNode;
          String resource = objectURI.getURI().toString();

          try {
            // Assert or deny the statement in the Lucene model
            if (occurs) {
              InputStream input = null;
              Reader reader = null;
              try {
                // Connect to the resource's content
                URLConnection connection = objectURI.getURI().toURL().openConnection();
                String contentType = connection.getContentType();

                if (logger.isDebugEnabled()) {
                  logger.debug("Content type of resource is " + contentType);
                }

                MimeType contentMimeType;

                try {
                  contentMimeType = new MimeType(contentType);
                } catch (MimeTypeParseException e) {
                  logger.warn("\"" + contentType + "\" didn't parse as MIME type", e);
                  try {
                    contentMimeType = new MimeType("content", "unknown");
                  } catch (MimeTypeParseException em) {
                    throw new ResolverException("Failed to create mime-type", em);
                  }
                }

                assert contentMimeType != null;

                // If no character encoding is specified, guess at Latin-1
                String charSet = contentMimeType.getParameter("charset");
                if (charSet == null) {
                  charSet = "ISO8859-1";
                }

                assert charSet != null;

                // Get the content, performing appropriate character encoding
                input = connection.getInputStream();
                reader = new InputStreamReader(input, charSet);

                // Add a filter if the content type is text/html, to strip out
                // HTML keywords that will clutter the index
                try {
                  if (contentMimeType.match(new MimeType("text", "html"))) {
                    reader = HtmlToTextConverter.convert(reader);
                  }
                } catch (MimeTypeParseException em) {
                  throw new ResolverException("Failed to create mime-type", em);
                }

                if (logger.isDebugEnabled()) {
                  logger.debug("Inserting " + subject + " " + predicate + " " + resource);
                }

                if (!stringIndex.add(subject, predicate, resource, reader)) {
                  logger.warn(
                      "Unable to add {"
                          + subject
                          + ", "
                          + predicate
                          + ", "
                          + resource
                          + "} to full text string index");
                }
              } catch (MalformedURLException e) {
                logger.info(resource + " is not a URL; ignoring Lucene insert");
              } catch (IOException e) {
                throw new ResolverException("Can't obtain content of " + resource, e);
              } catch (org.mulgara.util.conversion.html.ParseException e) {
                throw new ResolverException("Couldn't parse content of " + resource, e);
              } finally {
                try {
                  if (reader != null) reader.close();
                  else if (input != null) input.close();
                } catch (IOException e) {
                  logger.warn("Ignoring error closing resource content", e);
                }
              }
            } else { // (!occurs)
              if (logger.isDebugEnabled()) {
                logger.debug("Deleting " + subject + " " + predicate + " " + resource);
              }

              if (!stringIndex.remove(subject, predicate, resource)) {
                logger.warn(
                    "Unable to remove {"
                        + subject
                        + ", "
                        + predicate
                        + ", "
                        + resource
                        + "} from full text string index");
              }
            }
          } catch (FullTextStringIndexException e) {
            throw new ResolverException("Unable to modify full text index\n" + new StackTrace(e));
          }

        } else if (objectNode instanceof Literal) {
          Literal objectLiteral = (Literal) objectNode;
          String literal = objectLiteral.getLexicalForm();

          // Insert the statement into the text index
          try {
            if (occurs) {
              if (logger.isDebugEnabled()) {
                logger.debug("Inserting " + subject + " " + predicate + " " + literal);
              }

              if (!stringIndex.add(subject, predicate, literal)) {
                logger.warn(
                    "Unable to add {"
                        + subject
                        + ", "
                        + predicate
                        + ", "
                        + literal
                        + "} to full text string index");
              }
            } else {
              if (logger.isDebugEnabled()) {
                logger.debug("Deleting " + subject + " " + predicate + " " + literal);
              }

              if (!stringIndex.remove(subject, predicate, literal)) {
                logger.warn(
                    "Unable to remove {"
                        + subject
                        + ", "
                        + predicate
                        + ", "
                        + literal
                        + "} from full text string index");
              }
            }
          } catch (FullTextStringIndexException e) {
            throw new ResolverException(
                "Unable to "
                    + (occurs ? "add" : "delete")
                    + "'"
                    + literal
                    + "' to full text string index\n"
                    + new StackTrace(e));
          }
        } else {
          if (logger.isInfoEnabled()) {
            logger.info(objectNode + " is blank node; ignoring Lucene insert.");
          }
        }
      }
    } catch (TuplesException et) {
      throw new ResolverException("Error fetching statements", et);
    } catch (GlobalizeException eg) {
      throw new ResolverException("Error localizing statements", eg);
    } catch (IOException ioe) {
      throw new ResolverException("Failed to open string index", ioe);
    } catch (FullTextStringIndexException ef) {
      throw new ResolverException("Error in string index\n" + new StackTrace(ef));
    }
  }