001/* 002 * Copyright 2007-2018 The jdeb developers. 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 */ 016package org.vafer.jdeb.producers; 017 018import java.io.File; 019import java.io.IOException; 020 021import org.apache.tools.ant.DirectoryScanner; 022import org.vafer.jdeb.DataConsumer; 023import org.vafer.jdeb.DataProducer; 024import org.vafer.jdeb.mapping.Mapper; 025import org.vafer.jdeb.utils.Utils; 026 027/** 028 * DataProducer iterating over a directory. 029 * For cross-platform permissions and ownerships you probably want to use a Mapper, too. 030 */ 031public final class DataProducerDirectory extends AbstractDataProducer implements DataProducer { 032 033 private final DirectoryScanner scanner = new DirectoryScanner(); 034 035 public DataProducerDirectory( final File pDir, final String[] pIncludes, final String[] pExcludes, final Mapper[] pMappers ) { 036 super(pIncludes, pExcludes, pMappers); 037 scanner.setBasedir(pDir); 038 scanner.setIncludes(pIncludes); 039 scanner.setExcludes(pExcludes); 040 scanner.setCaseSensitive(true); 041 scanner.setFollowSymlinks(true); 042 } 043 044 public void produce( final DataConsumer pReceiver ) throws IOException { 045 046 scanner.scan(); 047 048 final File baseDir = scanner.getBasedir(); 049 050 for (String dir : scanner.getIncludedDirectories()) { 051 final File file = new File(baseDir, dir); 052 String dirname = getFilename(baseDir, file); 053 054 if ("".equals(dirname)) { 055 continue; 056 } 057 058 if ('/' != File.separatorChar) { 059 dirname = dirname.replace(File.separatorChar, '/'); 060 } 061 062 if (!isIncluded(dirname)) { 063 continue; 064 } 065 066 if (!dirname.endsWith("/")) { 067 dirname += "/"; 068 } 069 070 produceDir(pReceiver, dirname); 071 } 072 073 074 for (String f : scanner.getIncludedFiles()) { 075 final File file = new File(baseDir, f); 076 String filename = getFilename(baseDir, file); 077 078 if ('/' != File.separatorChar) { 079 filename = filename.replace(File.separatorChar, '/'); 080 } 081 082 if (!isIncluded(filename)) { 083 continue; 084 } 085 086 produceFile(pReceiver, file, filename); 087 } 088 } 089 090 private String getFilename( File root, File file ) { 091 092 final String relativeFilename = file.getAbsolutePath().substring(root.getAbsolutePath().length()); 093 094 return Utils.stripLeadingSlash(relativeFilename); 095 } 096 097}