1 17 18 package com.finalist.jag; 19 20 import java.util.*; 21 22 import com.finalist.jag.util.*; 23 import com.finalist.jag.JagException; 24 import com.finalist.jag.template.*; 25 import com.finalist.jag.taglib.*; 26 27 34 public class TagEngine implements TagConstants { 35 36 37 private PageContext pageContext = null; 38 39 40 private HashMap processMap = null; 41 42 43 private HashMap libraryMap = new HashMap(); 44 45 46 private List stackValidatorList = new LinkedList(); 47 48 49 private Stack bodyContents = new Stack(); 50 51 52 private TemplateStructure templateData = null; 53 54 55 private String currentBody; 56 57 58 63 public TagEngine() { 64 } 65 66 67 75 public void processTags(PageContext pageContext) { 76 this.processMap = new HashMap(); 77 this.pageContext = pageContext; 78 this.templateData = pageContext.getTemplateData(); 79 80 processChildTags(templateData.getRoot()); 81 } 82 83 84 91 private void processChildTags(TemplateTreeItem parentNode) { 92 TreeItem childNode = parentNode.getFirstChild(); 93 94 for (; childNode != null; childNode = childNode.getNextSibling()) { 95 TemplateTag tagRef = ((TemplateTreeItem) childNode).getTag(); 96 if ((tagRef == null) || tagRef.isProcessed()) 97 continue; 98 99 tagRef.setPageContext(pageContext); 100 101 try { 102 if (!processTag((TemplateTreeItem) childNode)) 103 return; 104 } 105 catch (JagException exc) { 106 Log.log(exc.getMessage()); 107 } 108 } 109 } 110 111 112 123 public boolean processTag(TemplateTreeItem tagItemNode) 124 throws JagException { 125 TagProcess tagProcess = getTagProcess(tagItemNode); 126 tagProcess.setBodyText(getBodyText(tagItemNode)); 127 128 switch (tagProcess.process()) { 129 case EVAL_BODY_TAG: 130 putBodyContent(tagItemNode); 132 processChildTags(tagItemNode); 133 processTag(tagItemNode); 134 break; 135 case SKIP_CLEAR_BODY: 136 clearBodyText(tagItemNode); 138 case SKIP_BODY: 139 popBodyContent(tagItemNode); 141 processTag(tagItemNode); 142 break; 143 case EVAL_PAGE: 144 break; 146 case SKIP_PAGE: 147 return false; 149 } 150 return true; 151 } 152 153 154 165 protected TagProcess getTagProcess(TemplateTreeItem tagItemNode) 166 throws JagException { 167 TemplateTag tagref = tagItemNode.getTag(); 168 TagProcess tagProcess = (TagProcess) processMap.get(tagref); 169 170 if (tagProcess == null) { 171 PageContext pageContext = tagref.getPageContext(); 172 TemplateHeaderCollection headers = pageContext.getHeaderCollection(); 173 UrlHeaderLine urlHeader = headers.getHeaderUrl(tagref.getTagLib()); 174 TagLibrary tagLibrary = getLibrary(urlHeader.getUrl()); 175 TagDef tagdef = tagLibrary.getTagDef(tagref); 176 177 tagref.setTagDefinition(tagdef); 178 tagProcess = new TagProcess(tagref); 179 processMap.put(tagref, tagProcess); 180 pushBodyContent(tagItemNode); 181 } 182 return tagProcess; 183 } 184 185 186 193 protected String getBodyText(TemplateTreeItem tagItemNode) { 194 TemplateStructure bodyStructure = new TemplateStructure(); 195 bodyStructure.setRoot(tagItemNode); 196 StringBuffer s = bodyStructure.getTextBlockList().getStringBuffer(); 197 return new String (s); 198 } 199 200 201 208 protected void clearBodyText(TemplateTreeItem parentItem) { 209 TemplateStructure bodyStructure = new TemplateStructure(); 210 bodyStructure.setRoot(parentItem); 211 bodyStructure.clearBodyText(); 212 } 213 214 215 226 protected TagLibrary getLibrary(String url) throws JagException { 227 TagLibrary library = (TagLibrary) libraryMap.get(url); 228 if (library == null) { 229 TagLibraryLoader libLoader = new TagLibraryLoader(new java.io.File (url)); 230 library = libLoader.getTagLibrary(); 231 Exception exc = libLoader.getException(); 232 233 if (exc != null) 234 throw new JagException(exc.toString()); 235 libraryMap.put(url, library); 236 } 237 return library; 238 } 239 240 241 248 protected void pushBodyContent(TemplateTreeItem tagItemNode) { 249 TemplateTag tagRef = tagItemNode.getTag(); 250 TreeItem childNode = tagItemNode.getFirstChild(); 251 252 if (childNode == null) 253 return; 254 if (tagRef.getClosingTextBuffer() == null) 255 return; 256 257 TemplateTextBlock closingTextBlock = tagRef.getClosingTextBuffer(); 258 TemplateTreeItem firstBlock = (TemplateTreeItem) childNode; 259 TemplateTreeItem closeBlock = templateData.getTemplateTreeItem(closingTextBlock); 260 TemplateTreeItem lastBlock = (TemplateTreeItem) closeBlock.getPrevSibling(); 261 TemplateStructure bodyStructure = templateData.cut(firstBlock, lastBlock); 262 263 BodyContent tagBody = new BodyContent(); 264 tagBody.setBodyStructure(bodyStructure); 265 bodyContents.push(tagBody); 266 stackValidatorList.add(tagItemNode); 267 } 268 269 270 277 protected void putBodyContent(TemplateTreeItem parentItem) { 278 BodyContent bodyContent = peekBodyContent(); 279 TemplateStructure bodyData = bodyContent.getBodyStructure(); 280 TemplateTreeItem rootItem = bodyData.getRoot(); 281 TreeItem childItem = rootItem.getFirstChild(); 282 TreeItem closingItem = parentItem.disconnectLastChild(); 283 TemplateTreeItem textItem = new TemplateTreeItem(); 284 285 parentItem.addChild(childItem); 286 parentItem.addChild(textItem); 287 parentItem.addChild(closingItem); 288 289 if (parentItem.getTag() != null) 290 parentItem.getTag().setTextBuffer(textItem.getTextBlock()); 291 } 292 293 294 301 protected BodyContent peekBodyContent() { 302 BodyContent copyBody = new BodyContent((BodyContent) bodyContents.peek()); 303 return copyBody; 304 } 305 306 307 314 protected BodyContent popBodyContent(TemplateTreeItem parentItem) { 315 if (stackValidatorList.remove(parentItem)) { 316 return (BodyContent) bodyContents.pop(); 317 } 318 return new BodyContent(); 320 } 321 322 } | Popular Tags |