Statistics
| Revision:

gvsig-raster / org.gvsig.raster.wmts / trunk / org.gvsig.raster.wmts / org.gvsig.raster.wmts.ogc / org.gvsig.raster.wmts.ogc.impl / src / main / java / org / gvsig / raster / wmts / ogc / impl / struct / TemplateSupport.java @ 2613

History | View | Annotate | Download (4.5 KB)

1
package org.gvsig.raster.wmts.ogc.impl.struct;
2

    
3
import java.util.ArrayList;
4
import java.util.List;
5
import java.util.regex.Matcher;
6
import java.util.regex.Pattern;
7

    
8
import org.gvsig.raster.wmts.ogc.WMTSStatus;
9
import org.gvsig.raster.wmts.ogc.impl.base.WMTSStatusImpl;
10
import org.gvsig.raster.wmts.ogc.struct.WMTSDimension;
11
import org.gvsig.raster.wmts.ogc.struct.WMTSResourceURL;
12
import org.gvsig.raster.wmts.ogc.struct.WMTSTile;
13

    
14
/**
15
 * Support for URL templates and parameters substitution
16
 *
17
 * @author Nacho Brodin (nachobrodin@gmail.com)
18
 */
19
public class TemplateSupport {
20
        private List<WMTSDimension>    dimensions   = null;
21
        private List<WMTSResourceURL>  resourcesURL = null;
22
        
23
        public TemplateSupport(List<WMTSDimension> dimensions, List<WMTSResourceURL> resourcesURL) {
24
                this.dimensions = dimensions;
25
                this.resourcesURL = resourcesURL;
26
        }
27
        
28
        public List<WMTSDimension> getDimensions() {
29
                if(dimensions == null)
30
                        dimensions = new ArrayList<WMTSDimension>(); 
31
                return dimensions;
32
        }
33
        
34
        public List<WMTSResourceURL> getResourceURL() {
35
                if(resourcesURL == null)
36
                        resourcesURL = new ArrayList<WMTSResourceURL>();
37
                return resourcesURL;
38
        }
39
        
40
        /**
41
         * Builds the list of URL whether the capabilities file uses templates.
42
         */
43
        public void buildResourceURLListFromTemplate(WMTSStatus status) {
44
                String template = getTemplate(status.getFormat(), status.getStyle());
45
                String dimensionValueToWrite = status.getValueForDimension();
46
                
47
                if(template == null)
48
                        return;
49
                
50
                if(dimensionValueToWrite == null)
51
                        dimensionValueToWrite = getDimensionValueToWriteInTemplate(template);
52
                
53
                replacePatternsInTemplate(template, status, dimensionValueToWrite);
54
        }
55
        
56
        /**
57
         * Gets the dimension which we want to write in the template, replacing the
58
         * dimension identifier. This function is just called if the user has not passed 
59
         * this value.
60
         * @param template
61
         * @return
62
         */
63
        private String getDimensionValueToWriteInTemplate(String template) {
64
                for (int iDimension = 0; iDimension < getDimensions().size(); iDimension++) {
65
                        WMTSDimension dim = getDimensions().get(iDimension);
66
                        String idDim = dim.getIdentifier();
67
                        if(template.toLowerCase().contains("{" + idDim.toLowerCase() + "}")) {
68
                                return dim.getDefaultValue();
69
                        }
70
                }
71
                return null;
72
        }
73
        
74
        /**
75
         * Replaces strings in the template and load each URL in an array in <code>WMTSStatus</code>
76
         * @param template
77
         * @param status
78
         * @param dimensionValue
79
         */
80
        private void replacePatternsInTemplate(String template, WMTSStatus status, String dimensionValue) {
81
                String idDimensionToBeReplaced = getDimensionIDToReplace(template);
82
                template = template.replaceAll("(?i)\\{" + idDimensionToBeReplaced + "\\}", dimensionValue);
83
                template = template.replace("{TileMatrixSet}", status.getTileMatrixSet());
84
                template = template.replace("{TileMatrix}", status.getTileMatrix());
85
                
86
                for (int i = 0; i < status.getTileList().size(); i++) {
87
                        WMTSTile tile = status.getTileList().get(i);
88
                        String url = new String(template);
89
                        url = url.replace("{TileRow}", tile.getRow() + "");
90
                        url = url.replace("{TileCol}", tile.getCol() + "");
91
                        ((WMTSStatusImpl)status).addResourceURL(tile.getRow() + "" + tile.getCol() + "", url);
92
                }
93
        }
94
        
95
        /**
96
         * Gets the dimension identifier to be replaced by the dimension value
97
         * @param template
98
         * @return
99
         */
100
        private String getDimensionIDToReplace(String template) {
101
                for (int iDimension = 0; iDimension < getDimensions().size(); iDimension++) {
102
                        WMTSDimension dim = getDimensions().get(iDimension);
103
                        String idDim = dim.getIdentifier();
104
                        if(template.toLowerCase().contains("{" + idDim.toLowerCase() + "}")) {
105
                                return idDim;
106
                        }
107
                }
108
                return null;
109
        }
110

    
111
        /**
112
         * Return a template which supports the format and the style selected
113
         * @param format
114
         * @param style
115
         * @return
116
         */
117
        private String getTemplate(String format, String style) {
118
                for (int iResourceURL = 0; iResourceURL < getResourceURL().size(); iResourceURL++) {
119
                        String currentFormat = getResourceURL().get(iResourceURL).getFormat();
120
                        if(currentFormat != null && currentFormat.equals(format)) {
121
                                String template = getResourceURL().get(iResourceURL).getTemplate();
122
                                if(isStyleSupported(template, style)) {
123
                                        return template;
124
                                }
125
                        }
126
                }
127
                return null;
128
        }
129
        
130
        /**
131
         * Returns true if the style is supported in the template URL
132
         * @param template
133
         * @param style
134
         * @return
135
         */
136
        private boolean isStyleSupported(String template, String style) {
137
                Pattern p = Pattern.compile("/" + style + "/", Pattern.CASE_INSENSITIVE);
138
                Matcher m = p.matcher(template);
139
                if(m.find())
140
                        return true;
141
                return false;
142
        }
143
        
144
}