KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > test > tools > ant > TestSyncMLBaseTask


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package sync4j.test.tools.ant;
19
20 import java.io.File JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.TreeMap JavaDoc;
25
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.taskdefs.Ant;
28 import org.apache.tools.ant.types.PatternSet;
29
30
31 /**
32  * This is the base class for Ant task that executes the protocol level tests.
33  *
34  * @author Stefano Nichele
35  * @version $Id: TestSyncMLBaseTask.java,v 1.2 2005/07/05 16:54:09 luigiafassina Exp $
36  */

37 public abstract class TestSyncMLBaseTask extends org.apache.tools.ant.Task {
38
39     // -------------------------------------------------------------- Properties
40

41     protected String JavaDoc url = null;
42     public void setUrl(String JavaDoc url) {
43         this.url = url;
44     }
45
46     protected String JavaDoc test = null;
47     public void setTest(String JavaDoc test) {
48         this.test = test;
49     }
50     
51     protected String JavaDoc basedir = null;
52     public void setBasedir(String JavaDoc basedir) {
53         this.basedir = basedir;
54     }
55
56     protected ArrayList JavaDoc xpathPatterns = new ArrayList JavaDoc();
57
58     public void addPatternset(PatternSet set) {
59         xpathPatterns.add(set);
60     }
61
62     // ----------------------------------------------------- Task Implementation
63

64     // ------------------------------------------------------- Protected methods
65

66     protected void validateAttributes() throws BuildException {
67         try {
68             new java.net.URL JavaDoc(url);
69         } catch (Exception JavaDoc e) {
70             new BuildException("Malformed url exception: " + url);
71         }
72     }
73
74     protected Ant loadAntTask(String JavaDoc test) {
75         Ant antTask = null;
76         if ( (new File JavaDoc(basedir + File.separator + test + File.separator + "build.xml").exists()) ) {
77             antTask = (Ant)getProject().createTask("ant");
78             antTask.setAntfile(basedir+ File.separator + test + File.separator + "build.xml");
79             antTask.setDir(new File JavaDoc(basedir, test));
80         }
81         return antTask;
82     }
83
84     protected String JavaDoc[] removeBuildFile(String JavaDoc[] filesList) {
85         List JavaDoc newFilesList = new ArrayList JavaDoc();
86         for (int i=0; i<filesList.length; i++) {
87
88             log("called removeBuildFile ["+i+"]");
89             log("filesList[i]: " + filesList[i]);
90
91             if (!filesList[i].equalsIgnoreCase("build.xml")) {
92                 newFilesList.add(filesList[i]);
93             }
94         }
95         return (String JavaDoc[])newFilesList.toArray(new String JavaDoc[0]);
96     }
97
98     /**
99      * Orders and filters list of files. Valid name file is msgNNNNN.xml where
100      * NNNNN is a positive integer.
101      *
102      * @param files String[]
103      * @return String[]
104      */

105     protected static String JavaDoc[] ordersAndFilterFiles(String JavaDoc[] files) {
106         int numFiles = files.length;
107
108         TreeMap JavaDoc map = new TreeMap JavaDoc();
109
110         String JavaDoc fileName = null;
111         int index = -1;
112
113         int numValidFiles = 0;
114
115         for (int i = 0; i < numFiles; i++) {
116             fileName = files[i];
117
118             index = getIndexOfFile(fileName);
119
120             if (index == -1) {
121                 continue;
122             }
123             numValidFiles++;
124             map.put(new Integer JavaDoc(index), fileName);
125         }
126
127         String JavaDoc[] newFilesList = new String JavaDoc[numValidFiles];
128
129         Iterator JavaDoc it = map.keySet().iterator();
130         int i = 0;
131
132         while (it.hasNext()) {
133             newFilesList[i++] = (String JavaDoc) (map.get(it.next()));
134         }
135
136         return newFilesList;
137     }
138
139     /**
140      * Given a fileName, if it is msgNNNNN.xml returns NNNNN, otherwise return -1
141      * @param fileName String
142      * @return int
143      */

144     protected static int getIndexOfFile(String JavaDoc fileName) {
145
146         int indexMsg = fileName.indexOf("msg");
147
148         if (indexMsg == -1) {
149             return -1;
150         }
151
152         int indexExtension = fileName.lastIndexOf('.');
153
154         if (indexExtension == -1) {
155             return -1;
156         }
157
158         int indexOfFile = -1;
159
160         try {
161             indexOfFile = Integer.parseInt(fileName.substring(3, indexExtension));
162         } catch (NumberFormatException JavaDoc ex) {
163             return -1;
164         }
165
166         return indexOfFile;
167     }
168 }
169
Popular Tags