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