Hello,

I was facing the problem of putting image (static part) in header, wich
resulted in a corrupted file.
So when I looked at the class
org.topcased.gendoc2.services.docx.DOCXAdditionalResourceService, I saw
some braces were missing around the else statement of function
"addMappingFiles". When adding these braces, my result file was no longer
corrupted.
 This is now this function.

private void addMappingFiles(File unzipLoc, String relsLocation) throws
AdditionalResourceException {
        ILogger logger =
GendocServices.getDefault().getService(ILogger.class);

        // Get Word directory
        File wordDir = new File(unzipLoc.getAbsolutePath() + SEPARATOR +
"word");
        if (!wordDir.isDirectory()) {
            throw new AdditionalResourceException("Invalid mapping files
directory :" + wordDir);
        }

        String[] fileNames = wordDir.list(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                name = name.toLowerCase();
                return name.endsWith(".xml") && (name.contains("header") ||
name.contains("footer"));
            }
        });
        try {
            // Add new relationships to all headers and footers
            for (String fileName : fileNames) {
                File rels = new File(relsLocation + fileName + ".rels");
                StringBuffer relsContent;
                if (relationShipsToAdd != null) {
                    if (rels.exists()) {
                        relsContent = new
StringBuffer(readFileContent(rels));
                        Pattern p = Pattern.compile("</Relationships>");
                        Matcher m = p.matcher(relsContent);
                        if (m.find()) {
                            int index = m.start();
                            relsContent.insert(index,
relationShipsToAdd.toString());
                            writeFileContent(rels, relsContent);
                        }
                    } else {
                        // handle case when header or footer rels file do
not
                        // exist : create it and insert relationships
                        rels.createNewFile();
                        relsContent = new StringBuffer("<?xml
version=\"1.0\" encoding=\"windows-1250\"?> <Relationships xmlns=\"
http://schemas.openxmlformats.org/package/2006/relationships\";>");
                        relsContent.append(relationShipsToAdd);
                        relsContent.append("</Relationships>");
                        writeFileContent(rels, relsContent);
                    }
                }
            }

        } catch (IOException e) {
            logger.log("Mapping files for headers and footers are not
copied properly. Some images in headers and footers can be missing.",
IStatus.INFO);
        }

    }


-- 
Jean Claude Twagiramungu
_______________________________________________
Topcased-users mailing list
[email protected]
http://lists.gforge.enseeiht.fr/cgi-bin/mailman/listinfo/topcased-users

Reply via email to