1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.doxia.siterenderer.sink;
20
21 import java.io.StringWriter;
22 import java.io.Writer;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.apache.maven.doxia.markup.HtmlMarkup;
27 import org.apache.maven.doxia.module.xhtml5.Xhtml5Sink;
28 import org.apache.maven.doxia.sink.SinkEventAttributes;
29 import org.apache.maven.doxia.siterenderer.DocumentContent;
30 import org.apache.maven.doxia.siterenderer.DocumentRenderingContext;
31 import org.codehaus.plexus.util.StringUtils;
32
33
34
35
36
37
38
39
40 @SuppressWarnings("checkstyle:methodname")
41 public class SiteRendererSink extends Xhtml5Sink implements DocumentContent {
42 private String date;
43
44 private String title;
45
46 private List<String> authors = new ArrayList<>();
47
48 private final StringWriter headWriter;
49
50 private final Writer writer;
51
52 private DocumentRenderingContext docRenderingContext;
53
54
55
56
57
58
59 public SiteRendererSink(DocumentRenderingContext docRenderingContext) {
60 this(new StringWriter(), docRenderingContext);
61 }
62
63
64
65
66
67
68
69 private SiteRendererSink(StringWriter writer, DocumentRenderingContext docRenderingContext) {
70 super(writer);
71
72 this.writer = writer;
73 this.headWriter = new StringWriter();
74 this.docRenderingContext = docRenderingContext;
75
76
77 super.contentStack.push(HtmlMarkup.MAIN);
78 }
79
80
81 @Override
82 public void title_() {
83 if (getTextBuffer().length() > 0) {
84 title = getTextBuffer().toString();
85 }
86
87 resetTextBuffer();
88 }
89
90
91
92
93
94
95
96 @Override
97 public void title(SinkEventAttributes attributes) {
98 resetTextBuffer();
99 }
100
101
102 @Override
103 public void author(SinkEventAttributes attributes) {
104 resetTextBuffer();
105 }
106
107
108 @Override
109 public void author_() {
110 if (getTextBuffer().length() > 0) {
111 String text = getTextBuffer().toString().trim();
112 authors.add(text);
113 }
114
115 resetTextBuffer();
116 }
117
118
119 @Override
120 public void date(SinkEventAttributes attributes) {
121 resetTextBuffer();
122 }
123
124
125 @Override
126 public void date_() {
127 if (getTextBuffer().length() > 0) {
128 date = getTextBuffer().toString().trim();
129 }
130
131 resetTextBuffer();
132 }
133
134
135
136
137
138
139
140 @Override
141 public void body_() {
142
143 }
144
145
146
147
148
149
150
151 @Override
152 public void body(SinkEventAttributes attributes) {
153
154 }
155
156
157 @Override
158 public void head_() {
159 setHeadFlag(false);
160 }
161
162
163 @Override
164 public void head(SinkEventAttributes attributes) {
165 setHeadFlag(true);
166 }
167
168
169 @Override
170 protected void write(String text) {
171 String txt = text;
172
173 if (isHeadFlag()) {
174 headWriter.write(unifyEOLs(txt));
175
176 return;
177 }
178
179 if (docRenderingContext != null) {
180 String relativePathToBasedir = docRenderingContext.getRelativePath();
181
182 if (relativePathToBasedir == null) {
183 txt = StringUtils.replace(txt, "$relativePath", ".");
184 } else {
185 txt = StringUtils.replace(txt, "$relativePath", relativePathToBasedir);
186 }
187 }
188
189 super.write(txt);
190 }
191
192
193
194
195 public String getTitle() {
196 return title;
197 }
198
199
200 public List<String> getAuthors() {
201 return authors;
202 }
203
204
205 public String getDate() {
206 return date;
207 }
208
209
210 public String getBody() {
211 String body = writer.toString();
212
213 return body.length() > 0 ? body : null;
214 }
215
216
217 public String getHead() {
218 String head = headWriter.toString();
219
220 return head.length() > 0 ? head : null;
221 }
222
223
224 public DocumentRenderingContext getRenderingContext() {
225 return docRenderingContext;
226 }
227 }