Statistics
| Revision:

root / trunk / libraries / libGPE / src / org / gvsig / xmlschema / som / impl / XSElementDeclarationImpl.java @ 12439

History | View | Annotate | Download (6.68 KB)

1
package org.gvsig.xmlschema.som.impl;
2

    
3
import java.util.Iterator;
4

    
5
import org.gvsig.xmlschema.som.IXSComplexTypeDefinition;
6
import org.gvsig.xmlschema.som.IXSElementDeclaration;
7
import org.gvsig.xmlschema.som.IXSSchema;
8
import org.gvsig.xmlschema.som.IXSTypeDefinition;
9
import org.gvsig.xmlschema.utils.SchemaCollection;
10
import org.gvsig.xmlschema.utils.SchemaTags;
11
import org.gvsig.xmlschema.utils.SchemaUtils;
12
import org.w3c.dom.Element;
13
import org.w3c.dom.Node;
14
import org.w3c.dom.NodeList;
15

    
16

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

    
96
        /**
97
         * @return The element type
98
         * @throws TypeNotFoundException 
99
         */
100
        public IXSTypeDefinition getTypeDefinition(){
101
                //If is defined in the same node
102
                Iterator it = new SchemaCollection(getSchema(),getElement()).iterator();
103
                it.hasNext();
104
                Object type = it.next();
105
                //If is defined in the same schema
106
                if (type == null){
107
                        type = getSchema().getTypeByName(getQName().getNamespaceURI(), 
108
                                        getElement().getAttribute(SchemaTags.TYPE));
109
                }
110
                if (type != null){
111
                        return (IXSTypeDefinition)type;
112
                }
113
                return null;
114
        }
115

    
116

    
117
        
118
        /*
119
         * (non-Javadoc)
120
         * @see org.gvsig.gpe.schema.som.IXSElementDeclaration#getTypeName()
121
         */
122
        public String getTypeName() {
123
                return getElement().getAttribute(SchemaTags.TYPE);
124
        }
125

    
126
        /*
127
         * (non-Javadoc)
128
         * @see org.gvsig.gpe.schema.som.IXSElementDeclaration#isNillable()
129
         */
130
        public boolean isNillable() {
131
                String nillable = getElement().getAttribute(SchemaTags.NILLABLE);
132
                if (nillable != null){
133
                        if (nillable.compareTo(SchemaTags.FALSE) == 0){
134
                                return false;
135
                        }
136
                }
137
                return true;
138
        }        
139
        
140
        /*
141
         * (non-Javadoc)
142
         * @see org.gvsig.gpe.schema.som.IXSElementDeclaration#getMaxOccurs()
143
         */
144
        public int getMaxOccurs() {
145
                String maxOccurs = getElement().getAttribute(SchemaTags.MAX_OCCURS);
146
                if (maxOccurs != null){
147
                        return Integer.parseInt(maxOccurs);
148
                }
149
                return 0;
150
        }
151

    
152
        /*
153
         * (non-Javadoc)
154
         * @see org.gvsig.gpe.schema.som.IXSElementDeclaration#getMinOccurs()
155
         */
156
        public int getMinOccurs() {
157
                String minOccurs = getElement().getAttribute(SchemaTags.MIN_OCCURS);
158
                if (minOccurs != null){
159
                        return Integer.parseInt(minOccurs);
160
                }
161
                return 0;
162
        }
163

    
164
        public IXSElementDeclaration getSubElementByName(String nodeName) {
165
                if (getTypeDefinition() == null){
166
                        return null;
167
                }
168
                Element element = getTypeDefinition().getElement();
169
                nodeName = nodeName.substring(nodeName.indexOf(":")+1,nodeName.length());
170
                Element newElement = searchNode(element, nodeName);
171
                if (newElement != null){
172
                        XSElementDeclarationImpl childElement =  new XSElementDeclarationImpl(getSchema());
173
                        childElement.setElement(newElement);
174
                        return childElement;
175
                }
176
                return null;        
177
        }
178
        
179
        private Element searchNode(Element element, String nodeName){
180
                NodeList nodeList = element.getChildNodes();
181
                for (int i=0 ; i<nodeList.getLength() ; i++){
182
                        Node node = nodeList.item(i);
183
                                if(node.getNodeType() == Node.ELEMENT_NODE){
184
                                Element childElement = (Element)node;                                
185
                                if (SchemaUtils.matches(node, SchemaTags.XS_NS, SchemaTags.COMPLEX_CONTENT)){
186
                                        return searchNode(childElement, nodeName);
187
                                }else if (SchemaUtils.matches(node, SchemaTags.XS_NS, SchemaTags.EXTENSION)){
188
                                        return searchNode(childElement, nodeName);
189
                                }else if (SchemaUtils.matches(node, SchemaTags.XS_NS, SchemaTags.RESTRICTION)){
190
                                        return searchNode(childElement, nodeName);
191
                                }else if (SchemaUtils.matches(node, SchemaTags.XS_NS, SchemaTags.SEQUENCE)){
192
                                        return searchNode(childElement, nodeName);
193
                                }else if (SchemaUtils.matches(node, SchemaTags.XS_NS, SchemaTags.ALL)){
194
                                        return searchNode(childElement, nodeName);
195
                                }else if (SchemaUtils.matches(node, SchemaTags.XS_NS, SchemaTags.CHOICE)){
196
                                        return searchNode(childElement, nodeName);
197
                                }else if (SchemaUtils.matches(node, SchemaTags.XS_NS, SchemaTags.GROUP)){
198
                                        return searchNode(childElement, nodeName);
199
                                }                        
200
                                if (childElement.getAttribute(SchemaTags.NAME).compareTo(nodeName) == 0){
201
                                        return childElement;
202
                                }        
203
                        }
204
                }
205
                return null;
206
        }
207

    
208
        /*
209
         * (non-Javadoc)
210
         * @see org.gvsig.gpe.schema.som.IXSElementDeclaration#addComplexType(java.lang.String, java.lang.String, java.lang.String)
211
         */
212
        public IXSComplexTypeDefinition addComplexType(String type,
213
                        String contentType, String contentTypeRestriction) {
214
                getElement().removeAttribute(SchemaTags.TYPE);
215
                Element eComplexType = getElementsFactory().createComplexType(
216
                                getSchema(), 
217
                                null,
218
                                type,
219
                                contentType,
220
                                contentTypeRestriction);
221
                addChildElement(eComplexType);
222
                XSComplexTypeDefinitionImpl complexType = new XSComplexTypeDefinitionImpl(getSchema());
223
                complexType.setElement(eComplexType);
224
                return complexType;
225
        }
226
        
227
}