org.docx4j.convert.out.html.TagClass 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 2010, 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.convert.out.html;
import java.io.IOException;
import java.util.HashMap;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import org.docx4j.XmlUtils;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.wml.SdtPr;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.traversal.NodeIterator;
import org.xml.sax.SAXException;
/**
* If the content control has a tag containing @class=foo,
* then create a div with that attribute and value.
*
* If the value is the special case collapse, ie @class=collapse,
* then make this a collapsible div using javascript.
*
* @author jason
*
*/
public class TagClass extends SdtTagHandler {
private static Logger log = LoggerFactory.getLogger(TagClass.class);
/* replaces XSLT:
*
*
*/
private Element createDiv(Document document, DocumentFragment docfrag,
SdtPr sdtPr,
HashMap tagMap) throws ParserConfigurationException, IOException, SAXException {
// log.info("Document: " + document.getClass().getName() );
String classVal = tagMap.get("@class");
String sdtId="??";
if (sdtPr.getId()!=null) sdtId=sdtPr.getId().getVal().toString();
String sdtAlias = "??";
SdtPr.Alias alias = this.getAlias(sdtPr);
if (alias!=null) sdtAlias = alias.getVal();
if (classVal.equals("collapse") ) {
XmlUtils.appendXmlFragment(document, docfrag,
""
);
}
Element xhtmlDiv = document.createElement("div");
docfrag.appendChild(xhtmlDiv);
xhtmlDiv.setAttribute("class", tagMap.get("@class"));
if (classVal.equals("collapse") ) {
xhtmlDiv.setAttribute("id", "t" + sdtId);
if (tagMap.get("display")!=null
&& tagMap.get("display").equals("block")) {
xhtmlDiv.setAttribute("style", "display: block;");
} else {
xhtmlDiv.setAttribute("style", "display: none;");
}
}
return xhtmlDiv;
}
@Override
public Node toNode(WordprocessingMLPackage wmlPackage, SdtPr sdtPr,
HashMap tagMap,
NodeIterator childResults) throws TransformerException {
try {
// Create a DOM builder and parse the fragment
Document document = XmlUtils.getNewDocumentBuilder().newDocument();
DocumentFragment docfrag = document.createDocumentFragment();
Element xhtmlDiv = this.createDiv(document, docfrag, sdtPr, tagMap);
return attachContents(docfrag, xhtmlDiv, childResults);
} catch (Exception e) {
throw new TransformerException(e);
}
}
@Override
public Node toNode(WordprocessingMLPackage wmlPackage, SdtPr sdtPr,
HashMap tagMap,
Node resultSoFar) throws TransformerException {
try {
// Create a DOM builder and parse the fragment
Document document = XmlUtils.getNewDocumentBuilder().newDocument();
DocumentFragment docfrag = document.createDocumentFragment();
Element xhtmlDiv = this.createDiv(document, docfrag, sdtPr, tagMap);
return attachContents(docfrag, xhtmlDiv, resultSoFar);
} catch (Exception e) {
throw new TransformerException(e);
}
}
}