View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.ken.util;
17  
18  import java.io.StringReader;
19  import java.io.StringWriter;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  
23  import javax.xml.transform.ErrorListener;
24  import javax.xml.transform.Source;
25  import javax.xml.transform.Transformer;
26  import javax.xml.transform.TransformerException;
27  import javax.xml.transform.TransformerFactory;
28  import javax.xml.transform.stream.StreamResult;
29  import javax.xml.transform.stream.StreamSource;
30  
31  import org.apache.log4j.Logger;
32  
33  /**
34   * This class handles XSLT transformations.
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   */
37  public class ContentTransformer {
38      private static final Logger LOG = Logger.getLogger(ContentTransformer.class);
39  
40      private static final class LoggingErrorListener implements ErrorListener {
41          private final ErrorListener delegate;
42          public LoggingErrorListener(ErrorListener delegate) {
43              this.delegate = delegate;
44          }
45  
46          public void error(TransformerException exception) throws TransformerException {
47              LOG.error("Error transforming document", exception);
48          }
49  
50          public void fatalError(TransformerException exception) throws TransformerException {
51              if (delegate != null) {
52                  delegate.fatalError(exception);
53              } else {
54                  throw exception;
55              }
56              
57          }
58  
59          public void warning(TransformerException exception) throws TransformerException {
60              LOG.warn("Error transforming document", exception);
61          }
62          
63      };
64  
65      private Transformer transformer;
66  
67      /**
68       * Constructs a ContentTransformer.java.
69       * @param aStyleSheet
70       * @throws Exception
71       */
72      public ContentTransformer(StreamSource aStyleSheet) throws Exception {
73          // create transformer        
74          TransformerFactory factory = TransformerFactory.newInstance();
75          transformer = factory.newTransformer( aStyleSheet );
76      }
77  
78      /**
79       * Constructs a ContentTransformer.java.
80       * @param aStyleSheet
81       * @param parametermap
82       * @throws Exception
83       */
84      public ContentTransformer(StreamSource aStyleSheet, HashMap parametermap) throws Exception {
85         // create transformer
86         TransformerFactory factory = TransformerFactory.newInstance();
87         transformer = factory.newTransformer( aStyleSheet );
88         Iterator iter = parametermap.keySet().iterator();
89         while (iter.hasNext()) {
90            Object o = iter.next();
91            String param = o.toString();
92            String value = (String) parametermap.get(param);
93            transformer.setParameter(param, value);
94         }
95         transformer.setErrorListener(new LoggingErrorListener(transformer.getErrorListener()));
96      }
97  
98      /**
99       * This method performs the actual transformation.
100      * @param xml
101      * @return
102      * @throws Exception
103      */
104     public String transform(String xml) throws Exception {
105 
106         // perform transformation
107         Source xmlsource = new StreamSource(new StringReader(xml));
108         StringWriter sout = new StringWriter();
109          
110         transformer.transform(xmlsource, new StreamResult(sout));
111 
112         // return resulting document
113         return sout.toString();
114     }
115 }