001/* 002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Mariana Cedica 016 */ 017package org.nuxeo.ecm.platform.importer.source; 018 019import java.io.File; 020import java.io.IOException; 021import java.io.Serializable; 022import java.util.ArrayList; 023import java.util.List; 024import java.util.Map; 025 026import org.apache.commons.logging.Log; 027import org.apache.commons.logging.LogFactory; 028import org.nuxeo.ecm.core.api.Blobs; 029import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 030import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolderWithProperties; 031import org.nuxeo.ecm.platform.importer.properties.IndividualMetadataCollector; 032 033/** 034 * {@link SourceNode} implementation that manages Meta-data from properties files per document 035 * <p> 036 * The properties are mapped by the collector using as key the path of the file/folder to import. 037 */ 038public class FileWithIndividualMetadasSourceNode extends FileSourceNode { 039 040 private static final Log log = LogFactory.getLog(FileWithIndividualMetadasSourceNode.class); 041 042 public static final String PROPERTY_FILE_SUFIX = ".properties"; 043 044 protected static IndividualMetadataCollector collector = new IndividualMetadataCollector(); 045 046 public FileWithIndividualMetadasSourceNode(File file) { 047 super(file); 048 } 049 050 public FileWithIndividualMetadasSourceNode(String path) { 051 super(path); 052 } 053 054 @Override 055 public BlobHolder getBlobHolder() throws IOException { 056 BlobHolder bh = new SimpleBlobHolderWithProperties(Blobs.createBlob(file), collector.getProperties(file)); 057 return bh; 058 } 059 060 public Map<String, Serializable> getMetadataForFolderishNode() { 061 return collector.getProperties(file); 062 } 063 064 @Override 065 public List<SourceNode> getChildren() { 066 List<SourceNode> children = new ArrayList<SourceNode>(); 067 File[] listFiles = file.listFiles(); 068 log.trace("Element " + this.getSourcePath() + " has children" + listFiles.toString()); 069 for (File child : listFiles) { 070 if (isPropertyFile(child)) { 071 try { 072 String propKey = getPropertyTargetKey(child, listFiles); 073 if (propKey != null) { 074 collector.addPropertyFile(child, propKey); 075 } 076 } catch (IOException e) { 077 log.error("Error during properties parsing", e); 078 } 079 } else { 080 children.add(new FileWithIndividualMetadasSourceNode(child)); 081 } 082 } 083 return children; 084 } 085 086 protected boolean isPropertyFile(File file) { 087 return (file.getName().contains(PROPERTY_FILE_SUFIX)); 088 } 089 090 protected String getPropertyTargetKey(File propFile, File[] listFiles) { 091 String fileName = propFile.getName(); 092 String absFileName = fileName.substring(0, fileName.lastIndexOf(PROPERTY_FILE_SUFIX)); 093 for (File file2 : listFiles) { 094 if (file2.isDirectory() && file2.getName().equals(absFileName)) { 095 return file2.getAbsolutePath(); 096 } 097 } 098 for (File file2 : listFiles) { 099 if (file2.isFile() && !isPropertyFile(file2) && getFileNameNoExt(file2).equals(absFileName)) { 100 return file2.getAbsolutePath(); 101 } 102 } 103 return null; 104 } 105 106 public static String getFileNameNoExt(File file) { 107 String name = file.getName(); 108 int p = name.lastIndexOf('.'); 109 if (p == -1) { 110 return name; 111 } 112 return name.substring(0, p); 113 } 114}