Revision 412 org.gvsig.hyperlink.app/trunk/org.gvsig.hyperlink.app/org.gvsig.hyperlink.app.extension/src/main/java/org/gvsig/hyperlink/app/extension/layers/VectLayerManager.java

View differences:

VectLayerManager.java
30 30
import java.net.URL;
31 31
import java.net.URLClassLoader;
32 32
import java.util.ArrayList;
33
import java.util.List;
33 34
import java.util.Map;
34
import java.util.logging.Level;
35
import java.util.logging.Logger;
36
import java.util.regex.Matcher;
37
import java.util.regex.Pattern;
38 35
import org.apache.commons.io.FileUtils;
39 36
import org.apache.commons.lang3.StringUtils;
40 37

  
......
52 49
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
53 50
import org.gvsig.fmap.mapcontext.layers.FLayer;
54 51
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
52
import org.gvsig.hyperlink.app.extension.DefaultLinkTarget;
53
import org.gvsig.hyperlink.app.extension.LinkTarget;
55 54
import org.gvsig.tools.dispose.DisposableIterator;
56 55

  
57 56
public class VectLayerManager implements ILinkLayerManager {
58 57

  
59 58
    private FLyrVect _layer = null;
60 59

  
61
    public URI[] getLink(Point2D point,
60
    @Override
61
    public LinkTarget[] getLink(Point2D point,
62 62
        double tolerance,
63 63
        String fieldName,
64 64
        String fileExtension) {
65 65
        FLyrVect lyrVect = (FLyrVect) _layer;
66
        ArrayList<URI> uriList = new ArrayList();
66
        //ArrayList<LinkTarget> uriList = new ArrayList();
67
        List<LinkTarget> uriList = new ArrayList();
67 68
        FeatureSet features;
68 69
        FeatureType featureType;
69 70

  
......
71 72
            // FIXME: Habr? que ver como lo hacemos con las capas multigeometr?a
72 73
            featureType = _layer.getFeatureStore().getDefaultFeatureType();
73 74
            features = lyrVect.queryByPoint(point, tolerance, featureType);
74
        } catch (Exception e) {
75
        } catch (DataException e) {
75 76
            return null;
76 77
        }
77 78

  
......
87 88

  
88 89
                while (it.hasNext()) {
89 90
                    Feature feature = (Feature) it.next();
90
                    val = feature.get(fieldName);
91
                    String fieldValue = (val == null) ? "" : val.toString();
92
                    if (!fieldValue.equals("")) {
93
                        try {
94
                            uriList.add(getURI(fieldValue, fileExtension));
95
                        } catch (URISyntaxException e) {
96
                            NotificationManager.addWarning(PluginServices.getText(this,
97
                                "Hyperlink__field_value_is_not_valid_file"),
98
                                e);
99
                        }
100
                    }
101 91

  
102
                }
92
                            DefaultLinkTarget target = new DefaultLinkTarget(_layer, feature, fieldName, fileExtension);
93
                            uriList.add(target); //getURI(fieldValue, fileExtension));
94
                    }
103 95
                it.dispose();
104
                return (URI[]) uriList.toArray(new URI[0]);
96
                return uriList.toArray(new LinkTarget[0]);//(LinkTarget[]) uriList.toArray(); //(URI[]) uriList.toArray(new URI[0]);
105 97
            } catch (DataException e1) {
106 98
                PluginServices.getLogger()
107 99
                    .error("Hyperlink__cant_get_the_iterator", e1);
108 100
            }
109 101
        }
110
        return new URI[0];
102
        return null; //new URI[0];
111 103
    }
112 104

  
113 105
    private URL toURL(File f) {
......
162 154
        return null;
163 155
    }
164 156
    
165
    protected URI getURI(String baseURI, String extension) throws URISyntaxException {
166
        if( StringUtils.isEmpty(baseURI) ) {
167
            return null;
168
        }
169
        URI uri = getBasicURI(baseURI, extension);
170
        if( uri!=null ) {
171
            return uri;
172
        }
173
        String value = baseURI.trim();
174 157

  
175
        Pattern pattern = Pattern.compile(".*<img[^>]*src=\"([^\"]*)\".*",  Pattern.CASE_INSENSITIVE|Pattern.MULTILINE|Pattern.DOTALL);
176
        Matcher m = pattern.matcher(value);
177
        if( m!=null && m.matches() ) {
178
            String x = m.group(1);
179
            if( !StringUtils.isEmpty(x) ) {
180
                uri = getBasicURI(x, extension);
181
                if( uri!=null ) {
182
                    return uri;
183
                }
184
            }
185
        }
186
        pattern = Pattern.compile(".*<a[^>]*href=\"([^\"]*)\".*", Pattern.CASE_INSENSITIVE|Pattern.MULTILINE|Pattern.DOTALL);
187
        m = pattern.matcher(value);
188
        if( m!=null && m.matches() ) {
189
            String x = m.group(1);
190
            if( !StringUtils.isEmpty(x) ) {
191
                uri = getBasicURI(x, extension);
192
                if( uri!=null ) {
193
                    return uri;
194
                }
195
            }
196
        }
197
        try {
198
            URI createURI = new URI(baseURI);
199
            return createURI;
200
        } catch (Exception e) {
201
            throw new URISyntaxException(baseURI, "Can't make a valid URI.");
202
        }
203
    }
204

  
158
    @Override
205 159
    public FLayer getLayer() {
206 160
        return _layer;
207 161
    }
