1 | /* |
---|---|
2 | * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. |
3 | * Copyright (C) 2011, 2013-2020 The JavaParser Team. |
4 | * |
5 | * This file is part of JavaParser. |
6 | * |
7 | * JavaParser can be used either under the terms of |
8 | * a) the GNU Lesser General Public License as published by |
9 | * the Free Software Foundation, either version 3 of the License, or |
10 | * (at your option) any later version. |
11 | * b) the terms of the Apache License |
12 | * |
13 | * You should have received a copy of both licenses in LICENCE.LGPL and |
14 | * LICENCE.APACHE. Please refer to those files for details. |
15 | * |
16 | * JavaParser is distributed in the hope that it will be useful, |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | * GNU Lesser General Public License for more details. |
20 | */ |
21 | |
22 | package com.github.javaparser.printer.lexicalpreservation; |
23 | |
24 | import com.github.javaparser.ast.Node; |
25 | |
26 | import java.util.LinkedList; |
27 | import java.util.List; |
28 | |
29 | /** |
30 | * This contains the lexical information for a single node. |
31 | * It is basically a list of tokens and children. |
32 | */ |
33 | class NodeText { |
34 | private final List<TextElement> elements; |
35 | |
36 | public static final int NOT_FOUND = -1; |
37 | |
38 | // |
39 | // Constructors |
40 | // |
41 | |
42 | NodeText(List<TextElement> elements) { |
43 | this.elements = elements; |
44 | } |
45 | |
46 | /** |
47 | * Initialize with an empty list of elements. |
48 | */ |
49 | NodeText() { |
50 | this(new LinkedList<>()); |
51 | } |
52 | |
53 | // |
54 | // Adding elements |
55 | // |
56 | |
57 | /** |
58 | * Add an element at the end. |
59 | */ |
60 | void addElement(TextElement nodeTextElement) { |
61 | this.elements.add(nodeTextElement); |
62 | } |
63 | |
64 | /** |
65 | * Add an element at the given position. |
66 | */ |
67 | void addElement(int index, TextElement nodeTextElement) { |
68 | this.elements.add(index, nodeTextElement); |
69 | } |
70 | |
71 | void addChild(Node child) { |
72 | addElement(new ChildTextElement(child)); |
73 | } |
74 | |
75 | void addChild(int index, Node child) { |
76 | addElement(index, new ChildTextElement(child)); |
77 | } |
78 | |
79 | void addToken(int tokenKind, String text) { |
80 | elements.add(new TokenTextElement(tokenKind, text)); |
81 | } |
82 | |
83 | void addToken(int index, int tokenKind, String text) { |
84 | elements.add(index, new TokenTextElement(tokenKind, text)); |
85 | } |
86 | |
87 | // |
88 | // Finding elements |
89 | // |
90 | |
91 | int findElement(TextElementMatcher matcher) { |
92 | return findElement(matcher, 0); |
93 | } |
94 | |
95 | int findElement(TextElementMatcher matcher, int from) { |
96 | int res = tryToFindElement(matcher, from); |
97 | if (res == NOT_FOUND) { |
98 | throw new IllegalArgumentException( |
99 | String.format("I could not find child '%s' from position %d. Elements: %s", matcher, from, elements)); |
100 | } |
101 | return res; |
102 | } |
103 | |
104 | int tryToFindElement(TextElementMatcher matcher, int from) { |
105 | for (int i = from; i < elements.size(); i++) { |
106 | TextElement element = elements.get(i); |
107 | if (matcher.match(element)) { |
108 | return i; |
109 | } |
110 | } |
111 | return NOT_FOUND; |
112 | } |
113 | |
114 | int findChild(Node child) { |
115 | return findChild(child, 0); |
116 | } |
117 | |
118 | int findChild(Node child, int from) { |
119 | return findElement(TextElementMatchers.byNode(child), from); |
120 | } |
121 | |
122 | int tryToFindChild(Node child) { |
123 | return tryToFindChild(child, 0); |
124 | } |
125 | |
126 | int tryToFindChild(Node child, int from) { |
127 | return tryToFindElement(TextElementMatchers.byNode(child), from); |
128 | } |
129 | |
130 | // |
131 | // Removing single elements |
132 | // |
133 | |
134 | public void remove(TextElementMatcher matcher, boolean potentiallyFollowingWhitespace) { |
135 | int i = 0; |
136 | for (TextElement e : elements) { |
137 | if (matcher.match(e)) { |
138 | elements.remove(e); |
139 | if (potentiallyFollowingWhitespace) { |
140 | if (i < elements.size()) { |
141 | if (elements.get(i).isWhiteSpace()) { |
142 | elements.remove(i); |
143 | } |
144 | } else { |
145 | throw new UnsupportedOperationException(); |
146 | } |
147 | } |
148 | return; |
149 | } |
150 | } |
151 | throw new IllegalArgumentException(); |
152 | } |
153 | |
154 | // |
155 | // Removing sequences |
156 | // |
157 | |
158 | void removeElement(int index) { |
159 | elements.remove(index); |
160 | } |
161 | |
162 | // |
163 | // Replacing elements |
164 | // |
165 | |
166 | void replace(TextElementMatcher position, TextElement newElement) { |
167 | int index = findElement(position, 0); |
168 | elements.remove(index); |
169 | elements.add(index, newElement); |
170 | } |
171 | |
172 | // |
173 | // Other methods |
174 | // |
175 | |
176 | /** |
177 | * Generate the corresponding string. |
178 | */ |
179 | String expand() { |
180 | StringBuffer sb = new StringBuffer(); |
181 | |
182 | elements.forEach(e -> sb.append(e.expand())); |
183 | return sb.toString(); |
184 | } |
185 | |
186 | // Visible for testing |
187 | int numberOfElements() { |
188 | return elements.size(); |
189 | } |
190 | |
191 | // Visible for testing |
192 | TextElement getTextElement(int index) { |
193 | return elements.get(index); |
194 | } |
195 | |
196 | // Visible for testing |
197 | List<TextElement> getElements() { |
198 | return elements; |
199 | } |
200 | |
201 | @Override |
202 | public String toString() { |
203 | return "NodeText{" + elements + '}'; |
204 | } |
205 | |
206 | } |
207 |
Members