1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.tools;
20
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.StringReader;
25 import java.text.SimpleDateFormat;
26 import java.util.ArrayList;
27 import java.util.Date;
28 import java.util.List;
29 import java.util.Locale;
30
31 import org.jdom2.Content;
32 import org.jdom2.DocType;
33 import org.jdom2.Document;
34 import org.jdom2.Element;
35 import org.jdom2.JDOMException;
36 import org.jdom2.Namespace;
37 import org.jdom2.input.SAXBuilder;
38 import org.mycore.frontend.servlets.MCRServlet;
39 import org.xml.sax.InputSource;
40 import org.xml.sax.SAXParseException;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public class MyCoReWebPageProvider {
60
61
62 public static final String EN = "en";
63
64
65 public static final String DE = "de";
66
67 public static final String XML_MYCORE_WEBPAGE = "MyCoReWebPage";
68
69 public static final String XML_SECTION = "section";
70
71 public static final String XML_LANG = "lang";
72
73 public static final String XML_TITLE = "title";
74
75 public static final String XML_META = "meta";
76
77 public static final String XML_LOG = "log";
78
79 public static final String XML_LASTEDITOR = "lastEditor";
80
81 public static final String XML_LABELPATH = "labelPath";
82
83 public static final String XML_DATE = "date";
84
85 public static final String XML_TIME = "time";
86
87 public static final String DATE_FORMAT = "yyyy-MM-dd";
88
89 public static final String TIME_FORMAT = "HH:mm";
90
91 private Document xml;
92
93 public MyCoReWebPageProvider() {
94 this.xml = new Document();
95 this.xml.setDocType(new DocType(XML_MYCORE_WEBPAGE));
96 this.xml.setRootElement(new Element(XML_MYCORE_WEBPAGE));
97 }
98
99
100
101
102
103
104
105
106
107 public Element addSection(String title, String xmlAsString, String lang) throws IOException, SAXParseException,
108 JDOMException {
109 String sb = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
110 + "<!DOCTYPE MyCoReWebPage PUBLIC \"-//MYCORE//DTD MYCOREWEBPAGE 1.0//DE\" "
111 + "\"http://www.mycore.org/mycorewebpage.dtd\">" + "<MyCoReWebPage>" + xmlAsString + "</MyCoReWebPage>";
112 SAXBuilder saxBuilder = new SAXBuilder();
113 saxBuilder.setEntityResolver((publicId, systemId) -> {
114 String resource = systemId.substring(systemId.lastIndexOf("/"));
115 InputStream is = getClass().getResourceAsStream(resource);
116 if (is == null) {
117 throw new IOException(new FileNotFoundException("Unable to locate resource " + resource));
118 }
119 return new InputSource(is);
120 });
121 StringReader reader = new StringReader(sb);
122 Document doc = saxBuilder.build(reader);
123 return this.addSection(title, doc.getRootElement().cloneContent(), lang);
124 }
125
126
127
128
129
130
131
132
133 public Element addSection(String title, Content content, String lang) {
134 List<Content> contentList = new ArrayList<>(1);
135 contentList.add(content);
136 return addSection(title, contentList, lang);
137 }
138
139
140
141
142
143
144
145
146 public Element addSection(String title, List<Content> content, String lang) {
147 Element section = new Element(XML_SECTION);
148 if (lang != null) {
149 section.setAttribute(XML_LANG, lang, Namespace.XML_NAMESPACE);
150 }
151 if (title != null && !title.equals("")) {
152 section.setAttribute(XML_TITLE, title);
153 }
154 section.addContent(content);
155 this.xml.getRootElement().addContent(section);
156 return section;
157 }
158
159
160
161
162
163
164
165 public void updateMeta(String editor, String labelPath) {
166
167 Element meta = this.xml.getRootElement().getChild(XML_META);
168 if (meta == null) {
169 meta = new Element(XML_META);
170 this.xml.getRootElement().addContent(meta);
171 }
172 Element log = meta.getChild(XML_LOG);
173 if (log == null) {
174 log = new Element(XML_LOG);
175 meta.addContent(log);
176 }
177
178 if (editor != null) {
179 log.setAttribute(XML_LASTEDITOR, editor);
180 }
181 if (labelPath != null) {
182 log.setAttribute(XML_LABELPATH, labelPath);
183 }
184 Date date = new Date(System.currentTimeMillis());
185 SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.ROOT);
186 log.setAttribute(XML_DATE, dateFormat.format(date));
187 SimpleDateFormat timeFormat = new SimpleDateFormat(TIME_FORMAT, Locale.ROOT);
188 log.setAttribute(XML_TIME, timeFormat.format(date));
189 }
190
191
192
193
194 public Document getXML() {
195 return this.xml;
196 }
197
198 }