/
IModelExtensions.groovy
459 lines (418 loc) · 12.7 KB
/
IModelExtensions.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
* Copyright 2016, Emanuel Rabina (http://www.ultraq.net.nz/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nz.net.ultraq.thymeleaf.models.extensions
import org.thymeleaf.engine.TemplateModel
import org.thymeleaf.model.ICloseElementTag
import org.thymeleaf.model.IModel
import org.thymeleaf.model.IModelFactory
import org.thymeleaf.model.IOpenElementTag
import org.thymeleaf.model.ITemplateEvent
/**
* Meta-programming extensions to the {@link IModel} class.
*
* @author Emanuel Rabina
*/
class IModelExtensions {
/**
* Applies several new methods to the {@code IModel} class.
*/
@SuppressWarnings(['EmptyIfStatement', 'MethodSize', 'NestedBlockDepth', 'UnnecessaryCallForLastElement'])
static void apply() {
IModel.metaClass {
/**
* Set that a model evaluates to 'false' if it has no events.
*
* @return {@code true} if this model has events.
*/
asBoolean << {
return delegate.size() > 0
}
/**
* If this model represents an element, then this method returns an
* iterator over any potential child items as models of their own.
*
* @param modelFactory
* @return New model iterator.
*/
childModelIterator << {
return delegate.element ? new ChildModelIterator(delegate) : null
}
/**
* Iterate through each event in the model.
*
* @param closure
*/
each << { Closure closure ->
delegate.iterator().each(closure)
}
/**
* Compare 2 models, returning {@code true} if all of the model's events
* are equal.
*
* @param other
* @return {@code true} if this model is the same as the other one.
*/
equals << { Object other ->
if (other instanceof IModel && delegate.size() == other.size()) {
return delegate.everyWithIndex { event, index -> event == other.get(index) }
}
return false
}
/**
* Compare 2 models, returning {@code true} if all of the model's events
* non-whitespace events are equal.
*
* @param other
* @return {@code true} if this model is the same (barring whitespace) as
* the other one.
*/
equalsIgnoreWhitespace << { IModel other ->
if (other instanceof IModel) {
def nonWhitespaceEvents = { event -> !event.whitespace }
return delegate.findAll(nonWhitespaceEvents) == other.findAll(nonWhitespaceEvents)
}
return false
}
/**
* Return {@code true} only if all the events in the model return
* {@code true} for the given closure.
*
* @param closure
* @return {@code true} if every event satisfies the closure.
*/
everyWithIndex << { Closure closure ->
for (def i = 0; i < delegate.size(); i++) {
if (!closure(delegate.get(i), i)) {
return false
}
}
return true
}
/**
* Returns the first event in the model that meets the criteria of the
* given closure.
*
* @param closure
* @return The first event to match the closure criteria, or {@code null}
* if nothing matched.
*/
find << { Closure closure ->
return delegate.iterator().find(closure)
}
/**
* Find all events in the model that match the given closure.
*
* @param closure
* @return A list of matched events.
*/
findAll << { Closure closure ->
return delegate.iterator().findAll(closure)
}
/**
* Returns the index of the first event in the model that meets the
* criteria of the given closure.
*
* @param closure
* @return The index of the first event to match the closure criteria, or
* {@code -1} if nothing matched.
*/
findIndexOf << { Closure closure ->
return delegate.iterator().findIndexOf(closure)
}
/**
* Returns the index of the first event in the model that meets the
* criteria of the given closure, starting from a specified position.
*
* @param closure
* @return The index of the first event to match the closure criteria, or
* {@code -1} if nothing matched.
*/
findIndexOf << { int startIndex, Closure closure ->
return delegate.iterator().findIndexOf(startIndex, closure)
}
/**
* A special variant of {@code findIndexOf} that uses models, as I seem to
* be using those a lot.
*
* This doesn't use an equality check, but an object reference check, so
* if a submodel is ever located from a parent (eg: any of the {@code find}
* methods, you can use this method to find the location of that submodel
* within the event queue.
*
* @param model
* @return Index of an extracted submodel within this model.
*/
findIndexOfModel << { IModel model ->
def modelEvent = model.first()
return delegate.findIndexOf { event -> event.is(modelEvent) }
}
/**
* Returns the first instance of a model that meets the given closure
* criteria.
*
* @param closure
* @return A model over the event that matches the closure criteria, or
* {@code null} if nothing matched.
*/
findModel << { Closure closure ->
return delegate.getModel(delegate.findIndexOf(closure))
}
/**
* Returns the first event on the model.
*
* @return The model's first event.
*/
first << {
return delegate.get(0)
}
/**
* Returns the model at the given index. If the event at the index is an
* opening element, then the returned model will consist of that element
* and all the way through to the matching closing element.
*
* @param pos A valid index within the current model.
* @return Model at the given position, or `null` if the position is
* outside of the event queue.
*/
getModel << { int pos ->
if (0 <= pos && pos < delegate.size()) {
def clone = delegate.cloneModel()
def removeBefore = delegate instanceof TemplateModel ? pos - 1 : pos
def removeAfter = clone.size() - (removeBefore + delegate.sizeOfModelAt(pos))
while (removeBefore-- > 0) {
clone.removeFirst()
}
while (removeAfter-- > 0) {
clone.removeLast()
}
return clone
}
return null
}
/**
* Inserts a model, creating a whitespace event before it so that it
* appears in line with all the existing events.
*
* @param pos A valid index within the current model.
* @param model
* @param modelFactory
*/
insertModelWithWhitespace << { int pos, IModel model, IModelFactory modelFactory ->
if (0 <= pos && pos <= delegate.size()) {
// Use existing whitespace found at or before the insertion point
def whitespace = delegate.getModel(pos)
if (whitespace?.whitespace) {
delegate.insertModel(pos, model)
delegate.insertModel(pos, whitespace)
return
}
if (pos > 0) {
whitespace = delegate.getModel(pos - 1)
if (whitespace?.whitespace) {
delegate.insertModel(pos, whitespace)
delegate.insertModel(pos, model)
return
}
}
// Generate whitespace on either side of the model to insert
whitespace = modelFactory.createModel(modelFactory.createText('\n\t'))
delegate.insertModel(pos, whitespace)
delegate.insertModel(pos, model)
delegate.insertModel(pos, whitespace)
}
}
/**
* Inserts an event, creating a whitespace event before it so that it
* appears in line with all the existing events.
*
* @param pos A valid index within the current model.
* @param event
* @param modelFactory
*/
insertWithWhitespace << { int pos, ITemplateEvent event, IModelFactory modelFactory ->
if (0 <= pos && pos <= delegate.size()) {
// TODO: Because I can't check the parent for whitespace hints, I
// should make this smarter and find whitespace within the model
// to copy.
def whitespace = delegate.getModel(pos)
if (whitespace?.whitespace) {
delegate.insert(pos, event)
delegate.insertModel(pos, whitespace)
}
else {
def newLine = modelFactory.createText('\n')
if (pos == 0) {
delegate.insert(pos, newLine)
delegate.insert(pos, event)
}
else if (pos == delegate.size()) {
delegate.insert(pos, newLine)
delegate.insert(pos, event)
delegate.insert(pos, newLine)
}
}
}
}
/**
* Returns whether or not this model represents an element with potential
* child elements.
*
* @return {@code true} if the first event in this model is an opening tag
* and the last event is the matching closing tag.
*/
isElement << {
return delegate.first().openingElement && delegate.last().closingElement
}
/**
* Returns whether or not this model represents an element of the given
* name.
*
* @param tagName
* @return {@code true} if the first event in this model is an opening tag,
* the last event is the matching closing tag, and whether the
* element has the given tag name.
*/
isElementOf << { tagName ->
return delegate.element && delegate.first().elementCompleteName == tagName
}
/**
* Returns whether or not this model represents collapsible whitespace.
*
* @return {@code true} if this is a collapsible text model.
*/
isWhitespace << {
return delegate.size() == 1 && delegate.first().whitespace
}
/**
* Used to make this class iterable as an event queue.
*
* @return A new iterator over the events of this model.
*/
iterator << {
return new EventIterator(delegate)
}
/**
* Returns the last event on the model.
*
* @return The model's last event.
*/
last << {
return delegate.get(delegate.size() - 1)
}
/**
* If the model represents an element open to close tags, then this method
* removes all of the inner events.
*/
removeChildren << {
if (delegate.element) {
while (delegate.size() > 2) {
delegate.remove(1)
}
}
}
/**
* Removes the first event on the model.
*/
removeFirst << {
delegate.remove(0)
}
/**
* Removes the last event on the model.
*/
removeLast << {
delegate.remove(delegate.size() - 1)
}
/**
* Removes a models-worth of events from the specified position. What
* this means is that, if the event at the position is an opening element,
* then it, and everything up to and including its matching end element,
* is removed.
*
* @param pos A valid index within the current model.
*/
removeModel << { int pos ->
if (0 <= pos && pos < delegate.size()) {
def modelSize = delegate.sizeOfModelAt(pos)
while (modelSize > 0) {
delegate.remove(pos)
modelSize--
}
}
}
/**
* Replaces the model at the specified index with the given model.
*
* @param pos A valid index within the current model.
* @param model
*/
replaceModel << { int pos, IModel model ->
if (0 <= pos && pos < delegate.size()) {
delegate.removeModel(pos)
delegate.insertModel(pos, model)
}
}
/**
* If an opening element exists at the given position, this method will
* return the 'size' of that element (number of events from here to its
* matching closing tag).
*
* @param model
* @param index
* @return Size of an element from the given position, or 1 if the event
* at the position isn't an opening element.
*/
sizeOfModelAt << { int index ->
def eventIndex = index
def event = delegate.get(eventIndex++)
if (event instanceof IOpenElementTag) {
def level = 0
while (true) {
event = delegate.get(eventIndex++)
if (event instanceof IOpenElementTag) {
level++
}
else if (event instanceof ICloseElementTag) {
if (event.unmatched) {
// Do nothing. Unmatched closing tags do not correspond to any
// opening element, and so should not affect the model level.
}
else if (level == 0) {
break
}
else {
level--
}
}
}
return eventIndex - index
}
return 1
}
/**
* Removes whitespace events from the head and tail of the model's
* underlying event queue.
*/
trim << {
while (delegate.first().whitespace) {
delegate.removeFirst()
}
while (delegate.last().whitespace) {
delegate.removeLast()
}
}
}
}
}