Statistics
| Revision:

root / org.gvsig.xmlschema / library / trunk / org.gvsig.xmlschema / org.gvsig.xmlschema.lib / org.gvsig.xmlschema.lib.spi / src / test / java / org / gvsig / xmlschema / lib / spi / reader / SchemaReaderBaseTest.java @ 257

History | View | Annotate | Download (9.66 KB)

1
package org.gvsig.xmlschema.lib.spi.reader;
2

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

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

    
26
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
27
 *
28
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
29
 *
30
 * This program is free software; you can redistribute it and/or
31
 * modify it under the terms of the GNU General Public License
32
 * as published by the Free Software Foundation; either version 2
33
 * of the License, or (at your option) any later version.
34
 *
35
 * This program is distributed in the hope that it will be useful,
36
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38
 * GNU General Public License for more details.
39
 *
40
 * You should have received a copy of the GNU General Public License
41
 * along with this program; if not, write to the Free Software
42
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
43
 *
44
 * For more information, contact:
45
 *
46
 *  Generalitat Valenciana
47
 *   Conselleria d'Infraestructures i Transport
48
 *   Av. Blasco Ib??ez, 50
49
 *   46010 VALENCIA
50
 *   SPAIN
51
 *
52
 *      +34 963862235
53
 *   gvsig@gva.es
54
 *      www.gvsig.gva.es
55
 *
56
 *    or
57
 *
58
 *   IVER T.I. S.A
59
 *   Salamanca 50
60
 *   46005 Valencia
61
 *   Spain
62
 *
63
 *   +34 963163400
64
 *   dac@iver.es
65
 */
66
/* CVS MESSAGES:
67
 *
68
 * $Id: SchemaReaderBaseTest.java 161 2007-06-28 13:05:27Z jorpiell $
69
 * $Log$
70
 * Revision 1.3  2007/06/28 13:04:33  jorpiell
71
 * The Qname has been updated to the 1.5 JVM machine. The schema validation is made in the GPEWriterHandlerImplementor class
72
 *
73
 * Revision 1.2  2007/06/22 12:21:18  jorpiell
74
 * The typeNotFoundException has been deleted. It never was thrown
75
 *
76
 * Revision 1.1  2007/06/14 16:15:03  jorpiell
77
 * builds to create the jars generated and add the schema code to the libGPEProject
78
 *
79
 * Revision 1.1  2007/06/14 13:50:07  jorpiell
80
 * The schema jar name has been changed
81
 *
82
 * Revision 1.3  2007/06/08 11:35:16  jorpiell
83
 * IXSSchema interface updated
84
 *
85
 * Revision 1.2  2007/06/08 07:31:20  jorpiell
86
 * Add the euroRoadS test
87
 *
88
 * Revision 1.1  2007/06/08 06:55:05  jorpiell
89
 * Fixed some bugs
90
 *
91
 * Revision 1.4  2007/06/07 14:54:13  jorpiell
92
 * Add the schema support
93
 *
94
 * Revision 1.3  2007/05/30 12:25:48  jorpiell
95
 * Add the element collection
96
 *
97
 * Revision 1.2  2007/05/28 12:38:03  jorpiell
98
 * Some bugs fixed
99
 *
100
 * Revision 1.1  2007/05/25 11:55:00  jorpiell
101
 * First update
102
 *
103
 *
104
 */
105
/**
106
 * This class must be implemented by all the xml Schema 
107
 * reading tests.
108
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
109
 */
