[Dropped] Change name of any style in form in odt

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Locked
abhishah4444
Posts: 5
Joined: Tue Mar 01, 2011 5:14 pm

[Dropped] Change name of any style in form in odt

Post by abhishah4444 »

I am using below but format is getting distorted any idea?

Code: Select all

import org.odftoolkit.odfdom.doc.OdfTextDocument;
import org.odftoolkit.odfdom.dom.OdfContentDom;
import org.odftoolkit.odfdom.dom.OdfStylesDom;
import org.odftoolkit.odfdom.dom.element.style.*;
import org.odftoolkit.odfdom.dom.element.text.*;
import org.odftoolkit.odfdom.incubator.doc.style.OdfStyle;
import org.w3c.dom.*;
import javax.xml.xpath.*;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class StyleNamePrepender {
    
    public static void prependStyleNames(String inputPath, String outputPath) throws Exception {
        // Extract filename without extension
        String filename = new File(inputPath).getName();
        filename = filename.substring(0, filename.lastIndexOf('.'));
        String prefix = filename + "_";
        
        // Load the ODT document
        OdfTextDocument odt = OdfTextDocument.loadDocument(inputPath);
        
        // Map to store old style names to new style names
        Map<String, String> styleNameMap = new HashMap<>();
        
        // Process styles in styles.xml
        OdfStylesDom stylesDom = odt.getStylesDom();
        processStylesInDom(stylesDom, prefix, styleNameMap);
        
        // Process styles in content.xml
        OdfContentDom contentDom = odt.getContentDom();
        processStylesInDom(contentDom, prefix, styleNameMap);
        
        // Update all style references throughout the document
        updateStyleReferences(odt, styleNameMap);
        
        // Save the modified document
        odt.save(outputPath);
        odt.close();
        
        System.out.println("Style names prepended successfully!");
        System.out.println("Prefix used: " + prefix);
        System.out.println("Total styles renamed: " + styleNameMap.size());
    }
    
    private static void processStylesInDom(Node dom, String prefix, Map<String, String> styleNameMap) {
        // Process using direct DOM traversal instead of XPath
        processNodeRecursively(dom, prefix, styleNameMap);
    }
    
    private static void processNodeRecursively(Node node, String prefix, Map<String, String> styleNameMap) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;
            String localName = element.getLocalName();
            
            // Check if this is a style element
            boolean isStyleElement = false;
            String nameAttrName = null;
            
            if ("style".equals(localName) || "default-style".equals(localName) || 
                "page-layout".equals(localName)) {
                isStyleElement = true;
                nameAttrName = "style:name";
            } else if ("list-style".equals(localName)) {
                isStyleElement = true;
                nameAttrName = "style:name";
            } else if ("number-style".equals(localName) || "date-style".equals(localName) ||
                       "time-style".equals(localName) || "currency-style".equals(localName) ||
                       "percentage-style".equals(localName)) {
                isStyleElement = true;
                nameAttrName = "style:name";
            } else if ("master-page".equals(localName)) {
                isStyleElement = true;
                nameAttrName = "style:name";
            }
            
            if (isStyleElement) {
                // Try to get the name attribute
                String oldName = element.getAttributeNS(
                    "urn:oasis:names:tc:opendocument:xmlns:style:1.0", "name");
                
                if (oldName != null && !oldName.isEmpty()) {
                    String newName = prefix + oldName;
                    
                    // Update the attribute
                    element.setAttributeNS(
                        "urn:oasis:names:tc:opendocument:xmlns:style:1.0", 
                        "style:name", 
                        newName);
                    
                    // Store mapping
                    styleNameMap.put(oldName, newName);
                    System.out.println("Renamed style: " + oldName + " -> " + newName);
                }
            }
        }
        
        // Process child nodes
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            processNodeRecursively(children.item(i), prefix, styleNameMap);
        }
    }
    
    private static void updateStyleReferences(OdfTextDocument odt, Map<String, String> styleNameMap) {
        // Update references in both content and styles DOMs
        Document[] doms = {odt.getContentDom(), odt.getStylesDom()};
        
        for (Document dom : doms) {
            updateReferencesRecursively(dom, styleNameMap);
        }
    }
    
    private static void updateReferencesRecursively(Node node, Map<String, String> styleNameMap) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;
            
            // Comprehensive list of attributes that reference style names
            String[] refAttrs = {
                "style-name", 
                "parent-style-name", 
                "list-style-name", 
                "data-style-name", 
                "page-layout-name", 
                "master-page-name",
                "next-style-name", 
                "default-cell-style-name",
                "text-style-name",           // for draw elements
                "default-style-name",         // for tables
                "apply-style-name",           // for conditional formatting
                "page-style-name",            // for page breaks
                "tab-stop-style-name",        // for tab stops
                "drop-cap-style-name",        // for drop caps
                "num-format",                 // can reference styles
                "cell-style-name",            // for table cells
                "paragraph-style-name",       // explicit paragraph styles
                "ruby-style-name",            // for ruby text
                "leader-style-name",          // for leader lines
                "default-outline-level-style" // for outline numbering
            };
            
            // All ODF namespaces that might contain style references
            String[] namespaces = {
                "urn:oasis:names:tc:opendocument:xmlns:text:1.0",      // text:
                "urn:oasis:names:tc:opendocument:xmlns:style:1.0",     // style:
                "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0",   // draw:
                "urn:oasis:names:tc:opendocument:xmlns:table:1.0",     // table:
                "urn:oasis:names:tc:opendocument:xmlns:presentation:1.0", // presentation:
                "urn:oasis:names:tc:opendocument:xmlns:fo:1.0",        // fo:
                "urn:oasis:names:tc:opendocument:xmlns:number:1.0"     // number:
            };
            
            for (String attrName : refAttrs) {
                for (String ns : namespaces) {
                    String oldStyleName = element.getAttributeNS(ns, attrName);
                    
                    if (oldStyleName != null && !oldStyleName.isEmpty()) {
                        String newStyleName = styleNameMap.get(oldStyleName);
                        
                        if (newStyleName != null) {
                            String nsPrefix = getNamespacePrefix(ns);
                            element.setAttributeNS(ns, nsPrefix + ":" + attrName, newStyleName);
                            System.out.println("Updated reference: " + oldStyleName + " -> " + newStyleName + 
                                             " (in " + element.getLocalName() + ", attr: " + attrName + ")");
                        }
                    }
                }
            }
            
            // Also check for attributes without namespace (some older ODT files)
            NamedNodeMap attrs = element.getAttributes();
            for (int i = 0; i < attrs.getLength(); i++) {
                Node attr = attrs.item(i);
                String attrName = attr.getNodeName();
                
                // Check if attribute name contains "style" and could be a reference
                if (attrName.contains("style") || attrName.contains("Style")) {
                    String oldStyleName = attr.getNodeValue();
                    String newStyleName = styleNameMap.get(oldStyleName);
                    
                    if (newStyleName != null) {
                        attr.setNodeValue(newStyleName);
                        System.out.println("Updated non-namespaced reference: " + oldStyleName + 
                                         " -> " + newStyleName + " (attr: " + attrName + ")");
                    }
                }
            }
        }
        
        // Process child nodes
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            updateReferencesRecursively(children.item(i), styleNameMap);
        }
    }
    
    private static String getNamespacePrefix(String namespace) {
        switch (namespace) {
            case "urn:oasis:names:tc:opendocument:xmlns:text:1.0":
                return "text";
            case "urn:oasis:names:tc:opendocument:xmlns:style:1.0":
                return "style";
            case "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0":
                return "draw";
            case "urn:oasis:names:tc:opendocument:xmlns:table:1.0":
                return "table";
            case "urn:oasis:names:tc:opendocument:xmlns:presentation:1.0":
                return "presentation";
            case "urn:oasis:names:tc:opendocument:xmlns:fo:1.0":
                return "fo";
            case "urn:oasis:names:tc:opendocument:xmlns:number:1.0":
                return "number";
            default:
                return "style";
        }
    }
    
    public static void main(String[] args) {
        try {
            if (args.length < 2) {
                System.out.println("Usage: java StyleNamePrepender <input.odt> <output.odt>");
                return;
            }
            
            String inputPath = args[0];
            String outputPath = args[1];
            
            prependStyleNames(inputPath, outputPath);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Last edited by MrProgrammer on Fri Feb 06, 2026 4:45 am, edited 2 times in total.
Reason: Dropped: No reply from abhishah4444
OpenOffice 3.3 on Windows XP
abhishah4444
Posts: 5
Joined: Tue Mar 01, 2011 5:14 pm

Re: Change name of any style in form in odt

Post by abhishah4444 »

I even tried to keep default out of scope with below

Code: Select all

import org.odftoolkit.odfdom.doc.OdfTextDocument;
import org.odftoolkit.odfdom.dom.OdfContentDom;
import org.odftoolkit.odfdom.dom.OdfStylesDom;
import org.odftoolkit.odfdom.dom.element.style.*;
import org.odftoolkit.odfdom.dom.element.text.*;
import org.odftoolkit.odfdom.incubator.doc.style.OdfStyle;
import org.w3c.dom.*;
import javax.xml.xpath.*;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class StyleNamePrepender {
    
    public static void prependStyleNames(String inputPath, String outputPath) throws Exception {
        // Extract filename without extension
        String filename = new File(inputPath).getName();
        filename = filename.substring(0, filename.lastIndexOf('.'));
        String prefix = filename + "_";
        
        // Load the ODT document
        OdfTextDocument odt = OdfTextDocument.loadDocument(inputPath);
        
        // Map to store old style names to new style names
        Map<String, String> styleNameMap = new HashMap<>();
        
        // Process styles in styles.xml
        OdfStylesDom stylesDom = odt.getStylesDom();
        processStylesInDom(stylesDom, prefix, styleNameMap);
        
        // Process styles in content.xml
        OdfContentDom contentDom = odt.getContentDom();
        processStylesInDom(contentDom, prefix, styleNameMap);
        
        // Update all style references throughout the document
        updateStyleReferences(odt, styleNameMap);
        
        // Save the modified document
        odt.save(outputPath);
        odt.close();
        
        System.out.println("Style names prepended successfully!");
        System.out.println("Prefix used: " + prefix);
        System.out.println("Total styles renamed: " + styleNameMap.size());
    }
    
    private static void processStylesInDom(Node dom, String prefix, Map<String, String> styleNameMap) {
        // Process using direct DOM traversal instead of XPath
        processNodeRecursively(dom, prefix, styleNameMap);
    }
    
    private static void processNodeRecursively(Node node, String prefix, Map<String, String> styleNameMap) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;
            String localName = element.getLocalName();
            
            // Check if this is a style element
            boolean isStyleElement = false;
            String nameAttrName = null;
            
            if ("style".equals(localName) || "default-style".equals(localName) || 
                "page-layout".equals(localName)) {
                isStyleElement = true;
                nameAttrName = "style:name";
            } else if ("list-style".equals(localName)) {
                isStyleElement = true;
                nameAttrName = "style:name";
            } else if ("number-style".equals(localName) || "date-style".equals(localName) ||
                       "time-style".equals(localName) || "currency-style".equals(localName) ||
                       "percentage-style".equals(localName)) {
                isStyleElement = true;
                nameAttrName = "style:name";
            } else if ("master-page".equals(localName)) {
                isStyleElement = true;
                nameAttrName = "style:name";
            }
            
            if (isStyleElement) {
                // Try to get the name attribute
                String oldName = element.getAttributeNS(
                    "urn:oasis:names:tc:opendocument:xmlns:style:1.0", "name");
                
                if (oldName != null && !oldName.isEmpty() && !isBuiltInStyle(oldName)) {
                    String newName = prefix + oldName;
                    
                    // Update the attribute
                    element.setAttributeNS(
                        "urn:oasis:names:tc:opendocument:xmlns:style:1.0", 
                        "style:name", 
                        newName);
                    
                    // Store mapping
                    styleNameMap.put(oldName, newName);
                    System.out.println("Renamed style: " + oldName + " -> " + newName);
                } else if (oldName != null && !oldName.isEmpty()) {
                    System.out.println("Skipped built-in style: " + oldName);
                }
            }
        }
        
        // Process child nodes
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            processNodeRecursively(children.item(i), prefix, styleNameMap);
        }
    }
    
    private static boolean isBuiltInStyle(String styleName) {
        // List of built-in/default ODF styles that should not be renamed
        String[] builtInStyles = {
            // Standard page styles
            "Standard", "First_20_Page", "Left_20_Page", "Right_20_Page",
            "First Page", "Left Page", "Right Page",
            "Endnote", "Footnote", "HTML",
            
            // Default paragraph styles
            "Text_20_body", "Text body",
            "Default_20_Style", "Default Style",
            "Standard",
            
            // Header and footer related
            "Header", "Footer", "Header_20_and_20_Footer",
            "Header_20_left", "Header_20_right", 
            "Footer_20_left", "Footer_20_right",
            
            // Table styles
            "Table_20_Contents", "Table Contents",
            "Table_20_Heading", "Table Heading",
            
            // Graphics
            "Graphics", "Frame",
            
            // Default numbering/bullets
            "Numbering_20_Symbols", "Numbering Symbols",
            
            // Master page defaults
            "Default", "default"
        };
        
        for (String builtIn : builtInStyles) {
            if (styleName.equals(builtIn) || styleName.equalsIgnoreCase(builtIn)) {
                return true;
            }
        }
        
        // Also skip styles that start with certain prefixes
        return styleName.startsWith("Mpm") ||      // Master page margin
               styleName.startsWith("MP") ||        // Master page
               styleName.startsWith("dp") ||        // Default paragraph
               styleName.startsWith("fr");          // Frame styles
    }
    
    private static void updateStyleReferences(OdfTextDocument odt, Map<String, String> styleNameMap) {
        // Update references in both content and styles DOMs
        Document[] doms = {odt.getContentDom(), odt.getStylesDom()};
        
        for (Document dom : doms) {
            updateReferencesRecursively(dom, styleNameMap);
        }
    }
    
    private static void updateReferencesRecursively(Node node, Map<String, String> styleNameMap) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;
            
            // Comprehensive list of attributes that reference style names
            String[] refAttrs = {
                "style-name", 
                "parent-style-name", 
                "list-style-name", 
                "data-style-name", 
                "page-layout-name", 
                "master-page-name",
                "next-style-name", 
                "default-cell-style-name",
                "text-style-name",           // for draw elements
                "default-style-name",         // for tables
                "apply-style-name",           // for conditional formatting
                "page-style-name",            // for page breaks
                "tab-stop-style-name",        // for tab stops
                "drop-cap-style-name",        // for drop caps
                "num-format",                 // can reference styles
                "cell-style-name",            // for table cells
                "paragraph-style-name",       // explicit paragraph styles
                "ruby-style-name",            // for ruby text
                "leader-style-name",          // for leader lines
                "default-outline-level-style" // for outline numbering
            };
            
            // All ODF namespaces that might contain style references
            String[] namespaces = {
                "urn:oasis:names:tc:opendocument:xmlns:text:1.0",      // text:
                "urn:oasis:names:tc:opendocument:xmlns:style:1.0",     // style:
                "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0",   // draw:
                "urn:oasis:names:tc:opendocument:xmlns:table:1.0",     // table:
                "urn:oasis:names:tc:opendocument:xmlns:presentation:1.0", // presentation:
                "urn:oasis:names:tc:opendocument:xmlns:fo:1.0",        // fo:
                "urn:oasis:names:tc:opendocument:xmlns:number:1.0"     // number:
            };
            
            for (String attrName : refAttrs) {
                for (String ns : namespaces) {
                    String oldStyleName = element.getAttributeNS(ns, attrName);
                    
                    if (oldStyleName != null && !oldStyleName.isEmpty()) {
                        String newStyleName = styleNameMap.get(oldStyleName);
                        
                        if (newStyleName != null) {
                            String nsPrefix = getNamespacePrefix(ns);
                            element.setAttributeNS(ns, nsPrefix + ":" + attrName, newStyleName);
                            System.out.println("Updated reference: " + oldStyleName + " -> " + newStyleName + 
                                             " (in " + element.getLocalName() + ", attr: " + attrName + ")");
                        }
                    }
                }
            }
            
            // Also check for attributes without namespace (some older ODT files)
            NamedNodeMap attrs = element.getAttributes();
            for (int i = 0; i < attrs.getLength(); i++) {
                Node attr = attrs.item(i);
                String attrName = attr.getNodeName();
                
                // Check if attribute name contains "style" and could be a reference
                if (attrName.contains("style") || attrName.contains("Style")) {
                    String oldStyleName = attr.getNodeValue();
                    String newStyleName = styleNameMap.get(oldStyleName);
                    
                    if (newStyleName != null) {
                        attr.setNodeValue(newStyleName);
                        System.out.println("Updated non-namespaced reference: " + oldStyleName + 
                                         " -> " + newStyleName + " (attr: " + attrName + ")");
                    }
                }
            }
        }
        
        // Process child nodes
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            updateReferencesRecursively(children.item(i), styleNameMap);
        }
    }
    
    private static String getNamespacePrefix(String namespace) {
        switch (namespace) {
            case "urn:oasis:names:tc:opendocument:xmlns:text:1.0":
                return "text";
            case "urn:oasis:names:tc:opendocument:xmlns:style:1.0":
                return "style";
            case "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0":
                return "draw";
            case "urn:oasis:names:tc:opendocument:xmlns:table:1.0":
                return "table";
            case "urn:oasis:names:tc:opendocument:xmlns:presentation:1.0":
                return "presentation";
            case "urn:oasis:names:tc:opendocument:xmlns:fo:1.0":
                return "fo";
            case "urn:oasis:names:tc:opendocument:xmlns:number:1.0":
                return "number";
            default:
                return "style";
        }
    }
    
    public static void main(String[] args) {
        try {
            if (args.length < 2) {
                System.out.println("Usage: java StyleNamePrepender <input.odt> <output.odt>");
                return;
            }
            
            String inputPath = args[0];
            String outputPath = args[1];
            
            prependStyleNames(inputPath, outputPath);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Last edited by MrProgrammer on Fri Jan 23, 2026 6:08 am, edited 1 time in total.
Reason: Added [code][/code] tags again. This is the last time I'm doing that for abhishah4444.
OpenOffice 3.3 on Windows XP
abhishah4444
Posts: 5
Joined: Tue Mar 01, 2011 5:14 pm

Re: Change name of any style in form in odt

Post by abhishah4444 »

import org.odftoolkit.odfdom.doc.OdfTextDocument;
import org.odftoolkit.odfdom.dom.OdfContentDom;
import org.odftoolkit.odfdom.dom.OdfStylesDom;
import org.odftoolkit.odfdom.dom.element.style.*;
import org.odftoolkit.odfdom.dom.element.text.*;
import org.odftoolkit.odfdom.incubator.doc.style.OdfStyle;
import org.w3c.dom.*;
import javax.xml.xpath.*;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class StyleNamePrepender {

public static void prependStyleNames(String inputPath, String outputPath) throws Exception {
// Extract filename without extension
String filename = new File(inputPath).getName();
filename = filename.substring(0, filename.lastIndexOf('.'));
String prefix = filename + "_";

// Load the ODT document
OdfTextDocument odt = OdfTextDocument.loadDocument(inputPath);

// Map to store old style names to new style names
Map<String, String> styleNameMap = new HashMap<>();

// Process styles in styles.xml
OdfStylesDom stylesDom = odt.getStylesDom();
processStylesInDom(stylesDom, prefix, styleNameMap);

// Process styles in content.xml
OdfContentDom contentDom = odt.getContentDom();
processStylesInDom(contentDom, prefix, styleNameMap);

// Update all style references throughout the document
updateStyleReferences(odt, styleNameMap);

// Save the modified document
odt.save(outputPath);
odt.close();

System.out.println("Style names prepended successfully!");
System.out.println("Prefix used: " + prefix);
System.out.println("Total styles renamed: " + styleNameMap.size());
}

private static void processStylesInDom(Node dom, String prefix, Map<String, String> styleNameMap) {
// Process using direct DOM traversal instead of XPath
processNodeRecursively(dom, prefix, styleNameMap);
}

private static void processNodeRecursively(Node node, String prefix, Map<String, String> styleNameMap) {
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
String localName = element.getLocalName();

// Check if this is a style element
boolean isStyleElement = false;
String nameAttrName = null;

if ("style".equals(localName) || "default-style".equals(localName) ||
"page-layout".equals(localName)) {
isStyleElement = true;
nameAttrName = "style:name";
} else if ("list-style".equals(localName)) {
isStyleElement = true;
nameAttrName = "style:name";
} else if ("number-style".equals(localName) || "date-style".equals(localName) ||
"time-style".equals(localName) || "currency-style".equals(localName) ||
"percentage-style".equals(localName)) {
isStyleElement = true;
nameAttrName = "style:name";
} else if ("master-page".equals(localName)) {
isStyleElement = true;
nameAttrName = "style:name";
}

if (isStyleElement) {
// Try to get the name attribute
String oldName = element.getAttributeNS(
"urn:oasis:names:tc:opendocument:xmlns:style:1.0", "name");

if (oldName != null && !oldName.isEmpty()) {
String newName = prefix + oldName;

// Update the attribute
element.setAttributeNS(
"urn:oasis:names:tc:opendocument:xmlns:style:1.0",
"style:name",
newName);

// Store mapping
styleNameMap.put(oldName, newName);
System.out.println("Renamed style: " + oldName + " -> " + newName);

// CRITICAL: If this is a master-page, also update its page-layout-name reference
if ("master-page".equals(localName)) {
String pageLayoutName = element.getAttributeNS(
"urn:oasis:names:tc:opendocument:xmlns:style:1.0", "page-layout-name");
if (pageLayoutName != null && !pageLayoutName.isEmpty()) {
// We'll update this reference later, but mark it for tracking
System.out.println("Master page '" + newName + "' references page-layout: " + pageLayoutName);
}
}
}
}
}

// Process child nodes
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
processNodeRecursively(children.item(i), prefix, styleNameMap);
}
}

private static void updateStyleReferences(OdfTextDocument odt, Map<String, String> styleNameMap) {
// Update references in both content and styles DOMs
Document[] doms = {odt.getContentDom(), odt.getStylesDom()};

for (Document dom : doms) {
updateReferencesRecursively(dom, styleNameMap);
}
}

private static void updateReferencesRecursively(Node node, Map<String, String> styleNameMap) {
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;

// Comprehensive list of attributes that reference style names
String[] refAttrs = {
"style-name",
"parent-style-name",
"list-style-name",
"data-style-name",
"page-layout-name",
"master-page-name",
"next-style-name",
"default-cell-style-name",
"text-style-name", // for draw elements
"default-style-name", // for tables
"apply-style-name", // for conditional formatting
"page-style-name", // for page breaks
"tab-stop-style-name", // for tab stops
"drop-cap-style-name", // for drop caps
"num-format", // can reference styles
"cell-style-name", // for table cells
"paragraph-style-name", // explicit paragraph styles
"ruby-style-name", // for ruby text
"leader-style-name", // for leader lines
"default-outline-level-style" // for outline numbering
};

// All ODF namespaces that might contain style references
String[] namespaces = {
"urn:oasis:names:tc:opendocument:xmlns:text:1.0", // text:
"urn:oasis:names:tc:opendocument:xmlns:style:1.0", // style:
"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0", // draw:
"urn:oasis:names:tc:opendocument:xmlns:table:1.0", // table:
"urn:oasis:names:tc:opendocument:xmlns:presentation:1.0", // presentation:
"urn:oasis:names:tc:opendocument:xmlns:fo:1.0", // fo:
"urn:oasis:names:tc:opendocument:xmlns:number:1.0" // number:
};

for (String attrName : refAttrs) {
for (String ns : namespaces) {
String oldStyleName = element.getAttributeNS(ns, attrName);

if (oldStyleName != null && !oldStyleName.isEmpty()) {
String newStyleName = styleNameMap.get(oldStyleName);

if (newStyleName != null) {
String nsPrefix = getNamespacePrefix(ns);
element.setAttributeNS(ns, nsPrefix + ":" + attrName, newStyleName);
System.out.println("Updated reference: " + oldStyleName + " -> " + newStyleName +
" (in " + element.getLocalName() + ", attr: " + attrName + ")");
}
}
}
}

// Also check for attributes without namespace (some older ODT files)
NamedNodeMap attrs = element.getAttributes();
for (int i = 0; i < attrs.getLength(); i++) {
Node attr = attrs.item(i);
String attrName = attr.getNodeName();

// Check if attribute name contains "style" and could be a reference
if (attrName.contains("style") || attrName.contains("Style")) {
String oldStyleName = attr.getNodeValue();
String newStyleName = styleNameMap.get(oldStyleName);

if (newStyleName != null) {
attr.setNodeValue(newStyleName);
System.out.println("Updated non-namespaced reference: " + oldStyleName +
" -> " + newStyleName + " (attr: " + attrName + ")");
}
}
}
}

// Process child nodes
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
updateReferencesRecursively(children.item(i), styleNameMap);
}
}

