001/* 002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Thierry Delprat 018 */ 019package org.nuxeo.ecm.platform.importer.source; 020 021import java.io.File; 022import java.io.FileInputStream; 023import java.io.IOException; 024import java.io.Serializable; 025import java.util.ArrayList; 026import java.util.Enumeration; 027import java.util.HashMap; 028import java.util.List; 029import java.util.Map; 030import java.util.Properties; 031import java.util.regex.Pattern; 032 033import org.apache.commons.logging.Log; 034import org.apache.commons.logging.LogFactory; 035import org.nuxeo.ecm.core.api.Blobs; 036import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 037import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolder; 038import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolderWithProperties; 039 040public class FileWithNonHeritedIndividalMetaDataSourceNode extends FileSourceNode { 041 042 private static final Log log = LogFactory.getLog(FileWithNonHeritedIndividalMetaDataSourceNode.class); 043 044 public static final String PROPERTY_FILE_SUFIX = ".properties"; 045 046 protected static Pattern numPattern = Pattern.compile("([0-9\\,\\.\\+\\-]+)"); 047 048 public static String LIST_SEPARATOR = "|"; 049 050 public static String REGEXP_LIST_SEPARATOR = "\\|"; 051 052 public static String ARRAY_SEPARATOR = "||"; 053 054 public static String REGEXP_ARRAY_SEPARATOR = "\\|\\|"; 055 056 public FileWithNonHeritedIndividalMetaDataSourceNode(File file) { 057 super(file); 058 } 059 060 public FileWithNonHeritedIndividalMetaDataSourceNode(String path) { 061 super(path); 062 } 063 064 @Override 065 public List<SourceNode> getChildren() { 066 List<SourceNode> children = new ArrayList<SourceNode>(); 067 File[] listFiles = file.listFiles(); 068 for (File child : listFiles) { 069 if (isPropertyFile(child)) { 070 // skip 071 } else { 072 children.add(new FileWithNonHeritedIndividalMetaDataSourceNode(child)); 073 } 074 } 075 return children; 076 } 077 078 protected boolean isPropertyFile(File file) { 079 return (file.getName().contains(PROPERTY_FILE_SUFIX)); 080 } 081 082 @Override 083 public BlobHolder getBlobHolder() throws IOException { 084 BlobHolder bh = null; 085 String metadataFilename = file.getParent() + File.separator + getFileNameNoExt(file) + PROPERTY_FILE_SUFIX; 086 File metadataFile = new File(metadataFilename); 087 if (metadataFile.exists()) { 088 bh = new SimpleBlobHolderWithProperties(Blobs.createBlob(file), loadPropertyFile(metadataFile)); 089 } else { 090 bh = new SimpleBlobHolder(Blobs.createBlob(file)); 091 } 092 return bh; 093 } 094 095 protected Map<String, Serializable> loadPropertyFile(File propertyFile) { 096 Properties mdProperties = new Properties(); 097 Map<String, Serializable> map = new HashMap<String, Serializable>(); 098 099 try { 100 mdProperties.load(new FileInputStream(propertyFile)); 101 Enumeration<?> names = mdProperties.propertyNames(); 102 while (names.hasMoreElements()) { 103 String name = (String) names.nextElement(); 104 map.put(name, parseFromString(mdProperties.getProperty(name))); 105 } 106 } catch (IOException e) { 107 log.error("Unable to read property file " + propertyFile, e); 108 } 109 return map; 110 } 111 112 protected Serializable parseFromString(String value) { 113 114 Serializable prop = value; 115 if (value.contains(ARRAY_SEPARATOR)) { 116 prop = value.split(REGEXP_ARRAY_SEPARATOR); 117 } else if (value.contains(LIST_SEPARATOR)) { 118 List<Serializable> lstprop = new ArrayList<Serializable>(); 119 String[] parts = value.split(REGEXP_LIST_SEPARATOR); 120 for (String part : parts) { 121 lstprop.add(parseFromString(part)); 122 } 123 prop = (Serializable) lstprop; 124 } 125 return prop; 126 } 127}