KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
26 import java.util.NoSuchElementException JavaDoc;
27
28 import org.w3c.dom.Attr JavaDoc;
29 import org.w3c.dom.NamedNodeMap JavaDoc;
30 import org.xml.sax.Attributes JavaDoc;
31
32 /** Provides a dynamic (no memory structure is built) wrapping of DOM2
33  * to SAX2 attributes (<B>assuming xmlns attributes are removed</B>).
34  */

35 public class DOM2SAXAttrs implements Attributes JavaDoc
36 {
37 private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
38 private static final String JavaDoc RCSName = "$Name: $";
39     
40     private NamedNodeMap JavaDoc atts;
41     private int length = 0;
42     private boolean hasXMLNSAttrs = false; // used to avoid list processing when no xmlms is present
43
// Could not remove because source cannot be changed and
44
// Clone() is to expensive for a dynamic wrapping
45
private boolean namespacePrefixes = false;
46     
47     public DOM2SAXAttrs()
48     {
49     }
50     public DOM2SAXAttrs(NamedNodeMap JavaDoc atts)
51     {
52         setDOMAttributes(atts);
53     }
54     
55     public DOM2SAXAttrs(NamedNodeMap JavaDoc atts, boolean namespacePrefixes)
56     {
57         setNamespacePrefixes(namespacePrefixes); // Must be first !!!
58
setDOMAttributes(atts);
59     }
60     
61     public DOM2SAXAttrs(boolean namespacePrefixes)
62     {
63         setNamespacePrefixes(namespacePrefixes);
64     }
65     
66     public void setDOMAttributes(NamedNodeMap JavaDoc atts)
67     {
68         this.atts = atts;
69         if (namespacePrefixes) // process xmlns attribute like any other
70
length = atts.getLength();
71         else // they must be ignored
72
{
73             length = 0;
74             for (int i = 0; i < atts.getLength(); i++)
75             {
76                 Attr JavaDoc a = (Attr JavaDoc)atts.item(i);
77                 if (isPrefixDef(a))
78                     hasXMLNSAttrs = true;
79                 else
80                     length++;
81             }
82         }
83     }
84     
85     public void setNamespacePrefixes(boolean namespacePrefixes)
86     {
87         this.namespacePrefixes = namespacePrefixes;
88     }
89         
90     /*
91      * returns an iteartor on DOM Attr nodes corresponding to xmlns attributes.
92      * <p><B>WARNING ! If Namespace-prefixes feature is on or no xmlms attributes
93      * : null is returned.<p>
94      */

95
96     public Iterator JavaDoc getPrefixIterator()
97     {
98         if (!hasXMLNSAttrs || namespacePrefixes) // NOTE : should return an iterator even if namespacePrefixes
99
return null;
100         else
101             return new Iterator JavaDoc() {
102                 int i = 0; // next index in atts to read
103
int remainingPrefixes = atts.getLength() - length; // number of prefixes remaining to browse
104
public boolean hasNext()
105                 {
106                     return (remainingPrefixes > 0);
107                 }
108                 public Object JavaDoc next()
109                 {
110                     if (remainingPrefixes == 0)
111                         throw new NoSuchElementException JavaDoc("No more xmlns attributes");
112                     Attr JavaDoc a;
113                     do
114                     {
115                         a = (Attr JavaDoc)atts.item(i);
116                         i++;
117                     }
118                     while (!isPrefixDef(a));
119
120                     remainingPrefixes--;
121                     return a;
122                 }
123                 public void remove()
124                 {
125                     throw new UnsupportedOperationException JavaDoc();
126                 }
127             };
128     }
129     
130     ///////////////////////////////////////////////////////////////////////////
131
// PRIVATE UTILITIES
132
///////////////////////////////////////////////////////////////////////////
133
private Attr JavaDoc getAttr(int index)
134     {
135         if (namespacePrefixes || !hasXMLNSAttrs)
136             return (Attr JavaDoc)atts.item(index);
137         else
138         {
139             for (int i = 0; i < atts.getLength(); i++)
140             {
141                 Attr JavaDoc a = (Attr JavaDoc)atts.item(i);
142                 if (!isPrefixDef(a))
143                 {
144                     if (index == 0)
145                         return a;
146                     index--;
147                 }
148             }
149         }
150         return null;
151     }
152     
153     private int getIndex(Attr JavaDoc attr)
154     {
155         if (attr != null && !isPrefixDef(attr))
156         {
157             int result = 0;
158             for (int i = 0; i < atts.getLength(); i++)
159             {
160                 Attr JavaDoc a = (Attr JavaDoc)atts.item(i);
161                 if (!hasXMLNSAttrs || !isPrefixDef(a))
162                 {
163                     if (attr == atts.item(i))
164                         return result;
165                     result++;
166                 }
167             }
168         }
169         return -1;
170     }
171     
172     private String JavaDoc getLocalName(Attr JavaDoc attr)
173     {
174         if (attr != null && !isPrefixDef(attr))
175             return attr.getLocalName() == null ? "" : attr.getLocalName();
176         else
177             return null;
178     }
179     
180     private String JavaDoc getQName(Attr JavaDoc attr)
181     {
182         if (attr != null && !isPrefixDef(attr))
183             return attr.getName() == null ? "" : attr.getName();
184         else
185             return null;
186     }
187     
188     private String JavaDoc getURI(Attr JavaDoc attr)
189     {
190         if (attr != null && !isPrefixDef(attr))
191             return attr.getNamespaceURI() == null ? "" : attr.getNamespaceURI();
192         else
193             return null;
194     }
195     
196     private String JavaDoc getValue(Attr JavaDoc attr)
197     {
198         if (attr != null && !isPrefixDef(attr))
199             return attr.getValue();
200         else
201             return null;
202     }
203     
204     private boolean isPrefixDef(Attr JavaDoc attr)
205     {
206         return (attr.getNamespaceURI() != null)
207             && (attr.getNamespaceURI().equals(SAXConstants.XMLNS_URI));
208     }
209     
210     ///////////////////////////////////////////////////////////////////////////
211
// Attributes IMPLEMENTATION
212
///////////////////////////////////////////////////////////////////////////
213
public int getLength()
214     {
215         return length;
216     }
217     
218     public String JavaDoc getLocalName(int index)
219     {
220         return getLocalName(getAttr(index));
221     }
222     
223     public String JavaDoc getQName(int index)
224     {
225         return getQName(getAttr(index));
226     }
227     
228     public String JavaDoc getType(int index)
229     {
230         return "CDATA";
231     }
232     
233     public String JavaDoc getURI(int index)
234     {
235         return getURI(getAttr(index));
236     }
237     
238     public String JavaDoc getValue(int index) {
239         return getValue(getAttr(index));
240     }
241     
242     public int getIndex(String JavaDoc uri, String JavaDoc localPart)
243     {
244         if ((uri == null) || (uri.length() == 0))
245             return getIndex((Attr JavaDoc)atts.getNamedItem(localPart));
246         else
247             return getIndex((Attr JavaDoc)atts.getNamedItemNS(uri, localPart));
248     }
249     
250     public String JavaDoc getType(String JavaDoc uri, String JavaDoc localPart)
251     {
252         return "CDATA";
253     }
254     
255     public String JavaDoc getValue(String JavaDoc uri, String JavaDoc localPart)
256     {
257         if ((uri == null) || (uri.length() == 0))
258             return getValue((Attr JavaDoc)atts.getNamedItem(localPart));
259         else
260             return getValue((Attr JavaDoc)atts.getNamedItemNS(uri, localPart));
261     }
262     
263     public int getIndex(String JavaDoc qName)
264     {
265         return getIndex((Attr JavaDoc)atts.getNamedItem(qName));
266     }
267     
268     public String JavaDoc getType(String JavaDoc qName)
269     {
270         return "CDATA";
271     }
272     
273     public String JavaDoc getValue(String JavaDoc qName)
274     {
275         return getValue((Attr JavaDoc)atts.getNamedItem(qName));
276     }
277     
278 }
279
280
Popular Tags