1 9 10 package org.jboss.portal.format.template; 11 12 import java.io.File ; 13 import java.io.FileNotFoundException ; 14 import java.io.FileReader ; 15 import java.io.IOException ; 16 import java.io.Reader ; 17 import java.io.StringReader ; 18 import java.util.LinkedList ; 19 20 import org.dom4j.Document; 21 import org.dom4j.DocumentFactory; 22 import org.dom4j.Element; 23 24 27 public final class TemplateParser 28 implements TemplateAnalyzerConstants 29 { 30 31 private Reader source; 32 private TemplateAnalyzerTokenManager analyzer; 33 private LinkedList stack = new LinkedList (); 34 private Document template; 35 private Element current; 36 private StringBuffer text = null; 37 38 public TemplateParser(File source) throws FileNotFoundException 39 { 40 this(new FileReader (source)); 41 } 42 43 public TemplateParser(String source) 44 { 45 this(new StringReader (source)); 46 } 47 48 public TemplateParser(Reader source) 49 { 50 this.source = source; 51 analyzer = new TemplateAnalyzerTokenManager(new ASCII_CharStream(source, 1, 1)); 52 template = DocumentFactory.getInstance().createDocument(); 53 current = template.addElement("node"); 54 } 55 56 60 public Document parse() throws ParseException 61 { 62 while(true) 63 { 64 Token t = analyzer.getNextToken(); 66 67 if (t.image.length() == 0) 68 { 69 if (text != null) 70 { 71 current.addText(text.toString()); 72 text = null; 73 } 74 75 if (current != template.getRootElement()) 77 { 78 throw new ParseException("An opening loop is not closed"); 79 } 80 return template; 81 } 82 83 switch(t.kind) 84 { 85 case BEGIN: 86 if (text != null) 87 { 88 current.addText(text.toString()); 89 text = null; 90 } 91 Element loop = current.addElement("loop"); 92 loop.addAttribute("name", t.image.substring("<!-- BEGIN ".length(), t.image.length() - " -->".length())); 93 push(loop.addElement("node")); 94 break; 95 96 case END: 97 if (text != null) 98 { 99 current.addText(text.toString()); 100 text = null; 101 } 102 if (!"node".equals(current.getName())) 103 { 104 throw new ParseException("Unexpected closing loop"); 105 } 106 pop(); 107 break; 112 113 case PROPERTY: 114 case TEXT: 115 if (text == null) 116 { 117 text = new StringBuffer (); 118 } 119 text.append(t.image); 120 break; 121 case NEWLINE: 122 if (text == null) 123 { 124 text = new StringBuffer (); 125 } 126 text.append("\n"); 127 break; 128 129 130 case REF: 131 if (text != null) 132 { 133 current.addText(text.toString()); 134 text = null; 135 } 136 Element ref = current.addElement("ref"); 137 String tmp = t.image.substring(1, t.image.length() - 1); 138 int count = 0; 139 int previous = -1; 140 while (true) 141 { 142 int index = tmp.indexOf('.', previous + 1); 143 if (index == -1) 144 { 145 String name = tmp.substring(previous + 1); 146 ref.addAttribute("name", name); 147 break; 148 } 149 count++; 150 previous = index; 151 } 152 ref.addAttribute("depth", "" + count); 153 break; 154 155 default: 156 throw new ParseException("Unexpected token"); 157 } 158 } 159 } 160 161 public void close() 162 { 163 try 164 { 165 source.close(); 166 } 167 catch (IOException ignore) 168 { 169 } 171 } 172 173 private void push(Element node) 174 { 175 stack.addLast(current); 176 current = node; 177 } 178 179 private void pop() 180 { 181 current = (Element)stack.removeLast(); 182 } 183 184 } 185 | Popular Tags |