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;
20  
21  import java.io.IOException;
22  import java.util.Map;
23  
24  import javax.xml.transform.TransformerFactory;
25  
26  import org.jdom2.Document;
27  import org.jdom2.Element;
28  import org.jdom2.JDOMException;
29  import org.jdom2.Text;
30  import org.jdom2.filter.Filters;
31  import org.mycore.common.MCRClassTools;
32  import org.mycore.common.config.MCRConfiguration2;
33  import org.mycore.common.content.MCRContent;
34  import org.mycore.common.content.MCRJDOMContent;
35  import org.mycore.common.content.transformer.MCRContentTransformer;
36  import org.mycore.common.content.transformer.MCRXSL2XMLTransformer;
37  import org.mycore.common.xml.MCRXMLFunctions;
38  import org.xml.sax.SAXException;
39  
40  /**
41   * PostProcessor for MyCoRe editor framework
42   * that allows execution of XSLT stylesheets after an editor is closed
43   * 
44   * &lt;xed:post-processor class="org.mycore.frontend.xeditor.MCRPostProcessorXSL" 
45   *      xsl="editor/ir_xeditor2mods.xsl" transformer="saxon" /&gt;
46   * 
47   * You can specify with param xsl the stylesheet, which should be processed and 
48   * you can specify with parm transformer the XSLStylesheetProcessor ('xalan' or 'saxon').
49   * If no transformer is specified the default transformer will be used
50   * (property: MCR.LayoutService.TransformerFactoryClass).
51   */
52  public class MCRPostProcessorXSL implements MCRXEditorPostProcessor {
53  
54      private String transformer;
55  
56      private String stylesheet;
57  
58      public Document process(Document xml) throws IOException, JDOMException, SAXException {
59          if (stylesheet == null) {
60              return xml.clone();
61          }
62  
63          Class<? extends TransformerFactory> factoryClass = null;
64          try {
65              if ("xalan".equals(transformer)) {
66                  factoryClass = MCRClassTools
67                      .forName("org.apache.xalan.processor.TransformerFactoryImpl");
68              }
69              if ("saxon".equals(transformer)) {
70                  factoryClass = MCRClassTools
71                      .forName("net.sf.saxon.TransformerFactoryImpl");
72              }
73          } catch (ClassNotFoundException e) {
74              //do nothing, use default
75          }
76  
77          if (factoryClass == null) {
78              factoryClass = MCRConfiguration2
79                  .<TransformerFactory>getClass("MCR.LayoutService.TransformerFactoryClass").orElseThrow();
80          }
81  
82          MCRContent source = new MCRJDOMContent(xml);
83          MCRContent transformed = MCRXSL2XMLTransformer.getInstance(factoryClass, "xsl/" + stylesheet).transform(source);
84          MCRContent normalized = new MCRNormalizeUnicodeTransformer().transform(transformed);
85          return normalized.asXML();
86      }
87  
88      @Override
89      public void setAttributes(Map<String, String> attributeMap) {
90          this.stylesheet = attributeMap.get("xsl");
91          if (attributeMap.containsKey("transformer")) {
92              this.transformer = attributeMap.get("transformer");
93          }
94      }
95  
96      public void setStylesheet(String stylesheet) {
97          this.stylesheet = stylesheet;
98      }
99  }
100 
101 class MCRNormalizeUnicodeTransformer extends MCRContentTransformer {
102 
103     public MCRJDOMContent transform(MCRContent source) throws IOException {
104         try {
105             Element root = source.asXML().getRootElement().clone();
106             for (Text text : root.getDescendants(Filters.text())) {
107                 text.setText(MCRXMLFunctions.normalizeUnicode(text.getText()));
108             }
109             return new MCRJDOMContent(root);
110         } catch (JDOMException | SAXException ex) {
111             throw new IOException(ex);
112         }
113     }
114 }