1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.jsieve.parser;
21
22 import java.util.LinkedList;
23 import java.util.List;
24
25 import org.apache.jsieve.ScriptCoordinate;
26 import org.apache.jsieve.parser.generated.Token;
27
28
29
30
31
32
33 public class SieveNode {
34
35
36
37
38 public SieveNode() {
39 super();
40 }
41
42 private Token firstToken;
43
44 private Token lastToken;
45
46
47
48
49 private String fieldName;
50
51
52
53
54 private Object fieldValue;
55
56
57
58
59
60
61 public String getName() {
62 return fieldName;
63 }
64
65
66
67
68
69
70 public Object getValue() {
71 return fieldValue;
72 }
73
74
75
76
77
78
79
80 public void setName(String name) {
81 fieldName = name;
82 }
83
84
85
86
87
88
89
90 public void setValue(Object value) {
91 fieldValue = value;
92 }
93
94
95
96
97
98
99 public Token getFirstToken() {
100 return firstToken;
101 }
102
103
104
105
106
107
108
109 public void setFirstToken(Token firstToken) {
110 this.firstToken = firstToken;
111 }
112
113
114
115
116
117
118 public Token getLastToken() {
119 return lastToken;
120 }
121
122
123
124
125
126
127
128 public void setLastToken(Token lastToken) {
129 this.lastToken = lastToken;
130 }
131
132
133
134
135
136
137
138 public ScriptCoordinate getCoordinate() {
139 final int lastColumn = lastToken.endColumn;
140 final int lastList = lastToken.endLine;
141 final int firstColumn = firstToken.beginColumn;
142 final int firstLine = firstToken.beginLine;
143 final ScriptCoordinate scriptCoordinate = new ScriptCoordinate(
144 firstLine, firstColumn, lastList, lastColumn);
145 return scriptCoordinate;
146 }
147
148
149
150
151
152
153
154 public List<String> getPrecedingComments() {
155 final LinkedList<String> results = new LinkedList<String>();
156 if (firstToken != null) {
157 Token special = firstToken.specialToken;
158 while (special != null) {
159 final String comment = parseComment(special);
160 results.addFirst(comment);
161 special = special.specialToken;
162 }
163 }
164 return results;
165 }
166
167 private String parseComment(Token special) {
168 final String image = special.image;
169 final String comment;
170 if ('#' == image.charAt(0)) {
171 final int leftHandCharactersToIgnore;
172 if ('\r' == image.charAt(image.length()-2)) {
173 leftHandCharactersToIgnore = 2;
174 } else {
175 leftHandCharactersToIgnore = 1;
176 }
177 comment = image.substring(1, image.length()-leftHandCharactersToIgnore);
178 } else {
179 comment = image.substring(2, image.length()-2);
180 }
181 return comment;
182 }
183
184
185
186
187
188
189
190
191 public String getLastComment() {
192 final String result;
193 if (firstToken == null) {
194 result = null;
195 } else {
196 Token special = firstToken.specialToken;
197 if (special == null) {
198 result = null;
199 } else {
200 result = parseComment(special);
201 }
202 }
203 return result;
204 }
205 }