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