001/* 002 * Copyright 2007-2021 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.ant; 017 018import java.io.File; 019import java.io.FileNotFoundException; 020import java.io.IOException; 021import java.util.ArrayList; 022import java.util.Collection; 023import java.util.Iterator; 024 025import org.apache.tools.ant.types.PatternSet; 026import org.vafer.jdeb.DataConsumer; 027import org.vafer.jdeb.DataProducer; 028import org.vafer.jdeb.producers.DataProducerArchive; 029import org.vafer.jdeb.producers.DataProducerDirectory; 030import org.vafer.jdeb.producers.DataProducerFile; 031 032import static org.vafer.jdeb.ant.MissingSourceBehavior.*; 033 034/** 035 * Ant "data" element acting as a factory for DataProducers. 036 * So far Archive and Directory producers are supported. 037 * Both support the usual ant pattern set matching. 038 */ 039public final class Data extends PatternSet implements DataProducer { 040 041 private final Collection<Mapper> mapperWrapper = new ArrayList<>(); 042 043 private File src; 044 045 private String type; 046 047 private Boolean conffile; 048 049 private String destinationName; 050 051 private MissingSourceBehavior missingSrc = FAIL; 052 053 public void setSrc(File src) { 054 this.src = src; 055 } 056 057 public String getType() { 058 return type; 059 } 060 061 public void setType(String type) { 062 this.type = type; 063 } 064 065 public void setConffile(Boolean conffile) { 066 this.conffile = conffile; 067 } 068 069 public Boolean getConffile() { 070 return this.conffile; 071 } 072 073 public void setDst(String destinationName) { 074 this.destinationName = destinationName; 075 } 076 077 public void addMapper(Mapper mapper) { 078 mapperWrapper.add(mapper); 079 } 080 081 082 public void setMissingSrc( String missingSrc ) { 083 this.missingSrc = MissingSourceBehavior.valueOf(missingSrc.trim().toUpperCase()); 084 } 085 086 public void produce( final DataConsumer pReceiver ) throws IOException { 087 088 if (src == null || !src.exists()) { 089 if (missingSrc == IGNORE) { 090 return; 091 } else { 092 throw new FileNotFoundException("Data source not found : " + src); 093 } 094 } 095 096 org.vafer.jdeb.mapping.Mapper[] mappers = new org.vafer.jdeb.mapping.Mapper[mapperWrapper.size()]; 097 final Iterator<Mapper> it = mapperWrapper.iterator(); 098 for (int i = 0; i < mappers.length; i++) { 099 mappers[i] = it.next().createMapper(); 100 } 101 102 if ("file".equalsIgnoreCase(type)) { 103 new DataProducerFile( 104 src, 105 destinationName, 106 getIncludePatterns(getProject()), 107 getExcludePatterns(getProject()), 108 mappers 109 ).produce(pReceiver); 110 111 } else if ("archive".equalsIgnoreCase(type)) { 112 new DataProducerArchive( 113 src, 114 getIncludePatterns(getProject()), 115 getExcludePatterns(getProject()), 116 mappers 117 ).produce(pReceiver); 118 119 } else if ("directory".equalsIgnoreCase(type)) { 120 new DataProducerDirectory( 121 src, 122 getIncludePatterns(getProject()), 123 getExcludePatterns(getProject()), 124 mappers 125 ).produce(pReceiver); 126 } 127 } 128}