Package com.itextpdf.xmp.impl
Class XMPSerializerRDF
- java.lang.Object
-
- com.itextpdf.xmp.impl.XMPSerializerRDF
-
public class XMPSerializerRDF extends java.lang.Object
Serializes theXMPMeta
-object using the standard RDF serialization format. The output is written to anOutputStream
according to theSerializeOptions
.- Since:
- 11.07.2006
-
-
Field Summary
Fields Modifier and Type Field Description private static int
DEFAULT_PAD
default paddingprivate SerializeOptions
options
the stored serialization optionsprivate CountOutputStream
outputStream
the output stream to serialize toprivate static java.lang.String
PACKET_HEADER
private static java.lang.String
PACKET_TRAILER
The w/r is missing inbetweenprivate static java.lang.String
PACKET_TRAILER2
private int
padding
the padding in the XMP Packet, or the length of the complete packet in case of option exactPacketLength.(package private) static java.util.Set
RDF_ATTR_QUALIFIER
a set of all rdf attribute qualifierprivate static java.lang.String
RDF_EMPTY_STRUCT
private static java.lang.String
RDF_RDF_END
private static java.lang.String
RDF_RDF_START
private static java.lang.String
RDF_SCHEMA_END
private static java.lang.String
RDF_SCHEMA_START
private static java.lang.String
RDF_STRUCT_END
private static java.lang.String
RDF_STRUCT_START
private static java.lang.String
RDF_XMPMETA_END
private static java.lang.String
RDF_XMPMETA_START
private int
unicodeSize
the size of one unicode char, for UTF-8 set to 1 (Note: only valid for ASCII chars lower than 0x80), set to 2 in case of UTF-16private java.io.OutputStreamWriter
writer
this writer is used to do the actual serializationprivate XMPMetaImpl
xmp
the metadata object to be serialized.
-
Constructor Summary
Constructors Constructor Description XMPSerializerRDF()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
addPadding(int tailLength)
Calculates the padding according to the options and write it to the stream.private void
appendNodeValue(java.lang.String value, boolean forAttribute)
Serializes the node value in XML encoding.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 propertyprotected void
checkOptionsConsistence()
Checks if the supplied options are consistent.private void
declareNamespace(java.lang.String prefix, java.lang.String namespace, java.util.Set usedPrefixes, int indent)
Writes one namespace declaration to the output.private void
declareUsedNamespaces(XMPNode node, java.util.Set usedPrefixes, int indent)
Writes all used namespaces of the subtree in node to the output.private void
emitRDFArrayTag(XMPNode arrayNode, boolean isStartTag, int indent)
Writes the array start and end tags.private void
endOuterRDFDescription(int level)
Write the end tag.void
serialize(XMPMeta xmp, java.io.OutputStream out, SerializeOptions options)
The actual serialization.private java.lang.String
serializeAsRDF()
Writes the (optional) packet header and the outer rdf-tags.private void
serializeCanonicalRDFProperty(XMPNode node, boolean useCanonicalRDF, boolean emitAsRDFValue, int indent)
Recursively handles the "value" for a node.private void
serializeCanonicalRDFSchema(XMPNode schemaNode, int level)
Serializes one schema with all contained properties in pretty-printed manner.
Each schema's properties are written to a single rdf:Description element.private void
serializeCanonicalRDFSchemas(int level)
Serializes the metadata in pretty-printed manner.private void
serializeCompactRDFArrayProp(XMPNode node, int indent)
Serializes an array property.private boolean
serializeCompactRDFAttrProps(XMPNode parentNode, int indent)
Write each of the parent's simple unqualified properties as an attribute.private void
serializeCompactRDFElementProps(XMPNode parentNode, int indent)
Recursively handles the "value" for a node that must be written as an RDF property element.private void
serializeCompactRDFGeneralQualifier(int indent, XMPNode node)
Serializes the general qualifier.private void
serializeCompactRDFSchemas(int level)
Serializes the metadata in compact manner.private java.lang.Object[]
serializeCompactRDFSimpleProp(XMPNode node)
Serializes a simple property.private boolean
serializeCompactRDFStructProp(XMPNode node, int indent, boolean hasRDFResourceQual)
Serializes a struct property.private void
startOuterRDFDescription(XMPNode schemaNode, int level)
Start the outer rdf:Description element, including all needed xmlns attributes.private void
write(int c)
Writes a char to the output.private void
write(java.lang.String str)
Writes a String to the output.private void
writeChars(int number, char c)
Writes an amount of chars, mostly spacesprivate void
writeIndent(int times)
Writes indents and automatically includes the baseindend from the options.private void
writeNewline()
Writes a newline according to the options.private void
writeTreeName()
-
-
-
Field Detail
-
DEFAULT_PAD
private static final int DEFAULT_PAD
default padding- See Also:
- Constant Field Values
-
PACKET_HEADER
private static final java.lang.String PACKET_HEADER
- See Also:
- Constant Field Values
-
PACKET_TRAILER
private static final java.lang.String PACKET_TRAILER
The w/r is missing inbetween- See Also:
- Constant Field Values
-
PACKET_TRAILER2
private static final java.lang.String PACKET_TRAILER2
- See Also:
- Constant Field Values
-
RDF_XMPMETA_START
private static final java.lang.String RDF_XMPMETA_START
- See Also:
- Constant Field Values
-
RDF_XMPMETA_END
private static final java.lang.String RDF_XMPMETA_END
- See Also:
- Constant Field Values
-
RDF_RDF_START
private static final java.lang.String RDF_RDF_START
- See Also:
- Constant Field Values
-
RDF_RDF_END
private static final java.lang.String RDF_RDF_END
- See Also:
- Constant Field Values
-
RDF_SCHEMA_START
private static final java.lang.String RDF_SCHEMA_START
- See Also:
- Constant Field Values
-
RDF_SCHEMA_END
private static final java.lang.String RDF_SCHEMA_END
- See Also:
- Constant Field Values
-
RDF_STRUCT_START
private static final java.lang.String RDF_STRUCT_START
- See Also:
- Constant Field Values
-
RDF_STRUCT_END
private static final java.lang.String RDF_STRUCT_END
- See Also:
- Constant Field Values
-
RDF_EMPTY_STRUCT
private static final java.lang.String RDF_EMPTY_STRUCT
- See Also:
- Constant Field Values
-
RDF_ATTR_QUALIFIER
static final java.util.Set RDF_ATTR_QUALIFIER
a set of all rdf attribute qualifier
-
xmp
private XMPMetaImpl xmp
the metadata object to be serialized.
-
outputStream
private CountOutputStream outputStream
the output stream to serialize to
-
writer
private java.io.OutputStreamWriter writer
this writer is used to do the actual serialization
-
options
private SerializeOptions options
the stored serialization options
-
unicodeSize
private int unicodeSize
the size of one unicode char, for UTF-8 set to 1 (Note: only valid for ASCII chars lower than 0x80), set to 2 in case of UTF-16
-
padding
private int padding
the padding in the XMP Packet, or the length of the complete packet in case of option exactPacketLength.
-
-
Method Detail
-
serialize
public void serialize(XMPMeta xmp, java.io.OutputStream out, SerializeOptions options) throws XMPException
The actual serialization.- Parameters:
xmp
- the metadata object to be serializedout
- outputStream the output stream to serialize tooptions
- the serialization options- Throws:
XMPException
- If case of wrong options or any other serialization error.
-
addPadding
private void addPadding(int tailLength) throws XMPException, java.io.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 paddingjava.io.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 java.lang.String serializeAsRDF() throws java.io.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:
java.io.IOException
- Forwarded writer exceptions.XMPException
-
serializeCanonicalRDFSchemas
private void serializeCanonicalRDFSchemas(int level) throws java.io.IOException, XMPException
Serializes the metadata in pretty-printed manner.- Parameters:
level
- indent level- Throws:
java.io.IOException
- Forwarded writer exceptionsXMPException
-
writeTreeName
private void writeTreeName() throws java.io.IOException
- Throws:
java.io.IOException
-
serializeCompactRDFSchemas
private void serializeCompactRDFSchemas(int level) throws java.io.IOException, XMPException
Serializes the metadata in compact manner.- Parameters:
level
- indent level to start with- Throws:
java.io.IOException
- Forwarded writer exceptionsXMPException
-
serializeCompactRDFAttrProps
private boolean serializeCompactRDFAttrProps(XMPNode parentNode, int indent) throws java.io.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 nodeindent
- the current indent level- Returns:
- Returns true if all properties can be rendered as RDF attribute.
- Throws:
java.io.IOException
-
serializeCompactRDFElementProps
private void serializeCompactRDFElementProps(XMPNode parentNode, int indent) throws java.io.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>
- Parameters:
parentNode
- the parent nodeindent
- the current indent level- Throws:
java.io.IOException
- Forwards writer exceptionsXMPException
- If qualifier and element fields are mixed.
-
serializeCompactRDFSimpleProp
private java.lang.Object[] serializeCompactRDFSimpleProp(XMPNode node) throws java.io.IOException
Serializes a simple property.- Parameters:
node
- an XMPNode- Returns:
- Returns an array containing the flags emitEndTag and indentEndTag.
- Throws:
java.io.IOException
- Forwards the writer exceptions.
-
serializeCompactRDFArrayProp
private void serializeCompactRDFArrayProp(XMPNode node, int indent) throws java.io.IOException, XMPException
Serializes an array property.- Parameters:
node
- an XMPNodeindent
- the current indent level- Throws:
java.io.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, java.io.IOException
Serializes a struct property.- Parameters:
node
- an XMPNodeindent
- the current indent levelhasRDFResourceQual
- Flag if the element has resource qualifier- Returns:
- Returns true if an end flag shall be emitted.
- Throws:
java.io.IOException
- Forwards the writer exceptions.XMPException
- If qualifier and element fields are mixed.
-
serializeCompactRDFGeneralQualifier
private void serializeCompactRDFGeneralQualifier(int indent, XMPNode node) throws java.io.IOException, XMPException
Serializes the general qualifier.- Parameters:
node
- the root node of the subtreeindent
- the current indent level- Throws:
java.io.IOException
- Forwards all writer exceptions.XMPException
- If qualifier and element fields are mixed.
-
serializeCanonicalRDFSchema
private void serializeCanonicalRDFSchema(XMPNode schemaNode, int level) throws java.io.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 nodelevel
-- Throws:
java.io.IOException
- Forwarded writer exceptionsXMPException
-
declareUsedNamespaces
private void declareUsedNamespaces(XMPNode node, java.util.Set usedPrefixes, int indent) throws java.io.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 subtreeusedPrefixes
- a set containing currently used prefixesindent
- the current indent level- Throws:
java.io.IOException
- Forwards all writer exceptions.
-
declareNamespace
private void declareNamespace(java.lang.String prefix, java.lang.String namespace, java.util.Set usedPrefixes, int indent) throws java.io.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 namespaceusedPrefixes
- a set containing currently used prefixesindent
- the current indent level- Throws:
java.io.IOException
- Forwards all writer exceptions.
-
startOuterRDFDescription
private void startOuterRDFDescription(XMPNode schemaNode, int level) throws java.io.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:
java.io.IOException
- If the writing to
-
endOuterRDFDescription
private void endOuterRDFDescription(int level) throws java.io.IOException
Write the end tag.- Throws:
java.io.IOException
-
serializeCanonicalRDFProperty
private void serializeCanonicalRDFProperty(XMPNode node, boolean useCanonicalRDF, boolean emitAsRDFValue, int indent) throws java.io.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 nodeemitAsRDFValue
- property shall be rendered as attribute rather than taguseCanonicalRDF
- use canonical form with inner description tag or the compact form with rdf:ParseType="resource" attribute.indent
- the current indent level- Throws:
java.io.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 java.io.IOException
Writes the array start and end tags.- Parameters:
arrayNode
- an array nodeisStartTag
- flag if its the start or end tagindent
- the current indent level- Throws:
java.io.IOException
- forwards writer exceptions
-
appendNodeValue
private void appendNodeValue(java.lang.String value, boolean forAttribute) throws java.io.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'
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 nodeforAttribute
- flag if value is an attribute value- Throws:
java.io.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 java.io.IOException
Writes indents and automatically includes the baseindend from the options.- Parameters:
times
- number of indents to write- Throws:
java.io.IOException
- forwards exception
-
write
private void write(int c) throws java.io.IOException
Writes a char to the output.- Parameters:
c
- a char- Throws:
java.io.IOException
- forwards writer exceptions
-
write
private void write(java.lang.String str) throws java.io.IOException
Writes a String to the output.- Parameters:
str
- a String- Throws:
java.io.IOException
- forwards writer exceptions
-
writeChars
private void writeChars(int number, char c) throws java.io.IOException
Writes an amount of chars, mostly spaces- Parameters:
number
- number of charsc
- a char- Throws:
java.io.IOException
-
writeNewline
private void writeNewline() throws java.io.IOException
Writes a newline according to the options.- Throws:
java.io.IOException
- Forwards exception
-
-