root / trunk / libraries / libGPE / src / org / gvsig / xmlschema / utils / SchemaCollection.java @ 12175
History | View | Annotate | Download (6.36 KB)
1 |
package org.gvsig.xmlschema.utils; |
---|---|
2 |
|
3 |
import java.util.Collection; |
4 |
import java.util.Iterator; |
5 |
|
6 |
import org.gvsig.xmlschema.exceptions.TypeNotFoundException; |
7 |
import org.gvsig.xmlschema.som.IXSNode; |
8 |
import org.gvsig.xmlschema.som.IXSSchema; |
9 |
import org.w3c.dom.Element; |
10 |
import org.w3c.dom.NodeList; |
11 |
|
12 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
13 |
*
|
14 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
15 |
*
|
16 |
* This program is free software; you can redistribute it and/or
|
17 |
* modify it under the terms of the GNU General Public License
|
18 |
* as published by the Free Software Foundation; either version 2
|
19 |
* of the License, or (at your option) any later version.
|
20 |
*
|
21 |
* This program is distributed in the hope that it will be useful,
|
22 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
23 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
24 |
* GNU General Public License for more details.
|
25 |
*
|
26 |
* You should have received a copy of the GNU General Public License
|
27 |
* along with this program; if not, write to the Free Software
|
28 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
29 |
*
|
30 |
* For more information, contact:
|
31 |
*
|
32 |
* Generalitat Valenciana
|
33 |
* Conselleria d'Infraestructures i Transport
|
34 |
* Av. Blasco Ib??ez, 50
|
35 |
* 46010 VALENCIA
|
36 |
* SPAIN
|
37 |
*
|
38 |
* +34 963862235
|
39 |
* gvsig@gva.es
|
40 |
* www.gvsig.gva.es
|
41 |
*
|
42 |
* or
|
43 |
*
|
44 |
* IVER T.I. S.A
|
45 |
* Salamanca 50
|
46 |
* 46005 Valencia
|
47 |
* Spain
|
48 |
*
|
49 |
* +34 963163400
|
50 |
* dac@iver.es
|
51 |
*/
|
52 |
/* CVS MESSAGES:
|
53 |
*
|
54 |
* $Id: SchemaCollection.java 12175 2007-06-14 16:15:05Z jorpiell $
|
55 |
* $Log$
|
56 |
* Revision 1.1 2007-06-14 16:15:03 jorpiell
|
57 |
* builds to create the jars generated and add the schema code to the libGPEProject
|
58 |
*
|
59 |
* Revision 1.1 2007/06/14 13:50:07 jorpiell
|
60 |
* The schema jar name has been changed
|
61 |
*
|
62 |
* Revision 1.5 2007/06/08 06:55:05 jorpiell
|
63 |
* Fixed some bugs
|
64 |
*
|
65 |
* Revision 1.4 2007/06/07 14:54:13 jorpiell
|
66 |
* Add the schema support
|
67 |
*
|
68 |
* Revision 1.3 2007/05/30 12:53:33 jorpiell
|
69 |
* Not used libraries deleted
|
70 |
*
|
71 |
* Revision 1.2 2007/05/30 12:50:53 jorpiell
|
72 |
* Refactoring of some duplicated methods
|
73 |
*
|
74 |
* Revision 1.1 2007/05/30 12:25:48 jorpiell
|
75 |
* Add the element collection
|
76 |
*
|
77 |
*
|
78 |
*/
|
79 |
/**
|
80 |
* This class represents the children from a
|
81 |
* DOM node
|
82 |
* @author Jorge Piera LLodr? (jorge.piera@iver.es)
|
83 |
*/
|
84 |
public class SchemaCollection implements Collection{ |
85 |
private Element element = null; |
86 |
private SchemaObjectsMapping collectionTypes = null; |
87 |
private IXSSchema schema = null; |
88 |
|
89 |
public SchemaCollection(IXSSchema schema, Element element) { |
90 |
this.schema = schema;
|
91 |
this.element = element;
|
92 |
this.collectionTypes = schema.getObjectsMapping();
|
93 |
} |
94 |
|
95 |
public SchemaCollection(IXSSchema schema, Element element, SchemaObjectsMapping collectionTypes) { |
96 |
this.schema = schema;
|
97 |
this.element = element;
|
98 |
this.collectionTypes = collectionTypes;
|
99 |
} |
100 |
|
101 |
/**
|
102 |
* Return the first element of the collection
|
103 |
* @return
|
104 |
*/
|
105 |
public IXSNode getFirstNode(){
|
106 |
Iterator it = iterator();
|
107 |
it.hasNext(); |
108 |
Object obj = it.next();
|
109 |
if (obj != null){ |
110 |
return (IXSNode)obj;
|
111 |
} |
112 |
return null; |
113 |
} |
114 |
|
115 |
/*
|
116 |
* (non-Javadoc)
|
117 |
* @see java.util.Collection#add(java.lang.Object)
|
118 |
*/
|
119 |
public boolean add(Object o) { |
120 |
if (o instanceof IXSNode){ |
121 |
element.appendChild(((IXSNode)o).getElement()); |
122 |
return true; |
123 |
} |
124 |
return false; |
125 |
} |
126 |
|
127 |
/*
|
128 |
* (non-Javadoc)
|
129 |
* @see java.util.Collection#addAll(java.util.Collection)
|
130 |
*/
|
131 |
public boolean addAll(Collection c) { |
132 |
Iterator it = c.iterator();
|
133 |
while (it.hasNext()){
|
134 |
Object obj = it.next();
|
135 |
if (!add(obj)){
|
136 |
return false; |
137 |
} |
138 |
} |
139 |
return true; |
140 |
} |
141 |
|
142 |
/*
|
143 |
* (non-Javadoc)
|
144 |
* @see java.util.Collection#clear()
|
145 |
*/
|
146 |
public void clear() { |
147 |
NodeList nl = element.getChildNodes(); |
148 |
for(int i = 0; i < nl.getLength(); i++){ |
149 |
element.removeChild(nl.item(i)); |
150 |
} |
151 |
} |
152 |
|
153 |
/*
|
154 |
* (non-Javadoc)
|
155 |
* @see java.util.Collection#contains(java.lang.Object)
|
156 |
*/
|
157 |
public boolean contains(Object o) { |
158 |
if (o instanceof IXSNode){ |
159 |
return false; |
160 |
} |
161 |
Element element = ((IXSNode)o).getElement();
|
162 |
NodeList nl = element.getChildNodes(); |
163 |
for(int i = 0; i < nl.getLength(); i++){ |
164 |
if (nl.item(i).equals(element)){
|
165 |
return true; |
166 |
} |
167 |
} |
168 |
return false; |
169 |
} |
170 |
|
171 |
|
172 |
public boolean containsAll(Collection c) { |
173 |
throw new UnsupportedOperationException(); |
174 |
} |
175 |
|
176 |
/*
|
177 |
* (non-Javadoc)
|
178 |
* @see java.util.Collection#isEmpty()
|
179 |
*/
|
180 |
public boolean isEmpty() { |
181 |
return element.getChildNodes().getLength() == 0; |
182 |
} |
183 |
|
184 |
/*
|
185 |
* (non-Javadoc)
|
186 |
* @see java.util.Collection#iterator()
|
187 |
*/
|
188 |
public Iterator iterator() { |
189 |
return new SchemaIterator(element.getChildNodes(), collectionTypes); |
190 |
} |
191 |
|
192 |
/*
|
193 |
* (non-Javadoc)
|
194 |
* @see java.util.Collection#remove(java.lang.Object)
|
195 |
*/
|
196 |
public boolean remove(Object o) { |
197 |
NodeList nl = element.getChildNodes(); |
198 |
for(int i = 0; i < nl.getLength(); i++){ |
199 |
if (o.equals(nl.item(i))){
|
200 |
element.removeChild(nl.item(i)); |
201 |
return true; |
202 |
} |
203 |
} |
204 |
return false; |
205 |
} |
206 |
|
207 |
/*
|
208 |
* (non-Javadoc)
|
209 |
* @see java.util.Collection#removeAll(java.util.Collection)
|
210 |
*/
|
211 |
public boolean removeAll(Collection c) { |
212 |
NodeList nl = element.getChildNodes(); |
213 |
for(int i = 0; i < nl.getLength(); i++){ |
214 |
element.removeChild(nl.item(i)); |
215 |
} |
216 |
return true; |
217 |
} |
218 |
|
219 |
/*
|
220 |
* (non-Javadoc)
|
221 |
* @see java.util.Collection#retainAll(java.util.Collection)
|
222 |
*/
|
223 |
public boolean retainAll(Collection c) { |
224 |
throw new UnsupportedOperationException(); |
225 |
} |
226 |
|
227 |
/*
|
228 |
* (non-Javadoc)
|
229 |
* @see java.util.Collection#size()
|
230 |
*/
|
231 |
public int size() { |
232 |
int size = 0; |
233 |
Iterator it = collectionTypes.getTypes().iterator();
|
234 |
while (it.hasNext()){
|
235 |
String type = (String)it.next(); |
236 |
Element childElement = SchemaUtils.searchChildByTagName(element, type);
|
237 |
if (childElement != null){ |
238 |
size++; |
239 |
} |
240 |
} |
241 |
return size;
|
242 |
} |
243 |
|
244 |
/*
|
245 |
* (non-Javadoc)
|
246 |
* @see java.util.Collection#toArray()
|
247 |
*/
|
248 |
public Object[] toArray() { |
249 |
throw new UnsupportedOperationException(); |
250 |
} |
251 |
|
252 |
/*
|
253 |
* (non-Javadoc)
|
254 |
* @see java.util.Collection#toArray(java.lang.Object[])
|
255 |
*/
|
256 |
public Object[] toArray(Object[] a) { |
257 |
throw new UnsupportedOperationException(); |
258 |
} |
259 |
|
260 |
/**
|
261 |
* Add a new type to the types collection
|
262 |
* @param type
|
263 |
* Type name that will be found in the XML file
|
264 |
* @param clazz
|
265 |
* Class used to create the object that envolves the element
|
266 |
* @throws TypeNotFoundException
|
267 |
*/
|
268 |
public void addType(String type, Class clazz) throws TypeNotFoundException{ |
269 |
if (collectionTypes == null){ |
270 |
collectionTypes = new SchemaObjectsMapping(schema);
|
271 |
} |
272 |
collectionTypes.addType(type, clazz); |
273 |
} |
274 |
} |