Statistics
| Revision:

root / org.gvsig.xmlschema / library / trunk / org.gvsig.xmlschema / org.gvsig.xmlschema.prov / org.gvsig.xmlschema.prov.dom / src / main / java / org / gvsig / xmlschema / prov / som / model / XSElementDeclarationImpl.java @ 250

History | View | Annotate | Download (7.05 KB)

1
package org.gvsig.xmlschema.prov.som.model;
2

    
3
import java.util.Iterator;
4

    
5
import org.gvsig.xmlschema.lib.api.som.IXSComplexTypeDefinition;
6
import org.gvsig.xmlschema.lib.api.som.IXSElement;
7
import org.gvsig.xmlschema.lib.api.som.IXSElementDeclaration;
8
import org.gvsig.xmlschema.lib.api.som.IXSNode;
9
import org.gvsig.xmlschema.lib.api.som.IXSNodeList;
10
import org.gvsig.xmlschema.lib.api.som.IXSSchema;
11
import org.gvsig.xmlschema.lib.api.som.IXSTypeDefinition;
12
import org.gvsig.xmlschema.lib.api.utils.SchemaTags;
13
import org.gvsig.xmlschema.prov.som.utils.SchemaCollection;
14
import org.gvsig.xmlschema.prov.som.utils.SchemaUtils;
15
import org.w3c.dom.Node;
16

    
17

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

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

    
117

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

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

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

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

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

    
228
    public IXSElementDeclaration getParentElement() {
229
        // TODO Auto-generated method stub
230
        return null;
231
    }
232
        
233
}