org.docx4j.model.datastorage.XHTMLAttrInjector 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.
package org.docx4j.model.datastorage;
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.ParserConfigurationException;
import org.docx4j.XmlUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class XHTMLAttrInjector {
public static Logger log = LoggerFactory.getLogger(XHTMLAttrInjector.class);
protected static String injectAttrs(String content, String classVal, String styleVal) throws ParserConfigurationException, SAXException, IOException {
// Convert xhtml string to DOM
int firstChar = content.codePointAt(0);
if (firstChar==0xFEFF) {
log.info("Removing BOM..");
content = content.substring(1);
}
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(content));
Document doc = XmlUtils.getNewDocumentBuilder().parse(is);
// traverse + inject (need traverse, to get p in table)
injectAttrs(doc, classVal, styleVal);
// convert back to string again
return XmlUtils.w3CDomNodeToString(doc);
}
public static void injectAttrs( Node sourceNode, String classVal, String styleVal ) {
log.debug("node type" + sourceNode.getNodeType());
switch (sourceNode.getNodeType() ) {
case Node.DOCUMENT_NODE: // type 9
case Node.DOCUMENT_FRAGMENT_NODE: // type 11
// log.debug("DOCUMENT:" + w3CDomNodeToString(sourceNode) );
// if (sourceNode.getChildNodes().getLength()==0) {
// log.debug("..no children!");
// }
// recurse on each child
NodeList nodes = sourceNode.getChildNodes();
if (nodes != null) {
for (int i=0; i clonedChildren = new ArrayList();
// List deletions = new ArrayList();
// NodeList children = sourceNode.getChildNodes();
// if (children != null) {
// for (int i=0; i");
// break;
//
// case Node.COMMENT_NODE:
// writer.write(indentLevel + "");
// writer.write(lineSeparator);
// break;
//
// case Node.PROCESSING_INSTRUCTION_NODE:
// writer.write("");
// writer.write(lineSeparator);
// break;
//
// case Node.ENTITY_REFERENCE_NODE:
// writer.write("&" + node.getNodeName() + ";");
// break;
//
// case Node.DOCUMENT_TYPE_NODE:
// DocumentType docType = (DocumentType)node;
// writer.write("");
// writer.write(lineSeparator);
// break;
}
}
/**
* @param args
* @throws IOException
* @throws SAXException
* @throws ParserConfigurationException
*/
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
// String content = "";
String content = "p1
p2
p1
";
System.out.println( injectAttrs(content, "foo", "bar") );
}
}