KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > wiki > parser > ParserQueue


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

25
26 package net.killingar.wiki.parser;
27
28 import net.killingar.EscapedString;
29 import net.killingar.wiki.Node;
30 import net.killingar.wiki.Parser;
31 import net.killingar.wiki.ParserException;
32 import net.killingar.wiki.Reader;
33 import net.killingar.wiki.impl.LineNode;
34 import net.killingar.wiki.impl.SimpleNode;
35 import net.killingar.wiki.impl.TagNode;
36
37 import java.io.FileNotFoundException JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.util.List JavaDoc;
40
41 public class ParserQueue implements Parser
42 {
43     private List JavaDoc parsers;
44     private int currentParser = 0;
45     private Object JavaDoc source;
46
47     /**
48      * Constructor for ParserQueue.
49      */

50     public ParserQueue()
51     {
52         parsers = new ArrayList JavaDoc();
53     }
54
55     public void queueParser(Parser inParser)
56     {
57         parsers.add(inParser);
58         if (parsers.size() == 1)
59             inParser.setSource(source);
60     }
61
62     public void setSource(Object JavaDoc o)
63     {
64         source = o;
65     }
66
67     /**
68      * @see Parser
69      */

70     public boolean hasMore()
71     {
72         if (currentParser == 0 && parsers.size() == 0)
73             return false;
74
75         if (currentParser >= parsers.size())
76             throw new ParserException("parser depth error");
77
78         boolean hasMore = ((Parser)parsers.get(currentParser)).hasMore();
79
80         while (!hasMore && currentParser != 0)
81         {
82             currentParser--;
83             hasMore = ((Parser)parsers.get(currentParser)).hasMore();
84         }
85
86         return hasMore;
87     }
88
89     public Node next()
90     {
91         if (currentParser >= parsers.size())
92             throw new ParserException("parser depth error");
93
94         Parser p = (Parser)parsers.get(currentParser);
95
96         Node node = (Node)p.next();
97
98         while (
99             currentParser < parsers.size()-1 &&
100             (node.getClass() == SimpleNode.class || node.getClass() == LineNode.class))
101         {
102             currentParser++;
103             p = (Parser)parsers.get(currentParser);
104
105             // assert that this node doesn't have more stuff to do before we overwrite its source
106
if (p.hasMore())
107                 throw new IllegalStateException JavaDoc("parser "+p+" has nodes left, cannot continue");
108
109             p.setSource(node);
110             if (p.hasMore())
111                 node = (Node)p.next();
112         }
113
114         return node;
115     }
116
117     public static final void main(String JavaDoc[] args) throws FileNotFoundException JavaDoc
118     {
119         ParserQueue q = new ParserQueue();
120         //q.setSource("foo<foobar>bar");
121
q.setSource(new java.io.FileReader JavaDoc("C:\\foo.txt"));
122
123         q.queueParser(new Reader());
124         q.queueParser(new WikiLinkParser());
125         q.queueParser(new StringTagParser("__", "__")
126             {
127                 protected Node createNode(int line, int startIndex, int endIndex, boolean start)
128                 {
129                     return new TagNode("u", start, line, startIndex, endIndex);
130                 }
131             });
132         q.queueParser(new StringTagParser("**", "**")
133             {
134                 protected Node createNode(int line, int startIndex, int endIndex, boolean start)
135                 {
136                     return new TagNode("b", start, line, startIndex, endIndex);
137                 }
138             });
139         q.queueParser(new StringTagParser("~~", "~~")
140             {
141                 protected Node createNode(int line, int startIndex, int endIndex, boolean start)
142                 {
143                     return new TagNode("i", start, line, startIndex, endIndex);
144                 }
145             });
146         q.queueParser(new ReplaceParser("<", "&lt;"));
147         q.queueParser(new ReplaceParser(">", "&gt;"));
148
149
150         //System.out.println(EscapedString.strip("\\\\\\__foo")); // this should render as \__foo
151

152         while (q.hasMore())
153         {
154             Node n = q.next();
155             /*if (n instanceof LinkNode)
156             {
157                 LinkNode link = (LinkNode)n;
158                 //if (!isWikiLink(link.getContext(), link.getWiki()))
159                     //System.out.print(link.getEditString());
160                 //else
161                     System.out.println(EscapedString.strip(link.getText()));
162             }
163             else*/

164
165             //System.out.println(n.getClass()+": "+EscapedString.strip(n.getText()));
166
System.out.print(EscapedString.strip(n.getText()));
167         }
168     }
169 }
170
Popular Tags