1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
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.kns.util.KNSConstants; |
35 | |
import org.kuali.rice.ksb.cache.RiceCacheAdministrator; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
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 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
@Override |
64 | |
public Style getStyle(String styleName) { |
65 | 0 | return styleRepositoryService.getStyle(styleName); |
66 | |
} |
67 | |
|
68 | |
@Override |
69 | |
public Templates getStyleAsTranslet(String name) throws TransformerConfigurationException { |
70 | 0 | if (name == null) return null; |
71 | 0 | Templates translet = fetchTemplatesFromCache(name); |
72 | 0 | if (translet == null) { |
73 | 0 | Style style = getStyle(name); |
74 | 0 | if (style == null) { |
75 | 0 | return null; |
76 | |
} |
77 | |
|
78 | 0 | boolean useXSLTC = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsBoolean(KEWConstants.KEW_NAMESPACE, KNSConstants.DetailTypes.EDOC_LITE_DETAIL_TYPE, KEWConstants.EDL_USE_XSLTC_IND); |
79 | 0 | if (useXSLTC) { |
80 | 0 | LOG.info("using xsltc to compile stylesheet"); |
81 | 0 | String key = "javax.xml.transform.TransformerFactory"; |
82 | 0 | String value = "org.apache.xalan.xsltc.trax.TransformerFactoryImpl"; |
83 | 0 | Properties props = System.getProperties(); |
84 | 0 | props.put(key, value); |
85 | 0 | System.setProperties(props); |
86 | |
} |
87 | |
|
88 | 0 | TransformerFactory factory = TransformerFactory.newInstance(); |
89 | 0 | factory.setURIResolver(new StyleUriResolver(this)); |
90 | |
|
91 | 0 | if (useXSLTC) { |
92 | 0 | factory.setAttribute("translet-name",name); |
93 | 0 | factory.setAttribute("generate-translet",Boolean.TRUE); |
94 | 0 | String debugTransform = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(KEWConstants.KEW_NAMESPACE, KNSConstants.DetailTypes.EDOC_LITE_DETAIL_TYPE, KEWConstants.EDL_DEBUG_TRANSFORM_IND); |
95 | 0 | if (debugTransform.trim().equals("Y")) { |
96 | 0 | factory.setAttribute("debug", Boolean.TRUE); |
97 | |
} |
98 | |
} |
99 | |
|
100 | 0 | translet = factory.newTemplates(new StreamSource(new StringReader(style.getXmlContent()))); |
101 | 0 | putTemplatesInCache(name, translet); |
102 | |
} |
103 | 0 | return translet; |
104 | |
} |
105 | |
|
106 | |
@Override |
107 | |
public void saveStyle(Style style) { |
108 | 0 | styleRepositoryService.saveStyle(style); |
109 | 0 | } |
110 | |
|
111 | |
@Override |
112 | |
public List<String> getAllStyleNames() { |
113 | 0 | return styleRepositoryService.getAllStyleNames(); |
114 | |
} |
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
protected String getTemplatesCacheKey(String styleName) { |
122 | 0 | return StyleRepositoryServiceImpl.TEMPLATES_CACHE_GROUP_NAME + ":" + styleName; |
123 | |
} |
124 | |
|
125 | |
protected Templates fetchTemplatesFromCache(String styleName) { |
126 | 0 | return (Templates) cache.getFromCache(getTemplatesCacheKey(styleName)); |
127 | |
} |
128 | |
|
129 | |
protected void putTemplatesInCache(String styleName, Templates templates) { |
130 | 0 | cache.putInCache(getTemplatesCacheKey(styleName), templates, StyleRepositoryServiceImpl.TEMPLATES_CACHE_GROUP_NAME); |
131 | 0 | } |
132 | |
|
133 | |
} |