Class RDFXMLPrettyWriter

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable, CharSink, Sink, RDFHandler, RDFWriter

    public class RDFXMLPrettyWriter
    extends RDFXMLWriter
    implements java.io.Closeable, java.io.Flushable
    An extension of RDFXMLWriter that outputs a more concise form of RDF/XML. The resulting output is semantically equivalent to the output of an RDFXMLWriter (it produces the same set of statements), but it is usually easier to read for humans.

    This is a quasi-streaming RDFWriter. Statements are cached as long as the striped syntax is followed (i.e. the subject of the next statement is the object of the previous statement) and written to the output when the stripe is broken.

    The abbreviations used are typed node elements, empty property elements and striped syntax. Note that these abbreviations require that statements are written in the appropriate order.

    Striped syntax means that when the object of a statement is the subject of the next statement we can nest the descriptions in each other.

    Example:

     <rdf:Seq>
        <rdf:li>
           <foaf:Person>
              <foaf:knows>
                 <foaf:Person>
                   <foaf:mbox rdf:resource="..."/>
                 </foaf:Person>
              </foaf:knows>
           </foaf:Person>
        </rdf:li>
     </rdf:Seq>
     

    Typed node elements means that we write out type information in the short form of

     <foaf:Person rdf:about="...">
         ...
      </foaf:Person>
     

    instead of

     <rdf:Description rdf:about="...">
        <rdf:type rdf:resource="http://xmlns.com/foaf/0.1/Person"/>
         ...
      </rdf:Description>
     

    Empty property elements are of the form

     <foaf:Person>
        <foaf:homepage rdf:resource="http://www.cs.vu.nl/˜marta"/>
     </foaf:Person>
     

    instead of

     <foaf:Person>
        <foaf:homepage>
           <rdf:Description rdf:about="http://www.cs.vu.nl/˜marta"/>
        <foaf:homepage>
     </foaf:Person>
     
    • Field Detail

      • nodeStack

        private final java.util.Stack<RDFXMLPrettyWriter.Node> nodeStack
        Stack for remembering the nodes (subjects/objects) of statements at each level.
      • predicateStack

        private final java.util.Stack<IRI> predicateStack
        Stack for remembering the predicate of statements at each level.
      • writingEnded

        private boolean writingEnded
    • Constructor Detail

      • RDFXMLPrettyWriter

        public RDFXMLPrettyWriter​(java.io.OutputStream out)
        Creates a new RDFXMLPrintWriter that will write to the supplied OutputStream.
        Parameters:
        out - The OutputStream to write the RDF/XML document to.
      • RDFXMLPrettyWriter

        public RDFXMLPrettyWriter​(java.io.OutputStream out,
                                  ParsedIRI baseIRI)
        Creates a new RDFXMLPrintWriter that will write to the supplied OutputStream.
        Parameters:
        out - The OutputStream to write the RDF/XML document to.
      • RDFXMLPrettyWriter

        public RDFXMLPrettyWriter​(java.io.Writer out)
        Creates a new RDFXMLPrintWriter that will write to the supplied Writer.
        Parameters:
        out - The Writer to write the RDF/XML document to.
      • RDFXMLPrettyWriter

        public RDFXMLPrettyWriter​(java.io.Writer writer,
                                  ParsedIRI baseIRI)
        Creates a new RDFXMLPrintWriter that will write to the supplied Writer.
        Parameters:
        writer - the Writer to write the RDF/XML document to
        baseIRI - base IRI
    • Method Detail

      • writeHeader

        protected void writeHeader()
                            throws java.io.IOException
        Overrides:
        writeHeader in class RDFXMLWriter
        Throws:
        java.io.IOException
      • flush

        public void flush()
                   throws java.io.IOException
        Specified by:
        flush in interface java.io.Flushable
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • popStacks

        private void popStacks​(Resource newSubject)
                        throws java.io.IOException,
                               RDFHandlerException
        Write out the stacks until we find subject. If subject == null, write out the entire stack
        Parameters:
        newSubject -
        Throws:
        java.io.IOException
        RDFHandlerException
      • writeNodeStartOfStartTag

        private void writeNodeStartOfStartTag​(RDFXMLPrettyWriter.Node node)
                                       throws java.io.IOException,
                                              RDFHandlerException
        Write out the opening tag of the subject or object of a statement up to (but not including) the end of the tag. Used both in writeStartSubject and writeEmptySubject.
        Throws:
        java.io.IOException
        RDFHandlerException
      • writeNodeEndTag

        private void writeNodeEndTag​(RDFXMLPrettyWriter.Node node)
                              throws java.io.IOException
        Write out the closing tag for the subject or object of a statement.
        Throws:
        java.io.IOException
      • writeStartTag

        protected void writeStartTag​(java.lang.String namespace,
                                     java.lang.String localName)
                              throws java.io.IOException
        Throws:
        java.io.IOException
      • writeIndents

        protected void writeIndents​(int n)
                             throws java.io.IOException
        Writes n indents.
        Throws:
        java.io.IOException