Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_1_RELEASE / libraries / libRemoteServices / src / org / gvsig / remoteClient / gml / factories / XMLElementsFactory.java @ 9531

History | View | Annotate | Download (4.77 KB)

1
package org.gvsig.remoteClient.gml.factories;
2

    
3
import java.io.IOException;
4
import java.util.Hashtable;
5
import java.util.Iterator;
6
import java.util.Map;
7
import java.util.Set;
8
import java.util.Vector;
9

    
10
import org.gvsig.remoteClient.gml.schemas.IXMLType;
11
import org.gvsig.remoteClient.gml.schemas.XMLComplexType;
12
import org.gvsig.remoteClient.gml.schemas.XMLElement;
13
import org.gvsig.remoteClient.gml.schemas.XMLSchemaParser;
14
import org.xmlpull.v1.XmlPullParserException;
15

    
16
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
17
 *
18
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
19
 *
20
 * This program is free software; you can redistribute it and/or
21
 * modify it under the terms of the GNU General Public License
22
 * as published by the Free Software Foundation; either version 2
23
 * of the License, or (at your option) any later version.
24
 *
25
 * This program is distributed in the hope that it will be useful,
26
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28
 * GNU General Public License for more details.
29
 *
30
 * You should have received a copy of the GNU General Public License
31
 * along with this program; if not, write to the Free Software
32
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
33
 *
34
 * For more information, contact:
35
 *
36
 *  Generalitat Valenciana
37
 *   Conselleria d'Infraestructures i Transport
38
 *   Av. Blasco Ib??ez, 50
39
 *   46010 VALENCIA
40
 *   SPAIN
41
 *
42
 *      +34 963862235
43
 *   gvsig@gva.es
44
 *      www.gvsig.gva.es
45
 *
46
 *    or
47
 *
48
 *   IVER T.I. S.A
49
 *   Salamanca 50
50
 *   46005 Valencia
51
 *   Spain
52
 *
53
 *   +34 963163400
54
 *   dac@iver.es
55
 */
56
/* CVS MESSAGES:
57
 *
58
 * $Id: XMLElementsFactory.java 9531 2007-01-03 17:07:37Z  $
59
 * $Log$
60
 * Revision 1.1.2.4  2006-11-20 08:58:17  jorpiell
61
 * Una excepci?n no capturada
62
 *
63
 * Revision 1.1.2.3  2006/11/17 11:38:03  ppiqueras
64
 * Corregidos bugs y a?adida nueva funcionalidad. Del HEAD.
65
 *
66
 * Revision 1.3  2006/11/16 13:27:12  jorpiell
67
 * El getElement les asocia al los hijos el padre correspondiente
68
 *
69
 * Revision 1.2  2006/10/10 12:52:28  jorpiell
70
 * Soporte para features complejas.
71
 *
72
 * Revision 1.1  2006/08/10 12:00:49  jorpiell
73
 * Primer commit del driver de Gml
74
 *
75
 *
76
 */
77
/**
78
 * The XML "element" is an XML tag of a XSD schema and represent
79
 * a simple "element" or "object". One element has its name and 
80
 * the data type that contains (and the data)
81
 * 
82
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
83
 */
84
public class XMLElementsFactory {
85
        private static Hashtable elements = new Hashtable();
86
        
87
        public static XMLElement getElement(String name){
88
                if (name == null){
89
                        return null;
90
                }
91
                XMLElement element = (XMLElement)elements.get(name.toUpperCase());
92
                setParentNode(element);
93
                return element;
94
        }
95
        
96
        private static void setParentNode(XMLElement element){
97
                if (element.getEntityType() != null){
98
                        if (element.getEntityType().getType() == IXMLType.COMPLEX){
99
                                Vector attributes = ((XMLComplexType)element.getEntityType()).getAttributes();
100
                                for (int i=0 ; i<attributes.size() ; i++){
101
                                        XMLElement child = (XMLElement)attributes.get(i);
102
                                        child.setParentElement(element);
103
                                        setParentNode(child);
104
                                }
105
                        }        
106
                }
107
        }
108
        
109
        /**
110
         * It parses the element and register it
111
         * @param schema
112
         * Schema parser that contains a "element"
113
         * @return
114
         */
115
        public static XMLElement addType(XMLSchemaParser schema){
116
                XMLElement element;
117
                try {
118
                        element = new XMLElement(schema);
119
                        if (element.getName() != null){
120
                                elements.put(element.getName().toUpperCase(),element);
121
                        }
122
                        return element;
123
                } catch (XmlPullParserException e) {
124
                        // TODO Auto-generated catch block
125
                        e.printStackTrace();
126
                } catch (IOException e) {
127
                        // TODO Auto-generated catch block
128
                        e.printStackTrace();
129
                }
130
                return null;
131
        }
132
        
133
        /**
134
         * Just for degug. It prints all the registred components.
135
         */
136
        public static void printEntities(){
137
                System.out.println("*** ELEMENTOS ***");
138
                Object[] keys = elements.keySet().toArray();
139
                for (int i=0 ; i<elements.size() ; i++){
140
                        XMLElement entity = (XMLElement)elements.get(keys[i]);
141
                        printEntity(entity,0);                        
142
                }
143
        }
144
        
145
        public static void printEntity(XMLElement entity,int level){
146
                String tab = "";
147
                for (int i=0 ; i<level ; i++){
148
                        tab = tab + "\t";
149
                }
150
                System.out.print(tab + "NAME: " + entity.getName());
151
                if (entity.getEntityType() != null){
152
                        System.out.print(" TYPE: " + entity.getEntityType().getName() + "\n");
153
                        if (entity.getEntityType().getType() == IXMLType.COMPLEX){
154
                                Map children = ((XMLComplexType)entity.getEntityType()).getSubtypes();
155
                                Set childrenKeys = children.keySet();
156
                                Iterator it = childrenKeys.iterator();
157
                                level++;
158
                                while(it.hasNext()){
159
                                        String child = (String)it.next();
160
                                        XMLElement eChild = (XMLElement)children.get(child);
161
                                        printEntity(eChild,level);        
162
                                }
163
                        }
164
                }else{
165
                        System.out.print(" TYPE: ERROR \n");
166
                }
167
                
168
        }
169
        
170
}