2 * Copyright 2005-2011 the original author or authors.
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.wamblee.xml;
18 import org.w3c.dom.ls.DOMImplementationLS;
19 import org.w3c.dom.ls.LSInput;
20 import org.w3c.dom.ls.LSResourceResolver;
21 import org.wamblee.io.ClassPathResource;
22 import org.wamblee.io.InputResource;
24 import java.io.IOException;
25 import java.io.InputStream;
27 import javax.xml.transform.Source;
28 import javax.xml.transform.TransformerException;
29 import javax.xml.transform.URIResolver;
30 import javax.xml.transform.stream.StreamSource;
33 * URI resolver that resolves stylesheets through the classpath.
35 public class ClasspathUriResolver implements URIResolver, LSResourceResolver {
40 * Constructs the resolver.
43 public ClasspathUriResolver() {
48 * Location in the classpath relative to which resolution takes place.
51 public ClasspathUriResolver(String aBase) {
58 * @see javax.xml.transform.URIResolver#resolve(java.lang.String,
61 public Source resolve(String aHref, String aBase)
62 throws TransformerException {
64 InputResource xslt = resolveImpl(aHref);
67 return new StreamSource(xslt.getInputStream());
68 } catch (IOException e) {
69 throw new TransformerException(
70 "Could not get XSLT style sheet in classpath '" + aHref + "'",
75 private InputResource resolveImpl(String aHref) {
76 String systemId = aHref;
78 systemId = base + "/" + aHref;
80 InputResource xslt = new ClassPathResource(systemId);
85 public LSInput resolveResource(String aType, String aNamespaceURI,
86 String aPublicId, String aSystemId, String aBaseURI) {
88 InputStream xslt = resolveImpl(aSystemId).getInputStream();
89 DOMImplementationLS impl = DomUtils.getDomImplementationLS();
90 LSInput input = impl.createLSInput();
91 input.setPublicId(aPublicId);
92 input.setSystemId(aSystemId);
93 input.setByteStream(xslt);
95 } catch (IOException e) {