org.docx4j.openpackaging.parts.JaxbXmlPartAltChunkHost Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of docx4j Show documentation
Show all versions of docx4j Show documentation
docx4j is a library which helps you to work with the Office Open
XML file format as used in docx
documents, pptx presentations, and xlsx spreadsheets.
/**
* Copyright 2012, Plutext Pty Ltd.
*
* This file is part of docx4j.
docx4j is licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.docx4j.openpackaging.parts;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.util.List;
import org.docx4j.TraversalUtil;
import org.docx4j.convert.in.xhtml.XHTMLImporter;
import org.docx4j.jaxb.Context;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.exceptions.InvalidFormatException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.AltChunkInterface;
import org.docx4j.openpackaging.parts.WordprocessingML.AltChunkType;
import org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart;
import org.docx4j.openpackaging.parts.relationships.RelationshipsPart.AddPartBehaviour;
import org.docx4j.relationships.Relationship;
import org.docx4j.utils.AltChunkFinder;
import org.docx4j.utils.AltChunkFinder.LocatedChunk;
import org.docx4j.wml.CTAltChunk;
import org.docx4j.wml.ContentAccessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author jharrop
* @since 3.0.0
*/
public abstract class JaxbXmlPartAltChunkHost extends JaxbXmlPartXPathAware implements AltChunkInterface {
protected static Logger log = LoggerFactory.getLogger(JaxbXmlPartAltChunkHost.class);
public JaxbXmlPartAltChunkHost(PartName partName)
throws InvalidFormatException {
super(partName);
// TODO Auto-generated constructor stub
}
/* (non-Javadoc)
* @see org.docx4j.openpackaging.parts.WordprocessingML.AltChunkInterface#addAltChunkOfTypeHTML(byte[])
*/
@Override
public AlternativeFormatInputPart addAltChunk(AltChunkType type, byte[] bytes) throws Docx4JException {
AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(type);
Relationship altChunkRel = this.addTargetPart(afiPart, AddPartBehaviour.RENAME_IF_NAME_EXISTS);
// now that its attached to the package ..
afiPart.registerInContentTypeManager();
afiPart.setBinaryData(bytes);
// .. the bit in document body
CTAltChunk ac = Context.getWmlObjectFactory().createCTAltChunk();
ac.setId(altChunkRel.getId() );
if (this instanceof ContentAccessor) {
((ContentAccessor)this).getContent().add(ac);
} else {
throw new Docx4JException(this.getClass().getName() + " doesn't implement ContentAccessor");
}
return afiPart;
}
/* (non-Javadoc)
* @see org.docx4j.openpackaging.parts.WordprocessingML.AltChunkInterface#addAltChunkOfTypeHTML(java.io.InputStream)
*/
@Override
public AlternativeFormatInputPart addAltChunk(AltChunkType type, InputStream is) throws Docx4JException {
AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(type);
Relationship altChunkRel = this.addTargetPart(afiPart, AddPartBehaviour.RENAME_IF_NAME_EXISTS);
// now that its attached to the package ..
afiPart.registerInContentTypeManager();
afiPart.setBinaryData(is);
// .. the bit in document body
CTAltChunk ac = Context.getWmlObjectFactory().createCTAltChunk();
ac.setId(altChunkRel.getId() );
if (this instanceof ContentAccessor) {
((ContentAccessor)this).getContent().add(ac);
} else {
throw new Docx4JException(this.getClass().getName() + " doesn't implement ContentAccessor");
}
return afiPart;
}
/* (non-Javadoc)
* @see org.docx4j.openpackaging.parts.WordprocessingML.AltChunkInterface#addAltChunkOfTypeHTML(byte[], org.docx4j.wml.ContentAccessor)
*/
@Override
public AlternativeFormatInputPart addAltChunk(AltChunkType type, byte[] bytes,
ContentAccessor attachmentPoint) throws Docx4JException {
AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(type);
Relationship altChunkRel = this.addTargetPart(afiPart, AddPartBehaviour.RENAME_IF_NAME_EXISTS);
// now that its attached to the package ..
afiPart.registerInContentTypeManager();
afiPart.setBinaryData(bytes);
// .. the bit in document body
CTAltChunk ac = Context.getWmlObjectFactory().createCTAltChunk();
ac.setId(altChunkRel.getId() );
attachmentPoint.getContent().add(ac);
return afiPart;
}
/* (non-Javadoc)
* @see org.docx4j.openpackaging.parts.WordprocessingML.AltChunkInterface#addAltChunkOfTypeHTML(java.io.InputStream, org.docx4j.wml.ContentAccessor)
*/
@Override
public AlternativeFormatInputPart addAltChunk(AltChunkType type, InputStream is,
ContentAccessor attachmentPoint) throws Docx4JException {
AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(type);
Relationship altChunkRel = this.addTargetPart(afiPart, AddPartBehaviour.RENAME_IF_NAME_EXISTS);
// now that its attached to the package ..
afiPart.registerInContentTypeManager();
afiPart.setBinaryData(is);
// .. the bit in document body
CTAltChunk ac = Context.getWmlObjectFactory().createCTAltChunk();
ac.setId(altChunkRel.getId() );
attachmentPoint.getContent().add(ac);
return afiPart;
}
/* (non-Javadoc)
* @see org.docx4j.openpackaging.parts.WordprocessingML.AltChunkInterface#processAltChunksOfTypeHTML()
*/
/**
* To convert an altChunk of type XHTML, this method requires docx4j-XHTMLImport.jar (LGPL) and its dependencies.
* */
@Override
public WordprocessingMLPackage convertAltChunks() throws Docx4JException {
// TODO: Currently only processes AltChunks in main document part.
if (!(this instanceof ContentAccessor)) {
throw new Docx4JException(this.getClass().getName() + " doesn't implement ContentAccessor");
}
PartName partName = this.getPartName();
WordprocessingMLPackage clonePkg = (WordprocessingMLPackage)this.getPackage().clone(); // consistent with MergeDocx approach
JaxbXmlPartAltChunkHost clonedPart = (JaxbXmlPartAltChunkHost)clonePkg.getParts().get(partName);
List