001package org.nuxeo.ecm.platform.importer.source;
002
003import java.io.File;
004import java.io.FileInputStream;
005import java.io.IOException;
006import java.io.Serializable;
007import java.util.ArrayList;
008import java.util.Enumeration;
009import java.util.HashMap;
010import java.util.List;
011import java.util.Map;
012import java.util.Properties;
013import java.util.regex.Pattern;
014
015import org.apache.commons.logging.Log;
016import org.apache.commons.logging.LogFactory;
017import org.nuxeo.ecm.core.api.Blobs;
018import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
019import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolder;
020import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolderWithProperties;
021
022public class FileWithNonHeritedIndividalMetaDataSourceNode extends FileSourceNode {
023
024    private static final Log log = LogFactory.getLog(FileWithNonHeritedIndividalMetaDataSourceNode.class);
025
026    public static final String PROPERTY_FILE_SUFIX = ".properties";
027
028    protected static Pattern numPattern = Pattern.compile("([0-9\\,\\.\\+\\-]+)");
029
030    public static String LIST_SEPARATOR = "|";
031
032    public static String REGEXP_LIST_SEPARATOR = "\\|";
033
034    public static String ARRAY_SEPARATOR = "||";
035
036    public static String REGEXP_ARRAY_SEPARATOR = "\\|\\|";
037
038    public FileWithNonHeritedIndividalMetaDataSourceNode(File file) {
039        super(file);
040    }
041
042    public FileWithNonHeritedIndividalMetaDataSourceNode(String path) {
043        super(path);
044    }
045
046    @Override
047    public List<SourceNode> getChildren() {
048        List<SourceNode> children = new ArrayList<SourceNode>();
049        File[] listFiles = file.listFiles();
050        for (File child : listFiles) {
051            if (isPropertyFile(child)) {
052                // skip
053            } else {
054                children.add(new FileWithNonHeritedIndividalMetaDataSourceNode(child));
055            }
056        }
057        return children;
058    }
059
060    protected boolean isPropertyFile(File file) {
061        return (file.getName().contains(PROPERTY_FILE_SUFIX));
062    }
063
064    @Override
065    public BlobHolder getBlobHolder() throws IOException {
066        BlobHolder bh = null;
067        String metadataFilename = file.getParent() + File.separator + getFileNameNoExt(file) + PROPERTY_FILE_SUFIX;
068        File metadataFile = new File(metadataFilename);
069        if (metadataFile.exists()) {
070            bh = new SimpleBlobHolderWithProperties(Blobs.createBlob(file), loadPropertyFile(metadataFile));
071        } else {
072            bh = new SimpleBlobHolder(Blobs.createBlob(file));
073        }
074        return bh;
075    }
076
077    protected Map<String, Serializable> loadPropertyFile(File propertyFile) {
078        Properties mdProperties = new Properties();
079        Map<String, Serializable> map = new HashMap<String, Serializable>();
080
081        try {
082            mdProperties.load(new FileInputStream(propertyFile));
083            Enumeration<?> names = mdProperties.propertyNames();
084            while (names.hasMoreElements()) {
085                String name = (String) names.nextElement();
086                map.put(name, parseFromString(mdProperties.getProperty(name)));
087            }
088        } catch (IOException e) {
089            log.error("Unable to read property file " + propertyFile, e);
090        }
091        return map;
092    }
093
094    protected Serializable parseFromString(String value) {
095
096        Serializable prop = value;
097        if (value.contains(ARRAY_SEPARATOR)) {
098            prop = value.split(REGEXP_ARRAY_SEPARATOR);
099        } else if (value.contains(LIST_SEPARATOR)) {
100            List<Serializable> lstprop = new ArrayList<Serializable>();
101            String[] parts = value.split(REGEXP_LIST_SEPARATOR);
102            for (String part : parts) {
103                lstprop.add(parseFromString(part));
104            }
105            prop = (Serializable) lstprop;
106        }
107        return prop;
108    }
109}