Statistics
| Revision:

root / trunk / libraries / libGPE / src-test / org / gvsig / xmlschema / reader / SchemaReaderBaseTest.java @ 12175

History | View | Annotate | Download (8.94 KB)

1
package org.gvsig.xmlschema.reader;
2

    
3
import java.io.FileInputStream;
4
import java.io.FileNotFoundException;
5
import java.util.Iterator;
6

    
7
import junit.framework.TestCase;
8

    
9
import org.gvsig.xmlschema.exceptions.SchemaCreationException;
10
import org.gvsig.xmlschema.exceptions.TypeNotFoundException;
11
import org.gvsig.xmlschema.som.IXSAll;
12
import org.gvsig.xmlschema.som.IXSChoice;
13
import org.gvsig.xmlschema.som.IXSComplexContent;
14
import org.gvsig.xmlschema.som.IXSComplexTypeDefinition;
15
import org.gvsig.xmlschema.som.IXSContentType;
16
import org.gvsig.xmlschema.som.IXSElementDeclaration;
17
import org.gvsig.xmlschema.som.IXSExtension;
18
import org.gvsig.xmlschema.som.IXSGroup;
19
import org.gvsig.xmlschema.som.IXSRestriction;
20
import org.gvsig.xmlschema.som.IXSSchema;
21
import org.gvsig.xmlschema.som.IXSSequence;
22
import org.gvsig.xmlschema.som.IXSSimpleContent;
23
import org.gvsig.xmlschema.som.IXSSimpleTypeDefinition;
24
import org.gvsig.xmlschema.som.IXSTypeDefinition;
25
import org.gvsig.xmlschema.utils.SchemaDocumentBuilder;
26

    
27
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
28
 *
29
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
30
 *
31
 * This program is free software; you can redistribute it and/or
32
 * modify it under the terms of the GNU General Public License
33
 * as published by the Free Software Foundation; either version 2
34
 * of the License, or (at your option) any later version.
35
 *
36
 * This program is distributed in the hope that it will be useful,
37
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39
 * GNU General Public License for more details.
40
 *
41
 * You should have received a copy of the GNU General Public License
42
 * along with this program; if not, write to the Free Software
43
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
44
 *
45
 * For more information, contact:
46
 *
47
 *  Generalitat Valenciana
48
 *   Conselleria d'Infraestructures i Transport
49
 *   Av. Blasco Ib??ez, 50
50
 *   46010 VALENCIA
51
 *   SPAIN
52
 *
53
 *      +34 963862235
54
 *   gvsig@gva.es
55
 *      www.gvsig.gva.es
56
 *
57
 *    or
58
 *
59
 *   IVER T.I. S.A
60
 *   Salamanca 50
61
 *   46005 Valencia
62
 *   Spain
63
 *
64
 *   +34 963163400
65
 *   dac@iver.es
66
 */
67
/* CVS MESSAGES:
68
 *
69
 * $Id: SchemaReaderBaseTest.java 12175 2007-06-14 16:15:05Z jorpiell $
70
 * $Log$
71
 * Revision 1.1  2007-06-14 16:15:03  jorpiell
72
 * builds to create the jars generated and add the schema code to the libGPEProject
73
 *
74
 * Revision 1.1  2007/06/14 13:50:07  jorpiell
75
 * The schema jar name has been changed
76
 *
77
 * Revision 1.3  2007/06/08 11:35:16  jorpiell
78
 * IXSSchema interface updated
79
 *
80
 * Revision 1.2  2007/06/08 07:31:20  jorpiell
81
 * Add the euroRoadS test
82
 *
83
 * Revision 1.1  2007/06/08 06:55:05  jorpiell
84
 * Fixed some bugs
85
 *
86
 * Revision 1.4  2007/06/07 14:54:13  jorpiell
87
 * Add the schema support
88
 *
89
 * Revision 1.3  2007/05/30 12:25:48  jorpiell
90
 * Add the element collection
91
 *
92
 * Revision 1.2  2007/05/28 12:38:03  jorpiell
93
 * Some bugs fixed
94
 *
95
 * Revision 1.1  2007/05/25 11:55:00  jorpiell
96
 * First update
97
 *
98
 *
99
 */
