// style-name ref count
// //////////////
// method 2:
// 2.1. get the list of the style definition
ArrayList<OdfElement> removeStyles = new ArrayList<OdfElement>();
OdfOfficeAutomaticStyles autoStyles = getContentDom().getAutomaticStyles();
NodeList stylesList = autoStyles.getChildNodes();
OdfFileDom contentDom = getContentDom();
XPath xpath = contentDom.getXPath();
// 2.2. get the reference of each style which occurred in the
// current page
for (int i = 0; i < stylesList.getLength(); i++) {
Node item = stylesList.item(i);
if (item instanceof OdfElement) {
OdfElement node = (OdfElement) item;
String styleName = node.getAttributeNS(OdfDocumentNamespace.STYLE.getUri(), "name");
if (styleName != null) {
// search the styleName contained at the current page
// element
NodeList styleNodes = (NodeList) xpath.evaluate("//*[@*='" + styleName + "']", contentDom,
XPathConstants.NODESET);
int styleCnt = styleNodes.getLength();
if (styleCnt > 1) {
// the first styleName is occurred in the style
// definition
// so check if the second styleName and last
// styleName is occurred in the current page element
// if yes, then remove it
OdfElement elementFirst = (OdfElement) styleNodes.item(1);
OdfElement elementLast = (OdfElement) styleNodes.item(styleCnt - 1);
boolean isSamePage = false;
if (elementFirst instanceof DrawPageElement) {
DrawPageElement tempPage = (DrawPageElement) elementFirst;
if (tempPage.equals(odfEle)) {
isSamePage = true;
}
}
int relationFirst = odfEle.compareDocumentPosition(elementFirst);
int relationLast = odfEle.compareDocumentPosition(elementLast);
// if slide element contains the child element which
// has the styleName reference
if (((relationFirst & Node.DOCUMENT_POSITION_CONTAINED_BY) > 0 && (relationLast & Node.DOCUMENT_POSITION_CONTAINED_BY) > 0)
|| (isSamePage && (styleCnt == 1))) {
if (node instanceof OdfStyleBase) {
removeStyles.add(node);
}
}
} else {
continue;
}
}
}
}
for (int i = 0; i < removeStyles.size(); i++) {
autoStyles.removeChild(removeStyles.get(i));
}
} catch (Exception e) {
Logger.getLogger(Document.class.getName()).log(Level.SEVERE, null, e);
success = false;
}