View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.mirae.j2me.xml.sax;
18  
19  import java.util.Enumeration;
20  import java.util.Hashtable;
21  import java.util.Vector;
22  
23  import org.xml.sax.Attributes;
24  
25  /***
26   * This is an implementation of org.xml.sax.Attributes
27   * @author Ias (iasandcb@tmax.co.kr)
28   * 
29   */
30  public class AttributesImpl implements Attributes {
31  
32  	private Vector keys;
33  	private Vector values;
34  	private Vector types;
35  	private Hashtable namespaces;
36  	private Hashtable typeDeclarations;
37  
38      /***
39       * The concrete constructor for Attributes
40       * @param namespaces
41       * @param defaultValues
42       * @param typeDefinitions
43       */
44  	public AttributesImpl(Hashtable namespaces, Hashtable defaultValues, Hashtable typeDefinitions) {
45  		this.namespaces = namespaces;
46  		keys = new Vector();
47  		values = new Vector();
48  		types = new Vector();
49  		this.typeDeclarations = typeDefinitions;
50  		if (defaultValues != null) {
51  			Enumeration enum = defaultValues.keys();
52  
53  			while (enum.hasMoreElements()) {
54  				String key = (String) enum.nextElement();
55  				add(key, getTypeFromDeclaration(key), (String) defaultValues.get(key));
56  			}
57  		}
58  	}
59  	
60  	public String getTypeFromDeclaration(String key) {
61  		String type = null;
62  		if (typeDeclarations != null) {
63  			type = (String) typeDeclarations.get(key);
64  		}
65  		if (type == null) {
66  			type = "CDATA";
67  		}
68  		return type;
69  	}
70  	
71  	public void add(String key, String type, String value) {
72  		keys.addElement(key);
73  		values.addElement(value);
74  		types.addElement(type);
75  	}
76  	/***
77  	 * @see org.xml.sax.Attributes#getLength()
78  	 */
79  	public int getLength() {
80  		return keys.size();
81  	}
82  
83  	private String getPrefix(String qName) {
84  		String prefix;
85  		int colonIndex = qName.indexOf(":");
86  		if (colonIndex < 0) {
87  			prefix = "";
88  		} else {
89  			prefix = qName.substring(0, colonIndex);
90  		}
91  		return prefix;
92  	}
93  
94  	private String getLocalName(String qName) {
95  		String localName;
96  		int colonIndex = qName.indexOf(":");
97  		if (colonIndex < 0) {
98  			localName = qName;
99  		} else {
100 			localName = qName.substring(colonIndex + 1);
101 		}
102 		return localName;
103 	}
104 
105 	/***
106 	 * @see org.xml.sax.Attributes#getURI(int)
107 	 */
108 	public String getURI(int index) {
109 		if (index >= keys.size()) {
110 			return null;
111 		}
112 		String key = (String) keys.elementAt(index);
113 		String uri = (String) namespaces.get(getPrefix(key));
114 		if (uri == null) {
115 			uri = "";
116 		}
117 		return uri;
118 	}
119 
120 	/***
121 	 * @see org.xml.sax.Attributes#getLocalName(int)
122 	 */
123 	public String getLocalName(int index) {
124 		return getLocalName((String) keys.elementAt(index));
125 	}
126 
127 	/***
128 	 * @see org.xml.sax.Attributes#getQName(int)
129 	 */
130 	public String getQName(int index) {
131 		return (String) keys.elementAt(index);
132 	}
133 
134 	/***
135 	 * @see org.xml.sax.Attributes#getType(int)
136 	 */
137 	public String getType(int index) {
138 		return (String) types.elementAt(index);
139 	}
140 
141 	/***
142 	 * @see org.xml.sax.Attributes#getValue(int)
143 	 */
144 	public String getValue(int index) {
145 		return (String) values.elementAt(index);
146 	}
147 
148 	/***
149 	 * @see org.xml.sax.Attributes#getIndex(java.lang.String, java.lang.String)
150 	 */
151 	public int getIndex(String uri, String localName) {
152 		String prefix = null;
153 		for (Enumeration uris = namespaces.elements(), prefixes = namespaces.keys(); uris.hasMoreElements();) {
154 			String namespaceUri = (String) uris.nextElement();
155 			String namespacePrefix = (String) prefixes.nextElement();
156 			if (namespaceUri.equals(uri)) {
157 				prefix = namespacePrefix;
158 				break;
159 			}
160 		}
161 		if (prefix == null) {
162 			return -1;
163 		}
164 		String qName;
165 		if (prefix.equals("")) {
166 			qName = localName;
167 		} else {
168 			qName = prefix + ":" + localName;
169 		}
170 		return getIndex(qName);
171 	}
172 
173 	/***
174 	 * @see org.xml.sax.Attributes#getIndex(java.lang.String)
175 	 */
176 	public int getIndex(String qName) {
177 		return keys.indexOf(qName);
178 	}
179 
180 	/***
181 	 * @see org.xml.sax.Attributes#getType(java.lang.String, java.lang.String)
182 	 */
183 	public String getType(String uri, String localName) {
184 		return (String) types.elementAt(getIndex(uri, localName));
185 	}
186 
187 	/***
188 	 * @see org.xml.sax.Attributes#getType(java.lang.String)
189 	 */
190 	public String getType(String qName) {
191 		return (String) types.elementAt(getIndex(qName));
192 	}
193 
194 	/***
195 	 * @see org.xml.sax.Attributes#getValue(java.lang.String, java.lang.String)
196 	 */
197 	public String getValue(String uri, String localName) {
198 		return (String) values.elementAt(getIndex(uri, localName));
199 	}
200 
201 	/***
202 	 * @see org.xml.sax.Attributes#getValue(java.lang.String)
203 	 */
204 	public String getValue(String qName) {
205 		return (String) values.elementAt(getIndex(qName));
206 	}
207 
208 }