100
/**
101
 * This class must be implemented by all the xml Schema 
102
 * reading tests.
103
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
104
 */
105
public abstract class SchemaReaderBaseTest extends TestCase {
106
        private SchemaDocumentBuilder schemaBuilder = null;
107
        private IXSSchema schema = null;
108
        
109
        public void setUp(){
110
                schemaBuilder = SchemaDocumentBuilder.getInstance();
111
        }
112
        
113
        public void testParse() throws SchemaCreationException, FileNotFoundException, TypeNotFoundException{
114
                schema = getSchemaBuilder().parse(new FileInputStream(getFile()));
115
                makeAsserts();
116
                printSchema();
117
        }
118
        
119
        /**
120
         * Gets the XML schema file to open
121
         * @return
122
         */
123
        public abstract String getFile();
124
        
125
        /**
126
         * This method must be used by the subclasses
127
         * to make the comparations. 
128
         * @throws TypeNotFoundException 
129
         */
130
        public abstract void makeAsserts() throws TypeNotFoundException;
131
        
132
        /**
133
         * @return the schema
134
         */
135
        public SchemaDocumentBuilder getSchemaBuilder() {
136
                return schemaBuilder;
137
        }
138

    
139
        /**
140
         * @return the schema
141
         */
142
        public IXSSchema getSchema() {
143
                return schema;
144
        }
145
        
146
        /**
147
         * Print the schema elements by console
148
         * @throws TypeNotFoundException
149
         */
150
        private void printSchema() throws TypeNotFoundException{
151
                Iterator it = getSchema().getElementDeclarations().iterator();
152
                while (it.hasNext()){
153
                        IXSElementDeclaration element = (IXSElementDeclaration)it.next();
154
                        printElement(element, "");                        
155
                }
156
        }                
157

    
158
        /**
159
         * Print one element
160
         * @param element
161
         * xml schema element to print
162
         * @param tab
163
         * Tabs to write on the left part of each line
164
         * @throws TypeNotFoundException
165
         */
166
        private void printElement(IXSElementDeclaration element, String tab) throws TypeNotFoundException{
167
                IXSTypeDefinition type = element.getTypeDefinition();
168
                System.out.print(tab + "ELEMENT: " + element.getQName().getLocalName());
169
                System.out.print(", TYPE: ");
170
                if (type == null){
171
                        System.out.print("NULL");
172
                        System.out.print(", TYPE NAME: " + element.getTypeName());
173
                        System.out.print("\n");
174
                }else{
175
                        System.out.print(type.getQName().getLocalPart());
176
                        System.out.print("\n");
177
                        printType(type,tab + "\t");
178
                }                
179
        }
180
        
181
        private void printType(IXSTypeDefinition type, String tab) throws TypeNotFoundException{
182
                if (type instanceof IXSSimpleTypeDefinition){
183
                        System.out.print(tab + "SIMPLE TYPE");
184
                        System.out.print("\n");
185
                }else if (type instanceof IXSComplexTypeDefinition){ 
186
                        System.out.print(tab + "COMPLEX TYPE");
187
                        System.out.print("\n");
188
                        printComplexType((IXSComplexTypeDefinition)type,tab + "\t");                        
189
                }
190
        }
191
        
192
        private void printComplexType(IXSComplexTypeDefinition complexType, String tab) throws TypeNotFoundException{
193
                IXSContentType contentType = complexType.getContentType();
194
                if (contentType != null){
195
                        printContentType(contentType,tab);
196
                }
197
                IXSGroup group = complexType.getGroup();
198
                if (group != null){
199
                        printGroup(group,tab);
200
                }
201
        }
202

    
203
        private void printContentType(IXSContentType contentType, String tab) throws TypeNotFoundException {
204
                if (contentType instanceof IXSSimpleContent){
205
                        System.out.println(tab + "SIMPLE CONTENT");
206
                        System.out.print("\n");
207
                        printSimpleContent((IXSSimpleContent)contentType, tab + "\t");
208
                }else if (contentType instanceof IXSComplexContent){
209
                        System.out.print(tab + "COMPLEX CONTENT");
210
                        System.out.print("\n");
211
                        printComplexContent((IXSComplexContent)contentType, tab + "\t");
212
                }else if (contentType instanceof IXSGroup){
213
                        printGroup((IXSGroup)contentType, tab + "\t");
214
                }                
215
        }
216
        
217
        private void printSimpleContent(IXSSimpleContent simpleContent, String tab) throws TypeNotFoundException{
218
                IXSRestriction restriction = simpleContent.getRestriction();
219
                if (restriction != null){
220
                        printRestriction(restriction, tab  + "\t");
221
                }
222
                IXSExtension extension = simpleContent.getExtension();
223
                if (extension != null){
224
                        printExtension(extension, tab  + "\t");
225
                }
226
        }
227
        
228
        private void printComplexContent(IXSComplexContent complexContent, String tab) throws TypeNotFoundException{
229
                IXSRestriction restriction = complexContent.getRestriction();
230
                if (restriction != null){
231
                        printRestriction(restriction, tab);
232
                }
233
                IXSExtension extension = complexContent.getExtension();
234
                if (extension != null){
235
                        printExtension(extension, tab);
236
                }
237
        }
238

    
239
        private void printExtension(IXSExtension extension, String tab) throws TypeNotFoundException {
240
                System.out.print(tab + "EXTENSION");
241
                System.out.print("\n");
242
                Iterator it = extension.getItems().iterator();
243
                while (it.hasNext()){
244
                        Object item = it.next();
245
                        if (item instanceof IXSElementDeclaration){
246
                                printElement((IXSElementDeclaration)item, tab + "\t");
247
                        }else {
248
                                printGroup((IXSGroup)item, tab + "\t");
249
                        }                                
250
                }                
251
        }
252
        
253
        private void printRestriction(IXSRestriction restriction, String tab) throws TypeNotFoundException {
254
                System.out.print(tab + "RESTRICTION");
255
                System.out.print("\n");
256
                Iterator it = restriction.getItems().iterator();
257
                while (it.hasNext()){
258
                        Object item = it.next();
259
                        if (item instanceof IXSElementDeclaration){
260
                                printElement((IXSElementDeclaration)item, tab + "\t");
261
                        }else {
262
                                printGroup((IXSGroup)item, tab + "\t");
263
                        }
264
                }                
265
        }
266
        
267
        private void printGroup(IXSGroup group, String tab) throws TypeNotFoundException {
268
                if (group instanceof IXSChoice){                        
269
                        System.out.print(tab + "CHOICE");        
270
                }else if (group instanceof IXSSequence){
271
                        System.out.print(tab + "SEQUENCE");        
272
                }else if (group instanceof IXSAll){
273
                        System.out.print(tab + "ALL");        
274
                }else {
275
                        System.out.print(tab + "GROUP");        
276
                }
277
                System.out.print("\n");
278
                Iterator it = group.getItems().iterator();
279
                while (it.hasNext()){
280
                        Object item = it.next();
281
                        if (item instanceof IXSElementDeclaration){
282
                                printElement((IXSElementDeclaration)item, tab + "\t");                                
283
                        }else {
284
                                printGroup((IXSGroup)item, tab);
285
                        }                                
286
                }
287
        }
288

    
289
        private void printChoice(IXSChoice choice, String tab) throws TypeNotFoundException {
290
                System.out.print(tab + "CHOICE");        
291
                System.out.print("\n");
292
                Iterator it = choice.getItems().iterator();
293
                while (it.hasNext()){
294
                        IXSElementDeclaration element = (IXSElementDeclaration)it.next();
295
                        printElement(element, "");
296
                        System.out.print("\n");
297
                }
298
        }        
299
}