2 * Copyright (C) 2007-2012 Mathieu Baudier
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.
16 package org
.argeo
.slc
.web
.mvc
;
18 import java
.io
.IOException
;
20 import java
.util
.TreeMap
;
22 import javax
.xml
.transform
.Source
;
23 import javax
.xml
.transform
.TransformerException
;
24 import javax
.xml
.transform
.URIResolver
;
25 import javax
.xml
.transform
.stream
.StreamSource
;
27 import org
.apache
.commons
.logging
.Log
;
28 import org
.apache
.commons
.logging
.LogFactory
;
29 import org
.argeo
.slc
.SlcException
;
30 import org
.springframework
.core
.io
.Resource
;
31 import org
.springframework
.oxm
.Marshaller
;
32 import org
.springframework
.web
.servlet
.view
.AbstractUrlBasedView
;
33 import org
.springframework
.web
.servlet
.view
.xslt
.XsltViewResolver
;
36 * Xslt View resolver implementing URI resolver as well.
39 * @see XsltViewResolver
41 public class XsltMarshallerViewResolver
extends XsltViewResolver
implements
43 private final static Log log
= LogFactory
44 .getLog(XsltMarshallerViewResolver
.class);
46 private Marshaller marshaller
;
47 private Map
<String
, Source
> cacheUriResolver
= new TreeMap
<String
, Source
>();
49 public XsltMarshallerViewResolver() {
54 protected AbstractUrlBasedView
buildView(String viewName
) throws Exception
{
55 AbstractUrlBasedView viewT
= super.buildView(viewName
);
56 XsltMarshallerView view
= (XsltMarshallerView
) viewT
;
57 view
.setMarshaller(marshaller
);
61 public void setMarshaller(Marshaller marshaller
) {
62 this.marshaller
= marshaller
;
65 public Source
resolve(String href
, String base
) throws TransformerException
{
66 if (log
.isTraceEnabled())
67 log
.trace("Resolve URI for href=" + href
+ " base=" + base
);
71 res
= cacheUriResolver
.get(href
);
74 res
= getStylesheetSource(href
);
77 res
= getStylesheetSource(getPrefix() + href
);
80 throw new SlcException("Can't resolve URI for href=" + href
83 if (isCache() && !cacheUriResolver
.containsKey(href
))
84 cacheUriResolver
.put(href
, res
);
89 protected Source
getStylesheetSource(String url
) {
90 if (log
.isDebugEnabled()) {
91 log
.debug("Loading XSLT stylesheet from '" + url
+ "'");
94 final Resource stylesheetResource
= getApplicationContext()
96 String systemId
= url
.substring(0, url
.lastIndexOf('/') + 1);
97 return new StreamSource(stylesheetResource
.getInputStream(),
99 } catch (IOException e
) {
100 if (log
.isTraceEnabled())
101 log
.trace("Cannot load stylesheet " + url
, e
);