1 /****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
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 * Class SieveNode defines aspects all jjTree parse nodes may require.
30 *
31 * Creation Date: 27-Jan-04
32 */
33 public class SieveNode {
34
35 /**
36 * Constructor for SieveNode.
37 */
38 public SieveNode() {
39 super();
40 }
41
42 private Token firstToken;
43
44 private Token lastToken;
45
46 /**
47 * The name associated to this node or null
48 */
49 private String fieldName;
50
51 /**
52 * The value associated to this node or null
53 */
54 private Object fieldValue;
55
56 /**
57 * Returns the name.
58 *
59 * @return String
60 */
61 public String getName() {
62 return fieldName;
63 }
64
65 /**
66 * Returns the value.
67 *
68 * @return Object
69 */
70 public Object getValue() {
71 return fieldValue;
72 }
73
74 /**
75 * Sets the name.
76 *
77 * @param name
78 * The name to set
79 */
80 public void setName(String name) {
81 fieldName = name;
82 }
83
84 /**
85 * Sets the value.
86 *
87 * @param value
88 * The value to set
89 */
90 public void setValue(Object value) {
91 fieldValue = value;
92 }
93
94 /**
95 * Gets the first token comprising this node.
96 *
97 * @return <code>Token</code>, not null
98 */
99 public Token getFirstToken() {
100 return firstToken;
101 }
102
103 /**
104 * Sets the first token comprising this node.
105 *
106 * @param firstToken
107 * <code>Token</code>, not null
108 */
109 public void setFirstToken(Token firstToken) {
110 this.firstToken = firstToken;
111 }
112
113 /**
114 * Gets the last token comprising this node.
115 *
116 * @return <code>Token</code>, not null
117 */
118 public Token getLastToken() {
119 return lastToken;
120 }
121
122 /**
123 * Sets the last token comprising this node.
124 *
125 * @param lastToken
126 * <code>Token</code>, not null
127 */
128 public void setLastToken(Token lastToken) {
129 this.lastToken = lastToken;
130 }
131
132 /**
133 * Gets the position of this node in the script.
134 *
135 * @return <code>ScriptCoordinate</code> containing the position of this
136 * node, not null
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 * Get any comments between this node and the previous one.
150 * Each comment is returned without whitespace trimming.
151 * Comments are returned in the order of occurance in the script.
152 * @return collection of strings, not null
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 * Get the last comment before this node and after the last node.
186 * Each comment is returned without whitespace trimming.
187 * Comments are returned in the order of occurance in the script.
188 * @return the comment without whitespace trimming,
189 * or null if there is no comment between this and the last node
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 }