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.elements;
35
36 import info.magnolia.rendering.engine.RenderException;
37
38 import java.io.IOException;
39
40 import javax.jcr.Node;
41 import javax.jcr.RepositoryException;
42
43
44
45
46 public class MarkupHelper implements Appendable {
47
48 private static final String EQUALS = "=";
49 private static final String SPACE = " ";
50 private static final String QUOTE = "\"";
51
52 private static final String LINE_BREAK = "\n";
53 private static final String GREATER_THAN = ">";
54 private static final String LESS_THAN = "<";
55 private static final String SLASH = "/";
56
57 private static final String XML_BEGIN_COMMENT = "<!-- ";
58 private static final String XML_END_COMMENT = " -->";
59
60 private static final String CMS_BEGIN_TAG = "cms:begin";
61 private static final String CMS_END_TAG = "cms:end";
62
63 private static final String CONTENT_ATTRIBUTE = "cms:content";
64 private static final String TYPE_ATTRIBUTE = "cms:type";
65
66 private final Appendable appendable;
67
68 public MarkupHelper(Appendable appendable) {
69 this.appendable = appendable;
70 }
71
72 public MarkupHelper attribute(String name, String value) throws IOException {
73
74 if (value != null) {
75 appendable.append(SPACE).append(name).append(EQUALS).append(QUOTE).append(value).append(QUOTE);
76 }
77 return this;
78 }
79
80 public MarkupHelper startContent(Node node) throws IOException, RenderException {
81 appendable.append(XML_BEGIN_COMMENT);
82 append(CMS_BEGIN_TAG);
83 attribute(CONTENT_ATTRIBUTE, getNodeId(node));
84 attribute(TYPE_ATTRIBUTE, getNodeType(node));
85 appendable.append(XML_END_COMMENT).append(LINE_BREAK);
86 return this;
87 }
88
89 public MarkupHelper endContent(Node node) throws IOException, RenderException {
90 appendable.append(XML_BEGIN_COMMENT);
91 append(CMS_END_TAG);
92 attribute(CONTENT_ATTRIBUTE, getNodeId(node));
93 appendable.append(XML_END_COMMENT).append(LINE_BREAK);
94 return this;
95 }
96
97 public MarkupHelper openTag(String tagName) throws IOException {
98 appendable.append(LESS_THAN).append(tagName);
99 return this;
100 }
101
102 public MarkupHelper closeTag(String tagName) throws IOException {
103 appendable.append(GREATER_THAN).append(LESS_THAN).append(SLASH).append(tagName).append(GREATER_THAN).append(LINE_BREAK);
104 return this;
105 }
106
107 public MarkupHelper openComment(String tagName) throws IOException {
108 appendable.append(XML_BEGIN_COMMENT).append(tagName);
109 return this;
110 }
111
112 public MarkupHelper closeComment(String tagName) throws IOException {
113 appendable.append(XML_BEGIN_COMMENT).append(SLASH).append(tagName).append(XML_END_COMMENT).append(LINE_BREAK);
114 return this;
115 }
116
117 protected String getNodeId(Node node) throws RenderException {
118 if (node == null) {
119 return "";
120 }
121 try {
122 return node.getSession().getWorkspace().getName() + ":" + node.getPath();
123 } catch (RepositoryException e) {
124 throw new RenderException("Can't construct node path for node " + node);
125 }
126 }
127
128 protected String getNodeType(Node node) throws RenderException {
129 if (node == null) {
130 return "";
131 }
132 try {
133 return node.getPrimaryNodeType().getName();
134 } catch (RepositoryException e) {
135 throw new RenderException("Can't read node type for node " + node);
136 }
137 }
138
139 @Override
140 public Appendable append(CharSequence charSequence) throws IOException {
141 return appendable.append(charSequence);
142 }
143
144 @Override
145 public Appendable append(CharSequence charSequence, int i, int i1) throws IOException {
146 return appendable.append(charSequence, i, i1);
147 }
148
149 @Override
150 public Appendable append(char c) throws IOException {
151 return appendable.append(c);
152 }
153 }