2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
4 * Licensed under the Apache 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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.slc
.web
.mvc
;
19 import java
.io
.IOException
;
21 import java
.util
.TreeMap
;
23 import javax
.xml
.transform
.Source
;
24 import javax
.xml
.transform
.TransformerException
;
25 import javax
.xml
.transform
.URIResolver
;
26 import javax
.xml
.transform
.stream
.StreamSource
;
28 import org
.apache
.commons
.logging
.Log
;
29 import org
.apache
.commons
.logging
.LogFactory
;
30 import org
.argeo
.slc
.SlcException
;
31 import org
.springframework
.core
.io
.Resource
;
32 import org
.springframework
.oxm
.Marshaller
;
33 import org
.springframework
.web
.servlet
.view
.AbstractUrlBasedView
;
34 import org
.springframework
.web
.servlet
.view
.xslt
.XsltViewResolver
;
37 * Xslt View resolver implementing URI resolver as well.
40 * @see XsltViewResolver
42 public class XsltMarshallerViewResolver
extends XsltViewResolver
implements
44 private final static Log log
= LogFactory
45 .getLog(XsltMarshallerViewResolver
.class);
47 private Marshaller marshaller
;
48 private Map
<String
, Source
> cacheUriResolver
= new TreeMap
<String
, Source
>();
50 public XsltMarshallerViewResolver() {
55 protected AbstractUrlBasedView
buildView(String viewName
) throws Exception
{
56 AbstractUrlBasedView viewT
= super.buildView(viewName
);
57 XsltMarshallerView view
= (XsltMarshallerView
) viewT
;
58 view
.setMarshaller(marshaller
);
62 public void setMarshaller(Marshaller marshaller
) {
63 this.marshaller
= marshaller
;
66 public Source
resolve(String href
, String base
) throws TransformerException
{
67 if (log
.isTraceEnabled())
68 log
.trace("Resolve URI for href=" + href
+ " base=" + base
);
72 res
= cacheUriResolver
.get(href
);
75 res
= getStylesheetSource(href
);
78 res
= getStylesheetSource(getPrefix() + href
);
81 throw new SlcException("Can't resolve URI for href=" + href
84 if (isCache() && !cacheUriResolver
.containsKey(href
))
85 cacheUriResolver
.put(href
, res
);
90 protected Source
getStylesheetSource(String url
) {
91 if (log
.isDebugEnabled()) {
92 log
.debug("Loading XSLT stylesheet from '" + url
+ "'");
95 final Resource stylesheetResource
= getApplicationContext()
97 String systemId
= url
.substring(0, url
.lastIndexOf('/') + 1);
98 return new StreamSource(stylesheetResource
.getInputStream(),
100 } catch (IOException e
) {
101 if (log
.isTraceEnabled())
102 log
.trace("Cannot load stylesheet " + url
, e
);