KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > schema > DefaultSchemaLocator


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 /*
24  * DefaultSchemaLocator.java
25  *
26  * Created on 14 décembre 2001, 15:36
27  */

28
29 package org.xquark.schema;
30
31 import java.net.URL JavaDoc;
32 import java.util.*;
33
34 import javax.xml.parsers.ParserConfigurationException JavaDoc;
35 import javax.xml.parsers.SAXParser JavaDoc;
36 import javax.xml.parsers.SAXParserFactory JavaDoc;
37
38 import org.xml.sax.InputSource JavaDoc;
39 import org.xml.sax.SAXException JavaDoc;
40 import org.xml.sax.XMLReader JavaDoc;
41
42 public class DefaultSchemaLocator implements SchemaLocator {
43     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
44     private static final String JavaDoc RCSName = "$Name: $";
45
46     private static SAXParserFactory JavaDoc factory = null;
47
48     static {
49     try {
50         factory = SAXParserFactory.newInstance();
51         factory.setNamespaceAware(true);
52         factory.setValidating(false);
53     }
54     catch ( Exception JavaDoc ee ) {
55         factory = null;
56     }
57     }
58   
59     private HashMap schemaLocations = new HashMap();
60
61     public XMLReader JavaDoc getSchemaReader() throws SAXException JavaDoc
62     {
63     if ( factory == null ) return null;
64     try {
65         SAXParser JavaDoc parser = factory.newSAXParser();
66         return parser.getXMLReader();
67     } catch (ParserConfigurationException JavaDoc e) {
68         e.fillInStackTrace();
69         throw new SAXException JavaDoc(e);
70     }
71     }
72   
73     public InputSource JavaDoc resolveLocation(String JavaDoc namespace, String JavaDoc location)
74     {
75     return null;
76     }
77   
78     public InputSource JavaDoc resolveLocation(String JavaDoc namespace, URL JavaDoc location)
79     {
80     try {
81         InputSource JavaDoc source = new InputSource JavaDoc(location.openStream());
82         source.setSystemId(location.toString());
83         return source;
84     } catch (java.io.IOException JavaDoc ex) {
85         return null;
86     }
87     }
88
89     public String JavaDoc resolveInclude(String JavaDoc location) {
90         return location;
91     }
92  
93     public void clearSchemaLocations() {
94     schemaLocations.clear();
95     }
96     
97     public Iterator getSchemaLocations(String JavaDoc namespace) {
98     List locs = (List)schemaLocations.get(namespace);
99     if (locs != null) return locs.iterator();
100     else return null;
101     }
102     
103     public void addSchemaLocation(String JavaDoc uri, String JavaDoc location) {
104     List list = (List)schemaLocations.get(uri);
105     if ( list == null ) {
106         list = new ArrayList();
107         schemaLocations.put(uri, list);
108     }
109     list.add(location);
110     }
111     
112 }
113
Popular Tags