Revision 15558 trunk/applications/appCatalogAndGazetteerClient/src/es/gva/cit/gazetteer/adl/protocols/ADLThesaurus.java
ADLThesaurus.java | ||
---|---|---|
1 | 1 |
|
2 | 2 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana |
3 |
* |
|
4 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA. |
|
19 |
* |
|
20 |
* For more information, contact: |
|
21 |
* |
|
22 |
* Generalitat Valenciana |
|
23 |
* Conselleria d'Infraestructures i Transport |
|
24 |
* Av. Blasco Ib??ez, 50 |
|
25 |
* 46010 VALENCIA |
|
26 |
* SPAIN |
|
27 |
* |
|
28 |
* +34 963862235 |
|
29 |
* gvsig@gva.es |
|
30 |
* www.gvsig.gva.es |
|
31 |
* |
|
32 |
* or |
|
33 |
* |
|
34 |
* IVER T.I. S.A |
|
35 |
* Salamanca 50 |
|
36 |
* 46005 Valencia |
|
37 |
* Spain |
|
38 |
* |
|
39 |
* +34 963163400 |
|
40 |
* dac@iver.es |
|
41 |
*/ |
|
3 |
*
|
|
4 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
|
5 |
*
|
|
6 |
* This program is free software; you can redistribute it and/or
|
|
7 |
* modify it under the terms of the GNU General Public License
|
|
8 |
* as published by the Free Software Foundation; either version 2
|
|
9 |
* of the License, or (at your option) any later version.
|
|
10 |
*
|
|
11 |
* This program is distributed in the hope that it will be useful,
|
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 |
* GNU General Public License for more details.
|
|
15 |
*
|
|
16 |
* You should have received a copy of the GNU General Public License
|
|
17 |
* along with this program; if not, write to the Free Software
|
|
18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
|
19 |
*
|
|
20 |
* For more information, contact:
|
|
21 |
*
|
|
22 |
* Generalitat Valenciana
|
|
23 |
* Conselleria d'Infraestructures i Transport
|
|
24 |
* Av. Blasco Ib??ez, 50
|
|
25 |
* 46010 VALENCIA
|
|
26 |
* SPAIN
|
|
27 |
*
|
|
28 |
* +34 963862235
|
|
29 |
* gvsig@gva.es
|
|
30 |
* www.gvsig.gva.es
|
|
31 |
*
|
|
32 |
* or
|
|
33 |
*
|
|
34 |
* IVER T.I. S.A
|
|
35 |
* Salamanca 50
|
|
36 |
* 46005 Valencia
|
|
37 |
* Spain
|
|
38 |
*
|
|
39 |
* +34 963163400
|
|
40 |
* dac@iver.es
|
|
41 |
*/
|
|
42 | 42 |
package es.gva.cit.gazetteer.adl.protocols; |
43 |
import es.gva.cit.catalogClient.metadataXML.XMLNode; |
|
44 |
import es.gva.cit.catalogClient.metadataXML.XMLTree; |
|
45 |
import es.gva.cit.catalogClient.protocols.HTTPGetProtocol; |
|
46 |
import es.gva.cit.gazetteer.querys.ThesaurusName; |
|
47 | 43 |
import java.net.MalformedURLException; |
48 | 44 |
import java.net.URL; |
49 | 45 |
import java.util.Collection; |
50 |
import java.util.Vector; |
|
46 |
|
|
51 | 47 |
import org.apache.commons.httpclient.NameValuePair; |
52 | 48 |
|
49 |
import es.gva.cit.catalog.metadataxml.XMLNode; |
|
50 |
import es.gva.cit.catalog.metadataxml.XMLTree; |
|
51 |
import es.gva.cit.catalog.protocols.HTTPGetProtocol; |
|
52 |
import es.gva.cit.gazetteer.querys.FeatureType; |
|
53 |
|
|
53 | 54 |
/** |
54 | 55 |
* This class implemets an ADL thesaurus client |
55 |
* |
|
56 |
* |
|
57 | 56 |
* @author Jorge Piera Llodra (piera_jor@gva.es) |
58 | 57 |
* @see http://middleware.alexandria.ucsb.edu:8080/thes/FTT/index.html |
59 | 58 |
*/ |
60 | 59 |
public class ADLThesaurus { |
60 |
private URL url; |
|
61 |
private FeatureType[] features; |
|
61 | 62 |
|
62 |
/** |
|
63 |
* |
|
64 |
* |
|
65 |
*/ |
|
66 |
private URL url; |
|
63 |
public ADLThesaurus(URL url) { |
|
64 |
setUrl(url); |
|
65 |
getNarrower(); |
|
66 |
} |
|
67 | 67 |
|
68 |
/** |
|
69 |
* |
|
70 |
* |
|
71 |
*/ |
|
72 |
private Vector features; |
|
68 |
/** |
|
69 |
* It implements the getNarrower ADL thesaurus operation |
|
70 |
* |
|
71 |
*/ |
|
72 |
private void getNarrower() { |
|
73 |
URL urlNarrower; |
|
74 |
try { |
|
75 |
urlNarrower = new URL ("http", getUrl().getHost(), getUrl().getPort(),getUrl().getFile() + "/get-narrower"); |
|
76 |
} catch (MalformedURLException e) { |
|
77 |
// TODO Auto-generated catch block |
|
78 |
e.printStackTrace(); |
|
79 |
return; |
|
80 |
} |
|
81 |
Collection nodes = new HTTPGetProtocol().doQuery(urlNarrower, getNarrowerParams(), 0); |
|
82 |
parseGetNarrowAnswer((XMLNode)nodes.toArray()[0]); |
|
83 |
} |
|
73 | 84 |
|
74 |
/** |
|
75 |
*
|
|
76 |
*
|
|
77 |
*
|
|
78 |
* @param url
|
|
79 |
*/
|
|
80 |
public ADLThesaurus(URL url) {
|
|
81 |
setUrl(url);
|
|
82 |
getNarrower();
|
|
83 |
}
|
|
85 |
/**
|
|
86 |
* It returns the name-value pair for the getNarrower opertaion
|
|
87 |
* @return Name-value pair
|
|
88 |
*/
|
|
89 |
private NameValuePair[] getNarrowerParams() {
|
|
90 |
String message = "";
|
|
91 |
System.out.println(message);
|
|
92 |
NameValuePair nvp1 = new NameValuePair("starting-term", "");
|
|
93 |
NameValuePair nvp2 = new NameValuePair("max-levels", "-1");
|
|
94 |
NameValuePair nvp3 = new NameValuePair("format", "term");
|
|
84 | 95 |
|
85 |
/** |
|
86 |
* It implements the getNarrower ADL thesaurus operation |
|
87 |
* |
|
88 |
*/ |
|
89 |
private void getNarrower() { |
|
90 |
URL urlNarrower; |
|
91 |
try { |
|
92 |
urlNarrower = new URL ("http", getUrl().getHost(), getUrl().getPort(),getUrl().getFile() + "/get-narrower"); |
|
93 |
} catch (MalformedURLException e) { |
|
94 |
// TODO Auto-generated catch block |
|
95 |
e.printStackTrace(); |
|
96 |
return; |
|
97 |
} |
|
98 |
Collection nodes = new HTTPGetProtocol().doQuery(urlNarrower, getNarrowerParams(), 0); |
|
99 |
parseGetNarrowAnswer((XMLNode)nodes.toArray()[0]); |
|
100 |
|
|
101 |
} |
|
96 |
return new NameValuePair[] { nvp1, nvp2, nvp3 }; |
|
97 |
} |
|
102 | 98 |
|
103 |
/** |
|
104 |
* It returns the name-value pair for the getNarrower opertaion
|
|
105 |
*
|
|
106 |
*
|
|
107 |
* @return Name-value pair
|
|
108 |
*/
|
|
109 |
private NameValuePair[] getNarrowerParams() {
|
|
110 |
String message = "";
|
|
111 |
System.out.println(message);
|
|
112 |
NameValuePair nvp1 = new NameValuePair("starting-term", "");
|
|
113 |
NameValuePair nvp2 = new NameValuePair("max-levels", "-1");
|
|
114 |
NameValuePair nvp3 = new NameValuePair("format", "term");
|
|
115 |
|
|
116 |
return new NameValuePair[] { nvp1, nvp2, nvp3 };
|
|
117 |
}
|
|
99 |
/**
|
|
100 |
* It parses the getNarrow answer into a Feature arrray
|
|
101 |
* @param node
|
|
102 |
*/
|
|
103 |
private void parseGetNarrowAnswer(XMLNode node) {
|
|
104 |
XMLNode[] rootNodes = XMLTree.searchMultipleNode(node,"hierarchy->node->node");
|
|
105 |
FeatureType[] features = new FeatureType[rootNodes.length];
|
|
106 |
for (int i=0 ; i<rootNodes.length ; i++){
|
|
107 |
FeatureType thesaurus = new FeatureType(XMLTree.searchNodeValue(rootNodes[i],"term"));
|
|
108 |
thesaurus.setFeatures(parseRecursiveFeatures(rootNodes[i],thesaurus));
|
|
109 |
thesaurus.setTitle(thesaurus.getName());
|
|
110 |
features[i] = (thesaurus);
|
|
111 |
} |
|
112 |
setFeatures(features);
|
|
113 |
}
|
|
118 | 114 |
|
119 |
/** |
|
120 |
* It parses the getNarrow answer into a Feature arrray |
|
121 |
* |
|
122 |
* |
|
123 |
* @param node |
|
124 |
*/ |
|
125 |
private void parseGetNarrowAnswer(XMLNode node) { |
|
126 |
XMLNode[] rootNodes = XMLTree.searchMultipleNode(node,"hierarchy->node->node"); |
|
127 |
Vector features = new Vector(); |
|
128 |
for (int i=0 ; i<rootNodes.length ; i++){ |
|
129 |
ThesaurusName thesaurus = new ThesaurusName(XMLTree.searchNodeValue(rootNodes[i],"term")); |
|
130 |
thesaurus.setFeatures(parseRecursiveFeatures(rootNodes[i],thesaurus)); |
|
131 |
thesaurus.setTitle(thesaurus.getName()); |
|
132 |
features.add(thesaurus); |
|
133 |
} |
|
134 |
setFeatures(features); |
|
135 |
|
|
136 |
} |
|
115 |
/** |
|
116 |
* It is used to parse a feature using recursivity. |
|
117 |
* @return |
|
118 |
* @param node Feature tree |
|
119 |
* @param thesaurus Feature to add the child Features |
|
120 |
*/ |
|
121 |
private FeatureType[] parseRecursiveFeatures(XMLNode node, FeatureType thesaurus) { |
|
122 |
XMLNode[] rootNodes = XMLTree.searchMultipleNode(node,"node"); |
|
137 | 123 |
|
138 |
/** |
|
139 |
* It is used to parse a feature using recursivity. |
|
140 |
* |
|
141 |
* |
|
142 |
* @return |
|
143 |
* @param node Feature tree |
|
144 |
* @param thesaurus Feature to add the child Features |
|
145 |
*/ |
|
146 |
private Vector parseRecursiveFeatures(XMLNode node, ThesaurusName thesaurus) { |
|
147 |
XMLNode[] rootNodes = XMLTree.searchMultipleNode(node,"node"); |
|
148 |
|
|
149 |
if ((rootNodes == null) || (rootNodes.length == 0)){ |
|
150 |
return null; |
|
151 |
} |
|
152 |
|
|
153 |
Vector vFeatures = new Vector(); |
|
154 |
for (int i=0 ; i<rootNodes.length ; i++){ |
|
155 |
ThesaurusName f = new ThesaurusName(XMLTree.searchNodeValue(rootNodes[i],"term")); |
|
156 |
f.setTitle(f.getName()); |
|
157 |
f.setFeatures(parseRecursiveFeatures(rootNodes[i],f)); |
|
158 |
vFeatures.add(f); |
|
159 |
} |
|
160 |
return vFeatures; |
|
161 |
} |
|
124 |
if ((rootNodes == null) || (rootNodes.length == 0)){ |
|
125 |
return null; |
|
126 |
} |
|
162 | 127 |
|
163 |
/**
|
|
164 |
*
|
|
165 |
*
|
|
166 |
*
|
|
167 |
* @return Returns the url.
|
|
168 |
*/
|
|
169 |
public URL getUrl() {
|
|
170 |
return url;
|
|
171 |
}
|
|
128 |
FeatureType[] features = new FeatureType[rootNodes.length];
|
|
129 |
for (int i=0 ; i<rootNodes.length ; i++){
|
|
130 |
FeatureType f = new FeatureType(XMLTree.searchNodeValue(rootNodes[i],"term"));
|
|
131 |
f.setTitle(f.getName());
|
|
132 |
f.setFeatures(parseRecursiveFeatures(rootNodes[i],f));
|
|
133 |
features[i] = f;
|
|
134 |
}
|
|
135 |
return features;
|
|
136 |
}
|
|
172 | 137 |
|
173 |
/** |
|
174 |
* |
|
175 |
* |
|
176 |
* |
|
177 |
* @param url The url to set. |
|
178 |
*/ |
|
179 |
public void setUrl(URL url) { |
|
180 |
this.url = url; |
|
181 |
} |
|
138 |
/** |
|
139 |
* @return Returns the url. |
|
140 |
*/ |
|
141 |
public URL getUrl() { |
|
142 |
return url; |
|
143 |
} |
|
182 | 144 |
|
183 |
/** |
|
184 |
* |
|
185 |
* |
|
186 |
* |
|
187 |
* @return Returns the features. |
|
188 |
*/ |
|
189 |
public Vector getFeatures() { |
|
190 |
return features; |
|
191 |
} |
|
145 |
/** |
|
146 |
* @param url The url to set. |
|
147 |
*/ |
|
148 |
public void setUrl(URL url) { |
|
149 |
this.url = url; |
|
150 |
} |
|
192 | 151 |
|
193 |
/** |
|
194 |
* |
|
195 |
* |
|
196 |
* |
|
197 |
* @param features The features to set. |
|
198 |
*/ |
|
199 |
public void setFeatures(Vector features) { |
|
200 |
this.features = features; |
|
201 |
} |
|
202 |
} |
|
152 |
/** |
|
153 |
* @return Returns the features. |
|
154 |
*/ |
|
155 |
public FeatureType[] getFeatures() { |
|
156 |
return features; |
|
157 |
} |
|
158 |
|
|
159 |
/** |
|
160 |
* @param features The features to set. |
|
161 |
*/ |
|
162 |
public void setFeatures(FeatureType[] features) { |
|
163 |
this.features = features; |
|
164 |
} |
|
165 |
} |
Also available in: Unified diff