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.frontend.xeditor.target;
20  
21  import java.io.IOException;
22  import java.io.PrintWriter;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.List;
26  import java.util.Map;
27  
28  import javax.servlet.ServletContext;
29  
30  import org.jdom2.Document;
31  import org.jdom2.Element;
32  import org.jdom2.output.Format;
33  import org.jdom2.output.XMLOutputter;
34  import org.mycore.frontend.servlets.MCRServletJob;
35  import org.mycore.frontend.xeditor.MCREditorSession;
36  import org.mycore.frontend.xeditor.tracker.MCRChangeTracker;
37  
38  /**
39   * @author Frank L\u00FCtzenkirchen
40   */
41  public class MCRDebugTarget implements MCREditorTarget {
42  
43      @Override
44      public void handleSubmission(ServletContext context, MCRServletJob job, MCREditorSession session, String parameter)
45          throws Exception {
46          job.getResponse().setContentType("text/html; charset=UTF-8");
47          PrintWriter out = job.getResponse().getWriter();
48          out.println("<html><body>");
49  
50          Map<String, String[]> parameters = job.getRequest().getParameterMap();
51          session.getSubmission().setSubmittedValues(parameters);
52  
53          Document result = session.getEditedXML().clone();
54          MCRChangeTracker tracker = session.getChangeTracker().clone();
55  
56          List<Step> steps = new ArrayList<>();
57          for (String label; (label = tracker.undoLastBreakpoint(result)) != null;)
58              steps.add(0, new Step(label, result.clone()));
59  
60          result = session.getEditedXML().clone();
61          result = MCRChangeTracker.removeChangeTracking(result);
62  
63          result = session.getXMLCleaner().clean(result);
64          steps.add(new Step("After cleaning", result));
65  
66          result = session.getPostProcessor().process(result);
67          steps.add(new Step("After postprocessing", result));
68  
69          for (int i = 0; i < steps.size(); i++) {
70              if (i == steps.size() - 3)
71                  outputParameters(parameters, out);
72  
73              steps.get(i).output(out);
74          }
75  
76          out.println("</body></html>");
77          out.close();
78      }
79  
80      private void outputParameters(Map<String, String[]> values, PrintWriter out) {
81          out.println("<h3>Submitted parameters:</h3>");
82          out.println("<p><pre>");
83  
84          List<String> names = new ArrayList<>(values.keySet());
85          Collections.sort(names);
86  
87          for (String name : names)
88              for (String value : values.get(name))
89                  out.println(name + " = " + value);
90  
91          out.println("</pre></p>");
92      }
93  
94      class Step {
95  
96          private String label;
97  
98          private Document xml;
99  
100         public Step(String label, Document xml) {
101             this.label = label;
102             this.xml = xml;
103         }
104 
105         private Format format = Format.getPrettyFormat().setLineSeparator("\n").setOmitDeclaration(true);
106 
107         public void output(PrintWriter out) throws IOException {
108             out.println("<h3>" + label + ":</h3>");
109             XMLOutputter outputter = new XMLOutputter(format);
110             out.println("<p>");
111             Element pre = new Element("pre");
112             pre.addContent(outputter.outputString(xml));
113             outputter.output(pre, out);
114             out.println("</p>");
115         }
116     }
117 }