Revision 370

View differences:

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
24 24

  
25 25
import java.awt.geom.Point2D;
26 26
import java.io.File;
27
import java.net.MalformedURLException;
27 28
import java.net.URI;
28 29
import java.net.URISyntaxException;
30
import java.net.URL;
31
import java.net.URLClassLoader;
29 32
import java.util.ArrayList;
30 33
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
import org.apache.commons.io.FileUtils;
39
import org.apache.commons.lang3.StringUtils;
31 40

  
32 41
import org.gvsig.andami.PluginServices;
33 42
import org.gvsig.andami.messages.NotificationManager;
43
import org.gvsig.app.ApplicationLocator;
44
import org.gvsig.app.project.Project;
45
import org.gvsig.app.project.ProjectManager;
34 46
import org.gvsig.fmap.dal.DataTypes;
35 47
import org.gvsig.fmap.dal.exception.DataException;
36 48
import org.gvsig.fmap.dal.feature.Feature;
37 49
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
38 50
import org.gvsig.fmap.dal.feature.FeatureSet;
39 51
import org.gvsig.fmap.dal.feature.FeatureType;
52
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
40 53
import org.gvsig.fmap.mapcontext.layers.FLayer;
41 54
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
42 55
import org.gvsig.tools.dispose.DisposableIterator;
......
97 110
        return new URI[0];
98 111
    }
99 112

  
100
    protected URI getURI(String baseURI, String extension) throws URISyntaxException {
101
        String stringURI;
102
        if (extension.equals("")) {
103
            stringURI = baseURI;
104
        } else
113
    private URL toURL(File f) {
114
        try {
115
            return f.toURI().toURL();
116
        } catch (MalformedURLException ex) {
117
            return null;
118
        }
119
    }
120
    
121
    protected URI getBasicURI(String baseURI, String extension) throws URISyntaxException {
122
        String pathname;
123
        if( StringUtils.isEmpty(extension) ) {
124
            pathname = baseURI;
125
        } else {
105 126
            if (extension.startsWith(".")) {
106
                stringURI = baseURI + extension;
127
                pathname = baseURI + extension;
107 128
            } else {
108
                stringURI = baseURI + "." + extension;
129
                pathname = baseURI + "." + extension;
109 130
            }
110
        File file = new File(stringURI);
111
        if (file.exists()) {
112
            return file.toURI();
113
        } else {
114
            return new URI(stringURI);
115 131
        }
132
        pathname = pathname.replace("\\","//");
133
        if( this._layer.getFeatureStore().getParameters() instanceof FilesystemStoreParameters ) {
134
            FilesystemStoreParameters params = (FilesystemStoreParameters) this._layer.getFeatureStore().getParameters();
135
            File f = params.getFile();
136
            URLClassLoader loader = new URLClassLoader(new URL[] {toURL(f)});
137
            URL url = loader.getResource(pathname);
138
            if( url != null ) {
139
                return url.toURI();
140
            }
141
            url = loader.getResource("/" + pathname);
142
            if( url != null ) {
143
                return url.toURI();
144
            }
145
            File ff = FileUtils.getFile(f.getParentFile(), pathname);
146
            if( ff.exists() ) {
147
                return ff.toURI();
148
            }
149
        }
150
        ProjectManager projectManager = ApplicationLocator.getProjectManager();
151
        Project project = projectManager.getCurrentProject();
152
        if( project.getFile()!=null ) {
153
            File ff = FileUtils.getFile(project.getFile().getParentFile(), pathname);
154
            if( ff.exists() ) {
155
                return ff.toURI();
156
            }
157
        }
158
        File ff = new File(pathname);
159
        if( ff.exists() ) {
160
            return ff.toURI();
161
        }
162
        return null;
116 163
    }
164
    
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();
117 174

  
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
        throw new URISyntaxException(baseURI, "Can't make a valid URI.");
198
    }
199

  
118 200
    public FLayer getLayer() {
119 201
        return _layer;
120 202
    }

Also available in: Unified diff