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-core Show documentation
Show all versions of docx4j-core 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.ByteArrayInputStream;
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.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
}
@Override
public AlternativeFormatInputPart addAltChunk(AltChunkType type, byte[] bytes) throws Docx4JException {
return addAltChunk(type, bytes, -1);
}
/* (non-Javadoc)
* @see org.docx4j.openpackaging.parts.WordprocessingML.AltChunkInterface#addAltChunkOfTypeHTML(byte[])
*/
@Override
public AlternativeFormatInputPart addAltChunk(AltChunkType type, byte[] bytes, int index) 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) {
if (index<0) {
((ContentAccessor)this).getContent().add(ac);
} else {
((ContentAccessor)this).getContent().add(index, ac);
}
} else {
throw new Docx4JException(this.getClass().getName() + " doesn't implement ContentAccessor");
}
return afiPart;
}
@Override
public AlternativeFormatInputPart addAltChunk(AltChunkType type, InputStream is) throws Docx4JException {
return addAltChunk(type, is, -1);
}
/* (non-Javadoc)
* @see org.docx4j.openpackaging.parts.WordprocessingML.AltChunkInterface#addAltChunkOfTypeHTML(java.io.InputStream)
*/
@Override
public AlternativeFormatInputPart addAltChunk(AltChunkType type, InputStream is, int index) 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) {
if (index<0) {
((ContentAccessor)this).getContent().add(ac);
} else {
((ContentAccessor)this).getContent().add(index, ac);
}
} else {
throw new Docx4JException(this.getClass().getName() + " doesn't implement ContentAccessor");
}
return afiPart;
}
@Override
public AlternativeFormatInputPart addAltChunk(AltChunkType type, byte[] bytes,
ContentAccessor attachmentPoint) throws Docx4JException {
return addAltChunk(type, bytes, attachmentPoint, -1);
}
/* (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, int index) 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 (index<0) {
attachmentPoint.getContent().add(ac);
} else {
attachmentPoint.getContent().add(index, ac);
}
return afiPart;
}
@Override
public AlternativeFormatInputPart addAltChunk(AltChunkType type, InputStream is,
ContentAccessor attachmentPoint) throws Docx4JException {
return addAltChunk(type, is, attachmentPoint, -1);
}
/* (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, int index) 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 (index<0) {
attachmentPoint.getContent().add(ac);
} else {
attachmentPoint.getContent().add(index, ac);
}
return afiPart;
}
@SuppressWarnings("unchecked")
@Override
public void convertAltChunks() throws Docx4JException {
if (!(this instanceof ContentAccessor)) {
throw new Docx4JException(this.getClass().getName() + " doesn't implement ContentAccessor");
}
PartName partName = this.getPartName();
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy