KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > format > template > TemplateParser


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9
10 package org.jboss.portal.format.template;
11
12 import java.io.File JavaDoc;
13 import java.io.FileNotFoundException JavaDoc;
14 import java.io.FileReader JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.Reader JavaDoc;
17 import java.io.StringReader JavaDoc;
18 import java.util.LinkedList JavaDoc;
19
20 import org.dom4j.Document;
21 import org.dom4j.DocumentFactory;
22 import org.dom4j.Element;
23
24 /**
25  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
26  */

27 public final class TemplateParser
28    implements TemplateAnalyzerConstants
29 {
30
31    private Reader JavaDoc source;
32    private TemplateAnalyzerTokenManager analyzer;
33    private LinkedList JavaDoc stack = new LinkedList JavaDoc();
34    private Document template;
35    private Element current;
36    private StringBuffer JavaDoc text = null;
37
38    public TemplateParser(File JavaDoc source) throws FileNotFoundException JavaDoc
39    {
40       this(new FileReader JavaDoc(source));
41    }
42
43    public TemplateParser(String JavaDoc source)
44    {
45       this(new StringReader JavaDoc(source));
46    }
47
48    public TemplateParser(Reader JavaDoc 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    /**
57     * This method should be called only once. If it is called more than once
58     * there are no garanty on the result.
59     */

60    public Document parse() throws ParseException
61    {
62       while(true)
63       {
64          // getChild the next token
65
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             // no more token to read
76
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 // if (!((Loop)current.getContent().getLast()).validate(t.image))
108
// {
109
// throw new ParseException("Closing loop does not match");
110
// }
111
break;
112
113          case PROPERTY:
114          case TEXT:
115             if (text == null)
116             {
117                text = new StringBuffer JavaDoc();
118             }
119             text.append(t.image);
120             break;
121          case NEWLINE:
122             if (text == null)
123             {
124                text = new StringBuffer JavaDoc();
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 JavaDoc 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 JavaDoc 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 JavaDoc ignore)
168       {
169          //
170
}
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