NXvalidate  1
 All Classes Namespaces Files Functions Variables
XSLTResolver.java
Go to the documentation of this file.
1 /* NeXus - Neutron & X-ray Common Data Format
2  *
3  * NeXus file validation GUI tool.
4  *
5  * Copyright (C) 2010 Stephen Rankin
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * For further information, see <http://www.nexusformat.org/>
22  *
23  * XSLTResolver.java
24  *
25  */
26 package org.nexusformat.nxvalidate.resources;
27 
32 import java.net.URISyntaxException;
33 import java.util.logging.Level;
34 import java.util.logging.Logger;
35 import javax.xml.transform.URIResolver;
36 import javax.xml.transform.Source;
37 import javax.xml.transform.TransformerException;
38 import javax.xml.transform.stream.StreamSource;
39 import java.io.InputStream;
40 import java.net.URI;
41 
42 public class XSLTResolver implements URIResolver {
43 
44  public Source resolve(String href, String base)
45  throws TransformerException {
46  String newHref = null;
47 
48  if(href==null){
49  return null;
50  }
51  else if(href.equals("")){
52 
53  try {
54  URI uri = new URI(base);
55  String[] path = uri.getPath().split("/");
56  newHref = path[path.length-1];
57  System.out.println("newHref: " + newHref);
58  } catch (URISyntaxException ex) {
59  Logger.getLogger(XSLTResolver.class.getName()).log(Level.SEVERE, null, ex);
60 
61  }
62  return null;
63 
64  }
65 
66  try
67  {
68  if(newHref != null){
69  InputStream is = XSLTResolver.class.getResourceAsStream(newHref);
70  return new StreamSource(is, newHref);
71  }
72  else{
73  InputStream is = XSLTResolver.class.getResourceAsStream(href);
74  return new StreamSource(is, href);
75  }
76  }
77  catch (Exception ex) {
78  throw new TransformerException(ex);
79  }
80  }
81 }