Statistics
| Revision:

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

History | View | Annotate | Download (7.74 KB)

1
package org.gvsig.xmlschema.utils;
2

    
3
import org.gvsig.xmlschema.som.IXSContentType;
4
import org.gvsig.xmlschema.som.IXSSchema;
5
import org.w3c.dom.Element;
6

    
7
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
8
 *
9
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
10
 *
11
 * This program is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU General Public License
13
 * as published by the Free Software Foundation; either version 2
14
 * of the License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
24
 *
25
 * For more information, contact:
26
 *
27
 *  Generalitat Valenciana
28
 *   Conselleria d'Infraestructures i Transport
29
 *   Av. Blasco Ib??ez, 50
30
 *   46010 VALENCIA
31
 *   SPAIN
32
 *
33
 *      +34 963862235
34
 *   gvsig@gva.es
35
 *      www.gvsig.gva.es
36
 *
37
 *    or
38
 *
39
 *   IVER T.I. S.A
40
 *   Salamanca 50
41
 *   46005 Valencia
42
 *   Spain
43
 *
44
 *   +34 963163400
45
 *   dac@iver.es
46
 */
47
/* CVS MESSAGES:
48
 *
49
 * $Id: DOMObjectsFactory.java 12175 2007-06-14 16:15:05Z jorpiell $
50
 * $Log$
51
 * Revision 1.1  2007-06-14 16:15:03  jorpiell
52
 * builds to create the jars generated and add the schema code to the libGPEProject
53
 *
54
 * Revision 1.1  2007/06/14 13:50:07  jorpiell
55
 * The schema jar name has been changed
56
 *
57
 * Revision 1.3  2007/06/08 11:35:16  jorpiell
58
 * IXSSchema interface updated
59
 *
60
 * Revision 1.2  2007/06/08 06:55:05  jorpiell
61
 * Fixed some bugs
62
 *
63
 * Revision 1.1  2007/06/07 14:54:13  jorpiell
64
 * Add the schema support
65
 *
66
 * Revision 1.3  2007/05/30 12:53:33  jorpiell
67
 * Not used libraries deleted
68
 *
69
 * Revision 1.2  2007/05/30 12:50:53  jorpiell
70
 * Refactoring of some duplicated methods
71
 *
72
 * Revision 1.1  2007/05/30 12:25:48  jorpiell
73
 * Add the element collection
74
 *
75
 *
76
 */
77
/**
78
 * This class contains methods to create DOM elements.
79
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
80
 */
81
public class DOMObjectsFactory {
82
        private static DOMObjectsFactory instance = null;
83
        private String xsNamespacePrefix = null;
84
        
85
        /**
86
         * This method cretaes the singleton instance
87
         *
88
         */
89
        private synchronized static void createInstance() {
90
                if (instance == null) { 
91
                        instance = new DOMObjectsFactory();
92
                }
93
        }
94

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

    
235
        /**
236
         * @return the group elements
237
         */
238
        private String[] getGroupElements(){
239
                String[] groupElements = new String[4];
240
                groupElements[0] = SchemaTags.SEQUENCE;
241
                groupElements[1] = SchemaTags.CHOICE;
242
                groupElements[2] = SchemaTags.ALL;
243
                groupElements[3] = SchemaTags.GROUP;
244
                return groupElements;
245
        }
246
        
247

    
248
        /**
249
         * @return the content elements
250
         */
251
        private String[] getContentElements(){
252
                String[] groupElements = new String[2];
253
                groupElements[0] = SchemaTags.RESTRICTION;
254
                groupElements[1] = SchemaTags.EXTENSION;
255
                return groupElements;
256
        }
257
        
258
        /**
259
         * Uset to create the XML schema tags. It adds the
260
         * namespace prefix (xs or xsd)
261
         * @param tagName
262
         * @return
263
         */
264
        private String addXSQname(String tagName){
265
                return xsNamespacePrefix + ":" + tagName;
266
        }
267

    
268
        /**
269
         * @param xsNamespacePrefix the xsNamespacePrefix to set
270
         */
271
        public void setXsNamespacePrefix(String xsNamespacePrefix) {
272
                this.xsNamespacePrefix = xsNamespacePrefix;
273
        }
274
}