CTAltChunk altChunk;
boolean encounteredDocxAltChunk = false;
for (LocatedChunk locatedChunk : bf.getAltChunks()) {
altChunk = locatedChunk.getAltChunk();
AlternativeFormatInputPart afip
= (AlternativeFormatInputPart)clonedPart.getRelationshipsPart().getPart(
altChunk.getId() );
// Can we process it?
AltChunkType type = afip.getAltChunkType();
if (type.equals(AltChunkType.Xhtml) ) {
XHTMLImporter xHTMLImporter= null;
try {
Class<?> xhtmlImporterClass = Class.forName("org.docx4j.convert.in.xhtml.XHTMLImporterImpl");
Constructor<?> ctor = xhtmlImporterClass.getConstructor(WordprocessingMLPackage.class);
xHTMLImporter = (XHTMLImporter) ctor.newInstance(clonePkg);
} catch (Exception e) {
log.error("docx4j-XHTMLImport jar not found. Please add this to your classpath.");
log.error(e.getMessage(), e);
return null;
}
List<Object> results = null;
try {
results = xHTMLImporter.convert(toString(afip.getBuffer()), null);
} catch (Exception e) {
log.error(e.getMessage(), e);
// Skip this one
continue;
}
int index = locatedChunk.getIndex();
locatedChunk.getContentList().remove(index); // handles case where it is nested eg in a tc
locatedChunk.getContentList().addAll(index, results);
log.info("Converted altChunk of type XHTML ");
} else if (type.equals(AltChunkType.Mht) ) {
log.warn("Skipping altChunk of type MHT ");
continue;
} else if (type.equals(AltChunkType.Xml) ) {
log.warn("Skipping altChunk of type XML "); // what does Word do??
continue;
} else if (type.equals(AltChunkType.TextPlain) ) {
String result= null;
try {
result = toString(afip.getBuffer());
} catch (UnsupportedEncodingException e) {
log.error(e.getMessage(), e);
// Skip this one
continue;
}