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