1 /*
2  * Copyright (c) 2002, 2015, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 package test.astro;
24 
25 import static jaxp.library.JAXPTestUtilities.filenameToURL;
26 import static test.astro.AstroConstants.DECXSL;
27 import static test.astro.AstroConstants.RAURIXSL;
28 import static test.astro.AstroConstants.STYPEXSL;
29 import static test.astro.AstroConstants.TOPTEMPLINCXSL;
30 
31 import java.io.IOException;
32 
33 import javax.xml.parsers.ParserConfigurationException;
34 import javax.xml.parsers.SAXParserFactory;
35 import javax.xml.transform.Source;
36 import javax.xml.transform.TransformerConfigurationException;
37 import javax.xml.transform.TransformerException;
38 import javax.xml.transform.TransformerFactory;
39 import javax.xml.transform.URIResolver;
40 import javax.xml.transform.sax.SAXTransformerFactory;
41 import javax.xml.transform.sax.TemplatesHandler;
42 import javax.xml.transform.sax.TransformerHandler;
43 import javax.xml.transform.stream.StreamSource;
44 
45 import org.xml.sax.InputSource;
46 import org.xml.sax.SAXException;
47 import org.xml.sax.XMLReader;
48 
49 /*
50  * Implementation of the filter factory interface that utilizes
51  * a TemplatesHandler and creates Templates from the stylesheets.
52  * The Templates objects are then used to create a TransformerHandler
53  * a.k.a Filter which is returned to the caller.
54  * This factory uses a Uri resolver which is registered with the
55  * Transformer factory.
56  *
57  */
58 public class TemplatesFilterFactoryImpl extends AbstractFilterFactory {
59     private final URIResolver uriResolver = new TemplatesFilterFactoryURIResolver();
60 
61     @Override
getRAXsl()62     protected String getRAXsl() {
63         return RAURIXSL;
64     }
65 
66     @Override
getDECXsl()67     protected String getDECXsl() {
68         return DECXSL;
69     }
70 
71     @Override
getRADECXsl()72     protected String getRADECXsl() {
73         return DECXSL;
74     }
75 
76     @Override
getStellarXsl()77     protected String getStellarXsl() {
78         return STYPEXSL;
79     }
80 
81     @Override
getTransformerHandler(String xslFileName)82     protected TransformerHandler getTransformerHandler(String xslFileName) throws SAXException, ParserConfigurationException,
83             TransformerConfigurationException, IOException {
84         SAXTransformerFactory factory = (SAXTransformerFactory) TransformerFactory.newInstance();
85         factory.setURIResolver(uriResolver);
86 
87         TemplatesHandler templatesHandler = factory.newTemplatesHandler();
88 
89         SAXParserFactory pFactory = SAXParserFactory.newInstance();
90         pFactory.setNamespaceAware(true);
91 
92         XMLReader xmlreader = pFactory.newSAXParser().getXMLReader();
93 
94         // create the stylesheet input source
95         InputSource xslSrc = new InputSource(xslFileName);
96 
97         xslSrc.setSystemId(filenameToURL(xslFileName));
98         // hook up the templates handler as the xsl content handler
99         xmlreader.setContentHandler(templatesHandler);
100         // call parse on the xsl input source
101 
102         xmlreader.parse(xslSrc);
103 
104         // extract the Templates object created from the xsl input source
105         return factory.newTransformerHandler(templatesHandler.getTemplates());
106     }
107 
108     /*
109      * Uri resolver used to resolve stylesheet used by the Templates filter
110      * factory.
111      */
112     private static class TemplatesFilterFactoryURIResolver implements URIResolver {
resolve(String href, String base)113         public Source resolve(String href, String base) throws TransformerException {
114             if ("http://astro.com/stylesheets/topleveltemplate".equals(href)) {
115                 StreamSource ss = new StreamSource(TOPTEMPLINCXSL);
116                 ss.setSystemId(filenameToURL(TOPTEMPLINCXSL));
117                 return ss;
118             } else {
119                 return null;
120             }
121         }
122     }
123 }
124