View Javadoc

1   package pl.psnc.dl.ege.tei;
2   
3   import java.io.File;
4   import java.net.URI;
5   
6   import javax.xml.transform.Source;
7   import javax.xml.transform.TransformerException;
8   import javax.xml.transform.URIResolver;
9   import javax.xml.transform.stream.StreamSource;
10  
11  /**
12   * Resolves URI addresses from include and import 
13   * methods of each xsl transformation scheme. 
14   * 
15   * @author mariuszs
16   */
17  public final class TEIConverterURIResolver
18  	implements URIResolver
19  {
20  
21  	private String basePath;
22  
23  	private TEIConverterURIResolver(String basePath)
24  	{
25  		this.basePath = basePath;
26  	}
27  
28  	
29  	public static TEIConverterURIResolver newInstance(String path)
30  	{
31  		return new TEIConverterURIResolver(path);
32  	}
33  
34  
35  	public Source resolve(String href, String base)
36  		throws TransformerException
37  	{
38  		try {
39  			URI bpuri;
40  			if (base.equals("")) {
41  				bpuri = new URI(basePath + "/");
42  			}
43  			else {
44  				bpuri = new URI(base);
45  			}
46  			URI rel = new URI(href);
47  			URI fin = bpuri.resolve(rel);
48  			File file = new File(fin.toString());
49  			if (file.exists())
50  				return new StreamSource(file);
51  		}
52  		catch (Exception ex) {
53  			throw new TransformerException(ex.getMessage());
54  		}
55  		return null;
56  	}
57  }