KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > templates > ControlStack


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.wizards.templates;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.Stack JavaDoc;
15
16 import org.eclipse.pde.ui.templates.IVariableProvider;
17
18 public class ControlStack {
19     private Stack JavaDoc stack;
20     private PreprocessorParser parser;
21     
22     class Entry {
23         boolean value;
24     }
25     
26     public ControlStack() {
27         stack = new Stack JavaDoc();
28         parser = new PreprocessorParser();
29     }
30     
31     public void setValueProvider(IVariableProvider provider) {
32         parser.setVariableProvider(provider);
33     }
34     
35     public void processLine(String JavaDoc line) {
36         if (line.startsWith("if")) { //$NON-NLS-1$
37
String JavaDoc expression = line.substring(2).trim();
38             boolean result = false;
39             try {
40                 result = parser.parseAndEvaluate(expression);
41             }
42             catch (Exception JavaDoc e) {
43             }
44             Entry entry = new Entry();
45             entry.value = result;
46             stack.push(entry);
47         }
48         else if (line.startsWith("else")) { //$NON-NLS-1$
49
if (stack.isEmpty()==false) {
50                 Entry entry = (Entry)stack.peek();
51                 entry.value = !entry.value;
52             }
53         }
54         else if (line.startsWith("endif")) { //$NON-NLS-1$
55
// pop the stack
56
if (!stack.isEmpty())
57                 stack.pop();
58         }
59         else {
60             // a preprocessor comment - ignore it
61
}
62     }
63     
64     public boolean getCurrentState() {
65         if (stack.isEmpty()) return true;
66         // All control levels must evaluate to true to
67
// return result==true
68
for (Iterator JavaDoc iter = stack.iterator(); iter.hasNext();) {
69             Entry entry = (Entry)iter.next();
70             if (!entry.value) return false;
71         }
72         return true;
73     }
74 }
75
Popular Tags