View Javadoc

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