PropertyCtrl.java

package org.docascode.office;

import org.apache.commons.io.FilenameUtils;
import org.docascode.utils.DocAsCodeException;
import org.docascode.utils.ExitCode;
import org.docascode.utils.Utils;
import org.docx4j.docProps.core.dc.elements.SimpleLiteral;
import org.docx4j.docProps.custom.Properties;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.SpreadsheetMLPackage;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;

import java.io.*;
import java.util.HashMap;
import java.util.List;


public class PropertyCtrl {
    public HashMap<String,String> getProperties(File file){
        String extension = FilenameUtils.getExtension(file.getAbsolutePath());
        switch (extension) {
            case "docx":
                return getDOCXProperties(file);
            case "xlsx":
                return listPropertiesXLSX(file);
            default:
                Utils.error("Unhandled file extension: "+extension, ExitCode.RUNTIME_ERROR);
                return new HashMap<String,String>();
        }
    }

    public HashMap<String,String> getDOCXProperties(File file){
        HashMap<String,String> properties = new HashMap<String,String>();
        try {
            WordprocessingMLPackage document = WordprocessingMLPackage.load(file);
            String key;
            String value;
            List<Properties.Property> listCustomProperties = document.getDocPropsCustomPart().getContents().getProperty();
            for (Properties.Property property : listCustomProperties) {
                key = property.getName();
                value = property.getLpwstr();
                properties.put(key,value);
            }
            HashMap<String,List <String>> coreProperties = getCoreProperties(document);
            for (HashMap.Entry<String, List <String>> entry : coreProperties.entrySet()) {
                if (entry.getValue().size()>0){
                    value=entry.getValue().get(0);
                } else {
                    value="";
                }
                properties.put(entry.getKey(),value);
            }
            }
        catch (Docx4JException e) {
            throw new DocAsCodeException("Unable to list properties of file "+file.getAbsolutePath(),e);
        }
        return properties;
    }

    public HashMap<String,List <String>> getCoreProperties(WordprocessingMLPackage document) throws Docx4JException {
        HashMap<String,List <String>> hashMap = new HashMap<>();
        hashMap.put("Title",document.getDocPropsCorePart().getContents().getTitle().getValue().getContent());
        hashMap.put("Description",document.getDocPropsCorePart().getContents().getDescription().getValue().getContent());
        return hashMap;
    }

    public HashMap<String,String> listPropertiesXLSX(File file){
        HashMap<String,String> properties = new HashMap<>();
        try {
            SpreadsheetMLPackage document = SpreadsheetMLPackage.load(file);
            String key;
            String value;
            List<Properties.Property> listCustomProperties = document.getDocPropsCustomPart().getContents().getProperty();
            for (Properties.Property property : listCustomProperties) {
                key = property.getName();
                value = property.getLpwstr();
                properties.put(key,value);
            }
        }
        catch (Docx4JException e) {
            e.printStackTrace();
        }

        return properties;
    }
}