KickJava   Java API By Example, From Geeks To Geeks.

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


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.impl.AbstractNode;
32 import net.killingar.wiki.impl.LineNode;
33 import net.killingar.wiki.impl.SimpleNode;
34
35 public abstract class StringTagParser implements Parser
36 {
37     private String JavaDoc parsableString;
38     private EscapedString data;
39     private int currentOffset = 0;
40     private int line = -1;
41
42     String JavaDoc startKeyword, endKeyword;
43
44     private int stage = 0;
45
46     /**
47      * Constructor for StringTagParser.
48      */

49     public StringTagParser(String JavaDoc startKeyword, String JavaDoc endKeyword)
50     {
51         this.startKeyword = startKeyword;
52         this.endKeyword = endKeyword;
53     }
54
55     public StringTagParser(String JavaDoc startKeyword)
56     {
57         this(startKeyword, startKeyword);
58     }
59
60     private void setParsableString(String JavaDoc inParsableString)
61     {
62         parsableString = inParsableString;
63         data = new EscapedString(parsableString);
64         currentOffset = 0;
65     }
66
67     public void setSource(Object JavaDoc o)
68     {
69         if (o instanceof String JavaDoc)
70             setParsableString((String JavaDoc)o);
71         else if (o.getClass() == SimpleNode.class || o.getClass() == LineNode.class)
72         {
73             setParsableString(((AbstractNode)o).getText());
74             line = ((AbstractNode)o).getLineNumber();
75         }
76         else
77             throw new IllegalArgumentException JavaDoc("unsupported type "+o.getClass());
78     }
79
80     public boolean hasMore()
81     {
82         if (parsableString == null)
83             return false;
84
85         return currentOffset < parsableString.length();
86     }
87
88     public Node next()
89     {
90         if (currentOffset >= data.length())
91             throw new IllegalStateException JavaDoc("past end");
92
93         Node n = null;
94
95         int foo = currentOffset;
96         if (stage == 0)
97             foo = data.indexOf(startKeyword, currentOffset);
98         else if (stage == 2)
99             foo = data.indexOf(endKeyword, currentOffset);
100
101         if (foo != -1)
102         {
103             if (stage == 0)
104             {
105                 n = new SimpleNode(parsableString.substring(currentOffset, foo), line, currentOffset, foo);
106                 stage++;
107
108             }
109             else if (stage == 1)
110             {
111                 if (data.indexOf(endKeyword, currentOffset) == -1) // if unmatched
112
{
113                     stage = 0;
114                     n = new SimpleNode(startKeyword, line, currentOffset, foo);
115
116                     foo += startKeyword.length();
117                 }
118                 else
119                 {
120                     n = createNode(line, currentOffset, foo-1, true);
121
122                     foo += endKeyword.length();
123
124                     stage++;
125                 }
126             }
127             else if (stage == 2)
128             {
129                 n = new SimpleNode(parsableString.substring(currentOffset, foo), line, currentOffset, foo);
130
131                 stage++;
132             }
133             else if (stage == 3)
134             {
135                 n = createNode(line, currentOffset, foo-1, false);
136
137                 foo += endKeyword.length();
138
139                 stage = 0;
140             }
141             else
142                 n = new SimpleNode(parsableString.substring(currentOffset, foo), line, currentOffset, foo);
143
144             currentOffset = foo;
145         }
146         else
147         {
148             // if (started) Error: non-ended tag
149
foo = parsableString.length();
150             n = new SimpleNode(parsableString.substring(currentOffset, foo), line, currentOffset, foo);
151
152             currentOffset = foo+1;
153         }
154
155         return n;
156     }
157
158     abstract protected Node createNode(int line, int startIndex, int endIndex, boolean start);
159 }
160
Popular Tags