1 11 package org.eclipse.pde.internal.ui.wizards.templates; 12 13 import java.util.Iterator ; 14 import java.util.Stack ; 15 16 import org.eclipse.pde.ui.templates.IVariableProvider; 17 18 public class ControlStack { 19 private Stack stack; 20 private PreprocessorParser parser; 21 22 class Entry { 23 boolean value; 24 } 25 26 public ControlStack() { 27 stack = new Stack (); 28 parser = new PreprocessorParser(); 29 } 30 31 public void setValueProvider(IVariableProvider provider) { 32 parser.setVariableProvider(provider); 33 } 34 35 public void processLine(String line) { 36 if (line.startsWith("if")) { String expression = line.substring(2).trim(); 38 boolean result = false; 39 try { 40 result = parser.parseAndEvaluate(expression); 41 } 42 catch (Exception e) { 43 } 44 Entry entry = new Entry(); 45 entry.value = result; 46 stack.push(entry); 47 } 48 else if (line.startsWith("else")) { if (stack.isEmpty()==false) { 50 Entry entry = (Entry)stack.peek(); 51 entry.value = !entry.value; 52 } 53 } 54 else if (line.startsWith("endif")) { if (!stack.isEmpty()) 57 stack.pop(); 58 } 59 else { 60 } 62 } 63 64 public boolean getCurrentState() { 65 if (stack.isEmpty()) return true; 66 for (Iterator 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 |