View Javadoc
1   /**
2    * Copyright 2005-2014 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.coreservice.impl.style;
17  
18  import org.apache.log4j.Logger;
19  import org.kuali.rice.coreservice.api.style.Style;
20  import org.kuali.rice.coreservice.api.style.StyleRepositoryService;
21  import org.kuali.rice.coreservice.api.style.StyleService;
22  import org.kuali.rice.coreservice.framework.CoreFrameworkServiceLocator;
23  import org.kuali.rice.kew.api.KewApiConstants;
24  import org.kuali.rice.krad.util.KRADConstants;
25  import org.springframework.beans.factory.annotation.Required;
26  
27  import javax.xml.transform.Templates;
28  import javax.xml.transform.TransformerConfigurationException;
29  import javax.xml.transform.TransformerFactory;
30  import javax.xml.transform.stream.StreamSource;
31  import java.io.StringReader;
32  import java.util.List;
33  import java.util.Properties;
34  
35  
36  /**
37   * Implements generic StyleService via existing EDL style table
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   */
40  public class StyleServiceImpl implements StyleService {
41  	
42      private static final Logger LOG = Logger.getLogger(StyleServiceImpl.class);
43  
44      private StyleRepositoryService styleRepositoryService;
45  
46      public void setStyleRepositoryService(StyleRepositoryService styleRepositoryService) {
47      	this.styleRepositoryService = styleRepositoryService;
48      }
49  
50      /**
51       * Loads the named style from the database, or (if configured) imports it from a file
52       * specified via a configuration parameter with a name of the format edl.style.<styleName>
53       * {@inheritDoc}
54       */
55      @Override
56      public Style getStyle(String styleName) {
57      	return styleRepositoryService.getStyle(styleName);
58      }
59  
60      @Override
61      public Templates getStyleAsTranslet(String name) throws TransformerConfigurationException {
62          if (name == null) {
63              return null;
64          }
65  
66          Style style = getStyle(name);
67          if (style == null) {
68              return null;
69          }
70  
71          boolean useXSLTC = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsBoolean(KewApiConstants.KEW_NAMESPACE, KRADConstants.DetailTypes.EDOC_LITE_DETAIL_TYPE, KewApiConstants.EDL_USE_XSLTC_IND);
72          if (useXSLTC) {
73              LOG.info("using xsltc to compile stylesheet");
74              String key = "javax.xml.transform.TransformerFactory";
75              String value = "org.apache.xalan.xsltc.trax.TransformerFactoryImpl";
76              Properties props = System.getProperties();
77              props.put(key, value);
78              System.setProperties(props);
79          }
80  
81          TransformerFactory factory = TransformerFactory.newInstance();
82          factory.setURIResolver(new StyleUriResolver(this));
83  
84          if (useXSLTC) {
85              factory.setAttribute("translet-name",name);
86              factory.setAttribute("generate-translet",Boolean.TRUE);
87              String debugTransform = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(KewApiConstants.KEW_NAMESPACE, KRADConstants.DetailTypes.EDOC_LITE_DETAIL_TYPE, KewApiConstants.EDL_DEBUG_TRANSFORM_IND);
88              if (debugTransform.trim().equals("Y")) {
89                  factory.setAttribute("debug", Boolean.TRUE);
90              }
91          }
92  
93          return factory.newTemplates(new StreamSource(new StringReader(style.getXmlContent())));
94      }
95  
96      @Override
97      public void saveStyle(Style style) {
98      	styleRepositoryService.saveStyle(style);
99      }
100     
101     @Override
102     public List<String> getAllStyleNames() {
103         return styleRepositoryService.getAllStyleNames();
104     }
105 }