1 5 package com.teamkonzept.publishing.markups; 6 7 import com.teamkonzept.lib.*; 8 9 public class TKDefinedMarkupParser extends TKMarkupParser { 10 11 protected TKMarkupDefinition currentDefinition; 13 protected TKMarkupParamCall currentParamDefinition; 14 protected String closeMarkup; 15 16 public TKDefinedMarkupParser (TKVector diagnostics) { 17 18 super (diagnostics, new TKMarkupTree()); 19 20 this.currentDefinition = null; 21 this.currentParamDefinition = null; 22 this.closeMarkup = null; 23 } 24 25 public void initMarkupParsing () { 26 27 currentDefinition = null; 28 currentParamDefinition = null; 29 } 30 31 public int parseValue (String text, int pos, StringBuffer value) 32 throws TKMarkupParserException { 33 34 if (currentParamDefinition == null) 35 throw new TKMarkupParserException ("Aktueller Parameter nicht definiert",pos); 36 37 return currentParamDefinition.param.typeClass.parseValue (text,pos,value); 38 } 39 40 public int forceValue (String text, int pos, StringBuffer value) 41 throws TKMarkupParserException { 42 43 if (currentParamDefinition == null) 44 throw new TKMarkupParserException ("Aktueller Parameter nicht definiert",pos); 45 46 int oldPos = pos; 47 48 pos = parseValue (text,pos,value); 49 if (pos == oldPos) 50 throw new TKMarkupParserException 51 ("Syntax: "+currentParamDefinition.param.typeClass.name+" erwartet", pos); 52 53 return pos; 54 } 55 56 public void cleanup (TKMarkupItemList itemList, int pos) { 57 58 if (closeLevel(itemList)) { 59 60 addDiagnostic ((pos < 0 ? "" : "Position "+ pos+": ")+ 61 "Syntax-Fehler in /"+ compoundName()+ 62 ": Markupanfang nicht gefunden"); 63 64 } else super.cleanup(itemList,pos); 65 66 cleanupCloseLevel(); 67 } 68 69 public TKMarkupParam createParam (String name, String value) { 70 71 try { 72 if (currentDefinition == null) 73 throw new Exception ("Aktuelles Markup f¸r Parameter "+name+" nicht definiert"); 74 75 TKMarkupParamCall definition = currentDefinition.lookupParam (name); 76 77 if (definition == null) 78 throw new Exception ("Parameter "+name+" nicht definiert"); 79 80 if (value == null) value = definition.param.typeClass.typeDefault(); 81 return new TKDefinedMarkupParam (name,value,definition); 82 83 } catch (Exception ex) { 84 85 addDiagnostic (TKMarkupParserException.demandMsg (ex)); 86 return super.createParam (name,value); 87 } 88 } 89 90 public TKMarkup createMarkup (String name, TKHashtable params, int pos) { 91 92 try { return new TKDefinedMarkup (name,params,pos); } 93 catch (Exception ex) { 94 95 addDiagnostic (TKMarkupParserException.demandMsg (ex)); 96 return super.createMarkup (name,params,pos); 97 } 98 } 99 100 public TKMarkup createMarkup (String name, TKHashtable params, int pos,boolean backsl) { 101 102 try { return new TKDefinedMarkup (name,params,pos,backsl); } 103 catch (Exception ex) { 104 105 addDiagnostic (TKMarkupParserException.demandMsg (ex)); 106 return super.createMarkup (name,params,pos); 107 } 108 } 109 110 public void registerMarkup (String markupName) { 111 112 super.registerMarkup (markupName); 113 114 currentDefinition = TKMarkupDefinition.lookup(markupName); 115 currentParamDefinition = null; 116 } 117 118 public void registerParam (String paramName) { 119 120 super.registerParam (paramName); 121 122 try { 123 if (currentDefinition == null) 124 throw new Exception ("Aktuelles Markup f¸r Parameter "+paramName+" nicht definiert"); 125 126 currentParamDefinition = currentDefinition.lookupParam (paramName); 127 128 if (currentParamDefinition == null) 129 throw new Exception ("Parameter "+paramName+" nicht definiert"); 130 131 } catch (Exception ex) { 132 133 addDiagnostic (TKMarkupParserException.demandMsg (ex)); 134 } 135 } 136 137 public void registerCompound (String markupName) { 138 139 registerMarkup (markupName); 140 141 if ((currentDefinition != null) && currentDefinition.isAtom) 142 addDiagnostic ("Attribut-Fehler in "+ markupName+": ist terminal"); 143 144 closeMarkup = markupName; 145 } 146 147 public String compoundName() { 148 149 return closeMarkup == null ? "" : closeMarkup; 150 } 151 152 public boolean closeLevel (TKMarkupItemList itemList) { 153 154 checkCompound (itemList); 155 return super.closeLevel(itemList) || (closeMarkup != null); 156 } 157 158 public void cleanupCloseLevel () { 159 160 super.cleanupCloseLevel(); 161 closeMarkup = null; 162 } 163 164 public void checkCompound (TKMarkupItemList itemList) { 165 166 if (closeMarkup == null) return; 167 168 TKMarkup markup = null; 169 int i; 170 171 for (i = itemList.size()-1; i >= 0; i--) { 172 173 Object item = itemList.get(i); 174 if (item instanceof TKDefinedMarkup) continue; 175 else if (item instanceof TKMarkup) { 176 177 markup = (TKMarkup) item; 178 if (closeMarkup.equalsIgnoreCase(markup.name)) break; 179 } 180 } 181 182 if (i < 0) return; 183 184 TKMarkupTree subTree = null; 185 186 if (i < itemList.size()-1) { 187 188 subTree = new TKMarkupTree (); 189 190 for (int j = i+1; j < itemList.size(); j++) 191 subTree.addElement (itemList.get(j)); 192 } 193 194 itemList.removeRange (i,itemList.size()); 195 itemList.addElement (new TKMarkupNode(markup,subTree)); 196 closeMarkup = null; 197 } 198 199 public int tieMarkup (String text, int pos, TKMarkup markup, TKMarkupItemList itemList) { 200 201 if (!(markup instanceof TKDefinedMarkup)) { 202 203 itemList.addElement (markup); 204 205 if ((closeMarkup != null) && closeMarkup.equalsIgnoreCase(markup.name)) closeMarkup = null; 206 else checkCompound (itemList); 207 return pos; 208 } 209 210 TKDefinedMarkup definedMarkup = (TKDefinedMarkup) markup; 211 212 if (definedMarkup.isAtom) { 213 214 itemList.addElement (definedMarkup); 215 return pos; 216 } 217 218 TKMarkupTree subTree = new TKMarkupTree(); 219 220 pos = parse (text,pos,subTree); 222 223 if (itemList instanceof TKMarkupTree) 224 itemList.addElement (new TKMarkupNode(definedMarkup,subTree)); 225 else itemList.concat (subTree); 226 227 if ((closeMarkup != null) && closeMarkup.equalsIgnoreCase(markup.name)) closeMarkup = null; 228 else checkCompound (itemList); 229 230 return pos; 231 } 232 233 public int parseAliens (String text, int pos) throws TKMarkupParserException { 234 235 StringBuffer name = new StringBuffer (); 236 237 int startPos = pos; 238 pos = parsePattern (text,pos,null,"/"); 239 240 if (pos > startPos) { 241 242 try { 243 pos = forceIdentifier (text,pos,name); 244 pos = parseWhitespace (text,pos,null); 245 pos = forcePattern (text,pos,null,">"); 246 247 String markupName = new String (name).toUpperCase(); 248 249 registerCompound(markupName); 250 251 return pos; 252 253 } catch (TKMarkupParserException ex) { 254 255 ex.throwAgain(pos); 256 257 } catch (Exception ex) { 258 259 TKMarkupParserException.throwAgain(ex,pos); 260 } 261 } 262 263 return pos; 264 } 265 } 268 | Popular Tags |