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;
}
if (result!=null) {
int index = locatedChunk.getIndex();
locatedChunk.getContentList().remove(index); // handles case where it is nested eg in a tc
org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();
org.docx4j.wml.P para = factory.createP();
locatedChunk.getContentList().add(index, para);
org.docx4j.wml.R run = factory.createR();
para.getContent().add(run);
org.docx4j.wml.Text t = factory.createText();
t.setValue(result);
run.getContent().add(t);
log.info("Converted altChunk of type text ");
}
} else if (type.equals(AltChunkType.WordprocessingML)
|| type.equals(AltChunkType.OfficeWordMacroEnabled)
|| type.equals(AltChunkType.OfficeWordTemplate)
||type.equals(AltChunkType.OfficeWordMacroEnabledTemplate) ) {
encounteredDocxAltChunk = true;
continue;
} else if (type.equals(AltChunkType.Rtf) ) {
log.warn("Skipping altChunk of type RTF ");
continue;
} else if (type.equals(AltChunkType.Html) ) {
log.warn("Skipping altChunk of type HTML ");
continue;
// if there was a pretty printer on class path,
// could use it via reflection?
}