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