001/* 002 * (C) Copyright 2006-2007 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 * Nuxeo - initial API and implementation 018 * 019 * $Id$ 020 */ 021package org.nuxeo.ecm.platform.scanimporter.processor; 022 023import java.io.File; 024import java.io.FileFilter; 025import java.io.IOException; 026import java.io.Serializable; 027import java.util.ArrayList; 028import java.util.List; 029import java.util.Map; 030 031import org.apache.commons.logging.Log; 032import org.apache.commons.logging.LogFactory; 033import org.nuxeo.ecm.core.api.Blob; 034import org.nuxeo.ecm.core.api.Blobs; 035import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 036import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolder; 037import org.nuxeo.ecm.platform.importer.source.FileSourceNode; 038import org.nuxeo.ecm.platform.importer.source.SourceNode; 039import org.nuxeo.ecm.platform.scanimporter.service.ScanFileBlobHolder; 040import org.nuxeo.ecm.platform.scanimporter.service.ScannedFileMapperService; 041import org.nuxeo.runtime.api.Framework; 042 043/** 044 * Custom implementation of {@link SourceNode}. Uses XML descriptors to get meta-data and binary files location. 045 * 046 * @author Thierry Delprat 047 */ 048public class ScanedFileSourceNode extends FileSourceNode { 049 050 private static final Log log = LogFactory.getLog(ScanedFileSourceNode.class); 051 052 protected Map<String, Serializable> properties; 053 054 protected ScanFileBlobHolder bh; 055 056 protected static boolean useXMLMapping = true; 057 058 public ScanedFileSourceNode(File file) { 059 super(file); 060 } 061 062 public ScanedFileSourceNode(File file, ScanFileBlobHolder bh) { 063 super(file); 064 this.bh = bh; 065 } 066 067 public ScanedFileSourceNode(File file, Map<String, Serializable> properties) { 068 super(file); 069 this.properties = properties; 070 } 071 072 @Override 073 public BlobHolder getBlobHolder() throws IOException { 074 if (bh == null) { 075 return new SimpleBlobHolder(Blobs.createBlob(file)); 076 } else { 077 return bh; 078 } 079 } 080 081 @Override 082 public List<SourceNode> getChildren() throws IOException { 083 List<SourceNode> children = new ArrayList<>(); 084 085 ScannedFileMapperService sfms = Framework.getService(ScannedFileMapperService.class); 086 for (File child : file.listFiles()) { 087 if (child.getName().endsWith(".xml") && useXMLMapping) { 088 try { 089 090 ScanFileBlobHolder bh = sfms.parseMetaData(child); 091 092 if (bh != null) { 093 children.add(new ScanedFileSourceNode(child, bh)); 094 } else { 095 log.error(child.getAbsolutePath() + " can not be parsed "); 096 } 097 } catch (IOException e) { 098 log.error("Error during properties parsing", e); 099 } 100 } else if (child.isDirectory()) { 101 if (child.listFiles(new DirectoryFilter()).length > 0) { 102 children.add(new ScanedFileSourceNode(child)); 103 } else { 104 if (useXMLMapping) { 105 if (child.list(new XmlMetaDataFileFilter()).length > 0) { 106 children.add(new ScanedFileSourceNode(child)); 107 } 108 } else { 109 if (child.list().length > 0) { 110 children.add(new ScanedFileSourceNode(child)); 111 } 112 } 113 } 114 } else if (!useXMLMapping) { 115 ScanFileBlobHolder bh = new ScanFileBlobHolder(Blobs.createBlob(child), sfms.getTargetLeafType()); 116 children.add(new ScanedFileSourceNode(child, bh)); 117 } 118 } 119 return children; 120 } 121 122 @Override 123 public String getName() { 124 if (bh != null) { 125 Blob blob = bh.getBlob(); 126 if (blob != null && blob.getFilename() != null) { 127 return blob.getFilename(); 128 } 129 } 130 return file.getName(); 131 } 132 133 public String getDescriptorFileName() { 134 return file.getAbsolutePath(); 135 } 136 137 private class DirectoryFilter implements FileFilter { 138 139 @Override 140 public boolean accept(File file) { 141 return file.isDirectory(); 142 } 143 144 } 145}