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 / utils / SchemaUtils.java @ 257
History | View | Annotate | Download (6.06 KB)
1 |
package org.gvsig.xmlschema.prov.som.utils; |
---|---|
2 |
|
3 |
|
4 |
|
5 |
import javax.xml.namespace.QName; |
6 |
|
7 |
import org.w3c.dom.Element; |
8 |
import org.w3c.dom.Node; |
9 |
|
10 |
import org.gvsig.xmlschema.lib.api.som.IXSElement; |
11 |
import org.gvsig.xmlschema.lib.api.som.IXSNode; |
12 |
import org.gvsig.xmlschema.lib.api.som.IXSNodeList; |
13 |
import org.gvsig.xmlschema.lib.api.utils.SchemaTags; |
14 |
|
15 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
16 |
*
|
17 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
18 |
*
|
19 |
* This program is free software; you can redistribute it and/or
|
20 |
* modify it under the terms of the GNU General Public License
|
21 |
* as published by the Free Software Foundation; either version 2
|
22 |
* of the License, or (at your option) any later version.
|
23 |
*
|
24 |
* This program is distributed in the hope that it will be useful,
|
25 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
26 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
27 |
* GNU General Public License for more details.
|
28 |
*
|
29 |
* You should have received a copy of the GNU General Public License
|
30 |
* along with this program; if not, write to the Free Software
|
31 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
32 |
*
|
33 |
* For more information, contact:
|
34 |
*
|
35 |
* Generalitat Valenciana
|
36 |
* Conselleria d'Infraestructures i Transport
|
37 |
* Av. Blasco Ib??ez, 50
|
38 |
* 46010 VALENCIA
|
39 |
* SPAIN
|
40 |
*
|
41 |
* +34 963862235
|
42 |
* gvsig@gva.es
|
43 |
* www.gvsig.gva.es
|
44 |
*
|
45 |
* or
|
46 |
*
|
47 |
* IVER T.I. S.A
|
48 |
* Salamanca 50
|
49 |
* 46005 Valencia
|
50 |
* Spain
|
51 |
*
|
52 |
* +34 963163400
|
53 |
* dac@iver.es
|
54 |
*/
|
55 |
/* CVS MESSAGES:
|
56 |
*
|
57 |
* $Id: SchemaUtils.java 192 2007-11-26 08:49:01Z jpiera $
|
58 |
* $Log$
|
59 |
* Revision 1.2 2007/06/28 13:04:33 jorpiell
|
60 |
* The Qname has been updated to the 1.5 JVM machine. The schema validation is made in the GPEWriterHandlerImplementor class
|
61 |
*
|
62 |
* Revision 1.1 2007/06/14 16:15:03 jorpiell
|
63 |
* builds to create the jars generated and add the schema code to the libGPEProject
|
64 |
*
|
65 |
* Revision 1.1 2007/06/14 13:50:07 jorpiell
|
66 |
* The schema jar name has been changed
|
67 |
*
|
68 |
* Revision 1.6 2007/06/08 13:00:40 jorpiell
|
69 |
* Add the targetNamespace to the file
|
70 |
*
|
71 |
* Revision 1.5 2007/06/08 07:31:20 jorpiell
|
72 |
* Add the euroRoadS test
|
73 |
*
|
74 |
* Revision 1.4 2007/06/08 06:55:05 jorpiell
|
75 |
* Fixed some bugs
|
76 |
*
|
77 |
* Revision 1.3 2007/05/30 12:50:53 jorpiell
|
78 |
* Refactoring of some duplicated methods
|
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 |
* Some utils to manage xml schemas
|
90 |
* @author Jorge Piera LLodr? (jorge.piera@iver.es)
|
91 |
*/
|
92 |
public class SchemaUtils { |
93 |
|
94 |
/**
|
95 |
* Compare a DOM nod3 by name
|
96 |
* @param n
|
97 |
* DOM node
|
98 |
* @param namespace
|
99 |
* Node namespace
|
100 |
* @param tagName
|
101 |
* Node name
|
102 |
* @return
|
103 |
* <code>true</code> if the node has this name
|
104 |
*/
|
105 |
public static boolean matches(IXSNode n, String namespace, String tagName){ |
106 |
String nodeName = n.getNodeName().substring(n.getNodeName().indexOf(":")+1, n.getNodeName().length()); |
107 |
int index = n.getNodeName().indexOf(":"); |
108 |
String nodeNSURI = null; |
109 |
if (index > 0){ |
110 |
nodeNSURI = n.getNodeName().substring(0,n.getNodeName().indexOf(":")); |
111 |
} |
112 |
if (nodeNSURI == null){ |
113 |
return objNullEq(tagName.toUpperCase(), nodeName.toUpperCase());
|
114 |
} |
115 |
return objNullEq(nodeNSURI.toUpperCase(), namespace.toUpperCase()) &&
|
116 |
objNullEq(tagName.toUpperCase(), nodeName.toUpperCase()); |
117 |
} |
118 |
|
119 |
/**
|
120 |
* Determines if two QNames are equal
|
121 |
* @param qname1
|
122 |
* First qname to compare
|
123 |
* @param qname2
|
124 |
* Second qname to compare
|
125 |
* @return
|
126 |
* <code>true</code> or <code>false</code> according
|
127 |
* to the above description.
|
128 |
*/
|
129 |
public static boolean matches(QName qname1, QName qname2){ |
130 |
return objNullEq(qname1.getNamespaceURI().toUpperCase(), qname2.getNamespaceURI().toUpperCase()) &&
|
131 |
objNullEq(qname1.getLocalPart().toUpperCase(), qname2.getLocalPart().toUpperCase()); |
132 |
} |
133 |
|
134 |
/**
|
135 |
* Search a Element by name
|
136 |
* @param element
|
137 |
* Parent element
|
138 |
* @param childName
|
139 |
* Element name to find
|
140 |
* @return
|
141 |
*/
|
142 |
public static IXSElement searchChildByTagName(IXSElement element, String childName){ |
143 |
IXSNodeList nodeList = element.getChildNodes(); |
144 |
for(int i = 0; i < nodeList.getLength(); i++){ |
145 |
IXSNode node = nodeList.item(i); |
146 |
if(node.getNodeType() == Node.ELEMENT_NODE){
|
147 |
if (SchemaUtils.matches(node, SchemaTags.XS_NS, childName)){
|
148 |
return (IXSElement)node;
|
149 |
} |
150 |
} |
151 |
} |
152 |
return null; |
153 |
} |
154 |
|
155 |
/**
|
156 |
* Search a Element with a specified type and
|
157 |
* a specified attribute name
|
158 |
* @param element
|
159 |
* Parent element
|
160 |
* @param tagName
|
161 |
* Element tag name to find
|
162 |
* @param targetNamespace
|
163 |
* Namespace for the name attribute
|
164 |
* @param attributeName
|
165 |
* Value for the attribute name
|
166 |
* @return
|
167 |
*/
|
168 |
public static IXSElement searchChildByAttributeName(IXSElement element, String tagName, String targetNamespace, String attributeName){ |
169 |
IXSNodeList nodeList = element.getChildNodes(); |
170 |
for(int i = 0; i < nodeList.getLength(); i++){ |
171 |
IXSNode node = nodeList.item(i); |
172 |
if(node.getNodeType() == Node.ELEMENT_NODE){
|
173 |
if (SchemaUtils.matches(node, SchemaTags.XS_NS, tagName)){
|
174 |
String name = ((Element)node).getAttribute(SchemaTags.NAME); |
175 |
if (objNullEq(name, attributeName)){
|
176 |
return (IXSElement)node;
|
177 |
} |
178 |
} |
179 |
} |
180 |
} |
181 |
return null; |
182 |
} |
183 |
|
184 |
/**
|
185 |
* Determines if two objects are equal.
|
186 |
* @param object1
|
187 |
* First object to compare.
|
188 |
* @param object2
|
189 |
* Second object to compare.
|
190 |
* @return
|
191 |
* <code>true</code> or <code>false</code> according
|
192 |
* to the above description.
|
193 |
*/
|
194 |
private static boolean objNullEq(Object object1, Object object2){ |
195 |
if((object1 == null) && (object2 == null)){ |
196 |
return true; |
197 |
} |
198 |
if((object1 == null) && (object2 != null)){ |
199 |
return false; |
200 |
} |
201 |
if((object1 != null) && (object2 == null)){ |
202 |
return false; |
203 |
} |
204 |
return object1.equals(object2);
|
205 |
} |
206 |
|
207 |
/**
|
208 |
* Gets the local name for a qualified name
|
209 |
* @param qualifiedName
|
210 |
* @return
|
211 |
*/
|
212 |
public static String getLocalName(String qualifiedName) { |
213 |
int index = qualifiedName.indexOf(":"); |
214 |
if (index < 0){ |
215 |
return qualifiedName;
|
216 |
} |
217 |
return qualifiedName.substring(index + 1, qualifiedName.length()); |
218 |
} |
219 |
} |