001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.ivy.ant;
019:
020: import java.io.File;
021: import java.io.FileNotFoundException;
022: import java.io.FileOutputStream;
023: import java.io.PrintWriter;
024: import java.io.StringWriter;
025: import java.io.Writer;
026: import java.util.ArrayList;
027: import java.util.Arrays;
028: import java.util.HashMap;
029: import java.util.HashSet;
030: import java.util.Iterator;
031: import java.util.List;
032: import java.util.Map;
033: import java.util.Set;
034:
035: import org.apache.ivy.core.module.id.ModuleRevisionId;
036: import org.apache.tools.ant.BuildException;
037: import org.apache.tools.ant.Project;
038: import org.apache.tools.ant.Task;
039: import org.apache.tools.ant.filters.LineContainsRegExp;
040: import org.apache.tools.ant.filters.TokenFilter;
041: import org.apache.tools.ant.taskdefs.Concat;
042: import org.apache.tools.ant.types.FileSet;
043: import org.apache.tools.ant.types.FilterChain;
044: import org.apache.tools.ant.types.RegularExpression;
045:
046: /**
047: * Extracts imports from a set of java sources and generate corresponding ivy file
048: */
049: public class IvyExtractFromSources extends Task {
050: public static class Ignore {
051: private String packageName;
052:
053: public String getPackage() {
054: return packageName;
055: }
056:
057: public void setPackage(String package1) {
058: packageName = package1;
059: }
060: }
061:
062: private String organisation;
063:
064: private String module;
065:
066: private String revision;
067:
068: private String status;
069:
070: private List ignoredPackaged = new ArrayList(); // List (String package)
071:
072: private Map mapping = new HashMap(); // Map (String package -> ModuleRevisionId)
073:
074: private Concat concat = new Concat();
075:
076: private File to;
077:
078: public void addConfiguredIgnore(Ignore ignore) {
079: ignoredPackaged.add(ignore.getPackage());
080: }
081:
082: public File getTo() {
083: return to;
084: }
085:
086: public void setTo(File to) {
087: this .to = to;
088: }
089:
090: public String getModule() {
091: return module;
092: }
093:
094: public void setModule(String module) {
095: this .module = module;
096: }
097:
098: public String getOrganisation() {
099: return organisation;
100: }
101:
102: public void setOrganisation(String organisation) {
103: this .organisation = organisation;
104: }
105:
106: public String getRevision() {
107: return revision;
108: }
109:
110: public void setRevision(String revision) {
111: this .revision = revision;
112: }
113:
114: public String getStatus() {
115: return status;
116: }
117:
118: public void setStatus(String status) {
119: this .status = status;
120: }
121:
122: public void addConfiguredMapping(PackageMapping mapping) {
123: this .mapping.put(mapping.getPackage(), mapping
124: .getModuleRevisionId());
125: }
126:
127: public void addFileSet(FileSet fileSet) {
128: concat.addFileset(fileSet);
129: }
130:
131: public void execute() throws BuildException {
132: configureConcat();
133: Writer out = new StringWriter();
134: concat.setWriter(out);
135: concat.execute();
136: Set importsSet = new HashSet(Arrays.asList(out.toString()
137: .split("\n")));
138: Set dependencies = new HashSet();
139: for (Iterator iter = importsSet.iterator(); iter.hasNext();) {
140: String pack = ((String) iter.next()).trim();
141: ModuleRevisionId mrid = getMapping(pack);
142: if (mrid != null) {
143: dependencies.add(mrid);
144: }
145: }
146: try {
147: PrintWriter writer = new PrintWriter(new FileOutputStream(
148: to));
149: writer.println("<ivy-module version=\"1.0\">");
150: writer.println("\t<info organisation=\"" + organisation
151: + "\"");
152: writer.println("\t module=\"" + module + "\"");
153: if (revision != null) {
154: writer
155: .println("\t revision=\"" + revision
156: + "\"");
157: }
158: if (status != null) {
159: writer.println("\t status=\"" + status + "\"");
160: } else {
161: writer.println("\t status=\"integration\"");
162: }
163: writer.println("\t/>");
164: if (!dependencies.isEmpty()) {
165: writer.println("\t<dependencies>");
166: for (Iterator iter = dependencies.iterator(); iter
167: .hasNext();) {
168: ModuleRevisionId mrid = (ModuleRevisionId) iter
169: .next();
170: writer.println("\t\t<dependency org=\""
171: + mrid.getOrganisation() + "\" name=\""
172: + mrid.getName() + "\" rev=\""
173: + mrid.getRevision() + "\"/>");
174: }
175: writer.println("\t</dependencies>");
176: }
177: writer.println("</ivy-module>");
178: writer.close();
179: log(dependencies.size() + " dependencies put in " + to);
180: } catch (FileNotFoundException e) {
181: throw new BuildException("impossible to create file " + to
182: + ": " + e, e);
183: }
184: }
185:
186: /**
187: * @param pack
188: * @return
189: */
190: private ModuleRevisionId getMapping(String pack) {
191: String askedPack = pack;
192: ModuleRevisionId ret = null;
193: while (ret == null && pack.length() > 0) {
194: if (ignoredPackaged.contains(pack)) {
195: return null;
196: }
197: ret = (ModuleRevisionId) mapping.get(pack);
198: int lastDotIndex = pack.lastIndexOf('.');
199: if (lastDotIndex != -1) {
200: pack = pack.substring(0, lastDotIndex);
201: } else {
202: break;
203: }
204: }
205: if (ret == null) {
206: log("no mapping found for " + askedPack,
207: Project.MSG_VERBOSE);
208: }
209: return ret;
210: }
211:
212: private void configureConcat() {
213: concat.setProject(getProject());
214: concat.setTaskName(getTaskName());
215: FilterChain filterChain = new FilterChain();
216: LineContainsRegExp lcre = new LineContainsRegExp();
217: RegularExpression regexp = new RegularExpression();
218: regexp.setPattern("^import .+;");
219: lcre.addConfiguredRegexp(regexp);
220: filterChain.add(lcre);
221: TokenFilter tf = new TokenFilter();
222: TokenFilter.ReplaceRegex rre = new TokenFilter.ReplaceRegex();
223: rre.setPattern("import (.+);.*");
224: rre.setReplace("\\1");
225: tf.add(rre);
226: filterChain.add(tf);
227: concat.addFilterChain(filterChain);
228: }
229: }
|