Ejemplo n.º 1
0
  /**
   * Used to write Resources for a Subject. Literals will by-pass this method and use "Literal"
   * method.
   *
   * @param predicate PredicateNode
   * @param object ObjectNode
   * @param writer PrintWriter
   * @throws GraphException
   */
  protected void writeStatement(
      Graph graph,
      SubjectNode subject,
      PredicateNode predicate,
      ObjectNode object,
      PrintWriter writer)
      throws GraphException {

    // Literals are written differently
    if (object instanceof Literal) {

      this.writeStatement(graph, subject, predicate, (Literal) object, writer);
    } else if (object instanceof BlankNode) {

      // write as:  <predicateURI> *blank node as subject* </predicateURI>
      writer.println("    <" + this.getURI(predicate) + ">");

      // write blank node as a "subject"
      this.writeSubject(graph, (BlankNode) object, writer);

      writer.println("    </" + this.getURI(predicate) + ">");
    } else if (subject instanceof BlankNode) {

      // predicatNode representing RDF Type
      PredicateNode rdfTypeNode = null;

      try {

        rdfTypeNode = graph.getElementFactory().createResource(RDF.TYPE);
      } catch (GraphElementFactoryException factoryException) {

        throw new GraphException("Could not create RDF Type node.", factoryException);
      }

      // do not write the RDF Type element
      if (!rdfTypeNode.equals(predicate)) {

        // write as:  <predicateURI rdf:resource="resourceURI"/>
        writer.println(
            "    <"
                + this.getURI(predicate)
                + " "
                + RDF_PREFIX
                + ":resource=\""
                + this.getNodeString(object)
                + "\"/>");
      }
    } else {

      // write as:  <predicateURI rdf:resource="resourceURI"/>
      writer.println(
          "    <"
              + this.getURI(predicate)
              + " "
              + RDF_PREFIX
              + ":resource=\""
              + this.getNodeString(object)
              + "\"/>");
    }
  }
Ejemplo n.º 2
0
  /**
   * Writes the XML Declaration and the opening RDF tag to the print Writer. Encoding attribute is
   * specified as the encoding argument.
   *
   * @param out PrintWriter
   * @throws IOException
   */
  protected void writeHeader(OutputStreamWriter out) throws IOException {

    // validate
    if (out != null) {

      // wrapper for output stream writer (enable autoflushing)
      PrintWriter writer = new PrintWriter(out, true);

      // get encoding from the Encoding map
      String encoding = EncodingMap.getJava2IANAMapping(out.getEncoding());

      // only insert encoding if there is a value
      if (encoding != null) {

        // print opening tags <?xml version="1.0" encoding=*encoding*?>
        writer.println("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>");
      } else {

        // print opening tags <?xml version="1.0"?>
        writer.println("<?xml version=\"1.0\"?>");
      }

      // print the Entities
      this.writeXMLEntities(writer);

      // print the opening RDF tag (including namespaces)
      this.writeRDFHeader(writer);
    } else {

      throw new IllegalArgumentException("Cannot write to null Writer.");
    }
  }
Ejemplo n.º 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)
              + ">");
    }
  }
Ejemplo n.º 4
0
  /**
   * Writes the closing RDF tag to the writer.
   *
   * @param out PrintWriter
   * @throws IOException
   */
  protected void writeFooter(PrintWriter out) throws IOException {

    // validate
    if (out != null) {
      // print closing RDF tag
      out.println("</" + RDF_PREFIX + ":RDF>");
    } else {
      throw new IllegalArgumentException("Cannot write to null Writer.");
    }
  }
Ejemplo n.º 5
0
  /**
   * Writes a Subject to the writer
   *
   * @param graph JRDFGraph
   * @param subject SubjectNode
   * @param writer PrintWriter
   * @throws GraphException
   */
  protected void writeSubject(Graph graph, SubjectNode subject, PrintWriter writer)
      throws GraphException {

    // opening subject tag (includes literals)
    this.writeOpeningTag(graph, subject, writer);

    // subject contents
    this.writeSubjectBody(graph, subject, writer);

    // closing subject tag
    this.writeClosingTag(graph, subject, writer);

    // put a space between Subjects
    writer.println("");
  }
Ejemplo n.º 6
0
  /**
   * Writes the XML Declaration and the opening RDF tag to the print Writer. Encoding not specified.
   *
   * @param out PrintWriter
   * @throws IOException
   */
  protected void writeHeader(PrintWriter out) throws IOException {

    // validate
    if (out != null) {

      // print opening tags
      out.println("<?xml version=\"1.0\"?>");

      // print the opening RDF tag (including namespaces)
      this.writeRDFHeader(out);
    } else {

      throw new IllegalArgumentException("Cannot write to null Writer.");
    }
  }