Coverage Report - org.kuali.rice.core.impl.style.StyleServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
StyleServiceImpl
0%
0/39
0%
0/12
1.889
 
 1  
 /*
 2  
  * Copyright 2006-2011 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  
 
 17  
 package org.kuali.rice.core.impl.style;
 18  
 
 19  
 import java.io.StringReader;
 20  
 import java.util.List;
 21  
 import java.util.Properties;
 22  
 
 23  
 import javax.xml.transform.Templates;
 24  
 import javax.xml.transform.TransformerConfigurationException;
 25  
 import javax.xml.transform.TransformerFactory;
 26  
 import javax.xml.transform.stream.StreamSource;
 27  
 
 28  
 import org.apache.log4j.Logger;
 29  
 import org.kuali.rice.core.api.style.Style;
 30  
 import org.kuali.rice.core.api.style.StyleRepositoryService;
 31  
 import org.kuali.rice.core.api.style.StyleService;
 32  
 import org.kuali.rice.core.framework.services.CoreFrameworkServiceLocator;
 33  
 import org.kuali.rice.kew.util.KEWConstants;
 34  
 import org.kuali.rice.krad.util.KRADConstants;
 35  
 import org.kuali.rice.ksb.api.cache.RiceCacheAdministrator;
 36  
 
 37  
 
 38  
 /**
 39  
  * Implements generic StyleService via existing EDL style table
 40  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 41  
  */
 42  0
 public class StyleServiceImpl implements StyleService {
 43  
         
 44  0
     private static final Logger LOG = Logger.getLogger(StyleServiceImpl.class);
 45  
 
 46  
     private StyleRepositoryService styleRepositoryService;
 47  
     private RiceCacheAdministrator cache;
 48  
 
 49  
     public void setStyleRepositoryService(StyleRepositoryService styleRepositoryService) {
 50  0
             this.styleRepositoryService = styleRepositoryService;
 51  0
     }
 52  
     
 53  
     public void setCache(RiceCacheAdministrator cache) {
 54  0
             this.cache = cache;
 55  0
     }
 56  
 
 57  
     /**
 58  
      * Loads the named style from the database, or (if configured) imports it from a file
 59  
      * specified via a configuration parameter with a name of the format edl.style.<styleName>
 60  
      * {@inheritDoc}
 61  
      * @see org.kuali.rice.edl.impl.service.StyleService#getStyle(java.lang.String)
 62  
      */
 63  
     @Override
 64  
     public Style getStyle(String styleName) {
 65  
             
 66  
             // TODO should really be caching here!
 67  
             
 68  0
             return styleRepositoryService.getStyle(styleName);
 69  
     }
 70  
 
 71  
     @Override
 72  
     public Templates getStyleAsTranslet(String name) throws TransformerConfigurationException {
 73  0
         if (name == null) return null;
 74  0
         Templates translet = fetchTemplatesFromCache(name);
 75  0
         if (translet == null) {
 76  0
             Style style = getStyle(name);
 77  0
             if (style == null) {
 78  0
                 return null;
 79  
             }
 80  
 
 81  0
             boolean useXSLTC = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsBoolean(KEWConstants.KEW_NAMESPACE, KRADConstants.DetailTypes.EDOC_LITE_DETAIL_TYPE, KEWConstants.EDL_USE_XSLTC_IND);
 82  0
             if (useXSLTC) {
 83  0
                 LOG.info("using xsltc to compile stylesheet");
 84  0
                 String key = "javax.xml.transform.TransformerFactory";
 85  0
                 String value = "org.apache.xalan.xsltc.trax.TransformerFactoryImpl";
 86  0
                 Properties props = System.getProperties();
 87  0
                 props.put(key, value);
 88  0
                 System.setProperties(props);
 89  
             }
 90  
 
 91  0
             TransformerFactory factory = TransformerFactory.newInstance();
 92  0
             factory.setURIResolver(new StyleUriResolver(this));
 93  
 
 94  0
             if (useXSLTC) {
 95  0
                 factory.setAttribute("translet-name",name);
 96  0
                 factory.setAttribute("generate-translet",Boolean.TRUE);
 97  0
                 String debugTransform = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(KEWConstants.KEW_NAMESPACE, KRADConstants.DetailTypes.EDOC_LITE_DETAIL_TYPE, KEWConstants.EDL_DEBUG_TRANSFORM_IND);
 98  0
                 if (debugTransform.trim().equals("Y")) {
 99  0
                     factory.setAttribute("debug", Boolean.TRUE);
 100  
                 }
 101  
             }
 102  
 
 103  0
             translet = factory.newTemplates(new StreamSource(new StringReader(style.getXmlContent())));
 104  0
             putTemplatesInCache(name, translet);
 105  
         }
 106  0
         return translet;
 107  
     }
 108  
 
 109  
     @Override
 110  
     public void saveStyle(Style style) {
 111  0
             styleRepositoryService.saveStyle(style);
 112  0
     }
 113  
     
 114  
     @Override
 115  
     public List<String> getAllStyleNames() {
 116  0
         return styleRepositoryService.getAllStyleNames();
 117  
     }
 118  
         
 119  
     // cache helper methods
 120  
 
 121  
     /**
 122  
      * Returns the key to be used for caching the Templates for the given style name.
 123  
      */
 124  
     protected String getTemplatesCacheKey(String styleName) {
 125  0
         return StyleRepositoryServiceImpl.TEMPLATES_CACHE_GROUP_NAME + ":" + styleName;
 126  
     }
 127  
 
 128  
     protected Templates fetchTemplatesFromCache(String styleName) {
 129  0
         return (Templates) cache.getFromCache(getTemplatesCacheKey(styleName));
 130  
     }
 131  
 
 132  
     protected void putTemplatesInCache(String styleName, Templates templates) {
 133  0
         cache.putInCache(getTemplatesCacheKey(styleName), templates, StyleRepositoryServiceImpl.TEMPLATES_CACHE_GROUP_NAME);
 134  0
     }
 135  
 
 136  
 }