Statistics
| Revision:

root / trunk / libraries / libGPE / src / org / gvsig / xmlschema / utils / DOMObjectsFactory.java @ 12418

History | View | Annotate | Download (8.25 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 12418 2007-06-29 12:19:48Z jorpiell $
51
 * $Log$
52
 * Revision 1.2  2007-06-29 12:19:14  jorpiell
53
 * The schema validation is made independently of the concrete writer
54
 *
55
 * Revision 1.1  2007/06/14 16:15:03  jorpiell
56
 * builds to create the jars generated and add the schema code to the libGPEProject
57
 *
58
 * Revision 1.1  2007/06/14 13:50:07  jorpiell
59
 * The schema jar name has been changed
60
 *
61
 * Revision 1.3  2007/06/08 11:35:16  jorpiell
62
 * IXSSchema interface updated
63
 *
64
 * Revision 1.2  2007/06/08 06:55:05  jorpiell
65
 * Fixed some bugs
66
 *
67
 * Revision 1.1  2007/06/07 14:54:13  jorpiell
68
 * Add the schema support
69
 *
70
 * Revision 1.3  2007/05/30 12:53:33  jorpiell
71
 * Not used libraries deleted
72
 *
73
 * Revision 1.2  2007/05/30 12:50:53  jorpiell
74
 * Refactoring of some duplicated methods
75
 *
76
 * Revision 1.1  2007/05/30 12:25:48  jorpiell
77
 * Add the element collection
78
 *
79
 *
80
 */
81
/**
82
 * This class contains methods to create DOM elements.
83
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
84
 */
85
public class DOMObjectsFactory {
86
        private static DOMObjectsFactory instance = null;
87
        private String xsNamespacePrefix = null;
88
        
89
        /**
90
         * This method cretaes the singleton instance
91
         *
92
         */
93
        private synchronized static void createInstance() {
94
                if (instance == null) { 
95
                        instance = new DOMObjectsFactory();
96
                }
97
        }
98

    
99
        /**
100
         * @return the factory instance
101
         */
102
        public static DOMObjectsFactory getInstance() {
103
                if (instance == null){
104
                        createInstance();
105
                }
106
                return instance;
107
        }
108
        
109
        public DOMObjectsFactory(){
110
                xsNamespacePrefix = SchemaTags.XS_NS;
111
        }
112
        
113
        /**
114
         * Fill the element node attributes  
115
         * @param schema
116
         * Schema that will be used to create the element
117
         * @param name
118
         * Element name
119
         * @param type
120
         * Element type
121
         * @param nillable
122
         * If the element is nillable
123
         * @param minOccurs
124
         * The min occurs
125
         * @param maxOccurs
126
         * The max occurs
127
         */
128
        public Element createElement(IXSSchema schema, String name, String type, boolean nillable, int minOccurs, int maxOccurs){
129
                Element element = schema.getDocument().createElement(addXSQname(SchemaTags.ELEMENT));
130
                element.setAttribute(SchemaTags.NAME, name);
131
                if (type != null){
132
                        element.setAttribute(SchemaTags.TYPE, type);
133
                }
134
                element.setAttribute(SchemaTags.NILLABLE, String.valueOf(nillable));
135
                String sMinOccurs = null;
136
                if (minOccurs == IXSElementDeclaration.MIN_OCCURS_UNBOUNDED){
137
                        sMinOccurs = SchemaTags.UNBOUNDED;
138
                }else{
139
                        sMinOccurs = String.valueOf(minOccurs);
140
                }
141
                element.setAttribute(SchemaTags.MIN_OCCURS, sMinOccurs);
142
                String sMaxOccurs = null;
143
                if (maxOccurs == IXSElementDeclaration.MAX_OCCURS_UNBOUNDED){
144
                        sMaxOccurs = SchemaTags.UNBOUNDED;
145
                }else{
146
                        sMaxOccurs = String.valueOf(maxOccurs);
147
                }                
148
                element.setAttribute(SchemaTags.MAX_OCCURS, sMaxOccurs);
149
                return element;
150
        }
151
        
152
        /**
153
         * Creates a new ComplxType element
154
         * @param schema
155
         * Schema that will be used to create the element
156
         * @param name
157
         * ComplexType name
158
         * @param type
159
         * See the IXSComplexType for possible values
160
         * @param contentType
161
         * A complex content or a simple content
162
         * See the IXSContentType for possible values
163
         * @param contentTypeRestriction
164
         * A extension or a restriction
165
         * @return
166
         */        
167
        public Element createComplexType(IXSSchema schema, String name, String type, String contentType, String conteTypeRestriction){
168
                Element eComplexType = schema.getDocument().createElement(addXSQname(SchemaTags.COMPLEX_TYPE));
169
                if (name != null){
170
                        eComplexType.setAttribute(SchemaTags.NAME, name);
171
                }                
172
                if (contentType.compareTo(IXSContentType.WITOUT_CONTENT) == 0){
173
                        Element eSimpleContent = schema.getDocument().createElement(addXSQname(type));
174
                        eComplexType.appendChild(eSimpleContent);
175
                }else{
176
                        Element eContentType = schema.getDocument().createElement(addXSQname(contentType));
177
                        eComplexType.appendChild(eContentType);
178
                        Element eRestriction = schema.getDocument().createElement(addXSQname(conteTypeRestriction));
179
                        eContentType.appendChild(eRestriction);
180
                        Element eSimpleContent = schema.getDocument().createElement(addXSQname(type));
181
                        eRestriction.appendChild(eSimpleContent);
182
                }
183
                return eComplexType;
184
        }        
185
        
186
        /**
187
         * Adds an element to a complex type. It only will works
188
         * if the type doesn't has a simple content or a complex
189
         * content 
190
         * @param schema
191
         * XML schema
192
         * @param eElement
193
         * The XML schema element
194
         * @param eComplexType
195
         * The XML schema complex type
196
         */
197
        public boolean addElementToComplexType(IXSSchema schema, Element eElement, Element eComplexType){
198
                //If is a ComplexContent..
199
                Element eComplexContent = SchemaUtils.searchChildByTagName(eComplexType, SchemaTags.COMPLEX_CONTENT);
200
                if (eComplexContent != null){
201
                        Element eContentElement = findElementToWrite(schema, eComplexContent, getContentElements());
202
                        if (eContentElement != null){
203
                                //Restriction or extension
204
                                Element eGroup = findElementToWrite(schema, eContentElement, getGroupElements());
205
                                if (eGroup != null){
206
                                        eGroup.appendChild(eElement);
207
                                        return true;
208
                                }
209
                        }                        
210
                }
211
                //If is a simpleContent..
212
                Element eSimpleContent = SchemaUtils.searchChildByTagName(eComplexType, SchemaTags.SIMPLE_CONTENT);
213
                if (eSimpleContent != null){
214
                        Element eContentElement = findElementToWrite(schema, eSimpleContent, getContentElements());
215
                        if (eContentElement != null){
216
                                eContentElement.appendChild(eElement);
217
                        }
218
                        return true;
219
                }
220
                //Else will be a group, a sequence, a choice or a all node.
221
                Element eGroup = findElementToWrite(schema, eComplexType, getGroupElements());
222
                if (eGroup != null){
223
                        eGroup.appendChild(eElement);
224
                         return true;
225
                }
226
                return false;
227
        }        
228
                
229
        /**
230
         * Find the root element to write the sub elements. 
231
         * @param schema
232
         * XML Schema
233
         * @param root
234
         * Root element
235
         * @param elements
236
         * Elements to search
237
         * @return
238
         */
239
        private Element findElementToWrite(IXSSchema schema, 
240
                        Element root,
241
                        String[] elements){        
242
                for (int i=0 ; i<elements.length ; i++){
243
                        Element element = SchemaUtils.searchChildByTagName(root, elements[i]);
244
                        if (element != null){
245
                                return element;
246
                        }
247
                }                
248
                return null;
249
        }        
250

    
251
        /**
252
         * @return the group elements
253
         */
254
        private String[] getGroupElements(){
255
                String[] groupElements = new String[4];
256
                groupElements[0] = SchemaTags.SEQUENCE;
257
                groupElements[1] = SchemaTags.CHOICE;
258
                groupElements[2] = SchemaTags.ALL;
259
                groupElements[3] = SchemaTags.GROUP;
260
                return groupElements;
261
        }
262
        
263

    
264
        /**
265
         * @return the content elements
266
         */
267
        private String[] getContentElements(){
268
                String[] groupElements = new String[2];
269
                groupElements[0] = SchemaTags.RESTRICTION;
270
                groupElements[1] = SchemaTags.EXTENSION;
271
                return groupElements;
272
        }
273
        
274
        /**
275
         * Uset to create the XML schema tags. It adds the
276
         * namespace prefix (xs or xsd)
277
         * @param tagName
278
         * @return
279
         */
280
        private String addXSQname(String tagName){
281
                return xsNamespacePrefix + ":" + tagName;
282
        }
283

    
284
        /**
285
         * @param xsNamespacePrefix the xsNamespacePrefix to set
286
         */
287
        public void setXsNamespacePrefix(String xsNamespacePrefix) {
288
                this.xsNamespacePrefix = xsNamespacePrefix;
289
        }
290
}