View Javadoc

1   package pl.psnc.dl.ege;
2   
3   import java.io.File;
4   import java.net.URI;
5   import java.net.URISyntaxException;
6   
7   import javax.xml.transform.Result;
8   
9   import net.sf.saxon.event.StandardOutputResolver;
10  import net.sf.saxon.trans.XPathException;
11  
12  /**
13   * Alternative output uri resolver for
14   * xsl transformation - controls output of xslt operation
15   * of "xsl:result-document". 
16   * 
17   * @author mariuszs
18   */
19  public class MultiXslOutputResolver
20  	extends StandardOutputResolver
21  {
22  	private String subPath;
23  	
24  	private static final URI NEW_BASE;
25  	static {
26  		String tmp = System.getProperty("java.io.tmpdir");
27  		if(!(tmp.endsWith("/") || tmp.endsWith("\\"))){
28  			tmp = tmp + File.separator;
29  		}
30  		File tmpFil = new File(tmp);
31  		NEW_BASE = tmpFil.toURI();
32  	}
33  	
34  	/**
35  	 * Constructor : output data is written
36  	 * to unique sub-directory for each
37  	 * different input data transformation.
38  	 * 
39  	 * @param subPath
40  	 */
41  	public MultiXslOutputResolver(String subPath)
42  	{
43  		this.subPath = subPath;
44  	}
45  
46  	@Override
47  	public Result resolve(String href, String base)
48  		throws XPathException
49  	{
50  		base = NEW_BASE.toString();
51  		try {
52  			URI absoluteURI;
53  			if (href.length() == 0) {
54  				if (base == null) {
55  					throw new XPathException(
56  							"The system identifier of the principal output file is unknown");
57  				}
58  				absoluteURI = new URI(base);
59  			}
60  			else {
61  				absoluteURI = new URI(href);
62  			}
63  			if (!absoluteURI.isAbsolute()) {
64  				if (base == null) {
65  					throw new XPathException(
66  							"The system identifier of the principal output file is unknown");
67  				}
68  				URI baseURI = new URI(base);
69  				absoluteURI = baseURI.resolve(href);
70  			}
71  			StringBuffer sb = new StringBuffer();
72  			String newURI = absoluteURI.toString();
73  			sb.append(newURI.substring(0,newURI.lastIndexOf("/")));
74  			sb.append("/");
75  			sb.append(subPath);
76  			sb.append(newURI.substring(newURI.lastIndexOf("/"),newURI.length()));
77  			return makeOutputFile(new URI(sb.toString()));
78  
79  		}
80  		catch (URISyntaxException err) {
81  			throw new XPathException("Invalid syntax for base URI", err);
82  		}
83  		catch (IllegalArgumentException err2) {
84  			throw new XPathException("Invalid URI syntax", err2);
85  		}
86  	}
87  	
88  }