KickJava   Java API By Example, From Geeks To Geeks.

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


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
26 import java.util.Collection JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.xml.sax.Attributes JavaDoc;
30 import org.xml.sax.SAXException JavaDoc;
31
32 public class NamespaceContextHandler extends HandlerDecorator implements NamespaceContext
33 {
34     private static final String JavaDoc RCSRevision = "$Revision: 1.3 $";
35     private static final String JavaDoc RCSName = "$Name: $";
36     
37     protected NamespaceContextStack contextStack = new NamespaceContextStack();
38     
39     private static final byte UNKNOWN = 0;
40     private static final byte START_ELEMENT = 1;
41     private static final byte END_ELEMENT = 2;
42     private static final byte START_PREFIX = 3;
43     private static final byte END_PREFIX = 4;
44
45     private byte status = UNKNOWN;
46
47     /** Creates new NamespaceContextHandler */
48     public NamespaceContextHandler()
49     {
50         super();
51     }
52     
53     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
54     throws SAXException JavaDoc
55     {
56         contextStack.declarePrefix(prefix, uri);
57         super.startPrefixMapping(prefix, uri);
58         status = START_PREFIX;
59     }
60     
61     public void endPrefixMapping(String JavaDoc prefix)
62     throws SAXException JavaDoc
63     {
64         // NOTE: the case where an end prefix event follows a start prefix event
65
// is due to a bug of XQuery reconstruction.
66
if (status == END_ELEMENT || status == START_PREFIX) // clean the stack of declarations of the level
67
{
68             contextStack.popContext();
69             contextStack.pushContext();
70         }
71         super.endPrefixMapping(prefix);
72         status = END_PREFIX;
73     }
74     
75     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts)
76     throws SAXException JavaDoc
77     {
78         super.startElement(namespaceURI, localName, qName, atts);
79         contextStack.pushContext();
80         status = START_ELEMENT;
81     }
82     
83     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName)
84     throws SAXException JavaDoc
85     {
86         contextStack.popContext();
87         super.endElement(namespaceURI, localName, qName);
88         status = END_ELEMENT;
89     }
90     
91     public NamespaceContext getNamespaceContext()
92     {
93         return contextStack.getNamespaceContext();
94     }
95     
96     public String JavaDoc getNamespaceURI(String JavaDoc prefix)
97     {
98         return contextStack.getNamespaceURI(prefix);
99     }
100     
101     public Collection JavaDoc getNamespaceURIs() {
102         return contextStack.getNamespaceURIs();
103     }
104     
105     public String JavaDoc getPrefix(String JavaDoc uri)
106     {
107         return contextStack.getPrefix(uri);
108     }
109     
110     public List JavaDoc getDeclaredPrefixes()
111     {
112         return contextStack.getDeclaredPrefixes();
113     }
114     
115     public Collection JavaDoc getPrefixes()
116     {
117         return contextStack.getPrefixes();
118     }
119     
120     public Collection JavaDoc getPrefixes(String JavaDoc uri)
121     {
122         return contextStack.getPrefixes(uri);
123     }
124     
125 }
126
Popular Tags