*** empty log message ***
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / common / ServletDigester.java
1 package edu.internet2.middleware.shibboleth.common;
2
3 import java.io.InputStream;
4 import java.util.StringTokenizer;
5
6 import javax.servlet.ServletContext;
7 import javax.xml.parsers.SAXParser;
8 import javax.xml.parsers.SAXParserFactory;
9
10 import org.apache.commons.digester.Digester;
11 import org.xml.sax.InputSource;
12 import org.xml.sax.SAXException;
13 import org.xml.sax.XMLReader;
14
15 /**
16  * This class is a jakarta Digester style parser that will pull schemas from /WEB-INF/schemas, if they 
17  * exist.
18  * 
19  * @author Walter Hoehn wassa@columbia.edu
20  */
21
22 public class ServletDigester extends Digester {
23         
24         ServletContext context;
25
26         public ServletDigester() {
27                 super();
28                 configure();
29         }
30
31         public ServletDigester(ServletContext context) {
32                 super();
33                 this.context = context;
34
35         }
36
37         public ServletDigester(SAXParser parser) {
38                 super(parser);
39                 configure();
40         }
41
42         public ServletDigester(XMLReader reader) {
43                 super(reader);
44                 configure();
45         }
46
47         /**
48          * @see org.xml.sax.EntityResolver#resolveEntity(String, String)
49          */
50         public InputSource resolveEntity(String publicId, String systemId)
51                 throws SAXException {
52
53                 if (context != null && systemId != null) {
54                         StringTokenizer tokenString = new StringTokenizer(systemId, "/");
55                         String xsdFile = "";
56                         while (tokenString.hasMoreTokens()) {
57                                 xsdFile = tokenString.nextToken();
58                         }
59                         if (xsdFile.endsWith(".xsd")) {
60                                 InputStream stream =
61                                         context.getResourceAsStream("/WEB-INF/schemas/" + xsdFile);
62                                 if (stream != null) {
63                                         return new InputSource(stream);
64                                 }
65                         }
66                 }
67                 return null;
68
69         }
70         
71          /**
72      * Return the SAXParser we will use to parse the input stream.  If there
73      * is a problem creating the parser, return <code>null</code>.
74      */
75     public SAXParser getParser() {
76
77         // Return the parser we already created (if any)
78         if (parser != null) {
79             return (parser);
80         }
81
82         // Create and return a new parser
83         synchronized (this) {
84             try {
85                 if (factory == null) {
86                     factory = SAXParserFactory.newInstance();
87                 }
88                 factory.setNamespaceAware(namespaceAware);
89                 factory.setValidating(validating);
90                 if (validating) {
91                         factory.setFeature("http://xml.org/sax/features/namespaces", true);
92                                         factory.setFeature("http://xml.org/sax/features/validation", true);
93                                         factory.setFeature("http://apache.org/xml/features/validation/schema", true);
94                                         factory.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
95                 }
96                 parser = factory.newSAXParser();
97                 return (parser);
98             } catch (Exception e) {
99                 return (null);
100             }
101         }
102
103     }
104 }