1 17 18 package com.finalist.jag.template; 19 20 21 import java.util.*; 22 23 import com.finalist.jag.template.parser.*; 24 25 32 public class TemplateStructureFactory implements JagParserConstants { 33 34 private TemplateStructure templateTree = null; 35 36 37 44 public void create(JagBlockCollection jagblocks) { 45 46 templateTree = new TemplateStructure(); 47 createTree(templateTree.getRoot(), jagblocks); 48 } 49 50 51 public TemplateStructure getTemplateStructure() { 52 return templateTree; 53 } 54 55 56 65 protected JagParameter[] createParamList(JagBlock block) { 66 67 Collection paramList = new ArrayList(); 68 JagBlock childBlock[] = {block.getFirstChild(), null}; 69 70 while (childBlock[0] != null) { 71 if (childBlock[0].getType() == PARAMDEF) { 72 String ident = null, value = null; 73 74 childBlock[1] = childBlock[0].getFirstChild(); 75 while (childBlock[1] != null) { 76 if (childBlock[1].getType() == IDENT) { 77 ident = childBlock[1].getText(); 78 } 79 else if (childBlock[1].getType() == STRING) { 80 value = childBlock[1].getText(); 81 } 82 childBlock[1] = childBlock[1].getNextSibling(); 83 } 84 85 if ((ident != null) && (value != null)) { 86 paramList.add(new JagParameter(ident, value)); 87 } 88 } 89 childBlock[0] = childBlock[0].getNextSibling(); 90 } 91 92 return (JagParameter[]) 93 paramList.toArray(new JagParameter[paramList.size()]); 94 } 95 96 97 protected void createTree(TemplateTreeItem parentItem, JagBlock block) { 99 JagBlock childBlock = block.getFirstChild(); 100 101 for (; childBlock != null; childBlock = childBlock.getNextSibling()) { 102 if (childBlock.getType() == HEADERDEF_BEGIN) { 103 trimFrontEOLSymbol(childBlock.getNextSibling()); 105 continue; 106 } 107 108 TemplateTextBlock textBlock = new TemplateTextBlock(childBlock.getText()); 109 TemplateTreeItem newItem = new TemplateTreeItem(); 110 newItem.setTextBlock(textBlock); 111 parentItem.addChild(newItem); 112 113 if (childBlock.getType() == TEXT) { 114 continue; 115 } 116 else if (childBlock.getType() == TAGSTART) { 117 TemplateTag tag = createTag(newItem, childBlock); 118 if (tag != null) 119 newItem.setTag(tag); 120 } 121 } 122 } 123 124 125 134 protected void trimFrontEOLSymbol(JagBlock block) { 135 if (block == null) return; 136 137 String text = block.getText(); 138 if (text.length() > 0 && text.charAt(0) == '\r') 139 text = text.substring(1); 140 if (text.length() > 0 && text.charAt(0) == '\n') 141 text = text.substring(1); 142 block.setText(text); 143 } 144 145 146 156 protected TemplateTag createTag(TemplateTreeItem parentItem, 157 JagBlock block) { 158 159 TemplateTextBlock textBlock = parentItem.getTextBlock(); 160 JagBlock blockChild = block.getFirstChild(); 161 String tagName = null; 162 String tagAction = null; 163 JagBlock blockSLIST = null; 164 JagBlock blockTagEnd = null; 165 TemplateTag tag = null; 166 167 while (blockChild != null) { 168 if (blockChild.getType() == TAGNAME) { 169 tagName = blockChild.getText(); 170 } 171 else if (blockChild.getType() == TAGACTION) { 172 tagAction = blockChild.getText(); 173 } 174 else if (blockChild.getType() == SLIST) { 175 blockSLIST = blockChild; 176 } 177 else if (blockChild.getType() == TAGEND) { 178 blockTagEnd = blockChild; 179 } 180 181 blockChild = blockChild.getNextSibling(); 182 } 183 184 if ((tagName != null) && (tagAction != null)) { 185 JagParameter[] paramArray = createParamList(block); 186 187 tag = new TemplateTag(tagName, tagAction, paramArray, textBlock); 188 } 189 190 if (blockSLIST != null) { 191 createTree(parentItem, blockSLIST); 192 } 193 194 if (blockTagEnd != null) { 195 TemplateTextBlock endBuffer = new TemplateTextBlock(blockTagEnd.getText()); 196 TemplateTreeItem newItem = new TemplateTreeItem(); 197 newItem.setTextBlock(endBuffer); 198 parentItem.addChild(newItem); 199 tag.setClosingTextBuffer(endBuffer); 200 } 201 202 return tag; 203 } 204 } | Popular Tags |