Coverage Report - org.kuali.rice.ken.util.ContentTransformer
 
Classes in this File Line Coverage Branch Coverage Complexity
ContentTransformer
0%
0/22
0%
0/2
1.429
ContentTransformer$LoggingErrorListener
0%
0/11
0%
0/2
1.429
 
 1  
 /*
 2  
  * Copyright 2007 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  0
 public class ContentTransformer {
 38  0
     private static final Logger LOG = Logger.getLogger(ContentTransformer.class);
 39  
 
 40  
     private static final class LoggingErrorListener implements ErrorListener {
 41  
         private final ErrorListener delegate;
 42  0
         public LoggingErrorListener(ErrorListener delegate) {
 43  0
             this.delegate = delegate;
 44  0
         }
 45  
 
 46  
         public void error(TransformerException exception) throws TransformerException {
 47  0
             LOG.error("Error transforming document", exception);
 48  0
         }
 49  
 
 50  
         public void fatalError(TransformerException exception) throws TransformerException {
 51  0
             if (delegate != null) {
 52  0
                 delegate.fatalError(exception);
 53  
             } else {
 54  0
                 throw exception;
 55  
             }
 56  
             
 57  0
         }
 58  
 
 59  
         public void warning(TransformerException exception) throws TransformerException {
 60  0
             LOG.warn("Error transforming document", exception);
 61  0
         }
 62  
         
 63  
     };
 64  
 
 65  
     private Transformer transformer;
 66  
 
 67  
     /**
 68  
      * Constructs a ContentTransformer.java.
 69  
      * @param aStyleSheet
 70  
      * @throws Exception
 71  
      */
 72  0
     public ContentTransformer(StreamSource aStyleSheet) throws Exception {
 73  
         // create transformer        
 74  0
         TransformerFactory factory = TransformerFactory.newInstance();
 75  0
         transformer = factory.newTransformer( aStyleSheet );
 76  0
     }
 77  
 
 78  
     /**
 79  
      * Constructs a ContentTransformer.java.
 80  
      * @param aStyleSheet
 81  
      * @param parametermap
 82  
      * @throws Exception
 83  
      */
 84  0
     public ContentTransformer(StreamSource aStyleSheet, HashMap parametermap) throws Exception {
 85  
        // create transformer
 86  0
        TransformerFactory factory = TransformerFactory.newInstance();
 87  0
        transformer = factory.newTransformer( aStyleSheet );
 88  0
        Iterator iter = parametermap.keySet().iterator();
 89  0
        while (iter.hasNext()) {
 90  0
           Object o = iter.next();
 91  0
           String param = o.toString();
 92  0
           String value = (String) parametermap.get(param);
 93  0
           transformer.setParameter(param, value);
 94  0
        }
 95  0
        transformer.setErrorListener(new LoggingErrorListener(transformer.getErrorListener()));
 96  0
     }
 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  0
         Source xmlsource = new StreamSource(new StringReader(xml));
 108  0
         StringWriter sout = new StringWriter();
 109  
          
 110  0
         transformer.transform(xmlsource, new StreamResult(sout));
 111  
 
 112  
         // return resulting document
 113  0
         return sout.toString();
 114  
     }
 115  
 }