1 17 18 19 20 package org.apache.fop.render.rtf.rtflib.tools; 21 22 import java.util.Stack ; 23 24 import org.apache.fop.render.rtf.rtflib.exceptions.RtfException; 25 import org.apache.fop.render.rtf.rtflib.rtfdoc.IRtfOptions; 26 import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfContainer; 27 28 29 38 39 public class BuilderContext { 40 41 private final Stack containers = new Stack (); 42 43 44 private final Stack tableContexts = new Stack (); 45 46 47 private final Stack builders = new Stack (); 48 49 50 private IRtfOptions options; 51 52 public BuilderContext(IRtfOptions rtfOptions) { 53 options = rtfOptions; 54 } 55 56 59 private Object getObjectFromStack(Stack s, Class desiredClass) { 60 Object result = null; 61 final Stack copy = (Stack )s.clone(); 62 while (!copy.isEmpty()) { 63 final Object o = copy.pop(); 64 if (desiredClass.isAssignableFrom(o.getClass())) { 65 result = o; 66 break; 67 } 68 } 69 return result; 70 } 71 72 84 85 89 public RtfContainer getContainer(Class containerClass, boolean required, 90 Object forWhichBuilder) throws RtfException { 91 final RtfContainer result = (RtfContainer)getObjectFromStack(containers, 94 containerClass); 95 96 if (result == null && required) { 97 throw new RtfException( 98 "No RtfContainer of class '" + containerClass.getName() 99 + "' available for '" + forWhichBuilder.getClass().getName() + "' builder" 100 ); 101 } 102 103 return result; 104 } 105 106 107 public void pushContainer(RtfContainer c) { 108 containers.push(c); 109 } 110 111 119 public void replaceContainer(RtfContainer oldC, RtfContainer newC) 120 throws Exception { 121 final int index = containers.indexOf(oldC); 123 if (index < 0) { 124 throw new Exception ("container to replace not found:" + oldC); 125 } 126 containers.setElementAt(newC, index); 127 } 128 129 130 public void popContainer() { 131 containers.pop(); 132 } 133 134 139 140 153 154 public TableContext getTableContext() { 155 return (TableContext)tableContexts.peek(); 156 } 157 158 159 public void pushTableContext(TableContext tc) { 160 tableContexts.push(tc); 161 } 162 163 164 public void popTableContext() { 165 tableContexts.pop(); 166 } 167 168 } 169 | Popular Tags |