Class XMPSerializerRDF

java.lang.Object
com.itextpdf.kernel.xmp.impl.XMPSerializerRDF

public class XMPSerializerRDF extends Object
Serializes the XMPMeta-object using the standard RDF serialization format. The output is written to an OutputStream according to the SerializeOptions.
Since:
11.07.2006
  • Field Details

  • Constructor Details

    • XMPSerializerRDF

      public XMPSerializerRDF()
  • Method Details

    • serialize

      public void serialize(XMPMeta xmp, OutputStream out, SerializeOptions options) throws XMPException
      The actual serialization.
      Parameters:
      xmp - the metadata object to be serialized
      out - outputStream the output stream to serialize to
      options - the serialization options
      Throws:
      XMPException - If case of wrong options or any other serialization error.
    • addPadding

      private void addPadding(int tailLength) throws XMPException, IOException
      Calculates the padding according to the options and write it to the stream.
      Parameters:
      tailLength - the length of the tail string
      Throws:
      XMPException - thrown if packet size is to small to fit the padding
      IOException - forwards writer errors
    • checkOptionsConsistence

      protected void checkOptionsConsistence() throws XMPException
      Checks if the supplied options are consistent.
      Throws:
      XMPException - Thrown if options are conflicting
    • serializeAsRDF

      private String serializeAsRDF() throws IOException, XMPException
      Writes the (optional) packet header and the outer rdf-tags.
      Returns:
      Returns the packet end processing instraction to be written after the padding.
      Throws:
      IOException - Forwarded writer exceptions.
      XMPException
    • serializeCanonicalRDFSchemas

      private void serializeCanonicalRDFSchemas(int level) throws IOException, XMPException
      Serializes the metadata in pretty-printed manner.
      Parameters:
      level - indent level
      Throws:
      IOException - Forwarded writer exceptions
      XMPException
    • writeTreeName

      private void writeTreeName() throws IOException
      Throws:
      IOException
    • serializeCompactRDFSchemas

      private void serializeCompactRDFSchemas(int level) throws IOException, XMPException
      Serializes the metadata in compact manner.
      Parameters:
      level - indent level to start with
      Throws:
      IOException - Forwarded writer exceptions
      XMPException
    • serializeCompactRDFAttrProps

      private boolean serializeCompactRDFAttrProps(XMPNode parentNode, int indent) throws IOException
      Write each of the parent's simple unqualified properties as an attribute. Returns true if all of the properties are written as attributes.
      Parameters:
      parentNode - the parent property node
      indent - the current indent level
      Returns:
      Returns true if all properties can be rendered as RDF attribute.
      Throws:
      IOException
    • serializeCompactRDFElementProps

      private void serializeCompactRDFElementProps(XMPNode parentNode, int indent) throws IOException, XMPException
      Recursively handles the "value" for a node that must be written as an RDF property element. It does not matter if it is a top level property, a field of a struct, or an item of an array. The indent is that for the property element. The patterns bwlow ignore attribute qualifiers such as xml:lang, they don't affect the output form.
              <ns:UnqualifiedStructProperty-1
                      ... The fields as attributes, if all are simple and unqualified
              />
      
              <ns:UnqualifiedStructProperty-2 rdf:parseType="Resource">
                      ... The fields as elements, if none are simple and unqualified
              </ns:UnqualifiedStructProperty-2>
      
              <ns:UnqualifiedStructProperty-3>
                      <rdf:Description
                              ... The simple and unqualified fields as attributes
                      >
                              ... The compound or qualified fields as elements
                      </rdf:Description>
              </ns:UnqualifiedStructProperty-3>
      
              <ns:UnqualifiedArrayProperty>
                      <rdf:Bag> or Seq or Alt
                              ... Array items as rdf:li elements, same forms as top level properties
                      </rdf:Bag>
              </ns:UnqualifiedArrayProperty>
      
              <ns:QualifiedProperty rdf:parseType="Resource">
                      <rdf:value> ... Property "value"
                              following the unqualified forms ... </rdf:value>
                      ... Qualifiers looking like named struct fields
              </ns:QualifiedProperty>
       
      *** Consider numbered array items, but has compatibility problems. *** Consider qualified form with rdf:Description and attributes.
      Parameters:
      parentNode - the parent node
      indent - the current indent level
      Throws:
      IOException - Forwards writer exceptions
      XMPException - If qualifier and element fields are mixed.
    • serializeCompactRDFSimpleProp

      private boolean[] serializeCompactRDFSimpleProp(XMPNode node) throws IOException
      Serializes a simple property.
      Parameters:
      node - an XMPNode
      Returns:
      Returns an array containing the flags emitEndTag and indentEndTag.
      Throws:
      IOException - Forwards the writer exceptions.
    • serializeCompactRDFArrayProp

      private void serializeCompactRDFArrayProp(XMPNode node, int indent) throws IOException, XMPException
      Serializes an array property.
      Parameters:
      node - an XMPNode
      indent - the current indent level
      Throws:
      IOException - Forwards the writer exceptions.
      XMPException - If qualifier and element fields are mixed.
    • serializeCompactRDFStructProp

      private boolean serializeCompactRDFStructProp(XMPNode node, int indent, boolean hasRDFResourceQual) throws XMPException, IOException
      Serializes a struct property.
      Parameters:
      node - an XMPNode
      indent - the current indent level
      hasRDFResourceQual - Flag if the element has resource qualifier
      Returns:
      Returns true if an end flag shall be emitted.
      Throws:
      IOException - Forwards the writer exceptions.
      XMPException - If qualifier and element fields are mixed.
    • serializeCompactRDFGeneralQualifier

      private void serializeCompactRDFGeneralQualifier(int indent, XMPNode node) throws IOException, XMPException
      Serializes the general qualifier.
      Parameters:
      indent - the current indent level
      node - the root node of the subtree
      Throws:
      IOException - Forwards all writer exceptions.
      XMPException - If qualifier and element fields are mixed.
    • serializeCanonicalRDFSchema

      private void serializeCanonicalRDFSchema(XMPNode schemaNode, int level) throws IOException, XMPException
      Serializes one schema with all contained properties in pretty-printed manner.
      Each schema's properties are written to a single rdf:Description element. All of the necessary namespaces are declared in the rdf:Description element. The baseIndent is the base level for the entire serialization, that of the x:xmpmeta element. An xml:lang qualifier is written as an attribute of the property start tag, not by itself forcing the qualified property form.
               <rdf:Description rdf:about="TreeName" xmlns:ns="URI" ... >
      
                      ... The actual properties of the schema, see SerializePrettyRDFProperty
      
                      <!-- ns1:Alias is aliased to ns2:Actual -->  ... If alias comments are wanted
      
               </rdf:Description>
       
      Parameters:
      schemaNode - a schema node
      level -
      Throws:
      IOException - Forwarded writer exceptions
      XMPException
    • declareUsedNamespaces

      private void declareUsedNamespaces(XMPNode node, Set<String> usedPrefixes, int indent) throws IOException
      Writes all used namespaces of the subtree in node to the output. The subtree is recursivly traversed.
      Parameters:
      node - the root node of the subtree
      usedPrefixes - a set containing currently used prefixes
      indent - the current indent level
      Throws:
      IOException - Forwards all writer exceptions.
    • declareNamespace

      private void declareNamespace(String prefix, String namespace, Set<String> usedPrefixes, int indent) throws IOException
      Writes one namespace declaration to the output.
      Parameters:
      prefix - a namespace prefix (without colon) or a complete qname (when namespace == null)
      namespace - the a namespace
      usedPrefixes - a set containing currently used prefixes
      indent - the current indent level
      Throws:
      IOException - Forwards all writer exceptions.
    • startOuterRDFDescription

      private void startOuterRDFDescription(XMPNode schemaNode, int level) throws IOException
      Start the outer rdf:Description element, including all needed xmlns attributes. Leave the element open so that the compact form can add property attributes.
      Throws:
      IOException - If the writing to
    • endOuterRDFDescription

      private void endOuterRDFDescription(int level) throws IOException
      Write the invalid input: '<'/rdf:Description> end tag.
      Throws:
      IOException
    • serializeCanonicalRDFProperty

      private void serializeCanonicalRDFProperty(XMPNode node, boolean useCanonicalRDF, boolean emitAsRDFValue, int indent) throws IOException, XMPException
      Recursively handles the "value" for a node. It does not matter if it is a top level property, a field of a struct, or an item of an array. The indent is that for the property element. An xml:lang qualifier is written as an attribute of the property start tag, not by itself forcing the qualified property form. The patterns below mostly ignore attribute qualifiers like xml:lang. Except for the one struct case, attribute qualifiers don't affect the output form.
              <ns:UnqualifiedSimpleProperty>value</ns:UnqualifiedSimpleProperty>
      
              <ns:UnqualifiedStructProperty> (If no rdf:resource qualifier)
                      <rdf:Description>
                              ... Fields, same forms as top level properties
                      </rdf:Description>
              </ns:UnqualifiedStructProperty>
      
              <ns:ResourceStructProperty rdf:resource="URI"
                      ... Fields as attributes
              >
      
              <ns:UnqualifiedArrayProperty>
                      <rdf:Bag> or Seq or Alt
                              ... Array items as rdf:li elements, same forms as top level properties
                      </rdf:Bag>
              </ns:UnqualifiedArrayProperty>
      
              <ns:QualifiedProperty>
                      <rdf:Description>
                              <rdf:value> ... Property "value" following the unqualified
                                      forms ... </rdf:value>
                              ... Qualifiers looking like named struct fields
                      </rdf:Description>
              </ns:QualifiedProperty>
       
      Parameters:
      node - the property node
      useCanonicalRDF - use canonical form with inner description tag or the compact form with rdf:ParseType="resource" attribute.
      emitAsRDFValue - property shall be rendered as attribute rather than tag
      indent - the current indent level
      Throws:
      IOException - Forwards all writer exceptions.
      XMPException - If "rdf:resource" and general qualifiers are mixed.
    • emitRDFArrayTag

      private void emitRDFArrayTag(XMPNode arrayNode, boolean isStartTag, int indent) throws IOException
      Writes the array start and end tags.
      Parameters:
      arrayNode - an array node
      isStartTag - flag if its the start or end tag
      indent - the current indent level
      Throws:
      IOException - forwards writer exceptions
    • appendNodeValue

      private void appendNodeValue(String value, boolean forAttribute) throws IOException
      Serializes the node value in XML encoding. Its used for tag bodies and attributes. Note: The attribute is always limited by quotes, thats why &apos; is never serialized. Note: Control chars are written unescaped, but if the user uses others than tab, LF and CR the resulting XML will become invalid.
      Parameters:
      value - the value of the node
      forAttribute - flag if value is an attribute value
      Throws:
      IOException
    • canBeRDFAttrProp

      private boolean canBeRDFAttrProp(XMPNode node)
      A node can be serialized as RDF-Attribute, if it meets the following conditions:
      • is not array item
      • don't has qualifier
      • is no URI
      • is no composite property
      Parameters:
      node - an XMPNode
      Returns:
      Returns true if the node serialized as RDF-Attribute
    • writeIndent

      private void writeIndent(int times) throws IOException
      Writes indents and automatically includes the baseindend from the options.
      Parameters:
      times - number of indents to write
      Throws:
      IOException - forwards exception
    • write

      private void write(int c) throws IOException
      Writes a char to the output.
      Parameters:
      c - a char
      Throws:
      IOException - forwards writer exceptions
    • write

      private void write(String str) throws IOException
      Writes a String to the output.
      Parameters:
      str - a String
      Throws:
      IOException - forwards writer exceptions
    • writeChars

      private void writeChars(int number, char c) throws IOException
      Writes an amount of chars, mostly spaces
      Parameters:
      number - number of chars
      c - a char
      Throws:
      IOException
    • writeNewline

      private void writeNewline() throws IOException
      Writes a newline according to the options.
      Throws:
      IOException - Forwards exception