]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/XsltMarshallerViewResolver.java
Introduce H
[gpl/argeo-slc.git] / runtime / org.argeo.slc.server / src / main / java / org / argeo / slc / web / mvc / XsltMarshallerViewResolver.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
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
17 package org.argeo.slc.web.mvc;
18
19 import java.io.IOException;
20 import java.util.Map;
21 import java.util.TreeMap;
22
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;
27
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;
35
36 /**
37 * Xslt View resolver implementing URI resolver as well.
38 *
39 * @see URIResolver
40 * @see XsltViewResolver
41 */
42 public class XsltMarshallerViewResolver extends XsltViewResolver implements
43 URIResolver {
44 private final static Log log = LogFactory
45 .getLog(XsltMarshallerViewResolver.class);
46
47 private Marshaller marshaller;
48 private Map<String, Source> cacheUriResolver = new TreeMap<String, Source>();
49
50 public XsltMarshallerViewResolver() {
51 setUriResolver(this);
52 }
53
54 @Override
55 protected AbstractUrlBasedView buildView(String viewName) throws Exception {
56 AbstractUrlBasedView viewT = super.buildView(viewName);
57 XsltMarshallerView view = (XsltMarshallerView) viewT;
58 view.setMarshaller(marshaller);
59 return view;
60 }
61
62 public void setMarshaller(Marshaller marshaller) {
63 this.marshaller = marshaller;
64 }
65
66 public Source resolve(String href, String base) throws TransformerException {
67 if (log.isTraceEnabled())
68 log.trace("Resolve URI for href=" + href + " base=" + base);
69
70 Source res = null;
71 if (isCache())
72 res = cacheUriResolver.get(href);
73
74 if (res == null)
75 res = getStylesheetSource(href);
76
77 if (res == null)
78 res = getStylesheetSource(getPrefix() + href);
79
80 if (res == null)
81 throw new SlcException("Can't resolve URI for href=" + href
82 + " base=" + base);
83
84 if (isCache() && !cacheUriResolver.containsKey(href))
85 cacheUriResolver.put(href, res);
86
87 return res;
88 }
89
90 protected Source getStylesheetSource(String url) {
91 if (log.isDebugEnabled()) {
92 log.debug("Loading XSLT stylesheet from '" + url + "'");
93 }
94 try {
95 final Resource stylesheetResource = getApplicationContext()
96 .getResource(url);
97 String systemId = url.substring(0, url.lastIndexOf('/') + 1);
98 return new StreamSource(stylesheetResource.getInputStream(),
99 systemId);
100 } catch (IOException e) {
101 if (log.isTraceEnabled())
102 log.trace("Cannot load stylesheet " + url, e);
103 return null;
104 }
105 }
106
107 }