KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > util > ant > JRubySerialize


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2002 Jan Arne Petersen <jpetersen@uni-bonn.de>
15  * Copyright (C) 2004-2005 Charles O Nutter <headius@headius.com>
16  * Copyright (C) 2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
17  * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
18  *
19  * Alternatively, the contents of this file may be used under the terms of
20  * either of the GNU General Public License Version 2 or later (the "GPL"),
21  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
22  * in which case the provisions of the GPL or the LGPL are applicable instead
23  * of those above. If you wish to allow use of your version of this file only
24  * under the terms of either the GPL or the LGPL, and not to allow others to
25  * use your version of this file under the terms of the CPL, indicate your
26  * decision by deleting the provisions above and replace them with the notice
27  * and other provisions required by the GPL or the LGPL. If you do not delete
28  * the provisions above, a recipient may use your version of this file under
29  * the terms of any one of the CPL, the GPL or the LGPL.
30  ***** END LICENSE BLOCK *****/

31 package org.jruby.util.ant;
32
33 import java.io.File JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Map JavaDoc;
39
40 import org.apache.tools.ant.BuildException;
41 import org.apache.tools.ant.DirectoryScanner;
42 import org.apache.tools.ant.Task;
43 import org.apache.tools.ant.types.FileSet;
44 import org.apache.tools.ant.types.Mapper;
45 import org.apache.tools.ant.util.FileNameMapper;
46 import org.apache.tools.ant.util.GlobPatternMapper;
47 import org.apache.tools.ant.util.SourceFileScanner;
48 import org.jruby.main.ASTSerializer;
49
50 /**
51  *
52  * @author jpetersen
53  */

54 public class JRubySerialize extends Task {
55     private File JavaDoc destdir = null;
56     private boolean verbose = false;
57
58     private List JavaDoc fileSets = new ArrayList JavaDoc();
59     private Mapper mapperElement = null;
60
61     public void execute() throws BuildException {
62         Map JavaDoc fileMap = new HashMap JavaDoc();
63
64         FileNameMapper mapper = null;
65         if (mapperElement != null) {
66             mapper = mapperElement.getImplementation();
67         } else {
68             mapper = new GlobPatternMapper();
69             mapper.setFrom("*.rb");
70             mapper.setTo("*.rb.ast.ser");
71         }
72
73         SourceFileScanner sfs = new SourceFileScanner(this);
74
75         for (int i = 0, size = fileSets.size(); i < size; i++) {
76             FileSet fs = (FileSet) fileSets.get(i);
77             DirectoryScanner ds = fs.getDirectoryScanner(getProject());
78
79             File JavaDoc dir = fs.getDir(getProject());
80             String JavaDoc[] files = ds.getIncludedFiles();
81
82             files = sfs.restrict(files, dir, destdir, mapper);
83
84             for (int j = 0; j < files.length; j++) {
85                 File JavaDoc src = new File JavaDoc(dir, files[j]);
86                 File JavaDoc dest = new File JavaDoc(destdir, mapper.mapFileName(files[j])[0]);
87                 fileMap.put(src, dest);
88             }
89         }
90
91         if (fileMap.size() > 0) {
92             log(
93                 "Serializing "
94                     + fileMap.size()
95                     + " file"
96                     + (fileMap.size() == 1 ? "" : "s")
97                     + " to "
98                     + destdir.getAbsolutePath());
99
100             Iterator JavaDoc iter = fileMap.entrySet().iterator();
101             while (iter.hasNext()) {
102                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
103                 try {
104                     ((File JavaDoc) entry.getValue()).getParentFile().mkdirs();
105                     if (verbose) System.out.println(entry.getKey());
106                     ASTSerializer.serialize((File JavaDoc) entry.getKey(), (File JavaDoc) entry.getValue());
107                 } catch (Exception JavaDoc e) {
108                     e.printStackTrace();
109                 }
110             }
111         }
112     }
113
114     public void setDestdir(File JavaDoc destdir) {
115         this.destdir = destdir;
116     }
117
118     /**
119      * Adds a set of files to copy.
120      */

121     public void addFileset(FileSet set) {
122         fileSets.add(set);
123     }
124     
125     public void setVerbose(boolean verbose) {
126         this.verbose = verbose;
127     }
128
129     /**
130      * Defines the mapper to map source to destination files.
131      */

132     public Mapper createMapper() throws BuildException {
133         if (mapperElement != null) {
134             throw new BuildException("Cannot define more than one mapper", getLocation());
135         }
136         mapperElement = new Mapper(getProject());
137         return mapperElement;
138     }
139 }
140
Popular Tags