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 package info.magnolia.templating.editor.client.dom;
35
36 import java.util.HashMap;
37 import java.util.Map;
38
39 import com.google.gwt.dom.client.Element;
40 import com.google.gwt.regexp.shared.MatchResult;
41 import com.google.gwt.regexp.shared.RegExp;
42
43
44
45
46
47
48
49 public class CMSComment {
50
51 private Element element;
52 private String comment;
53 private String tagName;
54 private boolean isClosing = false;
55
56 private HashMap<String, String> attributes;
57
58 public CMSComment(Comment comment) throws IllegalArgumentException {
59 this.setElement((Element)comment.cast());
60 this.comment = comment.getData().trim();
61
62
63 int delimiter = this.comment.indexOf(" ");
64 String attributeString = "";
65
66 if (delimiter < 0){
67 this.tagName = this.comment;
68 }
69 else {
70 this.tagName = this.comment.substring(0, delimiter);
71 attributeString = this.comment.substring(delimiter + 1);
72 }
73
74 if (this.tagName.startsWith("/")) {
75 setClosing(true);
76 this.tagName = this.tagName.substring(1);
77 }
78
79
80 if (this.tagName.startsWith("cms:")) {
81 String[] keyValue;
82 this.attributes = new HashMap<String, String>();
83 RegExp regExp = RegExp.compile("(\\S+=[\"'][^\"]*[\"'])", "g");
84 for (MatchResult matcher = regExp.exec(attributeString); matcher != null; matcher = regExp.exec(attributeString)) {
85 keyValue = matcher.getGroup(0).split("=");
86 this.attributes.put(keyValue[0], keyValue[1].replace("\"", ""));
87 }
88 }
89 else {
90 throw new IllegalArgumentException("Tagname must start with 'cms:'.");
91 }
92
93 }
94
95 public boolean isClosing() {
96 return isClosing;
97 }
98
99 public void setClosing(boolean isClosing) {
100 this.isClosing = isClosing;
101 }
102
103
104 public String getAttribute(String name) {
105 return this.attributes.get(name);
106 }
107
108 public Map<String, String> getAttributes() {
109 return this.attributes;
110 }
111
112 public boolean hasAttribute(String name) {
113 return this.attributes.containsKey(name);
114 }
115
116 public String getTagName() {
117 return this.tagName;
118 }
119
120 @Override
121 public String toString() {
122 return this.comment;
123 }
124
125 public void setElement(Element element) {
126 this.element = element;
127 }
128
129 public Element getElement() {
130 return element;
131 }
132
133 }