View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
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   * This class provides a simple way to dynamically create MyCoRe webpages. These pages might be rendered 
44   * through the layout service.
45   * 
46   * <br>Example:
47   *  <pre>
48   *   MyCoReWebPageProvider wp = new MyCoReWebPageProvider();
49   *   wp.addSection("Section Title", "Section Text", MyCoReWebPageProvider.DE);
50   *   Document xml = wp.getXML();
51   *   
52   *   //call the layout service of an {@link MCRServlet}
53   *   getLayoutService().doLayout(job.getRequest(), job.getResponse(), xml);
54   *  </pre>
55   * 
56   * @author shermann
57   * @author Matthias Eichner
58   */
59  public class MyCoReWebPageProvider {
60  
61      /** German language key */
62      public static final String EN = "en";
63  
64      /** English language key */
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      * Adds a section to the MyCoRe webpage.
101      * 
102      * @param title the title of the section
103      * @param xmlAsString xml string which is added to the section
104      * @param lang the language of the section specified by a language key.
105      * @return added section
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      * Adds a section to the MyCoRe webpage.
128      * @param title the title of the section
129      * @param content jdom element which is added to the section
130      * @param lang the language of the section specified by a language key.
131      * @return added section
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      * Adds a section to the MyCoRe webpage
141      * @param title the title of the section
142      * @param content list of content added to the section
143      * @param lang the language of the section specified by a language key.
144      * @return added section
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      * Updates the meta element of the webpage.
161      * 
162      * @param editor last editor of webpage
163      * @param labelPath path info
164      */
165     public void updateMeta(String editor, String labelPath) {
166         // get meta & log element
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         // update attributes
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      * @return an xml document with root element is <code> &lt;MyCoReWebPage/&gt;</code> 
193      */
194     public Document getXML() {
195         return this.xml;
196     }
197 
198 }