Package com.itextpdf.xmp.impl
Class XMPSerializerRDF
java.lang.Object
com.itextpdf.xmp.impl.XMPSerializerRDF
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 Summary
FieldsModifier and TypeFieldDescriptionprivate static final int
default paddingprivate SerializeOptions
the stored serialization optionsprivate CountOutputStream
the output stream to serialize toprivate static final String
private static final String
The w/r is missing inbetweenprivate static final String
private int
the padding in the XMP Packet, or the length of the complete packet in case of option exactPacketLength.(package private) static final Set
a set of all rdf attribute qualifierprivate static final String
private static final String
private static final String
private static final String
private static final String
private static final String
private static final String
private static final String
private static final String
private int
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 OutputStreamWriter
this writer is used to do the actual serializationprivate XMPMetaImpl
the metadata object to be serialized. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate void
addPadding
(int tailLength) Calculates the padding according to the options and write it to the stream.private void
appendNodeValue
(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
Checks if the supplied options are consistent.private void
declareNamespace
(String prefix, String namespace, Set usedPrefixes, int indent) Writes one namespace declaration to the output.private void
declareUsedNamespaces
(XMPNode node, 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 invalid input: '<'/rdf:Description> end tag.void
serialize
(XMPMeta xmp, OutputStream out, SerializeOptions options) The actual serialization.private String
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 Object[]
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
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
Writes a newline according to the options.private void
-
Field Details
-
DEFAULT_PAD
private static final int DEFAULT_PADdefault padding- See Also:
-
PACKET_HEADER
- See Also:
-
PACKET_TRAILER
The w/r is missing inbetween- See Also:
-
PACKET_TRAILER2
- See Also:
-
RDF_XMPMETA_START
- See Also:
-
RDF_XMPMETA_END
- See Also:
-
RDF_RDF_START
- See Also:
-
RDF_RDF_END
- See Also:
-
RDF_SCHEMA_START
- See Also:
-
RDF_SCHEMA_END
- See Also:
-
RDF_STRUCT_START
- See Also:
-
RDF_STRUCT_END
- See Also:
-
RDF_EMPTY_STRUCT
- See Also:
-
RDF_ATTR_QUALIFIER
a set of all rdf attribute qualifier -
xmp
the metadata object to be serialized. -
outputStream
the output stream to serialize to -
writer
this writer is used to do the actual serialization -
options
the stored serialization options -
unicodeSize
private int unicodeSizethe 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 paddingthe padding in the XMP Packet, or the length of the complete packet in case of option exactPacketLength.
-
-
Constructor Details
-
XMPSerializerRDF
public XMPSerializerRDF()
-
-
Method Details
-
serialize
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
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 paddingIOException
- forwards writer errors
-
checkOptionsConsistence
Checks if the supplied options are consistent.- Throws:
XMPException
- Thrown if options are conflicting
-
serializeAsRDF
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
Serializes the metadata in pretty-printed manner.- Parameters:
level
- indent level- Throws:
IOException
- Forwarded writer exceptionsXMPException
-
writeTreeName
- Throws:
IOException
-
serializeCompactRDFSchemas
Serializes the metadata in compact manner.- Parameters:
level
- indent level to start with- Throws:
IOException
- Forwarded writer exceptionsXMPException
-
serializeCompactRDFAttrProps
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:
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>
- Parameters:
parentNode
- the parent nodeindent
- the current indent level- Throws:
IOException
- Forwards writer exceptionsXMPException
- If qualifier and element fields are mixed.
-
serializeCompactRDFSimpleProp
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 XMPNodeindent
- 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 XMPNodeindent
- the current indent levelhasRDFResourceQual
- 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 levelnode
- 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 nodelevel
-- Throws:
IOException
- Forwarded writer exceptionsXMPException
-
declareUsedNamespaces
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:
IOException
- Forwards all writer exceptions.
-
declareNamespace
private void declareNamespace(String prefix, String namespace, Set 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 namespaceusedPrefixes
- a set containing currently used prefixesindent
- the current indent level- Throws:
IOException
- Forwards all writer exceptions.
-
startOuterRDFDescription
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
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 nodeuseCanonicalRDF
- 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 tagindent
- the current indent level- Throws:
IOException
- Forwards all writer exceptions.XMPException
- If "rdf:resource" and general qualifiers are mixed.
-
emitRDFArrayTag
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:
IOException
- forwards writer exceptions
-
appendNodeValue
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:
IOException
-
canBeRDFAttrProp
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
Writes indents and automatically includes the baseindend from the options.- Parameters:
times
- number of indents to write- Throws:
IOException
- forwards exception
-
write
Writes a char to the output.- Parameters:
c
- a char- Throws:
IOException
- forwards writer exceptions
-
write
Writes a String to the output.- Parameters:
str
- a String- Throws:
IOException
- forwards writer exceptions
-
writeChars
Writes an amount of chars, mostly spaces- Parameters:
number
- number of charsc
- a char- Throws:
IOException
-
writeNewline
Writes a newline according to the options.- Throws:
IOException
- Forwards exception
-