Beispiel #1
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.");
    }
  }
Beispiel #2
0
  /**
   * If the URI contains the URI of a known namespace, it is replaced.
   *
   * @param original original URI.
   * @return new URI with namespace references.
   */
  protected String replaceNamespace(String original) throws GraphException {

    // value to be returned
    String uri = original;

    // validate URI (only replace uri's with fragments)
    if (original != null) {

      // replace any URI occurances with namespace prefixes
      for (String currentKey : namespaces.keySet()) {

        String currentValue = namespaces.get(currentKey);

        // validate the Objects
        if ((currentKey != null) && (currentValue != null)) {

          // if the entire namespace is used, replace it with an entity
          if (original.equals(currentValue)) {
            uri = "&" + currentKey + ";";
          } else if (original.startsWith(currentValue.toString())) {
            // replace with namespace
            uri = original.replaceAll(currentValue.toString(), currentKey + ":");
          }
        }
      }
    }

    // return the URI with any collection/container items renamed
    return replaceCollection(uri);
  }
Beispiel #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.");
    }
  }