root / trunk / applications / appCatalogAndGazetteerClient / src / es / gva / cit / gazetteer / adl / protocols / ADLThesaurus.java @ 15558
History | View | Annotate | Download (4.58 KB)
1 |
|
---|---|
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 |
*/
|
42 |
package es.gva.cit.gazetteer.adl.protocols; |
43 |
import java.net.MalformedURLException; |
44 |
import java.net.URL; |
45 |
import java.util.Collection; |
46 |
|
47 |
import org.apache.commons.httpclient.NameValuePair; |
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 |
|
54 |
/**
|
55 |
* This class implemets an ADL thesaurus client
|
56 |
* @author Jorge Piera Llodra (piera_jor@gva.es)
|
57 |
* @see http://middleware.alexandria.ucsb.edu:8080/thes/FTT/index.html
|
58 |
*/
|
59 |
public class ADLThesaurus { |
60 |
private URL url; |
61 |
private FeatureType[] features; |
62 |
|
63 |
public ADLThesaurus(URL url) { |
64 |
setUrl(url); |
65 |
getNarrower(); |
66 |
} |
67 |
|
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 |
} |
84 |
|
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"); |
95 |
|
96 |
return new NameValuePair[] { nvp1, nvp2, nvp3 }; |
97 |
} |
98 |
|
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 |
} |
114 |
|
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"); |
123 |
|
124 |
if ((rootNodes == null) || (rootNodes.length == 0)){ |
125 |
return null; |
126 |
} |
127 |
|
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 |
} |
137 |
|
138 |
/**
|
139 |
* @return Returns the url.
|
140 |
*/
|
141 |
public URL getUrl() { |
142 |
return url;
|
143 |
} |
144 |
|
145 |
/**
|
146 |
* @param url The url to set.
|
147 |
*/
|
148 |
public void setUrl(URL url) { |
149 |
this.url = url;
|
150 |
} |
151 |
|
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 |
} |