-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
TagNode.java
152 lines (136 loc) · 4.45 KB
/
TagNode.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
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2022 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.kyori.adventure.text.minimessage.internal.parser.node;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import net.kyori.adventure.text.minimessage.internal.parser.ParsingExceptionImpl;
import net.kyori.adventure.text.minimessage.internal.parser.Token;
import net.kyori.adventure.text.minimessage.internal.parser.TokenParser;
import net.kyori.adventure.text.minimessage.tag.Tag;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Node that represents a tag.
*
* @since 4.10.0
*/
public final class TagNode extends ElementNode {
private final List<TagPart> parts;
private @Nullable Tag tag = null;
/**
* Creates a new element node.
*
* @param parent the parent of this node
* @param token the token that created this node
* @param sourceMessage the source message
* @param tagProvider the tag provider
* @since 4.10.0
*/
public TagNode(
final @NotNull ElementNode parent,
final @NotNull Token token,
final @NotNull String sourceMessage,
final @NotNull TokenParser.TagProvider tagProvider
) {
super(parent, token, sourceMessage);
this.parts = genParts(token, sourceMessage, tagProvider);
// Assert the tag node has parts.
if (this.parts.isEmpty()) {
throw new ParsingExceptionImpl("Tag has no parts? " + this, this.sourceMessage(), this.token());
}
}
private static @NotNull List<TagPart> genParts(
final @NotNull Token token,
final @NotNull String sourceMessage,
final @NotNull TokenParser.TagProvider tagProvider
) {
final ArrayList<TagPart> parts = new ArrayList<>();
if (token.childTokens() != null) {
for (final Token childToken : token.childTokens()) {
parts.add(new TagPart(sourceMessage, childToken, tagProvider));
}
}
return parts;
}
/**
* Returns the parts of this tag.
*
* @return the parts
* @since 4.10.0
*/
public @NotNull List<TagPart> parts() {
return this.parts;
}
/**
* Returns the name of this tag.
*
* @return the name
* @since 4.10.0
*/
public @NotNull String name() {
return this.parts.get(0).value();
}
@Override
public @NotNull Token token() {
return Objects.requireNonNull(super.token(), "token is not set");
}
/**
* Gets the tag attached to this tag node.
*
* @return the tag for this tag node
* @since 4.10.0
*/
public @NotNull Tag tag() {
return Objects.requireNonNull(this.tag, "no tag set");
}
/**
* Sets the tag logic that is represented by this tag node.
*
* @param tag the tag logic
* @since 4.10.0
*/
public void tag(final @NotNull Tag tag) {
this.tag = tag;
}
@Override
public @NotNull StringBuilder buildToString(final @NotNull StringBuilder sb, final int indent) {
final char[] in = this.ident(indent);
sb.append(in).append("TagNode(");
final int size = this.parts.size();
for (int i = 0; i < size; i++) {
final TagPart part = this.parts.get(i);
sb.append('\'').append(part.value()).append('\'');
if (i != size - 1) {
sb.append(", ");
}
}
sb.append(") {\n");
for (final ElementNode child : this.children()) {
child.buildToString(sb, indent + 1);
}
sb.append(in).append("}\n");
return sb;
}
}