Package org.docx4j.openpackaging.parts
Class JaxbXmlPart<E>
java.lang.Object
org.docx4j.openpackaging.Base
org.docx4j.openpackaging.parts.Part
org.docx4j.openpackaging.parts.JaxbXmlPart<E>
- Type Parameters:
E- type of the content tree object
- Direct Known Subclasses:
ChartColorStylePart,ChartExSpacePart,ChartStylePart,CommentsExtendedPart,CommentsIdsPart,CustomXmlDataStoragePropertiesPart,DocPropsCorePart,DocPropsCustomPart,DocPropsExtendedPart,FontTablePart,InkmlPart,JaxbCustomXmlDataStoragePart,JaxbXmlPartXPathAware,KeyMapCustomizationsPart,PeoplePart,RelationshipsPart,TableStylesPart,TaskpanesPart,VbaDataPart,WebExtensionPart,WebSettingsPart
OPC Parts are either XML, or binary (or text) documents.
Most are XML documents.
docx4j aims to represent XML parts using JAXB.
Any XML Part for which we have a JAXB representation (eg the main
document part) should extend this Part.
This class provides only one of the methods for serializing (marshalling) the
Java content tree back into XML data found in
jakarta.xml.bind.Marshaller interface. You can always use
any of the others by getting the jaxbElement required by those
methods.
Insofar as unmarshalling is concerned, at present it doesn't
contain all the methods in jakarta.xml.bind.unmarshaller interface.
This is because the content always comes from the same place
(ie from a zip file or JCR via org.docx4j.io.*).
TODO - what is the best thing to unmarshall from?
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected EThe content tree (ie JAXB representation of the Part)protected jakarta.xml.bind.JAXBContextprotected static org.slf4j.Loggerprotected static longFields inherited from class org.docx4j.openpackaging.Base
contentType, partName, relationships -
Constructor Summary
ConstructorsConstructorDescriptionJaxbXmlPart(PartName partName) JaxbXmlPart(PartName partName, jakarta.xml.bind.JAXBContext jc) -
Method Summary
Modifier and TypeMethodDescriptionvoidaddMcChoiceNamespace(String mcChoiceNamespace) Specify a namespace prefix (used in mc:Choice/@Requires) which docx4j should declare on the top-level element of the part (otherwise Microsoft Office won't be able to open the file).protected jakarta.xml.bind.UnmarshallergetConfiguredUnmarshaller(boolean eventHandlerContinue) An Unmarshaller configured with our event handler and listener.Get the live contents of this part (the JAXB object model).jakarta.xml.bind.JAXBContextGet the live contents of this part.getXML()See your content as XML.getXMLStreamReader(StreamFilter filter) protected XMLStreamReaderbooleanisContentEqual(Part other) booleanvoidmarshal(OutputStream os) Marshal the content tree rooted at jaxbElement into an output stream, using org.docx4j.jaxb.NamespacePrefixMapper.voidmarshal(OutputStream os, Object namespacePrefixMapper) Marshal the content tree rooted at jaxbElement into an output streamvoidMarshal the content tree rooted at jaxbElement into a DOM tree.voidMarshal the content tree rooted at jaxbElement into a DOM tree.voidpipe(SAXHandler saxHandler) Replace the contents of this part with the output of passing it through your SAXHandler.voidpipe(StAXHandlerInterface handler) Replace the contents of this part with the output of passing it through your StAXHandler.voidpipe(StAXHandlerInterface handler, StreamFilter filter) Replace the contents of this part with the output of passing it through your StAXHandler.protected voidpreprocess(InputStream is, boolean eventHandlerContinue) Transform the content through mcPreprocessorXsltvoidreplacePartContent(byte[] bytes) Replace the content of this part.voidsetContents(E jaxbElement) Set the contents of this part.voidsetJAXBContext(jakarta.xml.bind.JAXBContext jc) voidsetJaxbElement(E jaxbElement) voidsetJaxbElement(jakarta.xml.bind.util.JAXBResult result) protected voidsetMceIgnorable(McIgnorableNamespaceDeclarator namespacePrefixMapper) Where the mc:Ignorable attribute is present, ensure its contents matches the ignorable namespaces actually present.voidUse an XSLT to alter the contents of this part.protected DocumentPrepare input by transforming via mcPreprocessorXslt, outputting a DOM node, which we can use docx4jUnmarshallerListener on.unmarshal(InputStream is) Unmarshal XML data from the specified InputStream and return the resulting content tree.voidvariableReplace(Map<String, String> mappings) unmarshallFromTemplate.Methods inherited from class org.docx4j.openpackaging.parts.Part
getContentLengthAsLoaded, getOwningRelationshipPart, getPackage, getRelationshipType, getSourceRelationship, getSourceRelationships, getVersion, remove, rename, setContentLengthAsLoaded, setOwningRelationshipPart, setPackage, setPartShortcut, setRelationshipType, setSourceRelationship, setVersionMethods inherited from class org.docx4j.openpackaging.Base
addTargetPart, addTargetPart, addTargetPart, addTargetPart, getContentType, getPartName, getRelationshipsPart, getRelationshipsPart, getUserData, reset, setContentType, setPartName, setRelationships, setUserData
-
Field Details
-
log
protected static org.slf4j.Logger log -
jc
protected jakarta.xml.bind.JAXBContext jc -
MAX_BYTES_Unmarshal_Error
protected static long MAX_BYTES_Unmarshal_Error -
jaxbElement
The content tree (ie JAXB representation of the Part)
-
-
Constructor Details
-
JaxbXmlPart
- Throws:
InvalidFormatException
-
JaxbXmlPart
public JaxbXmlPart(PartName partName, jakarta.xml.bind.JAXBContext jc) throws InvalidFormatException - Throws:
InvalidFormatException
-
-
Method Details
-
setJAXBContext
public void setJAXBContext(jakarta.xml.bind.JAXBContext jc) -
getJAXBContext
public jakarta.xml.bind.JAXBContext getJAXBContext()- Since:
- 2.7
-
getContents
Get the live contents of this part (the JAXB object model). (An alias/synonym for older getJaxbElement(), but now throws exception)- Returns:
- Throws:
Docx4JException- Since:
- 3.0
-
getJaxbElement
Get the live contents of this part. (getContents() is preferred, this is the older/less friendly method name)- Returns:
-
setJaxbElement
-
setContents
Set the contents of this part. (Just an alias/synonym for setJaxbElement())- Parameters:
jaxbElement-- Since:
- 3.0
-
setJaxbElement
public void setJaxbElement(jakarta.xml.bind.util.JAXBResult result) throws jakarta.xml.bind.JAXBException - Throws:
jakarta.xml.bind.JAXBException
-
getXML
See your content as XML. An easy way to invoke XmlUtils.marshaltoString- Returns:
- Since:
- 3.0.0
-
isUnmarshalled
public boolean isUnmarshalled() -
variableReplace
public void variableReplace(Map<String, String> mappings) throws jakarta.xml.bind.JAXBException, Docx4JExceptionunmarshallFromTemplate. Where jaxbElement has not been unmarshalled yet, this is more efficient (3 times faster, in some testing) than calling XmlUtils.marshaltoString directly, since it avoids some JAXB processing.- Parameters:
mappings-- Throws:
jakarta.xml.bind.JAXBExceptionDocx4JException- Since:
- 3.0.0
-
transform
public void transform(Templates xslt, Map<String, Object> transformParameters, Result result) throws Docx4JExceptionUse an XSLT to alter the contents of this part. You can transform to whatever you like (ie it doesn't have to be WordML content), which is why the API design is that you provide the Result object. If you do want to replace the content in this part, convert your result to an element or input stream, then invoke unmarshal on it, then setContents. (Unmarshal takes care of any unexpected content, sidestepping the issue of whether to do that before the transform (where reading the part directly), or after).- Parameters:
xslt-transformParameters-- Throws:
ExceptionDocx4JException- Since:
- 3.3.6
-
pipe
public void pipe(SAXHandler saxHandler) throws ParserConfigurationException, SAXException, Docx4JException, IOException, jakarta.xml.bind.JAXBException Replace the contents of this part with the output of passing it through your SAXHandler. This is offered as an alternative to the similar methods which use StAX. If you are unsure which to use, you should probably use the StAX approach. This is most efficient in the case where there has been no need for JAXB to unmarshal the contents. In this case, it is possible to process then save the contents without incurring JAXB overhead (you may see 1/4 heap usage).- Parameters:
saxHandler-- Throws:
ParserConfigurationExceptionSAXExceptionDocx4JExceptionIOExceptionjakarta.xml.bind.JAXBException
-
pipe
public void pipe(StAXHandlerInterface handler) throws XMLStreamException, Docx4JException, jakarta.xml.bind.JAXBException Replace the contents of this part with the output of passing it through your StAXHandler. This is most efficient in the case where there has been no need for JAXB to unmarshal the contents. In this case, it is possible to process then save the contents without incurring JAXB overhead (you may see 1/4 heap usage).- Parameters:
handler-- Throws:
XMLStreamExceptionDocx4JExceptionjakarta.xml.bind.JAXBException
-
pipe
public void pipe(StAXHandlerInterface handler, StreamFilter filter) throws XMLStreamException, Docx4JException, jakarta.xml.bind.JAXBException Replace the contents of this part with the output of passing it through your StAXHandler. This is most efficient in the case where there has been no need for JAXB to unmarshal the contents. In this case, it is possible to process then save the contents without incurring JAXB overhead (you may see 1/4 heap usage).- Parameters:
handler-filter-- Throws:
XMLStreamExceptionDocx4JExceptionjakarta.xml.bind.JAXBException
-
getXMLStreamReader
public XMLStreamReader getXMLStreamReader(StreamFilter filter) throws FactoryConfigurationError, Docx4JException, XMLStreamException -
replacePartContent
Replace the content of this part. Unless the part has been unmashalled already, this is done at the PartStore level.- Throws:
jakarta.xml.bind.JAXBExceptionDocx4JException- Since:
- 11.5.3
-
marshal
Marshal the content tree rooted at jaxbElement into a DOM tree.- Parameters:
node- DOM nodes will be added as children of this node. This parameter must be a Node that accepts children (Document,DocumentFragment, orElement)- Throws:
jakarta.xml.bind.JAXBException- If any unexpected problem occurs during the marshalling.
-
marshal
Marshal the content tree rooted at jaxbElement into a DOM tree.- Parameters:
node- DOM nodes will be added as children of this node. This parameter must be a Node that accepts children (Document,DocumentFragment, orElement)- Throws:
jakarta.xml.bind.JAXBException- If any unexpected problem occurs during the marshalling.
-
marshal
Marshal the content tree rooted at jaxbElement into an output stream, using org.docx4j.jaxb.NamespacePrefixMapper.- Parameters:
os- XML will be added to this stream.- Throws:
jakarta.xml.bind.JAXBException- If any unexpected problem occurs during the marshalling.
-
marshal
public void marshal(OutputStream os, Object namespacePrefixMapper) throws jakarta.xml.bind.JAXBException Marshal the content tree rooted at jaxbElement into an output stream- Parameters:
os- XML will be added to this stream.namespacePrefixMapper- namespacePrefixMapper- Throws:
jakarta.xml.bind.JAXBException- If any unexpected problem occurs during the marshalling.
-
getMceIgnorable
-
setMceIgnorable
Where the mc:Ignorable attribute is present, ensure its contents matches the ignorable namespaces actually present. -
getMcChoiceNamespaces
-
addMcChoiceNamespace
Specify a namespace prefix (used in mc:Choice/@Requires) which docx4j should declare on the top-level element of the part (otherwise Microsoft Office won't be able to open the file). Specify a prefix (eg 'wpg') as opposed to the namespace itself. This is often done automatically (see further McIgnorableNamespaceDeclarator), but where it isn't, you should invoke this method directly from your code.- Parameters:
mcChoiceNamespace-
-
getConfiguredUnmarshaller
protected jakarta.xml.bind.Unmarshaller getConfiguredUnmarshaller(boolean eventHandlerContinue) throws jakarta.xml.bind.JAXBException An Unmarshaller configured with our event handler and listener.- Parameters:
eventHandlerContinue-- Returns:
- Throws:
jakarta.xml.bind.JAXBException
-
unmarshal
Unmarshal XML data from the specified InputStream and return the resulting content tree. Validation event location information may be incomplete when using this form of the unmarshal API.Implements Unmarshal Global Root Element.
- Parameters:
is- the InputStream to unmarshal XML data from- Returns:
- the newly created root object of the java content tree
- Throws:
jakarta.xml.bind.JAXBException- If any unexpected errors occur while unmarshalling
-
preprocess
protected void preprocess(InputStream is, boolean eventHandlerContinue) throws jakarta.xml.bind.JAXBException Transform the content through mcPreprocessorXslt- Parameters:
is-eventHandlerContinue-- Throws:
jakarta.xml.bind.JAXBException
-
transformToDom
protected Document transformToDom(XMLStreamReader xsr) throws FactoryConfigurationError, jakarta.xml.bind.JAXBException Prepare input by transforming via mcPreprocessorXslt, outputting a DOM node, which we can use docx4jUnmarshallerListener on.- Parameters:
xsr-- Returns:
- Throws:
FactoryConfigurationErrorjakarta.xml.bind.JAXBException
-
inputStreamToXSR
- Throws:
XMLStreamException
-
unmarshal
- Throws:
jakarta.xml.bind.JAXBException
-
isContentEqual
- Specified by:
isContentEqualin classPart- Throws:
Docx4JException
-