1 /*
2 * Copyright 2012-2025 Christophe Friederich
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.devacfr.maven.skins.reflow.snippet;
17
18 import com.google.common.base.MoreObjects;
19 import org.devacfr.maven.skins.reflow.snippet.SnippetComponent.Type;
20 import org.jsoup.nodes.Element;
21
22 /**
23 * Component token.
24 *
25 * @author Christophe Friederich
26 * @version 2.4
27 */
28 public class ComponentToken {
29
30 /**
31 * @author Christophe Friederich
32 * @version 2.4
33 */
34 public enum TagType {
35 /** empty-tag */
36 empty,
37 /** start-tag */
38 start,
39 /** end-tag */
40 end,
41 /** snippet written in html format */
42 html
43 }
44
45 /** */
46 private final Element element;
47
48 /** */
49 private final TagType tag;
50
51 /** */
52 private final Type type;
53
54 /** */
55 private final String name;
56
57 /**
58 * Constructor.
59 *
60 * @param element
61 * the element
62 * @param name
63 * the name
64 * @param tag
65 * the tag
66 */
67 public ComponentToken(final Element element, final String name, final TagType tag, final Type type) {
68 this.element = element;
69 this.name = name;
70 this.tag = tag;
71 this.type = type;
72 }
73
74 /**
75 * @return the element
76 */
77 public Element getElement() {
78 return element;
79 }
80
81 /**
82 * @return the name
83 */
84 public String name() {
85 return name;
86 }
87
88 /**
89 * @return the tag
90 */
91 public TagType tag() {
92 return tag;
93 }
94
95 /**
96 * @return the type
97 */
98 public Type type() {
99 return type;
100 }
101
102 /**
103 * @param startElement
104 * the start element
105 * @return true if this element is the close tag of the given start element
106 */
107 public boolean isCloseTagOf(final ComponentToken startElement) {
108 return name.equals(startElement.name) && type.equals(startElement.type) && TagType.start.equals(startElement.tag)
109 && TagType.end.equals(tag);
110 }
111
112 /**
113 * {@inheritDoc}
114 */
115 @Override
116 public String toString() {
117 return MoreObjects.toStringHelper(this).add("name", name).add("type", type).add("tag", tag).toString();
118
119 }
120
121 /**
122 * Get the close tag representation.
123 *
124 * @return the close tag representation.
125 */
126 public String getCloseTag() {
127 return "< /" + name + " >";
128 }
129 }