Statistics
| Revision:

root / trunk / libraries / libGPE-GML / src-test / org / gvsig / gpe / xml / XSOMSchemaParserTest.java @ 11159

History | View | Annotate | Download (5.52 KB)

1
package org.gvsig.gpe.xml;
2

    
3
import java.io.File;
4
import java.io.IOException;
5
import java.util.Iterator;
6

    
7
import org.xml.sax.ErrorHandler;
8
import org.xml.sax.SAXException;
9

    
10
import com.sun.xml.xsom.XSElementDecl;
11
import com.sun.xml.xsom.XSSchema;
12
import com.sun.xml.xsom.XSSchemaSet;
13
import com.sun.xml.xsom.XSType;
14
import com.sun.xml.xsom.parser.AnnotationParser;
15
import com.sun.xml.xsom.parser.AnnotationParserFactory;
16
import com.sun.xml.xsom.parser.XSOMParser;
17
import com.sun.xml.xsom.util.DomAnnotationParserFactory;
18

    
19
import junit.framework.TestCase;
20

    
21
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
22
 *
23
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
24
 *
25
 * This program is free software; you can redistribute it and/or
26
 * modify it under the terms of the GNU General Public License
27
 * as published by the Free Software Foundation; either version 2
28
 * of the License, or (at your option) any later version.
29
 *
30
 * This program is distributed in the hope that it will be useful,
31
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33
 * GNU General Public License for more details.
34
 *
35
 * You should have received a copy of the GNU General Public License
36
 * along with this program; if not, write to the Free Software
37
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
38
 *
39
 * For more information, contact:
40
 *
41
 *  Generalitat Valenciana
42
 *   Conselleria d'Infraestructures i Transport
43
 *   Av. Blasco Ib??ez, 50
44
 *   46010 VALENCIA
45
 *   SPAIN
46
 *
47
 *      +34 963862235
48
 *   gvsig@gva.es
49
 *      www.gvsig.gva.es
50
 *
51
 *    or
52
 *
53
 *   IVER T.I. S.A
54
 *   Salamanca 50
55
 *   46005 Valencia
56
 *   Spain
57
 *
58
 *   +34 963163400
59
 *   dac@iver.es
60
 */
61
/* CVS MESSAGES:
62
 *
63
 * $Id: XSOMSchemaParserTest.java 11159 2007-04-12 10:24:12Z jorpiell $
64
 * $Log$
65
 * Revision 1.2  2007-04-12 10:24:12  jorpiell
66
 * Add the GML and schema tests
67
 *
68
 * Revision 1.1  2007/04/11 11:16:42  jorpiell
69
 * A?adido el test del XSOM
70
 *
71
 *
72
 */
73
/**
74
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
75
 */
76
public class XSOMSchemaParserTest extends TestCase {
77
        protected void setUp() throws SAXException, IOException{
78
                XSOMParser parser = new XSOMParser();
79
                parser.parse(new File("testdata/schemas/2_1_2/feature.xsd"));
80
        }
81

    
82
        public void testParseLocalidadSchema() throws SAXException, IOException {
83
                long t1 = System.currentTimeMillis();
84
                XSOMParser parser = new XSOMParser();
85
                parser.parse(new File("testdata/schemas/localidad.xsd"));
86
                long t2 = System.currentTimeMillis();
87
                System.out.println(t2-t1);
88
                print(parser);
89
                assertElement(parser,"Localidad","localidad_Type");
90
                printElement(parser,"Localidad");
91
        }
92
        
93
        public void testParseCitySchema() throws SAXException, IOException {
94
                long t1 = System.currentTimeMillis();
95
                XSOMParser parser = new XSOMParser();
96
                AnnotationParser anParser = new DomAnnotationParserFactory().create();
97
                //parser.setAnnotationParser(.getClass());
98
                //parser.setErrorHandler(ErrorHandler)
99
                parser.parse(new File("testdata/schemas/CityGML.xsd"));
100
                long t2 = System.currentTimeMillis();
101
                System.out.println(t2-t1);                        
102
        }
103
        
104
        
105
        public void printElement(XSOMParser parser,String elementName) throws SAXException{
106
                Iterator itr = parser.getResult().iterateSchema();
107
                while( itr.hasNext() ) {
108
                        XSSchema s = (XSSchema)itr.next();                  
109
                        XSElementDecl element = s.getElementDecl(elementName);
110
                        if (element != null){
111
                                System.out.println(element.getName());                                
112
                        }
113
                }        
114
        }
115
        
116
        public void print(XSOMParser parser) throws SAXException{
117
                Iterator itr = parser.getResult().iterateSchema();
118
                while( itr.hasNext() ) {
119
                          XSSchema s = (XSSchema)itr.next();
120
                          System.out.println("Target namespace: "+s.getTargetNamespace());
121
                          
122
                          Iterator jtr = s.iterateElementDecls();
123
                          while( jtr.hasNext() ) {
124
                            XSElementDecl e = (XSElementDecl)jtr.next();
125
                            
126
                            System.out.print("ELEMENT:" + e.getName() );
127
                           if (e.getAnnotation() != null){
128
                                   System.out.print(" ANOTATION: " + e.getAnnotation().getAnnotation());
129
                           }
130
                           XSType type = e.getType();
131
                           if (type.getAnnotation() != null){
132
                                   System.out.print(" ANOTATION TYPE: " + type.getAnnotation().getLocator().toString());
133
                           }
134
                            if( e.isAbstract() ){
135
                              System.out.print(" (abstract)");
136
                            }
137
                            System.out.println();
138
                          }
139
                        } parser.getResult().iterateSchema();
140
                while( itr.hasNext() ) {
141
                  XSSchema s = (XSSchema)itr.next();
142
                  System.out.println("Target namespace: "+s.getTargetNamespace());
143
                  
144
                  Iterator jtr = s.iterateElementDecls();
145
                  while( jtr.hasNext() ) {
146
                    XSElementDecl e = (XSElementDecl)jtr.next();
147
                    
148
                    System.out.print( e.getName() );
149
                    if( e.isAbstract() )
150
                      System.out.print(" (abstract)");
151
                    System.out.println();
152
                  }
153
                }
154
        }
155
        
156
        /**
157
         * Gets a XSElement and try its type
158
         * @param elementName
159
         * XSElement name
160
         * @param elementType
161
         * XSElement type
162
         * @throws SAXException
163
         */
164
        private void assertElement(XSOMParser parser,String elementName,String elementType) throws SAXException{
165
                XSElementDecl element = getElement(parser,elementName);
166
                assertEquals(element == null,false);
167
                assertEquals(element.getType().getName(),elementType);
168
        }
169
        
170
        /**
171
         * Gets one XSElement 
172
         * @param elementName
173
         * XSElement name
174
         * @return
175
         * @throws SAXException
176
         */
177
        private XSElementDecl getElement(XSOMParser parser,String elementName) throws SAXException{
178
                Iterator itr = parser.getResult().iterateSchema();
179
                while( itr.hasNext() ) {
180
                        XSSchema s = (XSSchema)itr.next();                  
181
                        XSElementDecl element = s.getElementDecl(elementName);
182
                        if (element != null){
183
                                return element;
184
                        }
185
                }        
186
                return null;
187
        }
188
}