5fe5afb1e9a0c452c04f66b3b91d334de829d945
[utils] / support / general / src / main / java / org / wamblee / xml / ClasspathUriResolver.java
1 /*
2  * Copyright 2005 the original author or authors.
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.wamblee.xml;
17
18 import org.wamblee.io.ClassPathResource;
19 import org.wamblee.io.InputResource;
20
21 import java.io.IOException;
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
29 /**
30  * URI resolver that resolves stylesheets through the classpath.
31  */
32 public class ClasspathUriResolver implements URIResolver {
33 /**
34      * Constructs the resolver.
35      *
36      */
37     public ClasspathUriResolver() {
38         // Empty.
39     }
40
41     /*
42      * (non-Javadoc)
43      *
44      * @see javax.xml.transform.URIResolver#resolve(java.lang.String,
45      *      java.lang.String)
46      */
47     /**
48      * DOCUMENT ME!
49      *
50      * @param aHref DOCUMENT ME!
51      * @param aBase DOCUMENT ME!
52      *
53      * @return DOCUMENT ME!
54      *
55      * @throws TransformerException DOCUMENT ME!
56      */
57     public Source resolve(String aHref, String aBase)
58         throws TransformerException {
59         InputResource xslt = new ClassPathResource(aHref);
60
61         try {
62             return new StreamSource(xslt.getInputStream());
63         } catch (IOException e) {
64             throw new TransformerException(
65                 "Could not get XSLT style sheet in classpath '" + aHref + "'", e);
66         }
67     }
68 }