NodeList list = getStylesDom().getElementsByTagName("office:master-styles");
if (list.getLength() > 0) {
OdfOfficeMasterStyles officeMasterStyles = (OdfOfficeMasterStyles) list.item(0);
// Get back the StylePageLayoutName
for (int i = 0; i < officeMasterStyles.getLength(); i++) {
StyleMasterPageElement syleMasterPage = (StyleMasterPageElement) officeMasterStyles.item(i);
if(syleMasterPage.getStyleNameAttribute().equals("Standard")){
stylePageLayoutName = syleMasterPage.getStylePageLayoutNameAttribute();
break;
}
}
// Allows to know if StylePageLayoutName is unique
for (int i = 0; i < officeMasterStyles.getLength(); i++) {
StyleMasterPageElement syleMasterPage = (StyleMasterPageElement) officeMasterStyles.item(i);
if(syleMasterPage.getStylePageLayoutNameAttribute().equals(stylePageLayoutName)){
pageLayoutNameCount++;
}
}
}
OdfOfficeAutomaticStyles autoStyles = getStylesDom().getAutomaticStyles();
int autoStylesCount = autoStyles.getLength();
OdfStylePageLayout pageLayout = autoStyles.getPageLayout(stylePageLayoutName);
if(pageLayout != null) {
// Clone the OdfStylePageLayout if another master style possesses the same name before modifying its properties
if(pageLayoutNameCount > 1){
Node pageLayoutNew = pageLayout.cloneNode(true);
// Rename the style of the clone before modifying its properties
String oldPageLayoutName = pageLayout.getStyleNameAttribute();
pageLayout.setStyleNameAttribute("Mpm" + (autoStylesCount+1));
// Allocate the new name of the style to the master style (the cloned style)
if (list.getLength() > 0) {
OdfOfficeMasterStyles masterpage = (OdfOfficeMasterStyles) list.item(0);
for (int i = 0; i < masterpage.getLength(); i++) {
StyleMasterPageElement vSyleMasterPage = (StyleMasterPageElement) masterpage.item(i);
if(vSyleMasterPage.getStyleNameAttribute().equals("Standard")){
if(vSyleMasterPage.getStylePageLayoutNameAttribute().equals(oldPageLayoutName)){
vSyleMasterPage.setStylePageLayoutNameAttribute(pageLayout.getStyleNameAttribute());
}
}
}
}
autoStyles.appendChild(pageLayoutNew);