1 package org
.argeo
.slc
.web
.mvc
;
3 import java
.io
.IOException
;
5 import java
.util
.TreeMap
;
7 import javax
.xml
.transform
.Source
;
8 import javax
.xml
.transform
.TransformerException
;
9 import javax
.xml
.transform
.URIResolver
;
10 import javax
.xml
.transform
.stream
.StreamSource
;
12 import org
.apache
.commons
.logging
.Log
;
13 import org
.apache
.commons
.logging
.LogFactory
;
14 import org
.argeo
.slc
.SlcException
;
15 import org
.springframework
.core
.io
.Resource
;
16 import org
.springframework
.oxm
.Marshaller
;
17 import org
.springframework
.web
.servlet
.view
.AbstractUrlBasedView
;
18 import org
.springframework
.web
.servlet
.view
.xslt
.XsltViewResolver
;
21 * Xslt View resolver implementing URI resolver as well.
24 * @see XsltViewResolver
26 public class XsltMarshallerViewResolver
extends XsltViewResolver
implements
28 private final static Log log
= LogFactory
29 .getLog(XsltMarshallerViewResolver
.class);
31 private Marshaller marshaller
;
32 private Map
<String
, Source
> cacheUriResolver
= new TreeMap
<String
, Source
>();
34 public XsltMarshallerViewResolver() {
39 protected AbstractUrlBasedView
buildView(String viewName
) throws Exception
{
40 AbstractUrlBasedView viewT
= super.buildView(viewName
);
41 XsltMarshallerView view
= (XsltMarshallerView
) viewT
;
42 view
.setMarshaller(marshaller
);
46 public void setMarshaller(Marshaller marshaller
) {
47 this.marshaller
= marshaller
;
50 public Source
resolve(String href
, String base
) throws TransformerException
{
51 if (log
.isTraceEnabled())
52 log
.trace("Resolve URI for href=" + href
+ " base=" + base
);
56 res
= cacheUriResolver
.get(href
);
59 res
= getStylesheetSource(href
);
62 res
= getStylesheetSource(getPrefix() + href
);
65 throw new SlcException("Can't resolve URI for href=" + href
68 if (isCache() && !cacheUriResolver
.containsKey(href
))
69 cacheUriResolver
.put(href
, res
);
74 protected Source
getStylesheetSource(String url
) {
75 if (log
.isDebugEnabled()) {
76 log
.debug("Loading XSLT stylesheet from '" + url
+ "'");
79 final Resource stylesheetResource
= getApplicationContext()
81 String systemId
= url
.substring(0, url
.lastIndexOf('/') + 1);
82 return new StreamSource(stylesheetResource
.getInputStream(),
84 } catch (IOException e
) {
85 if (log
.isTraceEnabled())
86 log
.trace("Cannot load stylesheet " + url
, e
);