208 162

  
163
    @Override
209 164
    public void setLayer(FLayer layer) throws IncompatibleLayerException {
210 165
        try {
211 166
            _layer = (FLyrVect) layer;
......
226 181
        return this;
227 182
    }
228 183

  
229
    public URI[][] getLink(Point2D point,
230
        double tolerance,
231
        String[] fieldName,
232
        String fileExtension) {
233
        FLyrVect lyrVect = (FLyrVect) _layer;
234
        FeatureSet features;
235
        FeatureType featureType;
236
        URI uri[][] = null;
184
//    public URI[][] getLink(Point2D point,
185
//        double tolerance,
186
//        String[] fieldName,
187
//        String fileExtension) {
188
//        FLyrVect lyrVect = (FLyrVect) _layer;
189
//        FeatureSet features;
190
//        FeatureType featureType;
191
//        URI uri[][] = null;
192
//
193
//        try {
194
//            // FIXME: Habr? que ver como lo hacemos con las capas multigeometr?a
195
//            featureType = _layer.getFeatureStore().getDefaultFeatureType();
196
//            features = lyrVect.queryByPoint(point, tolerance, featureType);
197
//        } catch (Exception e) {
198
//            return null;
199
//        }
200
//
201
//        // Si el conjunto creado no est? vac?o creamos el vector de URLS
202
//        // correspondientes
203
//        // a la consulta que hemos hecho.
204
//
205
//        if (features != null) {
206
//            try {
207
//                // Creo el vector de URL?s con la misma longitud que features
208
//                uri = new URI[(int) features.getSize()][fieldName.length];
209
//
210
//                // Recorremos las features siguiendo el ejemplo de la clase que
211
//                // se
212
//                // proporciona en la API
213
//                int count = 0;
214
//                DisposableIterator it = features.fastIterator();
215
//                while (it.hasNext()) {
216
//                    Feature feat = (Feature) it.next();
217
//                    for (int fieldCount = 0; fieldCount < fieldName.length; fieldCount++) {
218
//                        // get the field ID using the field name
219
//                        String auxField =
220
//                            feat.get(fieldName[fieldCount]).toString();
221
//                        if (auxField != null) {
222
//                            if (auxField.startsWith("http:/")) {
223
//                                try {
224
//                                    uri[count][fieldCount] = new URI(auxField);
225
//                                } catch (URISyntaxException e) {
226
//                                    PluginServices.getLogger().error("", e);
227
//                                }
228
//                            } else {
229
//                                File file = new File(auxField);
230
//                                uri[count][fieldCount] = file.toURI();
231
//                            }
232
//                        } else {
233
//                            PluginServices.getLogger()
234
//                                .error("Hyperlink error. Field "
235
//                                    + fieldName[fieldCount] + "doesn't exist!!");
236
//                            uri[count][fieldCount] = null;
237
//                        }
238
//                    }
239
//                    count++;
240
//                }
241
//                it.dispose();
242
//
243
//                return uri;
244
//            } catch (DataException e) {
245
//                PluginServices.getLogger().error("", e);
246
//            }
247
//        }
248
//        return new URI[0][0];
249
//    }
237 250

  
238
        try {
239
            // FIXME: Habr? que ver como lo hacemos con las capas multigeometr?a
240
            featureType = _layer.getFeatureStore().getDefaultFeatureType();
241
            features = lyrVect.queryByPoint(point, tolerance, featureType);
242
        } catch (Exception e) {
243
            return null;
244
        }
245

  
246
        // Si el conjunto creado no est? vac?o creamos el vector de URLS
247
        // correspondientes
248
        // a la consulta que hemos hecho.
249

  
250
        if (features != null) {
251
            try {
252
                // Creo el vector de URL?s con la misma longitud que features
253
                uri = new URI[(int) features.getSize()][fieldName.length];
254

  
255
                // Recorremos las features siguiendo el ejemplo de la clase que
256
                // se
257
                // proporciona en la API
258
                int count = 0;
259
                DisposableIterator it = features.fastIterator();
260
                while (it.hasNext()) {
261
                    Feature feat = (Feature) it.next();
262
                    for (int fieldCount = 0; fieldCount < fieldName.length; fieldCount++) {
263
                        // get the field ID using the field name
264
                        String auxField =
265
                            feat.get(fieldName[fieldCount]).toString();
266
                        if (auxField != null) {
267
                            if (auxField.startsWith("http:/")) {
268
                                try {
269
                                    uri[count][fieldCount] = new URI(auxField);
270
                                } catch (URISyntaxException e) {
271
                                    PluginServices.getLogger().error("", e);
272
                                }
273
                            } else {
274
                                File file = new File(auxField);
275
                                uri[count][fieldCount] = file.toURI();
276
                            }
277
                        } else {
278
                            PluginServices.getLogger()
279
                                .error("Hyperlink error. Field "
280
                                    + fieldName[fieldCount] + "doesn't exist!!");
281
                            uri[count][fieldCount] = null;
282
                        }
283
                    }
284
                    count++;
285
                }
286
                it.dispose();
287

  
288
                return uri;
289
            } catch (DataException e) {
290
                PluginServices.getLogger().error("", e);
291
            }
292
        }
293
        return new URI[0][0];
294
    }
295

  
251
    @Override
296 252
    public String[] getFieldCandidates() {
297 253
        try {
298 254
            FeatureType featureType =

Also available in: Unified diff