Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 30495d3f492ae63bad497322ef7196573830646f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*******************************************************************************
 * Copyright (c) 2005, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Bjorn Freeman-Benson - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.examples.ant.tasks;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.util.FileUtils;

/**
 * Java preprocessor for code examples. Used to export source code for
 * example plug-ins with parts of code missing/inserted etc., for 
 * various exercises.
 * <p>
 * The preprocessor looks for #ifdef statements in java comments, and is
 * run with a set of symbols. For example:
 * <pre>
 * //#ifdef ex1
 * ... code to insert when 'ex1' symbol is on
 * //#else
 * ... code to insert when not 'ex1'
 * //#endif
 * </pre>
 * </p>
 */
public class PreProcessor extends Task {
	
	private Vector fFileSets = new Vector();
	private File fDestDir = null;
	private Set fSymbols = new HashSet(); 
	private FileUtils fUtils = FileUtils.getFileUtils();
	
	// possible states
	private static final int STATE_OUTSIDE_CONDITION = 0;
	private static final int STATE_TRUE_CONDITION = 1;
	private static final int STATE_FALSE_CONDITION = 2;
	private static final int STATE_POST_TRUE_CONDITION = 3;
	
	// matchers
	private Matcher IF_DEF_MATCHER = Pattern.compile("#ifdef\\s+\\w+").matcher("");
	private Matcher ELSE_IF_MATCHER = Pattern.compile("#elseif\\s+\\w+").matcher("");
	private Matcher ELSE_MATCHER = Pattern.compile("#else$|#else\\W+").matcher("");
	private Matcher END_MATCHER = Pattern.compile("#endif").matcher("");

	
	/**
	 * Constructs a new preprocessor task
	 */
	public PreProcessor() {
	}
	
    /**
     * Adds a set of files to process.
     * 
     * @param set a set of files to process
     */
    public void addFileset(FileSet set) {
        fFileSets.addElement(set);
    }
    
    /**
     * Sets the destination directory for processed files.
     * 
     * @param destDir destination directory for processed files
     */
    public void setDestdir(File destDir) {
    	fDestDir = destDir;
    }
    
    /**
	 * Sets the symbols that are "on" for the preprocessing.
	 * 
	 * @param symbols symbols that are "on" for the preprocessing
	 */
	public void setSymbols(String symbols) {
		String[] strings = symbols.split(",");
		for (int i = 0; i < strings.length; i++) {
			String string = strings[i].trim();
			if (string.length() > 0) {
				fSymbols.add(string);
			}
		}
	}

	public void execute() throws BuildException {
		if (fSymbols.size() == 0) {
			throw new BuildException("No symbols specified for preprocessor");
		}
		if (fFileSets.isEmpty()) {
			throw new BuildException("No filesets specified for processing");
		}
		if (!fDestDir.exists()) {
			throw new BuildException("destdir does not exist: " + fDestDir.getAbsolutePath());
		}
		StringBuffer buf = new StringBuffer("Symbols: ");
		String[] symbols = (String[]) fSymbols.toArray(new String[fSymbols.size()]);
		for (int i = 0; i < symbols.length; i++) {
			String symbol = symbols[i];
			buf.append(symbol);
			if(i < (symbols.length -1)) {
				buf.append(", ");
			}
		}
		log(buf.toString());
		
		Iterator fileSets = fFileSets.iterator();
		while (fileSets.hasNext()) {
			FileSet fileSet = (FileSet) fileSets.next();
			DirectoryScanner scanner = fileSet.getDirectoryScanner(getProject());
			String[] includedFiles = scanner.getIncludedFiles();
			File baseDir = fileSet.getDir(getProject());
			for (int i = 0; i < includedFiles.length; i++) {
				String fileName = includedFiles[i];
				processFile(baseDir, fileName, fDestDir);
			}
		}
		
	}

	/**
	 * Process the file
	 * @param baseDir base directory source file is relative to
	 * @param fileName source file name
	 * @param destDir root destination directory 
	 */
	private void processFile(File baseDir, String fileName, File destDir) throws BuildException {
		File destFile = new File(destDir, fileName);
		File srcFile = new File(baseDir, fileName);
		File dir = destFile.getParentFile();
		if (!dir.exists()) {
			dir.mkdirs();
		}
		String contents = null;
		if (fileName.endsWith(".java")) {
			contents = preProcessFile(srcFile, "//#");
		} else if (fileName.equals("plugin.xml")) {
			contents = preProcessFile(srcFile, null);
		}
		if (contents == null) {
			// no change, just copy file
			try {
				fUtils.copyFile(srcFile, destFile);
			} catch (IOException e) {
				throw new BuildException(e);
			}
		} else {
			// write new file
			FileWriter writer;
			try {
				writer = new FileWriter(destFile);
				writer.write(contents);
				writer.close();
			} catch (IOException e) {
				throw new BuildException(e);
			}
			
		}
	}

