Statistics
| Revision:

root / org.gvsig.hyperlink.app / trunk / org.gvsig.hyperlink.app / org.gvsig.hyperlink.app.extension / src / main / java / org / gvsig / hyperlink / app / extension / DefaultLinkTarget.java @ 412

History | View | Annotate | Download (6.55 KB)

1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.hyperlink.app.extension;
7

    
8
import java.io.File;
9
import java.net.MalformedURLException;
10
import java.net.URI;
11
import java.net.URISyntaxException;
12
import java.net.URL;
13
import java.net.URLClassLoader;
14
import java.util.logging.Level;
15
import java.util.logging.Logger;
16
import java.util.regex.Matcher;
17
import java.util.regex.Pattern;
18
import org.apache.commons.io.FileUtils;
19
import org.apache.commons.lang3.StringUtils;
20
import org.gvsig.app.ApplicationLocator;
21
import org.gvsig.app.project.Project;
22
import org.gvsig.app.project.ProjectManager;
23
import org.gvsig.fmap.dal.exception.DataException;
24
import org.gvsig.fmap.dal.feature.Feature;
25
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
26
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
27
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
28

    
29
/**
30
 *
31
 * @author osc
32
 */
33
public class DefaultLinkTarget implements LinkTarget {
34

    
35
    private URL url;
36
    private final String fieldExtension;
37
    private final FLyrVect _layer;
38
    private final String fieldName;
39
    private Object content;
40
    private String profileName;
41

    
42

    
43
    public DefaultLinkTarget(FLyrVect layer,Feature feature, String fieldName, String fieldExtension) {
44
        this.fieldName = fieldName;
45
        this.fieldExtension = fieldExtension;
46
        this._layer = layer;
47
        this.url = null;
48
        Object val = feature.get(fieldName);
49
        String fieldValue = (val == null) ? "" : val.toString();
50
        if (!fieldValue.equals("")) {
51
            try {
52
                URI uri = this.getURI(fieldValue, fieldExtension);
53
                if (uri != null) {
54
                    this.url = uri.toURL();
55
                } else {
56
                    this.url = null;
57
                }
58

    
59
            } catch (URISyntaxException ex) {
60
                Logger.getLogger(DefaultLinkTarget.class.getName()).log(Level.SEVERE, null, ex);
61
                this.url = null;
62
            } catch (MalformedURLException ex) {
63
                Logger.getLogger(DefaultLinkTarget.class.getName()).log(Level.SEVERE, null, ex);
64
                this.url = null;
65
            }
66
            if (this.url == null) {
67
                FeatureAttributeDescriptor fad;
68

    
69
                try {
70
                    this.content = feature.getFromProfile(fieldName);
71
                    fad = feature.getStore().getDefaultFeatureType().getAttributeDescriptor(fieldName);
72
                } catch (DataException ex) {
73
                    fad = null;
74
                    Logger.getLogger(DefaultLinkTarget.class.getName()).log(Level.SEVERE, null, ex);
75
                }
76
                if (fad != null) {
77
                    this.profileName = fad.getDataProfileName();
78
                } else {
79
                    this.profileName = null;
80
                }
81

    
82
            }
83
        }
84
        
85

    
86
    }
87
    protected URI getBasicURI(String baseURI, String extension) throws URISyntaxException {
88
        String pathname;
89
        if( StringUtils.isEmpty(extension) ) {
90
            pathname = baseURI;
91
        } else {
92
            if (extension.startsWith(".")) {
93
                pathname = baseURI + extension;
94
            } else {
95
                pathname = baseURI + "." + extension;
96
            }
97
        }
98
        pathname = pathname.replace("\\","//");
99
        if( this._layer.getFeatureStore().getParameters() instanceof FilesystemStoreParameters ) {
100
            FilesystemStoreParameters params = (FilesystemStoreParameters) this._layer.getFeatureStore().getParameters();
101
            File f = params.getFile();
102
            URLClassLoader loader = new URLClassLoader(new URL[] {toURL(f)});
103
            URL url = loader.getResource(pathname);
104
            if( url != null ) {
105
                return url.toURI();
106
            }
107
            url = loader.getResource("/" + pathname);
108
            if( url != null ) {
109
                return url.toURI();
110
            }
111
            File ff = FileUtils.getFile(f.getParentFile(), pathname);
112
            if( ff.exists() ) {
113
                return ff.toURI();
114
            }
115
        }
116
        ProjectManager projectManager = ApplicationLocator.getProjectManager();
117
        Project project = projectManager.getCurrentProject();
118
        if( project.getFile()!=null ) {
119
            File ff = FileUtils.getFile(project.getFile().getParentFile(), pathname);
120
            if( ff.exists() ) {
121
                return ff.toURI();
122
            }
123
        }
124
        File ff = new File(pathname);
125
        if( ff.exists() ) {
126
            return ff.toURI();
127
        }
128
        return null;
129
    }
130
    
131
     protected URI getURI(String baseURI, String extension) throws URISyntaxException {
132
        if( StringUtils.isEmpty(baseURI) ) {
133
            return null;
134
        }
135
        URI uri = getBasicURI(baseURI, extension);
136
        if( uri!=null ) {
137
            return uri;
138
        }
139
        String value = baseURI.trim();
140

    
141
        Pattern pattern = Pattern.compile(".*<img[^>]*src=\"([^\"]*)\".*",  Pattern.CASE_INSENSITIVE|Pattern.MULTILINE|Pattern.DOTALL);
142
        Matcher m = pattern.matcher(value);
143
        if( m!=null && m.matches() ) {
144
            String x = m.group(1);
145
            if( !StringUtils.isEmpty(x) ) {
146
                uri = getBasicURI(x, extension);
147
                if( uri!=null ) {
148
                    return uri;
149
                }
150
            }
151
        }
152
        pattern = Pattern.compile(".*<a[^>]*href=\"([^\"]*)\".*", Pattern.CASE_INSENSITIVE|Pattern.MULTILINE|Pattern.DOTALL);
153
        m = pattern.matcher(value);
154
        if( m!=null && m.matches() ) {
155
            String x = m.group(1);
156
            if( !StringUtils.isEmpty(x) ) {
157
                uri = getBasicURI(x, extension);
158
                if( uri!=null ) {
159
                    return uri;
160
                }
161
            }
162
        }
163
        try {
164
            URI createURI = new URI(baseURI);
165
            return createURI;
166
        } catch (Exception e) {
167
            //throw new URISyntaxException(baseURI, "Can't make a valid URI.");
168
            return null;
169
        }
170
    }
171
     
172
    private URL toURL(File f) {
173
        try {
174
            return f.toURI().toURL();
175
        } catch (MalformedURLException ex) {
176
            return null;
177
        }
178
    }
179
    
180
    @Override
181
    public URL getURL() {
182
        return this.url;
183
    }
184

    
185
    @Override
186
    public Object getFromProfile() {
187
        return this.content;
188
 }
189

    
190
    @Override
191
    public String getProfileName() {
192
        return this.profileName;
193
    }
194

    
195
    @Override
196
    public void setURL(URL url) {
197
        this.url = url;
198
    }
199
    
200
}