+++ /dev/null
-/*\r
- * Copyright (C) 2007-2012 Mathieu Baudier\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-package org.argeo.slc.web.mvc;\r
-\r
-import java.io.IOException;\r
-import java.util.Map;\r
-import java.util.TreeMap;\r
-\r
-import javax.xml.transform.Source;\r
-import javax.xml.transform.TransformerException;\r
-import javax.xml.transform.URIResolver;\r
-import javax.xml.transform.stream.StreamSource;\r
-\r
-import org.apache.commons.logging.Log;\r
-import org.apache.commons.logging.LogFactory;\r
-import org.argeo.slc.SlcException;\r
-import org.springframework.core.io.Resource;\r
-import org.springframework.oxm.Marshaller;\r
-import org.springframework.web.servlet.view.AbstractUrlBasedView;\r
-import org.springframework.web.servlet.view.xslt.XsltViewResolver;\r
-\r
-/**\r
- * Xslt View resolver implementing URI resolver as well.\r
- * \r
- * @see URIResolver\r
- * @see XsltViewResolver\r
- */\r
-public class XsltMarshallerViewResolver extends XsltViewResolver implements\r
- URIResolver {\r
- private final static Log log = LogFactory\r
- .getLog(XsltMarshallerViewResolver.class);\r
-\r
- private Marshaller marshaller;\r
- private Map<String, Source> cacheUriResolver = new TreeMap<String, Source>();\r
-\r
- public XsltMarshallerViewResolver() {\r
- setUriResolver(this);\r
- }\r
-\r
- @Override\r
- protected AbstractUrlBasedView buildView(String viewName) throws Exception {\r
- AbstractUrlBasedView viewT = super.buildView(viewName);\r
- XsltMarshallerView view = (XsltMarshallerView) viewT;\r
- view.setMarshaller(marshaller);\r
- return view;\r
- }\r
-\r
- public void setMarshaller(Marshaller marshaller) {\r
- this.marshaller = marshaller;\r
- }\r
-\r
- public Source resolve(String href, String base) throws TransformerException {\r
- if (log.isTraceEnabled())\r
- log.trace("Resolve URI for href=" + href + " base=" + base);\r
-\r
- Source res = null;\r
- if (isCache())\r
- res = cacheUriResolver.get(href);\r
-\r
- if (res == null)\r
- res = getStylesheetSource(href);\r
-\r
- if (res == null)\r
- res = getStylesheetSource(getPrefix() + href);\r
-\r
- if (res == null)\r
- throw new SlcException("Can't resolve URI for href=" + href\r
- + " base=" + base);\r
-\r
- if (isCache() && !cacheUriResolver.containsKey(href))\r
- cacheUriResolver.put(href, res);\r
-\r
- return res;\r
- }\r
-\r
- protected Source getStylesheetSource(String url) {\r
- if (log.isDebugEnabled()) {\r
- log.debug("Loading XSLT stylesheet from '" + url + "'");\r
- }\r
- try {\r
- final Resource stylesheetResource = getApplicationContext()\r
- .getResource(url);\r
- String systemId = url.substring(0, url.lastIndexOf('/') + 1);\r
- return new StreamSource(stylesheetResource.getInputStream(),\r
- systemId);\r
- } catch (IOException e) {\r
- if (log.isTraceEnabled())\r
- log.trace("Cannot load stylesheet " + url, e);\r
- return null;\r
- }\r
- }\r
-\r
-}\r