offline building of site deploy to improve performance.
[utils] / support / general / src / main / java / org / wamblee / xml / ClasspathUriResolver.java
1 /*
2  * Copyright 2005-2011 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.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;
23
24 import java.io.IOException;
25 import java.io.InputStream;
26
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;
31
32 /**
33  * URI resolver that resolves stylesheets through the classpath.
34  */
35 public class ClasspathUriResolver implements URIResolver, LSResourceResolver {
36     
37     private String base; 
38     
39     /**
40      * Constructs the resolver.
41      * 
42      */
43     public ClasspathUriResolver() {
44         base = null; 
45     }
46     
47     /**
48      * Location in the classpath relative to which resolution takes place. 
49      * @param aBase Base. 
50      */
51     public ClasspathUriResolver(String aBase) { 
52         base = aBase;
53     }
54
55     /*
56      * (non-Javadoc)
57      * 
58      * @see javax.xml.transform.URIResolver#resolve(java.lang.String,
59      * java.lang.String)
60      */
61     public Source resolve(String aHref, String aBase)
62         throws TransformerException {
63         
64         InputResource xslt = resolveImpl(aHref);
65
66         try {
67             return new StreamSource(xslt.getInputStream());
68         } catch (IOException e) {
69             throw new TransformerException(
70                 "Could not get XSLT style sheet in classpath '" + aHref + "'",
71                 e);
72         }
73     }
74
75     private InputResource resolveImpl(String aHref) {
76         String systemId = aHref;
77         if ( base != null ) { 
78              systemId = base + "/" + aHref;
79         }
80         InputResource xslt = new ClassPathResource(systemId);
81         return xslt;
82     }
83
84     @Override
85     public LSInput resolveResource(String aType, String aNamespaceURI,
86         String aPublicId, String aSystemId, String aBaseURI) {
87         try {
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);
94             return input;
95         } catch (IOException e) {
96             return null;
97         }
98        
99        
100     }
101
102 }