root / trunk / libraries / libGPE / src / org / gvsig / xmlschema / utils / DOMObjectsFactory.java @ 12439
History | View | Annotate | Download (9.07 KB)
1 |
package org.gvsig.xmlschema.utils; |
---|---|
2 |
|
3 |
import org.gvsig.xmlschema.som.IXSContentType; |
4 |
import org.gvsig.xmlschema.som.IXSElementDeclaration; |
5 |
import org.gvsig.xmlschema.som.IXSSchema; |
6 |
import org.w3c.dom.Element; |
7 |
|
8 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
9 |
*
|
10 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
11 |
*
|
12 |
* This program is free software; you can redistribute it and/or
|
13 |
* modify it under the terms of the GNU General Public License
|
14 |
* as published by the Free Software Foundation; either version 2
|
15 |
* of the License, or (at your option) any later version.
|
16 |
*
|
17 |
* This program is distributed in the hope that it will be useful,
|
18 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
19 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
20 |
* GNU General Public License for more details.
|
21 |
*
|
22 |
* You should have received a copy of the GNU General Public License
|
23 |
* along with this program; if not, write to the Free Software
|
24 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
25 |
*
|
26 |
* For more information, contact:
|
27 |
*
|
28 |
* Generalitat Valenciana
|
29 |
* Conselleria d'Infraestructures i Transport
|
30 |
* Av. Blasco Ib??ez, 50
|
31 |
* 46010 VALENCIA
|
32 |
* SPAIN
|
33 |
*
|
34 |
* +34 963862235
|
35 |
* gvsig@gva.es
|
36 |
* www.gvsig.gva.es
|
37 |
*
|
38 |
* or
|
39 |
*
|
40 |
* IVER T.I. S.A
|
41 |
* Salamanca 50
|
42 |
* 46005 Valencia
|
43 |
* Spain
|
44 |
*
|
45 |
* +34 963163400
|
46 |
* dac@iver.es
|
47 |
*/
|
48 |
/* CVS MESSAGES:
|
49 |
*
|
50 |
* $Id: DOMObjectsFactory.java 12439 2007-07-02 10:00:46Z jorpiell $
|
51 |
* $Log$
|
52 |
* Revision 1.3 2007-07-02 09:57:35 jorpiell
|
53 |
* The generated xsd schemas have to be valid
|
54 |
*
|
55 |
* Revision 1.2 2007/06/29 12:19:14 jorpiell
|
56 |
* The schema validation is made independently of the concrete writer
|
57 |
*
|
58 |
* Revision 1.1 2007/06/14 16:15:03 jorpiell
|
59 |
* builds to create the jars generated and add the schema code to the libGPEProject
|
60 |
*
|
61 |
* Revision 1.1 2007/06/14 13:50:07 jorpiell
|
62 |
* The schema jar name has been changed
|
63 |
*
|
64 |
* Revision 1.3 2007/06/08 11:35:16 jorpiell
|
65 |
* IXSSchema interface updated
|
66 |
*
|
67 |
* Revision 1.2 2007/06/08 06:55:05 jorpiell
|
68 |
* Fixed some bugs
|
69 |
*
|
70 |
* Revision 1.1 2007/06/07 14:54:13 jorpiell
|
71 |
* Add the schema support
|
72 |
*
|
73 |
* Revision 1.3 2007/05/30 12:53:33 jorpiell
|
74 |
* Not used libraries deleted
|
75 |
*
|
76 |
* Revision 1.2 2007/05/30 12:50:53 jorpiell
|
77 |
* Refactoring of some duplicated methods
|
78 |
*
|
79 |
* Revision 1.1 2007/05/30 12:25:48 jorpiell
|
80 |
* Add the element collection
|
81 |
*
|
82 |
*
|
83 |
*/
|
84 |
/**
|
85 |
* This class contains methods to create DOM elements.
|
86 |
* @author Jorge Piera LLodr? (jorge.piera@iver.es)
|
87 |
*/
|
88 |
public class DOMObjectsFactory { |
89 |
private static DOMObjectsFactory instance = null; |
90 |
private String xsNamespacePrefix = null; |
91 |
|
92 |
/**
|
93 |
* This method cretaes the singleton instance
|
94 |
*
|
95 |
*/
|
96 |
private synchronized static void createInstance() { |
97 |
if (instance == null) { |
98 |
instance = new DOMObjectsFactory();
|
99 |
} |
100 |
} |
101 |
|
102 |
/**
|
103 |
* @return the factory instance
|
104 |
*/
|
105 |
public static DOMObjectsFactory getInstance() { |
106 |
if (instance == null){ |
107 |
createInstance(); |
108 |
} |
109 |
return instance;
|
110 |
} |
111 |
|
112 |
public DOMObjectsFactory(){
|
113 |
xsNamespacePrefix = SchemaTags.XS_NS; |
114 |
} |
115 |
|
116 |
/**
|
117 |
* Fill the element node attributes
|
118 |
* @param schema
|
119 |
* Schema that will be used to create the element
|
120 |
* @param name
|
121 |
* Element name
|
122 |
* @param type
|
123 |
* Element type
|
124 |
* @param nillable
|
125 |
* If the element is nillable
|
126 |
* @param minOccurs
|
127 |
* The min occurs
|
128 |
* @param maxOccurs
|
129 |
* The max occurs
|
130 |
*/
|
131 |
public Element createElement(IXSSchema schema, String name, String type, boolean nillable, int minOccurs, int maxOccurs){ |
132 |
Element element = schema.getDocument().createElement(addXSQname(SchemaTags.ELEMENT));
|
133 |
element.setAttribute(SchemaTags.NAME, name); |
134 |
if (type != null){ |
135 |
element.setAttribute(SchemaTags.TYPE, type); |
136 |
} |
137 |
element.setAttribute(SchemaTags.NILLABLE, String.valueOf(nillable));
|
138 |
String sMinOccurs = null; |
139 |
if (minOccurs == IXSElementDeclaration.MIN_OCCURS_UNBOUNDED){
|
140 |
sMinOccurs = SchemaTags.UNBOUNDED; |
141 |
}else{
|
142 |
sMinOccurs = String.valueOf(minOccurs);
|
143 |
} |
144 |
element.setAttribute(SchemaTags.MIN_OCCURS, sMinOccurs); |
145 |
String sMaxOccurs = null; |
146 |
if (maxOccurs == IXSElementDeclaration.MAX_OCCURS_UNBOUNDED){
|
147 |
sMaxOccurs = SchemaTags.UNBOUNDED; |
148 |
}else{
|
149 |
sMaxOccurs = String.valueOf(maxOccurs);
|
150 |
} |
151 |
element.setAttribute(SchemaTags.MAX_OCCURS, sMaxOccurs); |
152 |
return element;
|
153 |
} |
154 |
|
155 |
/**
|
156 |
* Fill the element node attributes
|
157 |
* @param schema
|
158 |
* Schema that will be used to create the element
|
159 |
* @param name
|
160 |
* Element name
|
161 |
* @param type
|
162 |
* Element type
|
163 |
* @param nillable
|
164 |
* If the element is nillable
|
165 |
* @param minOccurs
|
166 |
* The min occurs
|
167 |
* @param maxOccurs
|
168 |
* The max occurs
|
169 |
*/
|
170 |
public Element createElement(IXSSchema schema, String name, String type, String substitutionGroup){ |
171 |
Element element = schema.getDocument().createElement(addXSQname(SchemaTags.ELEMENT));
|
172 |
element.setAttribute(SchemaTags.NAME, name); |
173 |
if (type != null){ |
174 |
element.setAttribute(SchemaTags.TYPE, type); |
175 |
} |
176 |
if (substitutionGroup != null){ |
177 |
element.setAttribute(SchemaTags.SUBSTITUTIONGROUP, type); |
178 |
} |
179 |
return element;
|
180 |
} |
181 |
|
182 |
/**
|
183 |
* Creates a new ComplxType element
|
184 |
* @param schema
|
185 |
* Schema that will be used to create the element
|
186 |
* @param name
|
187 |
* ComplexType name
|
188 |
* @param type
|
189 |
* See the IXSComplexType for possible values
|
190 |
* @param contentType
|
191 |
* A complex content or a simple content
|
192 |
* See the IXSContentType for possible values
|
193 |
* @param contentTypeRestriction
|
194 |
* A extension or a restriction
|
195 |
* @return
|
196 |
*/
|
197 |
public Element createComplexType(IXSSchema schema, String name, String type, String contentType, String conteTypeRestriction){ |
198 |
Element eComplexType = schema.getDocument().createElement(addXSQname(SchemaTags.COMPLEX_TYPE));
|
199 |
if (name != null){ |
200 |
eComplexType.setAttribute(SchemaTags.NAME, name); |
201 |
} |
202 |
if (contentType.compareTo(IXSContentType.WITOUT_CONTENT) == 0){ |
203 |
Element eSimpleContent = schema.getDocument().createElement(addXSQname(type));
|
204 |
eComplexType.appendChild(eSimpleContent); |
205 |
}else{
|
206 |
Element eContentType = schema.getDocument().createElement(addXSQname(contentType));
|
207 |
eComplexType.appendChild(eContentType); |
208 |
Element eRestriction = schema.getDocument().createElement(addXSQname(conteTypeRestriction));
|
209 |
eContentType.appendChild(eRestriction); |
210 |
Element eSimpleContent = schema.getDocument().createElement(addXSQname(type));
|
211 |
eRestriction.appendChild(eSimpleContent); |
212 |
} |
213 |
return eComplexType;
|
214 |
} |
215 |
|
216 |
/**
|
217 |
* Adds an element to a complex type. It only will works
|
218 |
* if the type doesn't has a simple content or a complex
|
219 |
* content
|
220 |
* @param schema
|
221 |
* XML schema
|
222 |
* @param eElement
|
223 |
* The XML schema element
|
224 |
* @param eComplexType
|
225 |
* The XML schema complex type
|
226 |
*/
|
227 |
public boolean addElementToComplexType(IXSSchema schema, Element eElement, Element eComplexType){ |
228 |
//If is a ComplexContent..
|
229 |
Element eComplexContent = SchemaUtils.searchChildByTagName(eComplexType, SchemaTags.COMPLEX_CONTENT);
|
230 |
if (eComplexContent != null){ |
231 |
Element eContentElement = findElementToWrite(schema, eComplexContent, getContentElements());
|
232 |
if (eContentElement != null){ |
233 |
//Restriction or extension
|
234 |
Element eGroup = findElementToWrite(schema, eContentElement, getGroupElements());
|
235 |
if (eGroup != null){ |
236 |
eGroup.appendChild(eElement); |
237 |
return true; |
238 |
} |
239 |
} |
240 |
} |
241 |
//If is a simpleContent..
|
242 |
Element eSimpleContent = SchemaUtils.searchChildByTagName(eComplexType, SchemaTags.SIMPLE_CONTENT);
|
243 |
if (eSimpleContent != null){ |
244 |
Element eContentElement = findElementToWrite(schema, eSimpleContent, getContentElements());
|
245 |
if (eContentElement != null){ |
246 |
eContentElement.appendChild(eElement); |
247 |
} |
248 |
return true; |
249 |
} |
250 |
//Else will be a group, a sequence, a choice or a all node.
|
251 |
Element eGroup = findElementToWrite(schema, eComplexType, getGroupElements());
|
252 |
if (eGroup != null){ |
253 |
eGroup.appendChild(eElement); |
254 |
return true; |
255 |
} |
256 |
return false; |
257 |
} |
258 |
|
259 |
/**
|
260 |
* Find the root element to write the sub elements.
|
261 |
* @param schema
|
262 |
* XML Schema
|
263 |
* @param root
|
264 |
* Root element
|
265 |
* @param elements
|
266 |
* Elements to search
|
267 |
* @return
|
268 |
*/
|
269 |
private Element findElementToWrite(IXSSchema schema, |
270 |
Element root,
|
271 |
String[] elements){ |
272 |
for (int i=0 ; i<elements.length ; i++){ |
273 |
Element element = SchemaUtils.searchChildByTagName(root, elements[i]);
|
274 |
if (element != null){ |
275 |
return element;
|
276 |
} |
277 |
} |
278 |
return null; |
279 |
} |
280 |
|
281 |
/**
|
282 |
* @return the group elements
|
283 |
*/
|
284 |
private String[] getGroupElements(){ |
285 |
String[] groupElements = new String[4]; |
286 |
groupElements[0] = SchemaTags.SEQUENCE;
|
287 |
groupElements[1] = SchemaTags.CHOICE;
|
288 |
groupElements[2] = SchemaTags.ALL;
|
289 |
groupElements[3] = SchemaTags.GROUP;
|
290 |
return groupElements;
|
291 |
} |
292 |
|
293 |
|
294 |
/**
|
295 |
* @return the content elements
|
296 |
*/
|
297 |
private String[] getContentElements(){ |
298 |
String[] groupElements = new String[2]; |
299 |
groupElements[0] = SchemaTags.RESTRICTION;
|
300 |
groupElements[1] = SchemaTags.EXTENSION;
|
301 |
return groupElements;
|
302 |
} |
303 |
|
304 |
/**
|
305 |
* Uset to create the XML schema tags. It adds the
|
306 |
* namespace prefix (xs or xsd)
|
307 |
* @param tagName
|
308 |
* @return
|
309 |
*/
|
310 |
private String addXSQname(String tagName){ |
311 |
return xsNamespacePrefix + ":" + tagName; |
312 |
} |
313 |
|
314 |
/**
|
315 |
* @param xsNamespacePrefix the xsNamespacePrefix to set
|
316 |
*/
|
317 |
public void setXsNamespacePrefix(String xsNamespacePrefix) { |
318 |
this.xsNamespacePrefix = xsNamespacePrefix;
|
319 |
} |
320 |
} |