01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.pde.internal.ui.wizards.templates;
11:
12: import java.util.Iterator;
13: import java.util.Stack;
14:
15: import org.eclipse.pde.ui.templates.IVariableProvider;
16:
17: public class ControlStack {
18: private Stack stack;
19: private PreprocessorParser parser;
20:
21: class Entry {
22: boolean value;
23: }
24:
25: public ControlStack() {
26: stack = new Stack();
27: parser = new PreprocessorParser();
28: }
29:
30: public void setValueProvider(IVariableProvider provider) {
31: parser.setVariableProvider(provider);
32: }
33:
34: public void processLine(String line) {
35: if (line.startsWith("if")) { //$NON-NLS-1$
36: String expression = line.substring(2).trim();
37: boolean result = false;
38: try {
39: result = parser.parseAndEvaluate(expression);
40: } catch (Exception e) {
41: }
42: Entry entry = new Entry();
43: entry.value = result;
44: stack.push(entry);
45: } else if (line.startsWith("else")) { //$NON-NLS-1$
46: if (stack.isEmpty() == false) {
47: Entry entry = (Entry) stack.peek();
48: entry.value = !entry.value;
49: }
50: } else if (line.startsWith("endif")) { //$NON-NLS-1$
51: // pop the stack
52: if (!stack.isEmpty())
53: stack.pop();
54: } else {
55: // a preprocessor comment - ignore it
56: }
57: }
58:
59: public boolean getCurrentState() {
60: if (stack.isEmpty())
61: return true;
62: // All control levels must evaluate to true to
63: // return result==true
64: for (Iterator iter = stack.iterator(); iter.hasNext();) {
65: Entry entry = (Entry) iter.next();
66: if (!entry.value)
67: return false;
68: }
69: return true;
70: }
71: }
|