private static String getNamespacePrefix(String namespace) {
switch (namespace) {
case "urn:oasis:names:tc:opendocument:xmlns:text:1.0":
return "text";
case "urn:oasis:names:tc:opendocument:xmlns:style:1.0":
return "style";
case "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0":
return "draw";
case "urn:oasis:names:tc:opendocument:xmlns:table:1.0":
return "table";
case "urn:oasis:names:tc:opendocument:xmlns:presentation:1.0":
return "presentation";
case "urn:oasis:names:tc:opendocument:xmlns:fo:1.0":
return "fo";
case "urn:oasis:names:tc:opendocument:xmlns:number:1.0":
return "number";
default:
return "style";
}
}

public static void main(String[] args) {
try {
if (args.length < 2) {
System.out.println("Usage: java StyleNamePrepender <input.odt> <output.odt>");
return;
}

String inputPath = args[0];
String outputPath = args[1];

prependStyleNames(inputPath, outputPath);

} catch (Exception e) {
e.printStackTrace();
}
}
}
OpenOffice 3.3 on Windows XP
abhishah4444
Posts: 5
Joined: Tue Mar 01, 2011 5:14 pm

Re: Change name of any style in form in odt

Post by abhishah4444 »

OpenOffice 3.3 on Windows XP
User avatar
Lupp
Volunteer
Posts: 3761
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Change name of any style in form in odt

Post by Lupp »

Can you, please, clearly explain what you actually want to achieve without posting lots of code which seems to not work to your satisfaction. (I personally don't speek Java.)
On Windows 10: LibreOffice 25.8.4 and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
Locked