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 opening RDF tag (with namespaces) to the print Writer.
   *
   * @param out PrintWriter
   * @throws IOException
   */
  protected void writeRDFHeader(PrintWriter out) throws IOException {

    // validate
    if (out != null) {

      // print opening RDF tag (including namespaces)
      out.print("<rdf:RDF ");

      // print namespaces
      Set<String> keys = namespaces.keySet();

      if (keys != null) {

        for (String currentKey : keys) {
          String currentValue = namespaces.get(currentKey);

          if ((currentKey != null) && (currentValue != null)) {

            // use entities: xmlns:ns="&ns;"
            out.print(NEWLINE + "  xmlns:" + currentKey + "=\"&" + currentKey + ";\"");
          }
        }
      }

      // close the opening tag (add a space for readability)
      out.print(">" + NEWLINE + NEWLINE);
    } else {

      throw new IllegalArgumentException("Cannot write to null Writer.");
    }
  }
Ejemplo n.º 3
0
  /**
   * Writes the XML Entities (used for namespaces) to the print Writer.
   *
   * @param out PrintWriter
   * @throws IOException
   */
  protected void writeXMLEntities(PrintWriter out) throws IOException {

    // validate
    if (out != null) {

      // print opening DOCTYPE DECLARATION tag
      out.print(NEWLINE + "<!DOCTYPE rdf:RDF [");

      // print namespaces
      Set<String> keys = namespaces.keySet();

      if (keys != null) {

        for (String currentKey : keys) {
          String currentValue = namespaces.get(currentKey);

          if ((currentKey != null) && (currentValue != null)) {

            // write as: <!ENTITY ns 'http://example.org/abc#'>
            out.print(NEWLINE + "  <!ENTITY " + currentKey + " '" + currentValue + "'>");
          }
        }
      }

      // close the opening tag (add a space for readability)
      out.print("]>" + NEWLINE + NEWLINE);
    } else {

      throw new IllegalArgumentException("Cannot write to null Writer.");
    }
  }
Ejemplo n.º 4
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.º 5
0
  /**
   * Writes the opening tag for the subject
   *
   * @param graph JRDFGraph
   * @param subject SubjectNode
   * @param writer PrintWriter
   * @throws GraphException
   */
  protected void writeClosingTag(Graph graph, BlankNode subject, PrintWriter writer)
      throws GraphException {

    ObjectNode subjectType = this.getSubjectType(graph, subject);

    // only display as a subject if it can be found
    if (subjectType != null) {

      // closing tag
      writer.print("  </" + this.getURI(subjectType) + ">" + NEWLINE);
    } else {

      // closing tag
      writer.print("  </" + RDF_PREFIX + ":Description>" + NEWLINE);
    }
  }
Ejemplo n.º 6
0
  /**
   * Writes the opening tag for the subject
   *
   * @param graph JRDFGraph
   * @param subject SubjectNode
   * @param writer PrintWriter
   * @throws GraphException
   */
  protected void writeOpeningTag(Graph graph, SubjectNode subject, PrintWriter writer)
      throws GraphException {

    if (writer != null) {

      // write the subject type and rdf:about
      if (subject != null) {

        // open tag - BlankNodes are a special case
        if (subject instanceof BlankNode) {

          this.writeOpeningTag(graph, (BlankNode) subject, writer);
        } else {

          writer.print(
              "  <"
                  + RDF_PREFIX
                  + ":Description "
                  + RDF_PREFIX
                  + ":about=\""
                  + this.getNodeString(subject)
                  + "\">"
                  + NEWLINE);
        }
      } else {
        throw new IllegalArgumentException(
            "Could not write opening tag for subject. Subject Node is null.");
      }
    }
  }
Ejemplo n.º 7
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.º 8
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.º 9
0
  /**
   * Writes the opening tag for the subject
   *
   * @param graph JRDFGraph
   * @param subject SubjectNode
   * @param writer PrintWriter
   * @throws GraphException
   */
  protected void writeClosingTag(Graph graph, SubjectNode subject, PrintWriter writer)
      throws GraphException {

    // Blank Nodes are written differently
    if (subject instanceof BlankNode) {

      this.writeClosingTag(graph, (BlankNode) subject, writer);
    } else {

      // closing tag
      writer.print("  </" + RDF_PREFIX + ":Description>" + NEWLINE);
    }
  }
Ejemplo n.º 10
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.º 11
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.");
    }
  }