110
public abstract class SchemaReaderBaseTest extends XMLSchemaBaseTest {
111
    private IXSSchema schema = null;
112

    
113
    public void testParse() throws SchemaCreationException, FileNotFoundException{
114
        File file = new File(SchemaReaderBaseTest.class.getClassLoader().getResource(getResourceFileName()).getFile());       
115
        
116
        assertNotNull(file);
117
        assertTrue(file.exists());
118
        schema = XMLSchemaLocator.getXMLSchemaManager().parse(getProviderName(), new FileInputStream(file));
119
        printSchema();
120
        makeAsserts();
121
    }
122

    
123
    public abstract String getProviderName();
124

    
125
    /**
126
     * Gets the XML schema file to open
127
     * @return
128
     */
129
    public abstract String getResourceFileName();
130

    
131
    /**
132
     * This method must be used by the subclasses
133
     * to make the comparations. 
134
     */
135
    public abstract void makeAsserts() ;
136

    
137
    /**
138
     * @return the schema
139
     */
140
    public IXSSchema getSchema() {
141
        return schema;
142
    }
143

    
144
    /**
145
     * Print the schema elements by console
146
     * @
147
     */
148
    private void printSchema() {
149
        Iterator it = getSchema().getElementDeclarations().iterator();
150
        while (it.hasNext()){
151
            IXSElementDeclaration element = (IXSElementDeclaration)it.next();
152
            printElement(element, "");                        
153
        }
154
    }                
155

    
156
    /**
157
     * Print one element
158
     * @param element
159
     * xml schema element to print
160
     * @param tab
161
     * Tabs to write on the left part of each line
162
     * @
163
     */
164
    private void printElement(IXSElementDeclaration element, String tab) {
165
        IXSTypeDefinition type = element.getTypeDefinition();
166
        System.out.print(tab + "ELEMENT: ");
167
        if (element.getQName() != null){
168
            System.out.print(element.getQName().getLocalPart());
169
        }else{
170
            System.out.print("NULL");
171
        }
172
       
173
        System.out.print(", TYPE: ");
174
        if (type == null){
175
            System.out.print("NULL");
176
            System.out.print(", TYPE NAME: " + element.getTypeName());
177
            System.out.print("\n");
178
        }else{
179
            System.out.print(type.getQName().getLocalPart());
180
            System.out.print("\n");
181
            printType(type,tab + "\t");
182
        }                
183
    }
184

    
185
    private void printType(IXSTypeDefinition type, String tab) {
186
        if (type instanceof IXSSimpleTypeDefinition){
187
            System.out.print(tab + "SIMPLE TYPE");
188
            System.out.print("\n");
189
        }else if (type instanceof IXSComplexTypeDefinition){ 
190
            System.out.print(tab + "COMPLEX TYPE");
191
            System.out.print("\n");
192
            printComplexType((IXSComplexTypeDefinition)type,tab + "\t");                        
193
        }
194
    }
195

    
196
    private void printComplexType(IXSComplexTypeDefinition complexType, String tab) {
197
        IXSContentType contentType = complexType.getContentType();
198
        if (contentType != null){
199
            printContentType(contentType,tab);
200
        }
201
        IXSGroup group = complexType.getGroup();
202
        if (group != null){
203
            printGroup(group,tab);
204
        }
205
    }
206

    
207
    private void printContentType(IXSContentType contentType, String tab)  {
208
        if (contentType instanceof IXSSimpleContent){
209
            System.out.println(tab + "SIMPLE CONTENT");
210
            System.out.print("\n");
211
            printSimpleContent((IXSSimpleContent)contentType, tab + "\t");
212
        }else if (contentType instanceof IXSComplexContent){
213
            System.out.print(tab + "COMPLEX CONTENT");
214
            System.out.print("\n");
215
            printComplexContent((IXSComplexContent)contentType, tab + "\t");
216
        }else if (contentType instanceof IXSGroup){
217
            printGroup((IXSGroup)contentType, tab + "\t");
218
        }                
219
    }
220

    
221
    private void printSimpleContent(IXSSimpleContent simpleContent, String tab) {
222
        IXSRestriction restriction = simpleContent.getRestriction();
223
        if (restriction != null){
224
            printRestriction(restriction, tab  + "\t");
225
        }
226
        IXSExtension extension = simpleContent.getExtension();
227
        if (extension != null){
228
            printExtension(extension, tab  + "\t");
229
        }
230
    }
231

    
232
    private void printComplexContent(IXSComplexContent complexContent, String tab) {
233
        IXSRestriction restriction = complexContent.getRestriction();
234
        if (restriction != null){
235
            printRestriction(restriction, tab);
236
        }
237
        IXSExtension extension = complexContent.getExtension();
238
        if (extension != null){
239
            printExtension(extension, tab);
240
        }
241
    }
242

    
243
    private void printExtension(IXSExtension extension, String tab)  {
244
        System.out.print(tab + "EXTENSION");
245
        System.out.print("\n");
246
        IXSGroup group = extension.getGroup();
247
        if (group != null){
248
            printGroup(group,tab + "\t");
249
        }
250
    }
251

    
252
    private void printRestriction(IXSRestriction restriction, String tab){
253
        System.out.print(tab + "RESTRICTION");
254
        System.out.print("\n");
255
        IXSGroup group = restriction.getGroup();
256
        if (group != null){
257
            printGroup(group,tab + "\t");
258
        }
259
    }
260

    
261
    private void printGroup(IXSGroup group, String tab)  {
262
        if (group instanceof IXSChoice){                        
263
            System.out.print(tab + "CHOICE");        
264
        }else if (group instanceof IXSSequence){
265
            System.out.print(tab + "SEQUENCE");        
266
        }else if (group instanceof IXSAll){
267
            System.out.print(tab + "ALL");        
268
        }else {
269
            System.out.print(tab + "GROUP");        
270
        }
271
        System.out.print("\n");
272
        Iterator it = group.getItems().iterator();
273
        while (it.hasNext()){
274
            Object item = it.next();
275
            if (item instanceof IXSElementDeclaration){
276
                printElement((IXSElementDeclaration)item, tab + "\t");                                
277
            }else {
278
                printGroup((IXSGroup)item, tab);
279
            }                                
280
        }
281
    }
282

    
283
    //        private void printChoice(IXSChoice choice, String tab){
284
    //                System.out.print(tab + "CHOICE");        
285
    //                System.out.print("\n");
286
    //                Iterator it = choice.getItems().iterator();
287
    //                while (it.hasNext()){
288
    //                        IXSElementDeclaration element = (IXSElementDeclaration)it.next();
289
    //                        printElement(element, "");
290
    //                        System.out.print("\n");
291
    //                }
292
    //        }        
293
}