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 org.apache.commons.compress.archivers.tar.TarArchiveEntry; 019import org.apache.tools.ant.types.selectors.SelectorUtils; 020import org.vafer.jdeb.DataConsumer; 021import org.vafer.jdeb.DataProducer; 022import org.vafer.jdeb.mapping.Mapper; 023 024import java.io.File; 025import java.io.FileInputStream; 026import java.io.IOException; 027 028/** 029 * Base Producer class providing including/excluding. 030 */ 031public abstract class AbstractDataProducer implements DataProducer { 032 033 private final String[] includes; 034 private final String[] excludes; 035 private final Mapper[] mappers; 036 037 038 public AbstractDataProducer( final String[] pIncludes, final String[] pExcludes, final Mapper[] pMapper ) { 039 excludes = (pExcludes != null) ? pExcludes : new String[0]; 040 includes = (pIncludes != null) ? pIncludes : new String[] { "**" }; 041 mappers = (pMapper != null) ? pMapper : new Mapper[0]; 042 } 043 044 public boolean isIncluded( final String pName ) { 045 if (!isIncluded(pName, includes)) { 046 return false; 047 } 048 if (isExcluded(pName, excludes)) { 049 return false; 050 } 051 return true; 052 } 053 054 private boolean isIncluded( String name, String[] includes ) { 055 for (String include : includes) { 056 if (SelectorUtils.matchPath(include, name)) { 057 return true; 058 } 059 } 060 return false; 061 } 062 063 064 private boolean isExcluded( String name, String[] excludes ) { 065 for (String exclude : excludes) { 066 if (SelectorUtils.matchPath(exclude, name)) { 067 return true; 068 } 069 } 070 return false; 071 } 072 073 public void produceDir( final DataConsumer consumer, 074 final String dirName ) throws IOException { 075 final String name = dirName.endsWith("/") ? dirName : dirName + "/"; 076 TarArchiveEntry entry = Producers.defaultDirEntryWithName(name); 077 entry = map(entry); 078 entry.setSize(0); 079 Producers.produceDirEntry(consumer, entry); 080 } 081 082 public void produceFile( final DataConsumer consumer, 083 final File file, 084 final String fileName ) throws IOException { 085 TarArchiveEntry fileEntry = Producers.defaultFileEntryWithName(fileName); 086 fileEntry.setSize(file.length()); 087 fileEntry = map(fileEntry); 088 Producers.produceInputStreamWithEntry(consumer, new FileInputStream(file), fileEntry); 089 } 090 091 public TarArchiveEntry map( final TarArchiveEntry pEntry ) { 092 093 TarArchiveEntry entry = pEntry; 094 095 for (Mapper mapper : mappers) { 096 entry = mapper.map(entry); 097 } 098 099 return entry; 100 } 101}