*** empty log message ***
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / common / XML.java
1 package edu.internet2.middleware.shibboleth.common;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.InputStream;
5 import org.xml.sax.EntityResolver;
6 import org.xml.sax.InputSource;
7 import org.xml.sax.SAXException;
8
9 /**
10  *  Utility class for XML constants and schema handling
11  *
12  * @author     Scott Cantor
13  * @created    January 2, 2002
14  */
15 public class XML
16 {
17     /**  Shibboleth extension XML namespace */
18     public final static String SHIB_NS = "urn:mace:shibboleth:1.0";
19
20     /**  Shibboleth XML schema identifier */
21     public final static String SHIB_SCHEMA_ID = "shibboleth.xsd";
22
23     private static byte[] Shib_schema;
24
25     /**
26      *  Custom schema resolver class
27      *
28      * @author     Scott Cantor
29      * @created    May 18, 2002
30      */
31     protected static class SchemaResolver implements EntityResolver
32     {
33         /**
34          *  A customized entity resolver for the Shibboleth extension schema
35          *
36          * @param  publicId                 The public identifier of the entity
37          * @param  systemId                 The system identifier of the entity
38          * @return                          A source of bytes for the entity or
39          *      null
40          * @exception  SAXException         Raised if an XML parsing problem
41          *      occurs
42          * @exception  java.io.IOException  Raised if an I/O problem is detected
43          */
44         public InputSource resolveEntity(String publicId, String systemId)
45             throws SAXException, java.io.IOException
46         {
47             InputSource src = null;
48             if (systemId.endsWith('/' + SHIB_SCHEMA_ID) && Shib_schema != null)
49                 src = new InputSource(new ByteArrayInputStream(Shib_schema));
50             return src;
51         }
52     }
53
54     static
55     {
56         try
57         {
58             StringBuffer buf = new StringBuffer(1024);
59             InputStream xmlin = XML.class.getResourceAsStream("/schemas/" + SHIB_SCHEMA_ID);
60             if (xmlin == null)
61                 throw new RuntimeException("XML static initializer unable to locate Shibboleth schema");
62             else
63             {
64                 int b;
65                 while ((b = xmlin.read()) != -1)
66                     buf.append((char)b);
67                 Shib_schema = buf.toString().getBytes();
68                 xmlin.close();
69             }
70         }
71         catch (java.io.IOException e)
72         {
73             throw new RuntimeException("XML static initializer caught an I/O error");
74         }
75     }
76 }
77