-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Element.java
2567 lines (2386 loc) · 103 KB
/
Element.java
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package net.aufdemrand.denizencore.objects;
import net.aufdemrand.denizencore.scripts.commands.core.Comparable;
import net.aufdemrand.denizencore.scripts.queues.ScriptQueue;
import net.aufdemrand.denizencore.tags.Attribute;
import net.aufdemrand.denizencore.tags.TagContext;
import net.aufdemrand.denizencore.tags.TagManager;
import net.aufdemrand.denizencore.tags.core.EscapeTags;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import net.aufdemrand.denizencore.utilities.SQLEscaper;
import net.aufdemrand.denizencore.utilities.debugging.dB;
import javax.xml.bind.DatatypeConverter;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
// <--[language]
// @name Element
// @group Object System
// @description
// Elements are simple objects that contain either a boolean (true/false),
// string, or number value. Their main usage is within the replaceable tag
// system, often times returned from the use of another tag that isn't returning
// a specific object type, such as a location or entity. For example,
// <player.name> or <li@item|item2|item3.as_cslist> will both return Elements.
//
// Pluses to the Element system is the ability to utilize its attributes that
// can provide a range of functionality that should be familiar from any other
// programming language, such as 'to_uppercase', 'split', 'replace', 'contains',
// as_int, any many more. See 'element' tags for more information.
//
// While information fetched from other tags resulting in an Element is often
// times automatically handled, it may be desirable to utilize element
// attributes from strings/numbers/etc. that aren't already an element object.
// To accomplish this, the object fetcher can be used to create a new element.
// Element has a constructor, el@val[element_value], that will allow the
// creation of a new element. For example: <el@val[This_is_a_test.].to_uppercase>
// will result in the value 'THIS_IS_A_TEST.' Note that while other objects often
// return their object identifier (el@, li@, e@, etc.), elements do not.
// -->
public class Element implements dObject, dObject.ObjectAttributable {
@Deprecated
public final static Element TRUE = new Element(Boolean.TRUE);
@Deprecated
public final static Element FALSE = new Element(Boolean.FALSE);
@Deprecated
public final static Element SERVER = new Element("server");
@Deprecated
public final static Element NULL = new Element("null");
final static Pattern VALUE_PATTERN =
Pattern.compile("el@val(?:ue)?\\[([^\\[\\]]+)\\].*",
Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE);
public static Element valueOf(String string) {
return valueOf(string, null);
}
/**
* @param string the string or dScript argument String
* @return a dScript dList
*/
@Fetchable("el")
public static Element valueOf(String string, TagContext context) {
if (string == null) {
return null;
}
Matcher m = VALUE_PATTERN.matcher(string);
// Allow construction of elements with el@val[<value>]
if (m.matches()) {
String value = m.group(1);
return new Element(value);
}
return new Element(CoreUtilities.toLowerCase(string).startsWith("el@") ? string.substring(3) : string);
}
public static boolean matches(String string) {
return string != null;
}
/**
* Handle null dObjects appropriately for potentionally null tags.
* Will show a dB error message and return Element.NULL for null objects.
*
* @param tag The input string that produced a potentially null object, for debugging.
* @param object The potentially null object.
* @param type The type of object expected, for debugging. (EG: 'dNPC')
* @return The object or Element.NULL if the object is null.
*/
public static <T extends dObject> T handleNull(String tag, T object, String type, boolean has_fallback) {
if (object == null) {
if (!has_fallback) {
dB.echoError("'" + tag + "' is an invalid " + type + "!");
}
return null;
}
return object;
}
private final String element;
public Element(String string) {
this.prefix = "element";
if (string == null) {
if (dB.verbose) {
try {
throw new RuntimeException("Trace");
}
catch (Exception ex) {
dB.echoError(ex);
}
dB.log("Element - Null construction!");
}
this.element = "null";
}
else {
this.element = TagManager.cleanOutput(string);
}
}
public Element(boolean bool) {
this.prefix = "boolean";
this.element = String.valueOf(bool);
}
public Element(int integer) {
this.prefix = "number";
this.element = String.valueOf(integer);
}
public Element(byte byt) {
this.prefix = "number";
this.element = String.valueOf(byt);
}
public Element(short shrt) {
this.prefix = "number";
this.element = String.valueOf(shrt);
}
public Element(long lng) {
this.prefix = "number";
this.element = String.valueOf(lng);
}
public Element(BigDecimal bdl) {
this.prefix = "decimal";
this.element = CoreUtilities.bigDecToString(bdl);
}
public Element(double dbl) {
this.prefix = "decimal";
this.element = CoreUtilities.doubleToString(dbl);
}
public Element(float flt) {
this.prefix = "decimal";
this.element = CoreUtilities.doubleToString(flt);
}
public Element(String prefix, String string) {
if (prefix == null) {
this.prefix = "element";
}
else {
this.prefix = prefix;
}
this.element = TagManager.cleanOutput(string);
}
static final BigDecimal max = new BigDecimal("10E1000");
private BigDecimal getBD(String text) {
BigDecimal bd = new BigDecimal(text);
if (bd.compareTo(max) >= 1) {
dB.echoError("Unreasonably large number detected!");
return max;
}
return bd;
}
public BigDecimal asBigDecimal() {
return getBD(element.replaceAll("%", ""));
}
public double asDouble() {
return Double.valueOf(element.replaceAll("%", ""));
}
public float asFloat() {
return Float.valueOf(element.replaceAll("%", ""));
}
public int asInt() {
try {
return Integer.valueOf(element.replaceAll("(%)|(\\.\\d+)", ""));
}
catch (NumberFormatException ex) {
dB.echoError("'" + element + "' is not a valid integer!");
return 0;
}
}
public long asLong() {
try {
return Long.valueOf(element.replaceAll("(%)|(\\.\\d+)", ""));
}
catch (NumberFormatException ex) {
dB.echoError("'" + element + "' is not a valid integer!");
return 0;
}
}
public boolean asBoolean() {
return Boolean.valueOf(element.replaceAll("el@", ""));
}
public String asString() {
return element;
}
public boolean isBoolean() {
return (element != null && (element.equalsIgnoreCase("true") || element.equalsIgnoreCase("false")));
}
public boolean isDouble() {
try {
if (!Double.valueOf(element).isNaN()) {
return true;
}
}
catch (Exception e) {
}
return false;
}
public boolean isFloat() {
try {
if (!Float.valueOf(element).isNaN()) {
return true;
}
}
catch (Exception e) {
}
return false;
}
public boolean isInt() {
try {
Integer val = Integer.valueOf(element.replaceAll("(%)|(\\.\\d+)", ""));
if (val.hashCode() != 0.5) { // if intentionally always passes
return true;
}
}
catch (Exception e) {
}
return false;
}
public boolean isString() {
return (element != null && !element.isEmpty());
}
public boolean matchesType(Class<? extends dObject> dClass) {
return ObjectFetcher.checkMatch(dClass, element);
}
public <T extends dObject> T asType(Class<T> dClass) {
return ObjectFetcher.getObjectFrom(dClass, element);
}
public <T extends dObject> T asType(Class<T> dClass, TagContext context) {
return ObjectFetcher.getObjectFrom(dClass, element, context);
}
public boolean matchesEnum(Enum[] values) {
for (Enum value : values) {
if (value.name().equalsIgnoreCase(element)) {
return true;
}
}
return false;
}
private String prefix;
@Override
public String getObjectType() {
return "Element";
}
@Override
public String getPrefix() {
return prefix;
}
@Override
public dObject setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
@Override
public String debug() {
return (prefix + "='<A>" + identify() + "<G>' ");
}
@Override
public String identify() {
return element;
}
@Override
public String identifySimple() {
return identify();
}
@Override
public String toString() {
return identify();
}
@Override
public boolean isUnique() {
return false;
}
public static void registerTags() {
/////////////////////
// CONVERSION ATTRIBUTES
/////////////////
registerTag("is", new TagRunnable.ObjectForm() {
@Override
public dObject run(Attribute attribute, dObject object) {
// <--[tag]
// @attribute <el@element.is[<operator>].to[<element>]>
// @returns Element(Boolean)
// @group comparison
// @description
// Takes an operator, and compares the value of the element to the supplied
// element. Returns the outcome of the comparable, either true or false. For
// information on operators, see <@link language operator>.
// Equivalent to <@link tag el@element.is[<operator>].than[<element>]>
// -->
// <--[tag]
// @attribute <el@element.is[<operator>].than[<element>]>
// @returns Element(Boolean)
// @group comparison
// @description
// Takes an operator, and compares the value of the element to the supplied
// element. Returns the outcome of the comparable, either true or false. For
// information on operators, see <@link language operator>.
// Equivalent to <@link tag el@element.is[<operator>].to[<element>]>
// -->
if (attribute.hasContext(1)
&& (attribute.startsWith("to", 2) || attribute.startsWith("than", 2)) && attribute.hasContext(2)) {
// Use the Comparable object as implemented for the IF command. First, a new Comparable!
Comparable com = new Comparable();
// Check for negative logic
String operator;
if (attribute.getContext(1).startsWith("!")) {
operator = attribute.getContext(1).substring(1);
com.setNegativeLogic();
}
else {
operator = attribute.getContext(1);
}
// Operator is the value of the .is[] context. Valid are Comparable.Operators, same
// as used by the IF command.
Comparable.Operator comparableOperator = null;
try {
comparableOperator = Comparable.Operator.valueOf(operator.replace("==", "EQUALS")
.replace(">=", "OR_MORE").replace("<=", "OR_LESS").replace("<", "LESS")
.replace(">", "MORE").replace("=", "EQUALS").toUpperCase());
}
catch (IllegalArgumentException e) {
}
if (comparableOperator != null) {
com.setOperator(comparableOperator);
// Comparable is the value of this element
com.setComparable(object.toString());
// Compared_to is the value of the .to[] context.
com.setComparedto(attribute.getContext(2));
return new Element(com.determineOutcome()).getObjectAttribute(attribute.fulfill(2));
}
else {
net.aufdemrand.denizencore.utilities.debugging.dB.echoError("Unknown operator '" + operator + "'.");
}
}
return null;
}
});
// <--[tag]
// @attribute <el@element.as_element>
// @returns Element
// @group conversion
// @description
// Returns the element as itself.
// For use in special cases, generally not very useful.
// -->
registerTag("as_element", new TagRunnable.ObjectForm() {
@Override
public dObject run(Attribute attribute, dObject object) {
return ((Element) object).getObjectAttribute(attribute.fulfill(1));
}
});
registerTag("aselement", registeredObjectTags.get("as_element"));
// <--[tag]
// @attribute <el@element.as_boolean>
// @returns Element(Boolean)
// @group conversion
// @description
// Returns the element as true/false.
// -->
registerTag("as_boolean", new TagRunnable.ObjectForm() {
@Override
public dObject run(Attribute attribute, dObject object) {
String element = ((Element) object).element;
return new Element(element.equalsIgnoreCase("true")
|| element.equalsIgnoreCase("t")
|| element.equalsIgnoreCase("1"))
.getObjectAttribute(attribute.fulfill(1));
}
});
registerTag("asboolean", registeredObjectTags.get("as_boolean"));
// <--[tag]
// @attribute <el@element.as_decimal>
// @returns Element(Decimal)
// @group conversion
// @description
// Returns the element as a decimal number, or shows an error.
// -->
registerTag("as_decimal", new TagRunnable.ObjectForm() {
@Override
public dObject run(Attribute attribute, dObject object) {
String element = ((Element) object).element;
try {
return new Element(Double.valueOf(element))
.getObjectAttribute(attribute.fulfill(1));
}
catch (NumberFormatException e) {
if (!attribute.hasAlternative()) {
dB.echoError("'" + element + "' is not a valid decimal number.");
}
return null;
}
}
});
registerTag("as_double", registeredObjectTags.get("as_decimal"));
registerTag("asdouble", registeredObjectTags.get("as_decimal"));
// <--[tag]
// @attribute <el@element.as_int>
// @returns Element(Number)
// @group conversion
// @description
// Returns the element as a number without a decimal. Rounds decimal values.
// NOTE: Please use .round_down instead of .as_int!
// -->
registerTag("as_int", new TagRunnable.ObjectForm() {
@Override
public dObject run(Attribute attribute, dObject object) {
String element = ((Element) object).element;
try {
return new Element(Double.valueOf(element).longValue())
.getObjectAttribute(attribute.fulfill(1));
}
catch (NumberFormatException e) {
if (!attribute.hasAlternative()) {
dB.echoError("'" + element + "' is not a valid decimal number.");
}
return null;
}
}
});
registerTag("asint", registeredObjectTags.get("as_int"));
// <--[tag]
// @attribute <el@element.as_money>
// @returns Element(Decimal)
// @group conversion
// @description
// Returns the element as a number with two decimal places.
// -->
registerTag("as_money", new TagRunnable.ObjectForm() {
@Override
public dObject run(Attribute attribute, dObject object) {
String element = ((Element) object).element;
try {
DecimalFormat d = new DecimalFormat("0.00");
return new Element(d.format(Double.valueOf(element)))
.getObjectAttribute(attribute.fulfill(1));
}
catch (NumberFormatException e) {
if (!attribute.hasAlternative()) {
dB.echoError("'" + element + "' is not a valid decimal number.");
}
return null;
}
}
});
registerTag("asmoney", registeredObjectTags.get("as_money"));
// <--[tag]
// @attribute <el@element.as_list>
// @returns dList
// @group conversion
// @description
// Returns the element as a dList.
// -->
registerTag("as_list", new TagRunnable.ObjectForm() {
@Override
public dObject run(Attribute attribute, dObject object) {
String element = ((Element) object).element;
dList obj = handleNull(element, dList.valueOf(element), "dList", attribute.hasAlternative());
if (obj != null) {
return obj.getObjectAttribute(attribute.fulfill(1));
}
return null;
}
});
registerTag("aslist", registeredObjectTags.get("as_list"));
// <--[tag]
// @attribute <el@element.as_custom>
// @returns dList
// @group conversion
// @description
// Returns the element as a custom object.
// -->
registerTag("as_custom", new TagRunnable.ObjectForm() {
@Override
public dObject run(Attribute attribute, dObject object) {
String element = ((Element) object).element;
CustomObject obj = handleNull(element, CustomObject.valueOf(element, null), "Custom", attribute.hasAlternative());
if (obj != null) {
return obj.getObjectAttribute(attribute.fulfill(1));
}
return null;
}
});
registerTag("ascustom", registeredObjectTags.get("as_custom"));
// <--[tag]
// @attribute <el@element.as_script>
// @returns dScript
// @group conversion
// @description
// Returns the element as a dScript.
// Note: the value must be a valid script.
// -->
registerTag("as_script", new TagRunnable.ObjectForm() {
@Override
public dObject run(Attribute attribute, dObject object) {
String element = ((Element) object).element;
dScript obj = handleNull(element, dScript.valueOf(element), "dScript", attribute.hasAlternative());
if (obj != null) {
return CoreUtilities.autoAttrib(obj, attribute.fulfill(1));
}
return null;
}
});
registerTag("asscript", registeredObjectTags.get("as_script"));
// <--[tag]
// @attribute <el@element.as_queue>
// @returns ScriptQueue
// @group conversion
// @description
// Returns the element as a ScriptQueue.
// Note: the value must be a valid ScriptQueue.
// -->
registerTag("as_queue", new TagRunnable.ObjectForm() {
@Override
public dObject run(Attribute attribute, dObject object) {
String element = ((Element) object).element;
ScriptQueue obj = handleNull(element, ScriptQueue.valueOf(element), "ScriptQueue", attribute.hasAlternative());
if (obj != null) {
return CoreUtilities.autoAttrib(obj, attribute.fulfill(1));
}
return null;
}
});
registerTag("asqueue", registeredObjectTags.get("as_queue"));
// <--[tag]
// @attribute <el@element.as_duration>
// @returns Duration
// @group conversion
// @description
// Returns the element as a Duration.
// Note: the value must be a valid Duration.
// -->
registerTag("as_duration", new TagRunnable.ObjectForm() {
@Override
public dObject run(Attribute attribute, dObject object) {
String element = ((Element) object).element;
Duration obj = handleNull(element, Duration.valueOf(element), "Duration", attribute.hasAlternative());
if (obj != null) {
return CoreUtilities.autoAttrib(obj, attribute.fulfill(1));
}
return null;
}
});
registerTag("asduration", registeredObjectTags.get("as_duration"));
// <--[tag]
// @attribute <el@element.escaped>
// @returns Element
// @group conversion
// @description
// Returns the element, escaped for safe reuse.
// Inverts <@link tag el@element.unescaped>
// See <@link language property escaping>
// -->
registerTag("escaped", new TagRunnable.ObjectForm() {
@Override
public dObject run(Attribute attribute, dObject object) {
String element = ((Element) object).element;
return new Element(EscapeTags.escape(element)).getObjectAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <el@element.sql_escaped>
// @returns Element
// @group conversion
// @description
// Returns the element, escaped for safe use in SQL.
// -->
registerTag("sql_escaped", new TagRunnable.ObjectForm() {
@Override
public dObject run(Attribute attribute, dObject object) {
String element = ((Element) object).element;
return new Element(SQLEscaper.escapeSQL(element)).getObjectAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <el@element.unescaped>
// @returns Element
// @group conversion
// @description
// Returns the element, unescaped.
// Inverts <@link tag el@element.escaped>
// See <@link language property escaping>
// -->
registerTag("unescaped", new TagRunnable.ObjectForm() {
@Override
public dObject run(Attribute attribute, dObject object) {
String element = ((Element) object).element;
return new Element(EscapeTags.unEscape(element)).getObjectAttribute(attribute.fulfill(1));
}
});
/////////////////////
// DEBUG ATTRIBUTES
/////////////////
// <--[tag]
// @attribute <el@element.debug>
// @returns Element
// @group debug
// @description
// Returns a standard debug representation of the Element.
// -->
registerTag("debug", new TagRunnable.ObjectForm() {
@Override
public dObject run(Attribute attribute, dObject object) {
return new Element(object.debug())
.getObjectAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <el@element.prefix>
// @returns Element
// @group debug
// @description
// Returns the prefix of the element.
// -->
registerTag("prefix", new TagRunnable.ObjectForm() {
@Override
public dObject run(Attribute attribute, dObject object) {
return new Element(object.getPrefix())
.getObjectAttribute(attribute.fulfill(1));
}
});
/////////////////////
// ELEMENT CHECKING ATTRIBUTES
/////////////////
// <--[tag]
// @attribute <el@element.difference[<element>]>
// @returns Element(Number)
// @group element checking
// @description
// Returns a number representing the difference between the two elements. (Uses Levenshtein logic).
// -->
registerTag("difference", new TagRunnable.ObjectForm() {
@Override
public dObject run(Attribute attribute, dObject object) {
String element = ((Element) object).element;
String two = attribute.getContext(1);
return new Element(CoreUtilities.getLevenshteinDistance(element, two))
.getObjectAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <el@element.contains_any_case_sensitive_text[<element>|...]>
// @returns Element(Boolean)
// @group element checking
// @description
// Returns whether the element contains any of a list of specified elements, case sensitive.
// -->
registerTag("contains_any_case_sensitive", new TagRunnable.ObjectForm() {
@Override
public dObject run(Attribute attribute, dObject object) {
String element = ((Element) object).element;
dList list = dList.valueOf(attribute.getContext(1));
for (String list_element : list) {
if (element.contains(list_element)) {
return new Element(true).getObjectAttribute(attribute.fulfill(1));
}
}
return new Element(false).getObjectAttribute(attribute.fulfill(1));
}
});
TagRunnable.ObjectForm r = registeredObjectTags.get("contains_any_case_sensitive").clone();
r.name = null;
registerTag("contains_any_case_sensitive_text", r);
// <--[tag]
// @attribute <el@element.contains_any_text[<element>|...]>
// @returns Element(Boolean)
// @group element checking
// @description
// Returns whether the element contains any of a list of specified elements, case insensitive.
// -->
registerTag("contains_any", new TagRunnable.ObjectForm() {
@Override
public dObject run(Attribute attribute, dObject object) {
String element = ((Element) object).element;
dList list = dList.valueOf(CoreUtilities.toLowerCase(attribute.getContext(1)));
String ellow = CoreUtilities.toLowerCase(element);
for (String list_element : list) {
if (ellow.contains(list_element)) {
return new Element(true).getObjectAttribute(attribute.fulfill(1));
}
}
return new Element(false).getObjectAttribute(attribute.fulfill(1));
}
});
r = registeredObjectTags.get("contains_any").clone();
r.name = null;
registerTag("contains_any_text", r);
// <--[tag]
// @attribute <el@element.contains_case_sensitive_text[<element>]>
// @returns Element(Boolean)
// @group element checking
// @description
// Returns whether the element contains a specified element, case sensitive.
// -->
registerTag("contains_case_sensitive", new TagRunnable.ObjectForm() {
@Override
public dObject run(Attribute attribute, dObject object) {
String element = ((Element) object).element;
String contains = attribute.getContext(1);
if (element.contains(contains)) {
return new Element("true").getObjectAttribute(attribute.fulfill(1));
}
else {
return new Element("false").getObjectAttribute(attribute.fulfill(1));
}
}
});
r = registeredObjectTags.get("contains_case_sensitive").clone();
r.name = null;
registerTag("contains_case_sensitive_text", r);
// <--[tag]
// @attribute <el@element.contains_text[<element>]>
// @returns Element(Boolean)
// @group element checking
// @description
// Returns whether the element contains a specified element, case insensitive. Can use
// regular expression by prefixing the element with regex:
// -->
registerTag("contains", new TagRunnable.ObjectForm() {
@Override
public dObject run(Attribute attribute, dObject object) {
String element = ((Element) object).element;
String contains = attribute.getContext(1);
if (CoreUtilities.toLowerCase(contains).startsWith("regex:")) {
if (Pattern.compile(contains.substring(("regex:").length()), Pattern.CASE_INSENSITIVE).matcher(element).matches()) {
return new Element("true").getObjectAttribute(attribute.fulfill(1));
}
else {
return new Element("false").getObjectAttribute(attribute.fulfill(1));
}
}
else if (CoreUtilities.toLowerCase(element).contains(CoreUtilities.toLowerCase(contains))) {
return new Element("true").getObjectAttribute(attribute.fulfill(1));
}
else {
return new Element("false").getObjectAttribute(attribute.fulfill(1));
}
}
});
r = registeredObjectTags.get("contains").clone();
r.name = null;
registerTag("contains_text", r);
// <--[tag]
// @attribute <el@element.contains_all_text[<element>|...]>
// @returns Element(Boolean)
// @group element checking
// @description
// Returns whether the element contains all of the specified strings, case insensitive.
// -->
registerTag("contains_all", new TagRunnable.ObjectForm() {
@Override
public dObject run(Attribute attribute, dObject object) {
String element = ((Element) object).element;
dList list = dList.valueOf(CoreUtilities.toLowerCase(attribute.getContext(1)));
String ellow = CoreUtilities.toLowerCase(element);
for (String list_element : list) {
if (!ellow.contains(list_element)) {
return new Element("false").getObjectAttribute(attribute.fulfill(1));
}
}
return new Element("true").getObjectAttribute(attribute.fulfill(1));
}
});
r = registeredObjectTags.get("contains_all").clone();
r.name = null;
registerTag("contains_all_text", r);
// <--[tag]
// @attribute <el@element.contains_all_case_sensitive_text[<element>|...]>
// @returns Element(Boolean)
// @group element checking
// @description
// Returns whether the element contains all of the specified strings, case sensitive.
// -->
registerTag("contains_all_case_sensitive", new TagRunnable.ObjectForm() {
@Override
public dObject run(Attribute attribute, dObject object) {
String element = ((Element) object).element;
dList list = dList.valueOf(attribute.getContext(1));
for (String list_element : list) {
if (!element.contains(list_element)) {
return new Element("false").getObjectAttribute(attribute.fulfill(1));
}
}
return new Element("true").getObjectAttribute(attribute.fulfill(1));
}
});
r = registeredObjectTags.get("contains_all_case_sensitive").clone();
r.name = null;
registerTag("contains_all_case_sensitive_text", r);
// <--[tag]
// @attribute <el@element.ends_with[<element>]>
// @returns Element(Boolean)
// @group element checking
// @description
// Returns whether the element ends with a specified element.
// -->
registerTag("ends_with", new TagRunnable.ObjectForm() {
@Override
public dObject run(Attribute attribute, dObject object) {
return new Element(CoreUtilities.toLowerCase(((Element) object).element).
endsWith(CoreUtilities.toLowerCase(attribute.getContext(1))))
.getObjectAttribute(attribute.fulfill(1));
}
});
registerTag("endswith", registeredObjectTags.get("ends_with"));
// <--[tag]
// @attribute <el@element.equals_case_sensitive[<element>]>
// @returns Element(Boolean)
// @group element checking
// @description
// Returns whether the element matches another element, case-sensitive.
// -->
registerTag("equals_case_sensitive", new TagRunnable.ObjectForm() {
@Override
public dObject run(Attribute attribute, dObject object) {
if (!attribute.hasContext(1)) {
dB.echoError("The tag el@element.equals_case_sensitive[...] must have a value.");
return null;
}
return new Element(((Element) object).element.equals(attribute.getContext(1)))
.getObjectAttribute(attribute.fulfill(1));
}
});
registerTag("equals_with_case", registeredObjectTags.get("equals_case_sensitive"));
// <--[tag]
// @attribute <el@element.matches[<regex>]>
// @returns Element(Boolean)
// @group element checking
// @description
// Returns whether the element matches a regex input.
// -->
registerTag("matches", new TagRunnable.ObjectForm() {
@Override
public dObject run(Attribute attribute, dObject object) {
if (!attribute.hasContext(1)) {
dB.echoError("The tag el@element.matches[...] must have a value.");
return null;
}
return new Element(((Element) object).element.matches(attribute.getContext(1))).getObjectAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <el@element.regex[<regex>].group[<group>]>
// @returns Element
// @group element checking
// @description
// Returns the specific group from a regex match.
// Specify group 0 for the whole match.
// For example, <el@val[hello5world].regex[.*(\d).*].group[1]> returns '5'.
// -->
registerTag("regex", new TagRunnable.ObjectForm() {
@Override
public dObject run(Attribute attribute, dObject object) {
if (!attribute.hasContext(1) || !attribute.hasContext(2)) {
dB.echoError("The tag el@element.regex[...] must have a value.");
return null;
}
String regex = attribute.getContext(1);
Matcher m = Pattern.compile(regex).matcher(((Element) object).element);
if (!m.matches()) {
return null;
}
int group = new Element(attribute.getContext(2)).asInt();
if (group < 0) {
group = 0;
}
if (group > m.groupCount()) {
group = m.groupCount();
}
return new Element(m.group(group)).getObjectAttribute(attribute.fulfill(2));
}
});
// <--[tag]
// @attribute <el@element.length>
// @returns Element(Number)
// @group element checking
// @description
// Returns the length of the element.
// -->
registerTag("length", new TagRunnable.ObjectForm() {
@Override
public dObject run(Attribute attribute, dObject object) {
return new Element(((Element) object).element.length()).getObjectAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <el@element.not>
// @returns Element(Boolean)