KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > util > SAXLoader


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 package org.xquark.util;
24
25 import java.util.*;
26
27 import org.xml.sax.*;
28
29 public class SAXLoader implements ContentHandler {
30 private static final String JavaDoc RCSRevision = "$Revision: 1.2 $";
31 private static final String JavaDoc RCSName = "$Name: $";
32   
33   private NamespaceContextStack contexts = new NamespaceContextStack();
34   private ElementHandler currentHandler = null;
35   private LinkedList handlers = new LinkedList();
36   private HashMap currentMap = new HashMap();
37   private Locator locator = null;
38   
39   public SAXLoader(ElementHandler handler) {
40     this.currentHandler = handler;
41   }
42   
43   public void setElementHandler(ElementHandler handler) {
44     this.currentHandler = handler;
45   }
46   
47   public String JavaDoc getPrefixMapping(String JavaDoc prefix) {
48     return getNamespaceURI(prefix);
49   }
50   
51   public HashMap getElementPrefixMap() {
52     if (currentMap.isEmpty()) return null;
53     return (HashMap)currentMap.clone();
54   }
55   
56   public Locator getDocumentLocator() {
57     return locator;
58   }
59   
60   public final void characters(char[] ch, int start, int length)
61   throws SAXException {
62     currentHandler.characters(ch, start, length);
63   }
64   
65   public void endDocument() throws SAXException {
66     currentHandler = null;
67   }
68   
69   public final void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName)
70   throws SAXException {
71     contexts.popContext();
72     currentHandler = (ElementHandler)handlers.removeFirst();
73     int index = localName.indexOf(':');
74     if (index != -1) localName = localName.substring(index+1);
75     currentHandler.endElement(namespaceURI, localName);
76   }
77   
78   public void endPrefixMapping(String JavaDoc prefix) throws SAXException {
79   }
80   
81   public final void ignorableWhitespace(char[] ch, int start, int length)
82   throws SAXException {
83     currentHandler.ignorableWhitespace(ch, start, length);
84   }
85   
86   public final void processingInstruction(String JavaDoc target, String JavaDoc data)
87   throws SAXException {
88     currentHandler.processingInstruction(target, data);
89   }
90   
91   public void setDocumentLocator(Locator locator) {
92     this.locator = locator;
93   }
94   
95   public final void skippedEntity(String JavaDoc name)
96   throws SAXException {
97     currentHandler.skippedEntity(name);
98   }
99   
100   public void startDocument() throws SAXException {
101     contexts.reset();
102     currentMap.put("xml", "http://www.w3.org/XML/1998/namespace");
103   }
104   
105   public final void startElement(String JavaDoc namespaceURI, String JavaDoc localName,
106   String JavaDoc qName, Attributes atts)
107   throws SAXException {
108     handlers.addFirst(currentHandler);
109     currentHandler = currentHandler.startElement(namespaceURI, localName, atts);
110     currentMap.clear();
111     contexts.pushContext();
112   }
113   
114   public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException {
115     contexts.declarePrefix(prefix, uri);
116     currentMap.put(prefix, uri);
117   }
118   
119   public NamespaceContext getNamespaceContext() {
120     return contexts.getNamespaceContext();
121   }
122   
123   public String JavaDoc getNamespaceURI(String JavaDoc prefix) {
124     return contexts.getNamespaceURI(prefix);
125   }
126   
127   public String JavaDoc getPrefix(String JavaDoc uri) {
128     return contexts.getPrefix(uri);
129   }
130   
131   public List getDeclaredPrefixes() {
132     return contexts.getDeclaredPrefixes();
133   }
134   
135   public Collection getPrefixes() {
136     return contexts.getPrefixes();
137   }
138   
139   public Collection getPrefixes(String JavaDoc uri) {
140     return contexts.getPrefixes(uri);
141   }
142 }
143
Popular Tags