001 /*
002 * Copyright 2010 The Apache Software Foundation.
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 package org.vafer.jdeb.maven;
017
018 import java.io.File;
019 import java.io.FileNotFoundException;
020 import java.io.IOException;
021 import java.util.ArrayList;
022
023 import java.util.List;
024 import java.util.StringTokenizer;
025 import org.vafer.jdeb.DataConsumer;
026 import org.vafer.jdeb.DataProducer;
027 import org.vafer.jdeb.producers.DataProducerArchive;
028 import org.vafer.jdeb.producers.DataProducerDirectory;
029 import org.vafer.jdeb.producers.DataProducerFile;
030
031 /**
032 * Maven "data" elment acting as a factory for DataProducers. So far Archive and
033 * Directory producers are supported. Both support the usual ant pattern set
034 * matching.
035 *
036 * @author Bryan Sant <bryan.sant@gmail.com>
037 */
038 public final class Data implements DataProducer {
039
040 private File src;
041
042 /**
043 * @parameter expression="${src}"
044 * @required
045 */
046 public void setSrc(File src) {
047 this.src = src;
048 }
049
050
051 private String type;
052
053 /**
054 * @parameter expression="${type}"
055 */
056 public void setType(String type) {
057 this.type = type;
058 }
059
060
061 /**
062 * @parameter expression="${includes}" alias="includes"
063 */
064 public void setIncludes(String includes) {
065 includePatterns = splitPatterns(includes);
066 }
067
068 private String[] includePatterns;
069
070 /**
071 * @parameter expression="${excludes}" alias="excludes"
072 */
073 public void setExcludes(String excludes) {
074 excludePatterns = splitPatterns(excludes);
075 }
076
077 private String[] excludePatterns;
078 /**
079 * @parameter expression="${mapper}"
080 */
081 private Mapper mapper;
082
083 public String[] splitPatterns(String patterns) {
084 String[] result = null;
085 if (patterns != null && patterns.length() > 0) {
086 List tokens = new ArrayList();
087 StringTokenizer tok = new StringTokenizer(patterns, ", ", false);
088 while (tok.hasMoreTokens()) {
089 tokens.add(tok.nextToken());
090 }
091 result = (String[]) tokens.toArray(new String[tokens.size()]);
092 }
093 return result;
094 }
095
096 public void produce(final DataConsumer pReceiver) throws IOException {
097 if (!src.exists()) {
098 throw new FileNotFoundException("Data source not found : " + src);
099 }
100
101 org.vafer.jdeb.mapping.Mapper[] mappers = null;
102 if (mapper != null) {
103 mappers = new org.vafer.jdeb.mapping.Mapper[] { mapper.createMapper() };
104 }
105
106 if ("file".equalsIgnoreCase(type)) {
107 new DataProducerFile(src, includePatterns, excludePatterns, mappers).produce(pReceiver);
108 return;
109 }
110
111 if ("archive".equalsIgnoreCase(type)) {
112 new DataProducerArchive(src, includePatterns, excludePatterns, mappers).produce(pReceiver);
113 return;
114 }
115
116 if ("directory".equalsIgnoreCase(type)) {
117 new DataProducerDirectory(src, includePatterns, excludePatterns, mappers).produce(pReceiver);
118 return;
119 }
120
121 // @deprecated
122
123 if (src.isFile()) {
124 new DataProducerArchive(src, includePatterns, excludePatterns, mappers).produce(pReceiver);
125 } else {
126 new DataProducerDirectory(src, includePatterns, excludePatterns, mappers).produce(pReceiver);
127 }
128 }
129 }