	/**
	 * Preprocesses a file
	 * 
	 * @param srcFile the file to process
	 * @param strip chars to stip off lines in a true condition, or <code>null</code>
	 * @return
	 */
	public String preProcessFile(File srcFile, String strip) {
		try {
			FileReader fileReader = new FileReader(srcFile);
			BufferedReader reader = new BufferedReader(fileReader);
			StringBuffer buffer = new StringBuffer();
			String line = reader.readLine();
			String activeSymbol = null;
			int state = STATE_OUTSIDE_CONDITION;
			boolean changed = false;
			while (line != null) {
				boolean ifdef = IF_DEF_MATCHER.reset(line).find();
				boolean elseif = ELSE_IF_MATCHER.reset(line).find();
				boolean elze = ELSE_MATCHER.reset(line).find();
				boolean endif = END_MATCHER.reset(line).find();
				boolean commandLine = ifdef || elseif || elze || endif;
				boolean written = false;
				switch (state) {
					case STATE_OUTSIDE_CONDITION:
						if (ifdef) {
							String condition = line.substring(IF_DEF_MATCHER.start(), IF_DEF_MATCHER.end());
							String[] strings = condition.split("\\s+");
							activeSymbol = strings[1].trim();
							if (fSymbols.contains(activeSymbol)) {
								state = STATE_TRUE_CONDITION;
							} else {
								state = STATE_FALSE_CONDITION;
							}							
						} else if (elseif) {
							throw new BuildException("#elseif encountered without corresponding #ifdef");
						} else if (elze) {
							throw new BuildException("#else encountered without corresponding #ifdef (" + srcFile.getPath() + ")");
						} else if (endif) {
							throw new BuildException("#endif encountered without corresponding #ifdef");
						}
						break;
					case STATE_TRUE_CONDITION:
						if (elze || elseif) {
							state = STATE_POST_TRUE_CONDITION;
							break;
						} else if (endif) {
							state = STATE_OUTSIDE_CONDITION;
							break;
						} else if (ifdef) {
							throw new BuildException("illegal nested #ifdef");
						}
					case STATE_FALSE_CONDITION:
						if (elseif) {
							String condition = line.substring(ELSE_IF_MATCHER.start(), ELSE_IF_MATCHER.end());
							String[] strings = condition.split("\\s+");
							activeSymbol = strings[1].trim();
							if (fSymbols.contains(activeSymbol)) {
								state = STATE_TRUE_CONDITION;
							} else {
								state = STATE_FALSE_CONDITION;
							}
						} else if (elze) {
							state = STATE_TRUE_CONDITION;
							break;
						} else if (endif) {
							state = STATE_OUTSIDE_CONDITION;
							break;
						} else if (ifdef) {
							throw new BuildException("illegal nested #ifdef");
						}
					case STATE_POST_TRUE_CONDITION:
						if (endif) {
							state = STATE_OUTSIDE_CONDITION;
							break;
						} else if (ifdef) {
							throw new BuildException("illegal nested #ifdef");
						}
				}
				if (!commandLine) {
					if (state == STATE_OUTSIDE_CONDITION || state == STATE_TRUE_CONDITION) {
						if (state == STATE_TRUE_CONDITION && strip != null) {
							if (line.startsWith(strip)) {
								line = line.substring(strip.length());
							}
						}
						buffer.append(line);
						buffer.append("\n");
						written = true;
					}
				} 
				changed = changed || !written;								
				line = reader.readLine();
			}
			if (!changed) {
				return null;
			}
			return buffer.toString();
		} catch (IOException e) {
			throw new BuildException(e);
		}
	}

    public static void main(String[] args) {
		PreProcessor processor = new PreProcessor();
		processor.setSymbols("ex2");
		String string = processor.preProcessFile(new File("c:\\eclipse3.1\\dev\\example.debug.core\\src\\example\\debug\\core\\launcher\\PDALaunchDelegate.java"), "//#");
		//String string = processor.preProcessFile(new File("c:\\eclipse3.1\\dev\\example.debug.core\\plugin.xml"), null);
		System.out.println(string);
	}
}

Back to the top