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.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<Mapper>(); 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 MissingSourceBehavior value = MissingSourceBehavior.valueOf(missingSrc.trim().toUpperCase()); 084 if (value == null) { 085 throw new IllegalArgumentException("Unknown " + MissingSourceBehavior.class.getSimpleName() + ": " + missingSrc); 086 } 087 this.missingSrc = value; 088 } 089 090 public void produce( final DataConsumer pReceiver ) throws IOException { 091 092 if (src == null || !src.exists()) { 093 if (missingSrc == IGNORE) { 094 return; 095 } else { 096 throw new FileNotFoundException("Data source not found : " + src); 097 } 098 } 099 100 org.vafer.jdeb.mapping.Mapper[] mappers = new org.vafer.jdeb.mapping.Mapper[mapperWrapper.size()]; 101 final Iterator<Mapper> it = mapperWrapper.iterator(); 102 for (int i = 0; i < mappers.length; i++) { 103 mappers[i] = it.next().createMapper(); 104 } 105 106 if ("file".equalsIgnoreCase(type)) { 107 new DataProducerFile( 108 src, 109 destinationName, 110 getIncludePatterns(getProject()), 111 getExcludePatterns(getProject()), 112 mappers 113 ).produce(pReceiver); 114 115 } else if ("archive".equalsIgnoreCase(type)) { 116 new DataProducerArchive( 117 src, 118 getIncludePatterns(getProject()), 119 getExcludePatterns(getProject()), 120 mappers 121 ).produce(pReceiver); 122 123 } else if ("directory".equalsIgnoreCase(type)) { 124 new DataProducerDirectory( 125 src, 126 getIncludePatterns(getProject()), 127 getExcludePatterns(getProject()), 128 mappers 129 ).produce(pReceiver); 130 } 131 } 132}