Revision 33889

View differences:

tags/v2_0_0_Build_2020/extensions/extWMS/src/org/gvsig/wmc/ExportWebMapContextExtension.java
1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41

  
42
/* CVS MESSAGES:
43
 *
44
 * $Id$
45
 * $Log$
46
 * Revision 1.14  2007-01-08 07:57:34  jaume
47
 * *** empty log message ***
48
 *
49
 * Revision 1.13  2006/09/27 13:28:38  jaume
50
 * *** empty log message ***
51
 *
52
 * Revision 1.12  2006/09/20 10:38:43  jaume
53
 * *** empty log message ***
54
 *
55
 * Revision 1.11  2006/09/20 07:45:21  caballero
56
 * constante registerName
57
 *
58
 * Revision 1.10  2006/09/18 08:28:44  caballero
59
 * cambio de nombre
60
 *
61
 * Revision 1.9  2006/09/15 10:44:24  caballero
62
 * extensibilidad de documentos
63
 *
64
 * Revision 1.8  2006/08/29 07:56:15  cesar
65
 * Rename the *View* family of classes to *Window* (ie: SingletonView to SingletonWindow, ViewInfo to WindowInfo, etc)
66
 *
67
 * Revision 1.7  2006/08/29 07:13:43  cesar
68
 * Rename class com.iver.andami.ui.mdiManager.View to com.iver.andami.ui.mdiManager.IWindow
69
 *
70
 * Revision 1.6  2006/07/21 10:31:05  jaume
71
 * *** empty log message ***
72
 *
73
 * Revision 1.5  2006/06/19 11:20:26  jorpiell
74
 * Cambiadas las comprobaciones de una clase con == por instanceof.
75
 *
76
 * Revision 1.4  2006/05/30 08:56:57  jaume
77
 * fixed bug http://www.gvsig.org/phpcollab/tasks/viewtask.php?id=279&msg=addAssignment&PHPSESSID=3fea791a62107ef5325283641039b311#etDAnchor
78
 *
79
 * Revision 1.3  2006/05/12 07:47:39  jaume
80
 * removed unnecessary imports
81
 *
82
 * Revision 1.2  2006/05/03 11:10:54  jaume
83
 * *** empty log message ***
84
 *
85
 * Revision 1.1  2006/05/03 07:51:21  jaume
86
 * *** empty log message ***
87
 *
88
 * Revision 1.7  2006/05/02 16:12:12  jorpiell
89
 * Se ha cambiado la interfaz Extension por dos clases: una interfaz (IExtension) y una clase abstract(Extension). A partir de ahora todas las extensiones deben heredar de Extension
90
 *
91
 * Revision 1.6  2006/05/02 15:57:44  jaume
92
 * Few better javadoc
93
 *
94
 * Revision 1.5  2006/04/25 11:40:56  jaume
95
 * *** empty log message ***
96
 *
97
 * Revision 1.4  2006/04/21 10:27:32  jaume
98
 * exporting now supported
99
 *
100
 * Revision 1.3  2006/04/20 17:11:54  jaume
101
 * Attempting to export
102
 *
103
 * Revision 1.2  2006/04/19 16:34:29  jaume
104
 * *** empty log message ***
105
 *
106
 * Revision 1.1  2006/04/19 07:57:29  jaume
107
 * *** empty log message ***
108
 *
109
 * Revision 1.3  2006/04/12 17:10:53  jaume
110
 * *** empty log message ***
111
 *
112
 * Revision 1.2  2006/04/07 12:10:37  jaume
113
 * *** empty log message ***
114
 *
115
 * Revision 1.1  2006/04/04 14:22:22  jaume
116
 * Now exports MapContext (not yet tested)
117
 *
118
 *
119
 */
120
package org.gvsig.wmc;
121

  
122
import java.io.BufferedWriter;
123
import java.io.File;
124
import java.io.FileWriter;
125
import java.io.IOException;
126
import java.util.ArrayList;
127
import java.util.List;
128

  
129
import org.gvsig.andami.PluginServices;
130
import org.gvsig.andami.messages.NotificationManager;
131
import org.gvsig.andami.plugins.Extension;
132
import org.gvsig.andami.plugins.IExtension;
133
import org.gvsig.andami.ui.mdiManager.IWindow;
134
import org.gvsig.app.project.Project;
135
import org.gvsig.app.project.ProjectManager;
136
import org.gvsig.app.project.documents.Document;
137
import org.gvsig.app.project.documents.view.DefaultViewDocument;
138
import org.gvsig.app.project.documents.view.ViewManager;
139
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel;
140
import org.gvsig.fmap.mapcontext.layers.FLayer;
141
import org.gvsig.fmap.mapcontext.layers.FLayers;
142
import org.gvsig.wms.fmap.layers.FLyrWMS;
143
import org.gvsig.wms.gui.panels.WebMapContextSettingsPanel;
144

  
145

  
146
/**
147
 * Extension to export a view with WMS layers to a OGC's Web Map Context XML
148
 * file
149
 *
150
 * @author jaume dom?nguez faus - jaume.dominguez@iver.es
151
 * @author laura d?az s?nchez - laura.diaz@iver.es
152
 */
153
public class ExportWebMapContextExtension extends Extension {
154
	private static ArrayList supportedVersions;
155
	private DefaultViewPanel viewToExport;
156
	private WebMapContextSettingsPanel mc;
157
	private static IExtension thisExtension;
158

  
159
	{
160
		supportedVersions = new ArrayList();
161
		supportedVersions.add("1.1.0");
162
	}
163

  
164
	public void initialize() {
165
		thisExtension = PluginServices
166
				.getExtension(ExportWebMapContextExtension.class);
167
	}
168

  
169
	public void execute(String actionCommand) {
170
		if (actionCommand.equals("EXPORT")) {
171
			// Here we collect the info
172
			DefaultViewDocument[] views = getExportableViews();
173
			if (views.length <= 0) {
174
				return;
175
			}
176
			mc = new WebMapContextSettingsPanel(views);
177
			PluginServices.getMDIManager().addWindow(mc);
178

  
179
		} else if (actionCommand.equals("DO_EXPORT")) {
180
			// Here the target file is produced (called from the WebMapContextSettingsPanel)
181
			String xml = mc.getXML();
182
			File f = mc.getTargetFile();
183
			if (xml != null) {
184
				createFile(f, xml);
185
			}
186

  
187
		}
188
	}
189

  
190
	/**
191
	 * Takes a File object and its XML contents and stores it as a regular
192
	 * file in the file system.
193
	 * @param f
194
	 * @param xml
195
	 */
196
	public static void createFile(File f, String xml) {
197
		if (xml != null) {
198
			try {
199
				if (!f.exists()) {
200
					f.createNewFile();
201
				}
202
				BufferedWriter bw = new BufferedWriter(new FileWriter(f));
203
				bw.write(xml);
204
				bw.close();
205
				bw = null;
206
			} catch (IOException e) {
207
				NotificationManager.addError(PluginServices.getText(
208
						thisExtension, "error_writting_file"), e);
209
			}
210
		}
211
	}
212

  
213
	public boolean isEnabled() {
214
		return true;
215
	}
216

  
217
	public boolean isVisible() {
218
		// Will be visible if the current project has, at least, one FLyrWMS.
219
//		Project project = ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject();
220
		final Project project = ProjectManager.getInstance().getCurrentProject();
221

  
222
		if (project == null) {
223
			return false;
224
		}
225
		IWindow f = PluginServices.getMDIManager().getActiveWindow();
226
		if (f instanceof DefaultViewPanel) {
227
			DefaultViewPanel v = (DefaultViewPanel) f;
228
			if (v != null && v  instanceof DefaultViewPanel) {
229
				// Check if the active contains WMS layers. If so, this view
230
				// will be the one to be exported.
231
				FLayers lyrs = v.getMapControl().getMapContext().getLayers();
232
				for (int i = 0; i < lyrs.getLayersCount(); i++) {
233
					FLayer lyr = lyrs.getLayer(i);
234
					if (WebMapContext.containsExportableLayers(lyr)) {
235
						viewToExport = v;
236
						return true;
237
					}
238
				}
239
			}
240
		}
241

  
242
		// Since the active view does not contain WMS layers then will
243
		// see what about the others. In this case, no view is set to be
244
		// the exported one.
245
		viewToExport = null;
246
		List<Document> views = project.getDocuments(ViewManager.TYPENAME);
247
		for (int i = 0; i < views.size(); i++) {
248
			DefaultViewDocument v = ((DefaultViewDocument) views.get(i));
249
			if (v != null) {
250
				FLayers lyrs = v.getMapContext().getLayers();
251
				for (int j = 0; j < lyrs.getLayersCount(); j++) {
252
					FLayer lyr = lyrs.getLayer(j);
253
					if (WebMapContext.containsExportableLayers(lyr)) {
254
						return true;
255
					}
256
				}
257
			}
258
		}
259
		return false;
260
	}
261

  
262
	/**
263
	 * <p>
264
	 * Searches the views in the current project that can be exported to a
265
	 * WebMapContext file (with ".cml" extension) and return them in a
266
	 * ProjectView array.<br>
267
	 * </p>
268
	 * <p>
269
	 * A view is exportable to WebMapContext if it contains at least one FLyrWMS
270
	 * and in a near future, any other OGC layer such as WCS, WFS, and so on. Only
271
	 * these layers will be exported. Other kind of layers are ignored since they
272
	 * are out of the OGC premises.
273
	 * </p>
274
	 * @return
275
	 */
276
	private DefaultViewDocument[] getExportableViews() {
277
//		Project project = ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject();
278
//		ArrayList views = project.getDocumentsByType(ProjectViewFactory.registerName);
279
		final Project project = ProjectManager.getInstance().getCurrentProject();
280

  
281
		List<Document> views = project.getDocuments(ViewManager.TYPENAME);
282

  
283
		List exportableViews = new ArrayList();
284
		if (viewToExport!=null) {
285
			exportableViews.add(viewToExport.getModel());
286
		}
287

  
288
		for (int i = 0; i < views.size(); i++) {
289
			DefaultViewDocument v = ((DefaultViewDocument) views.get(i));
290
			if (v != null) {
291
				FLayers lyrs = v.getMapContext().getLayers();
292
				for (int j = 0; j < lyrs.getLayersCount(); j++) {
293
					FLayer lyr = lyrs.getLayer(j);
294
					if (lyr instanceof FLyrWMS && !exportableViews.contains(v)) {
295
						exportableViews.add(v);
296
						break;
297
					}
298
				}
299
			}
300
		}
301
		return (DefaultViewDocument[]) exportableViews.toArray(new DefaultViewDocument[0]);
302
	}
303

  
304
}
0 305

  
tags/v2_0_0_Build_2020/extensions/extWMS/src/org/gvsig/wmc/WebMapContextTags.java
1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41

  
42
/* CVS MESSAGES:
43
*
44
* $Id$
45
* $Log$
46
* Revision 1.3  2006-05-24 16:36:28  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.2  2006/05/12 07:47:39  jaume
50
* removed unnecessary imports
51
*
52
* Revision 1.1  2006/05/03 07:51:21  jaume
53
* *** empty log message ***
54
*
55
* Revision 1.5  2006/05/02 15:58:20  jaume
56
* Few better javadoc
57
*
58
* Revision 1.4  2006/05/02 15:57:44  jaume
59
* Few better javadoc
60
*
61
* Revision 1.3  2006/04/21 10:27:32  jaume
62
* exporting now supported
63
*
64
* Revision 1.2  2006/04/19 16:34:29  jaume
65
* *** empty log message ***
66
*
67
* Revision 1.1  2006/04/19 07:57:29  jaume
68
* *** empty log message ***
69
*
70
* Revision 1.2  2006/04/12 17:10:53  jaume
71
* *** empty log message ***
72
*
73
* Revision 1.1  2006/04/04 14:22:22  jaume
74
* Now exports MapContext (not yet tested)
75
*
76
*
77
*/
78
package org.gvsig.wmc;
79

  
80
/**
81
 * WebMapContextTags is a class with only static public fields containing the
82
 * String constants of the WebMapContext XML document.
83
 * @author jaume dominguez faus - jaume.dominguez@iver.es
84
 *
85
 */
86
public class WebMapContextTags {
87
	private WebMapContextTags() {};
88
	
89
	public static final String VIEW_CONTEXT = "ViewContext";
90
	public static final String VIEW_CONTEXT_0_1_4 = "WMS_Viewer_Context";
91
	public static final String VERSION = "version";
92
	public static final String ID = "id";
93
	public static final String XMLNS = "xmlns";
94
	public static final String XMLNS_XLINK = "xmlns:xlink";
95
	public static final String XMLNS_XLINK_VALUE = "http://www.w3.org/1999/xlink";
96
	public static final String XMLNS_VALUE = "http://www.opengis.net/context"; // "http://www.opengeospatial.net/context"
97
	public static final String XMLNS_XSI = "xmlns:xsi"; 
98
	public static final String XMLNS_XSI_VALUE = "http://www.w3.org/2001/XMLSchema-instance";
99
	public static final String XSI_SCHEMA_LOCATION = "xsi:schemaLocation";
100
	public static final String XSI_SCHEMA_LOCATION_VALUE = "http://www.opengeospatial.net/context context.xsd";
101
	public static final String GENERAL = "General";
102
	public static final String WIDTH = "width";
103
	public static final String HEIGHT = "height";
104
	public static final String WINDOW = "Window";
105
	public static final String SRS = "SRS";
106
	public static final String X_MIN = "minx";
107
	public static final String Y_MIN = "miny";
108
	public static final String X_MAX = "maxx";
109
	public static final String Y_MAX = "maxy";
110
	public static final String BOUNDING_BOX = "BoundingBox";
111
	public static final String TITLE = "Title";
112
	public static final String LAYER_LIST = "LayerList";
113
	public static final String LAYER = "Layer";
114
	public static final String QUERYABLE = "queryable";
115
	public static final String HIDDEN = "hidden";
116
	public static final String SERVICE = "service";
117
	public static final String WMS = "WMS";
118
	public static final String SERVER_TITLE = "title";
119
	public static final String SERVER = "Server";
120
	public static final String XLINK_TYPE = "xlink:type";
121
	public static final String XLINK_HREF = "xlink:href";
122
	public static final String ONLINE_RESOURCE = "OnlineResource";
123
	public static final String NAME = "Name";
124
	public static final String ABSTRACT = "Abstract";
125
	public static final String FORMAT_LIST = "FormatList";
126
	public static final String CURRENT = "current";
127
	public static final String FORMAT = "Format";
128
	public static final String STYLE_LIST = "StyleList";
129
	public static final String STYLE = "Style";
130
	public static final String KEYWORD_LIST = "KeywordList";
131
	public static final String KEYWORD = "Keyword";
132
	public static final String LOGO_URL = "LogoURL";
133
	public static final String DESCRIPTION_URL = "DescriptionURL";
134
	public static final String CONTACT_INFORMATION = "ContactInformation";
135
	public static final String CONTACT_PERSON_PRIMARY = "ContactPersonPrimary";
136
	public static final String CONTACT_PERSON = "ContactPerson";
137
	public static final String CONTACT_ORGANIZATION = "ContactOrganization";
138
	public static final String CONTACT_POSITION = "ContactPosition";
139
	public static final String CONTACT_ADDRESS = "ContactAddress";
140
	public static final String ADDRESS_TYPE = "AddressType";
141
	public static final String ADDRESS = "Address";
142
	public static final String CITY = "City";
143
	public static final String STATE_OR_PROVINCE = "StateOrProvince";
144
	public static final String POSTCODE = "PostCode";
145
	public static final String COUNTRY = "Country";
146
	public static final String CONTACT_VOICE_TELEPHONE = "ContactVoiceTelephone";
147
	public static final String CONTACT_FACSIMILE_TELEPHONE = "ContactFacsimileTelephone";
148
	public static final String CONTACT_ELECTRONIC_MAIL_ADDRESS = "ContactElectronicMailAddress";
149
	public static final String DIMENSION_LIST = "DimensionList";
150
	public static final String LEGEND_URL = "LegendURL";
151
	
152
}
0 153

  
tags/v2_0_0_Build_2020/extensions/extWMS/src/org/gvsig/wmc/WebMapContext.java
1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41

  
42
/* CVS MESSAGES:
43
*
44
* $Id$
45
* $Log$
46
* Revision 1.9  2007-03-06 17:06:43  caballero
47
* Exceptions
48
*
49
* Revision 1.8  2007/01/08 07:57:34  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.7  2006/09/26 14:41:48  jaume
53
* *** empty log message ***
54
*
55
* Revision 1.6  2006/09/25 10:23:26  caballero
56
* Projection
57
*
58
* Revision 1.5  2006/09/15 10:44:24  caballero
59
* extensibilidad de documentos
60
*
61
* Revision 1.4  2006/07/21 10:31:05  jaume
62
* *** empty log message ***
63
*
64
* Revision 1.3  2006/05/24 16:36:28  jaume
65
* *** empty log message ***
66
*
67
* Revision 1.2  2006/05/12 07:47:39  jaume
68
* removed unnecessary imports
69
*
70
* Revision 1.1  2006/05/03 07:51:21  jaume
71
* *** empty log message ***
72
*
73
* Revision 1.10  2006/05/02 15:57:44  jaume
74
* Few better javadoc
75
*
76
* Revision 1.9  2006/04/25 11:40:56  jaume
77
* *** empty log message ***
78
*
79
* Revision 1.8  2006/04/25 10:01:56  jaume
80
* fixed extend bug when importing
81
*
82
* Revision 1.7  2006/04/21 12:46:53  jaume
83
* *** empty log message ***
84
*
85
* Revision 1.6  2006/04/21 11:53:50  jaume
86
* *** empty log message ***
87
*
88
* Revision 1.5  2006/04/21 11:02:25  jaume
89
* few translations
90
*
91
* Revision 1.4  2006/04/21 10:27:32  jaume
92
* exporting now supported
93
*
94
* Revision 1.3  2006/04/20 17:11:54  jaume
95
* Attempting to export
96
*
97
* Revision 1.2  2006/04/19 16:34:29  jaume
98
* *** empty log message ***
99
*
100
* Revision 1.1  2006/04/19 07:57:29  jaume
101
* *** empty log message ***
102
*
103
* Revision 1.1  2006/04/12 17:10:53  jaume
104
* *** empty log message ***
105
*
106
*
107
*/
108
package org.gvsig.wmc;
109

  
110
import java.awt.Dimension;
111
import java.awt.geom.Rectangle2D;
112
import java.io.BufferedReader;
113
import java.io.File;
114
import java.io.FileInputStream;
115
import java.io.FileNotFoundException;
116
import java.io.FileReader;
117
import java.io.IOException;
118
import java.net.URL;
119
import java.util.ArrayList;
120
import java.util.HashMap;
121
import java.util.HashSet;
122
import java.util.Hashtable;
123
import java.util.Vector;
124

  
125
import org.cresques.cts.ICoordTrans;
126
import org.cresques.cts.IProjection;
127
import org.gvsig.andami.PluginServices;
128
import org.gvsig.andami.messages.NotificationManager;
129
import org.gvsig.app.ApplicationLocator;
130
import org.gvsig.app.project.documents.view.DefaultViewDocument;
131
import org.gvsig.fmap.crs.CRSFactory;
132
import org.gvsig.fmap.mapcontext.exceptions.UnsupportedVersionLayerException;
133
import org.gvsig.fmap.mapcontext.layers.FLayer;
134
import org.gvsig.fmap.mapcontext.layers.FLayers;
135
import org.gvsig.wms.fmap.exceptions.ImportMapContextException;
136
import org.gvsig.wms.fmap.layers.FLyrWMS;
137
import org.gvsig.wms.fmap.layers.WMSLayerNode;
138
import org.gvsig.wms.fmap.layers.WMSLayerNode.FMapWMSStyle;
139
import org.kxml2.io.KXmlParser;
140
import org.xmlpull.v1.XmlPullParserException;
141

  
142

  
143
/**
144
 * Class that loads and produces WebMapContext files and holds its attributes.
145
 *
146
 * You can create a blank WebMapContext instance using the default constructor
147
 * or an already initialized instance by specifying a source file. In the last
148
 * case the file will be readed and the values loaded an holded by the instance.
149
 *
150
 * You can get the equivalent WebMapContext XML document by calling the method
151
 * getXML() supplying the ProjectView that you want to export and depending on
152
 * the value of the fileVersion field (currently you can use "1.1.0" (default),
153
 * "1.0.0" or "0.4.1" to specify the destination document version.
154
 *
155
 * @author jaume dominguez faus - jaume.dominguez@iver.es
156
 */
157
public class WebMapContext {
158
	public final static String FILE_EXTENSION = ".cml";
159

  
160
	static final ArrayList supportedVersions = new ArrayList();
161
	static public final ArrayList exportVersions    = new ArrayList();
162
	static private HashSet supportedLayers = new HashSet();
163
	private File mapContextFile;
164
	private String encoding = "UTF-8";
165
	private String WMC_START_TAG;
166

  
167
	// MapContext stuff
168
	public String fileVersion = null;
169
	public Dimension windowSize = null;
170
	public String srs = null;
171
	public Rectangle2D bBox = null;
172
	public String title = null;
173
	public String id = null;
174
	public String xmlns = null;
175
	public String xmlns_xlink = null;
176
	public String xmlns_xsi = null;;
177
	public String xsi_schemaLocation = null;
178
	public String _abstract = null;
179
	public ArrayList keywordList = null;
180
	public Dimension logoURLSize = null;
181
	public String logoURLFormat = null;
182
	public String logoURL = null;
183
	public String descriptionURLFormat = null;
184
	public String descriptionURL = null;
185
	public boolean contactInfo = false;
186
	public String contactPerson = null;
187
	public String contactOrganization = null;
188
	public String contactPosition = null;
189
	public String address = null;
190
	public String city = null;
191
	public String stateOrProvince = null;
192
	public String postCode = null;
193
	public String country = null;
194
	public String telephone = null;
195
	public String fax = null;
196
	public String email = null;
197

  
198
	private StringBuffer errorMessages;
199

  
200
	/**
201
	 * list of FLyrWMS.
202
	 */
203
	ArrayList<FLyrWMS> layerList = null;
204

  
205
	/**
206
	 * key: server URL (URL)
207
	 * value: server title (String)
208
	 */
209
	Hashtable serverTitles = null;
210

  
211
	/**
212
	 * key: layer FLyrWMS
213
	 * value: layer abstract (String)
214
	 */
215
	Hashtable layerAbstracts = null;
216

  
217
	/**
218
	 * key: layer FLyrWMS
219
	 * value: layer formats (String[])
220
	 */
221
	Hashtable layerFormats = null;
222

  
223
	/**
224
	 * key: layer FLyrWMS
225
	 * value: styles (FMapWMSStyle[])
226
	 */
227
	Hashtable layerStyles = null;
228

  
229

  
230
	static {
231
		supportedVersions.add("1.1.0");
232
		supportedVersions.add("1.0.0");
233
		supportedVersions.add("0.1.4");
234
	}
235

  
236
	static {
237
		exportVersions.add("1.1.0");
238
		exportVersions.add("1.0.0");
239
		exportVersions.add("0.1.4");
240
	}
241

  
242
	static {
243
		supportedLayers.add(FLyrWMS.class);
244
	}
245

  
246

  
247
	/**
248
	 * Initializes the WebMapContext properties from the values in the WebMapContext (.cml)
249
	 * file passed in the argument.
250
	 * @param file
251
	 * @throws UnsupportedVersionLayerException
252
	 * @throws ImportMapContextException
253
	 */
254
	public void readFile(File file) throws UnsupportedVersionLayerException, ImportMapContextException {
255
		this.mapContextFile = file;
256
		errorMessages = new StringBuffer();
257
		if (getVersion()!=null) {
258
			if (supportedVersions.contains(getVersion())) {
259
				WMC_START_TAG = WebMapContextTags.VIEW_CONTEXT;
260
				if (getVersion().equals("0.1.4")) {
261
					WMC_START_TAG = WebMapContextTags.VIEW_CONTEXT_0_1_4;
262
					parse1_1_0(file);
263
				} else if (getVersion().equals("1.1.0")) {
264
					parse1_1_0(file);
265
				} else if (getVersion().equals("1.0.0")) {
266
					parse1_1_0(file);
267
				} else {
268
					parseDefaultVersion(file);
269
				}
270

  
271
				// Once parsed, check errors
272
				if (errorMessages.length()>0) {
273
					throw new ImportMapContextException(errorMessages.toString(), false);
274
				}
275
			} else {
276
				throw new UnsupportedVersionLayerException(file.getName(),null);
277
			}
278
		}
279
	}
280

  
281

  
282
	/**
283
	 * If no version was recognized then will parse the default one which is supposed
284
	 * to be the best available.
285
	 * @param file
286
	 * @throws ImportMapContextException
287
	 */
288
	private void parseDefaultVersion(File file) throws ImportMapContextException {
289
		parse1_1_0(file);
290
	}
291

  
292
	/**
293
	 * Reads the header of the Weg Map Context file and determines which version
294
	 * it belongs to.
295
	 * @return String.
296
	 */
297
	private String getVersion() {
298
		if (fileVersion == null) {
299
			String v = null;
300
			try {
301
				FileReader fr = new FileReader(mapContextFile);
302
				KXmlParser parser = new KXmlParser();
303
				parser.setInput(fr);
304
				parser.nextTag();
305
	    		if ( parser.getEventType() != KXmlParser.END_DOCUMENT )	{
306
	    			if ((parser.getName().compareTo(WebMapContextTags.VIEW_CONTEXT) == 0) ||
307
	    				 parser.getName().compareTo(WebMapContextTags.VIEW_CONTEXT_0_1_4) == 0) {
308
	    				v = parser.getAttributeValue("", WebMapContextTags.VERSION);
309
	    			}
310
	    		}
311
			} catch (FileNotFoundException fnfEx) {
312
			} catch (XmlPullParserException xmlEx) {
313
				xmlEx.printStackTrace();
314
			} catch (IOException e) {
315
			}
316
			fileVersion = v;
317
		}
318
		return fileVersion;
319
	}
320

  
321
	/**
322
	 * Reads a Web Map Context version 1.1.0. As far as v1.0.0 is a subset of
323
	 * v1.1.0 it can be used to read files belonging to 1.0.0 as well.
324
	 * @param file, the web map context file.
325
	 */
326
	private void parse1_1_0(File file) throws ImportMapContextException{
327
		try {
328
			FileReader fr;
329
			try
330
	    	{
331
				fr = new FileReader(file);
332
	    		BufferedReader br = new BufferedReader(fr);
333
	    		char[] buffer = new char[100];
334
	    		br.read(buffer);
335
	    		StringBuffer st = new StringBuffer(new String(buffer));
336
	    		String searchText = "encoding=\"";
337
	    		int index = st.indexOf(searchText);
338
	    		if (index>-1) {
339
	    			st.delete(0, index+searchText.length());
340
	    			encoding = st.substring(0, st.indexOf("\""));
341
	    		}
342
	    	} catch(FileNotFoundException ex)	{
343
	    		ex.printStackTrace();
344
	    	} catch (IOException e) {
345
				e.printStackTrace();
346
			}
347

  
348
			fr = new FileReader(file);
349
			KXmlParser parser = new KXmlParser();
350
			parser.setInput(new FileInputStream(file), encoding);
351
			int tag = parser.nextTag();
352
			if ( parser.getEventType() != KXmlParser.END_DOCUMENT )
353
			{
354
				parser.require(KXmlParser.START_TAG, null, WMC_START_TAG);
355
				while(tag != KXmlParser.END_DOCUMENT) {
356
					switch(tag) {
357
						case KXmlParser.START_TAG:
358
							if (parser.getName().compareTo(WMC_START_TAG) == 0) {
359
								id = parser.getAttributeValue("", WebMapContextTags.ID);
360
								xmlns = parser.getAttributeValue("", WebMapContextTags.XMLNS);
361
								xmlns_xlink = parser.getAttributeValue("", WebMapContextTags.XMLNS_XLINK);
362
								xmlns_xsi = parser.getAttributeValue("", WebMapContextTags.XMLNS_XSI);
363
								xsi_schemaLocation = parser.getAttributeValue("", WebMapContextTags.XSI_SCHEMA_LOCATION) ;
364
							} else if (parser.getName().compareTo(WebMapContextTags.GENERAL) == 0) {
365
								parseGeneral1_1_0(parser);
366
							} else if (parser.getName().compareTo(WebMapContextTags.LAYER_LIST) == 0) {
367
								int layerListTag = parser.nextTag();
368
								boolean bLayerListEnd = false;
369
								layerList = new ArrayList();
370
								while (!bLayerListEnd) {
371
									switch(layerListTag) {
372
									case KXmlParser.START_TAG:
373
										if (parser.getName().compareTo(WebMapContextTags.LAYER) == 0) {
374
											FLyrWMS layer = parseLayer1_1_0(parser);
375
											// will use the mapcontext's bounding box as layer's fullextent
376
											IProjection proj =  CRSFactory.getCRS(srs);
377
											if (proj == null) {
378
												// non supported srs, and cannot continue
379
												String msg = PluginServices.getText(this, "unsupported_crs") +
380
															 " (" + srs + ")";
381
												throw new ImportMapContextException(msg, true);
382
											}
383
											String[] availableSRS = layer.getSRS().split(",");
384
											ICoordTrans ct = null;
385
											String mySRS = null;
386
											for (int i = 0; i < availableSRS.length; i++) {
387
												mySRS = availableSRS[i];
388
												IProjection dstProj = CRSFactory.getCRS(mySRS);
389
												if (dstProj != null) {
390
													try{
391
															ct = proj.getCT(dstProj);
392
														}catch(Exception e){
393
															NotificationManager.showMessageError(e.getLocalizedMessage(), e);
394
														}
395
													if (mySRS.equals(srs)) {
396
														// this is the most suitable SRS,
397
														break;
398
													}
399
												}
400
											}
401

  
402
											if (ct != null) {
403
												// I've found a supported projection
404
												layer.setFullExtent(ct.convert(bBox));
405
												layer.setSRS(mySRS);
406
											} else {
407
												// can't reproject
408
												errorMessages.append("[").
409
														      append(PluginServices.getText(this, "layer")).
410
														      append(" ").
411
														      append(layer.getName()).
412
														      append("] ").
413
															  append(PluginServices.getText(this, "cant_reproject_from_any_of")).
414
															  append(" [").append(layer.getSRS()).append("] ").
415
															  append(PluginServices.getText(this, "to")).
416
															  append(srs).
417
															  append("\n");
418

  
419
												/*
420
												 * will use view's projection and bounding box
421
												 * strictly this is incorrect but, at least it allows
422
												 * the user to recover by changin layer properties.
423
												 */
424
												layer.setFullExtent(bBox);
425
												layer.setSRS(srs);
426
											}
427

  
428
											layer.setWmsTransparency(true);
429
											layerList.add(layer);
430
										} else {
431
					                    	System.out.println("Unrecognized "+parser.getName());
432
										}
433
										break;
434
									case KXmlParser.END_TAG:
435
										if (parser.getName().compareTo(WebMapContextTags.LAYER_LIST) == 0) {
436
											bLayerListEnd = true;
437
										}
438
										break;
439
									case KXmlParser.TEXT:
440
										//System.out.println("[TEXT]["+kxmlParser.getText()+"]");
441
										break;
442
									}
443
									layerListTag = parser.next();
444
								}
445
							} else if (parser.getName().compareTo(WebMapContextTags.DIMENSION_LIST) == 0) {
446
								// TODO
447
								System.out.println("WebMapContext's Dimension not yet implemented");
448
							} else {
449
		                    	System.out.println("Unrecognized "+parser.getName());
450
							}
451
							break;
452
						case KXmlParser.END_TAG:
453
							break;
454
						case KXmlParser.TEXT:
455
							//System.out.println("[TEXT]["+kxmlParser.getText()+"]");
456
							break;
457
					}
458
					tag = parser.next();
459
				}
460
				parser.require(KXmlParser.END_DOCUMENT, null, null);
461
			}
462
		} catch (ImportMapContextException e){
463
			throw e;
464
		} catch (Exception e) {
465
			NotificationManager.addError(PluginServices.getText(this, "map_context_file_error"),e);
466
			e.printStackTrace();
467
		}
468

  
469
	}
470

  
471
	/**
472
	 * Parses the General tag of a web map context 1.1.0 (and 1.0.0) file
473
	 * @param parser
474
	 * @throws XmlPullParserException
475
	 * @throws IOException
476
	 */
477
	private void parseGeneral1_1_0(KXmlParser parser) throws XmlPullParserException, IOException {
478
		boolean end = false;
479
    	int tag = parser.next();
480
    	while (!end) {
481
    		switch(tag) {
482
        		case KXmlParser.START_TAG:
483
        			if (parser.getName().compareTo(WebMapContextTags.WINDOW) == 0) {
484
        				if (windowSize == null) {
485
							windowSize = new Dimension();
486
						}
487
						windowSize.setSize(Integer.parseInt(parser.getAttributeValue("", WebMapContextTags.WIDTH)),
488
										   Integer.parseInt(parser.getAttributeValue("", WebMapContextTags.HEIGHT)));
489
        			} else if (parser.getName().compareTo(WebMapContextTags.BOUNDING_BOX) == 0) {
490
        				srs = parser.getAttributeValue("", WebMapContextTags.SRS);
491
        				double minx = Double.parseDouble(parser.getAttributeValue("", WebMapContextTags.X_MIN));
492
        				double miny = Double.parseDouble(parser.getAttributeValue("", WebMapContextTags.Y_MIN));
493
        				double maxx = Double.parseDouble(parser.getAttributeValue("", WebMapContextTags.X_MAX));
494
        				double maxy = Double.parseDouble(parser.getAttributeValue("", WebMapContextTags.Y_MAX));
495
        				bBox = new Rectangle2D.Double(minx, miny, maxx-minx, maxy-miny);
496
            		} else if (parser.getName().compareTo(WebMapContextTags.TITLE) == 0) {
497
        				title = parser.nextText();
498
        			} else if (parser.getName().compareTo(WebMapContextTags.ABSTRACT) == 0) {
499
        				_abstract = parser.nextText();
500
        			} else if (parser.getName().compareTo(WebMapContextTags.KEYWORD_LIST) == 0) {
501
        				keywordList = new ArrayList();
502
        				boolean keywordEnd = false;
503
        		    	int keywordTag = parser.next();
504
        		    	while (!keywordEnd) {
505
        		    		switch(keywordTag) {
506
        		        		case KXmlParser.START_TAG:
507
        		        			if (parser.getName().compareTo(WebMapContextTags.KEYWORD) == 0) {
508
        		        				keywordList.add(parser.nextText());
509
        		        			} else {
510
        		                    	System.out.println("Unrecognized "+parser.getName());
511
        							}
512
        							break;
513
        		         		case KXmlParser.END_TAG:
514
        		        			if (parser.getName().compareTo(WebMapContextTags.KEYWORD_LIST) == 0) {
515
										keywordEnd = true;
516
									}
517
        		        			break;
518
        		        		case KXmlParser.TEXT:
519
        		        			if (parser.getName()!=null) {
520
										System.out.println("[TAG]["+parser.getName()+"]\n[TEXT]["+parser.getText().trim()+"]");
521
									}
522
        		        			break;
523
        		    		}
524
        		    		keywordTag = parser.next();
525
        		    	}
526
        			} else if (parser.getName().compareTo(WebMapContextTags.LOGO_URL) == 0) {
527
        				logoURLSize = new Dimension(Integer.parseInt(parser.getAttributeValue("", WebMapContextTags.WIDTH)),
528
        											Integer.parseInt(parser.getAttributeValue("", WebMapContextTags.HEIGHT)));
529
        				logoURLFormat = parser.getAttributeValue("", WebMapContextTags.FORMAT.toLowerCase());
530
        				parser.nextTag();
531
        				if (parser.getName().compareTo(WebMapContextTags.ONLINE_RESOURCE) == 0) {
532
        					logoURL = parser.getAttributeValue("", WebMapContextTags.XLINK_HREF);
533
        				} else {
534
                        	System.out.println("Unrecognized "+parser.getName());
535
    					}
536
        			} else if (parser.getName().compareTo(WebMapContextTags.DESCRIPTION_URL) == 0) {
537
        				descriptionURLFormat = parser.getAttributeValue("", WebMapContextTags.FORMAT.toLowerCase());
538
        				parser.nextTag();
539
        				if (parser.getName().compareTo(WebMapContextTags.ONLINE_RESOURCE) == 0) {
540
        					descriptionURL = parser.getAttributeValue("", WebMapContextTags.XLINK_HREF);
541
        				} else {
542
                        	System.out.println("Unrecognized "+parser.getName());
543
    					}
544
        			} else if (parser.getName().compareTo(WebMapContextTags.CONTACT_INFORMATION) == 0) {
545
        				boolean contactInfoEnd = false;
546
        		    	int contactInfoTag = parser.next();
547
        		    	while (!contactInfoEnd) {
548
        		    		switch(contactInfoTag) {
549
        		        		case KXmlParser.START_TAG:
550
        		        			if (parser.getName().compareTo(WebMapContextTags.CONTACT_PERSON) == 0) {
551
        		        				contactPerson = parser.nextText();
552
        		        			} else if (parser.getName().compareTo(WebMapContextTags.CONTACT_ORGANIZATION) == 0) {
553
        		        				contactOrganization = parser.nextText();
554
        		        			} else if (parser.getName().compareTo(WebMapContextTags.CONTACT_POSITION) == 0) {
555
        		        				contactPosition = parser.nextText();
556
        		        			} else if (parser.getName().compareTo(WebMapContextTags.ADDRESS) == 0) {
557
        		        				address = parser.nextText();
558
        		        			} else if (parser.getName().compareTo(WebMapContextTags.CITY) == 0) {
559
        		        				city = parser.nextText();
560
        		        			} else if (parser.getName().compareTo(WebMapContextTags.STATE_OR_PROVINCE) == 0) {
561
        		        				stateOrProvince = parser.nextText();
562
        		        			} else if (parser.getName().compareTo(WebMapContextTags.POSTCODE) == 0) {
563
        		        				postCode = parser.nextText();
564
        		        			} else if (parser.getName().compareTo(WebMapContextTags.COUNTRY) == 0) {
565
        		        				country = parser.nextText();
566
        		        			} else if (parser.getName().compareTo(WebMapContextTags.CONTACT_VOICE_TELEPHONE) == 0) {
567
        		        				telephone = parser.nextText();
568
        		        			} else if (parser.getName().compareTo(WebMapContextTags.CONTACT_FACSIMILE_TELEPHONE) == 0) {
569
        		        				fax = parser.nextText();
570
        		        			} else if (parser.getName().compareTo(WebMapContextTags.CONTACT_ELECTRONIC_MAIL_ADDRESS) == 0) {
571
        		        				email = parser.nextText();
572
        		        			} else if (parser.getName().compareTo(WebMapContextTags.CONTACT_PERSON_PRIMARY) == 0) {
573
        		        				// DO NOTHING
574
        		        			} else if (parser.getName().compareTo(WebMapContextTags.CONTACT_ADDRESS) == 0) {
575
        		        				// DO NOTHING
576
        		        			} else {
577
        		                    	System.out.println("Unrecognized "+parser.getName());
578
        							}
579
        		        			break;
580
        		         		case KXmlParser.END_TAG:
581
        		        			if (parser.getName().compareTo(WebMapContextTags.CONTACT_INFORMATION) == 0) {
582
										contactInfoEnd = true;
583
									}
584
        		        			break;
585
        		        		case KXmlParser.TEXT:
586
        		        			if (parser.getName()!=null) {
587
										System.out.println("[TAG]["+parser.getName()+"]\n[TEXT]["+parser.getText().trim()+"]");
588
									}
589
        		        			break;
590
        		    		}
591
        		    		contactInfoTag = parser.next();
592
        		    	}
593
        		    } else {
594
                    	System.out.println("Unrecognized "+parser.getName());
595
					}
596
					break;
597
         		case KXmlParser.END_TAG:
598
        			if (parser.getName().compareTo(WebMapContextTags.GENERAL) == 0) {
599
						end = true;
600
					}
601
        			break;
602
        		case KXmlParser.TEXT:
603
        			if (parser.getName()!=null) {
604
						System.out.println("[TAG]["+parser.getName()+"]\n[TEXT]["+parser.getText().trim()+"]");
605
					}
606
        			break;
607
    		}
608
    		tag = parser.next();
609
    	}
610
	}
611

  
612
	private FLyrWMS parseLayer1_1_0(KXmlParser parser) throws XmlPullParserException, IOException {
613
		boolean end = false;
614
		FLyrWMS layer = new FLyrWMS();
615

  
616
		String queryable = parser.getAttributeValue("", WebMapContextTags.QUERYABLE);
617
		layer.setQueryable(queryable!=null && (queryable.equals("1") || queryable.toLowerCase().equals("true")));
618

  
619
		String hidden = parser.getAttributeValue("", WebMapContextTags.HIDDEN);
620
		layer.setVisible(hidden==null || !hidden.equals("1") || !hidden.toLowerCase().equals("true"));
621
    	int tag = parser.next();
622
    	while (!end) {
623
    		switch(tag) {
624
        		case KXmlParser.START_TAG:
625
        			if (parser.getName().compareTo(WebMapContextTags.SERVER) == 0) {
626
        				String serverTitle = parser.getAttributeValue("", WebMapContextTags.TITLE.toLowerCase());
627
        				parser.nextTag();
628
        				if (parser.getName().compareTo(WebMapContextTags.ONLINE_RESOURCE) == 0) {
629
        					layer.setHost(new URL(parser.getAttributeValue("", WebMapContextTags.XLINK_HREF)));
630
        					if (serverTitles == null) {
631
								serverTitles = new Hashtable();
632
							}
633
            				serverTitles.put(parser.getAttributeValue("", WebMapContextTags.XLINK_HREF), serverTitle);
634
        				}
635
        			} else if (parser.getName().compareTo(WebMapContextTags.TITLE) == 0) {
636
        				layer.setName(parser.nextText());
637
        			} else if (parser.getName().compareTo(WebMapContextTags.NAME) == 0) {
638
        				layer.setLayerQuery(parser.nextText());
639
        			/* //TODO This case would handle nested layer definitions.
640
        			 *
641
        			 } else if (parser.getName().compareTo(WebMapContextTags.LAYER) == 0) {
642
        				FLyrWMS sonLayer = parseLayer1_1_0(parser);
643
        				String q = layer.getLayerQuery();
644
        				if (q == null) q = "";
645
        				else q += ",";
646
        				layer.setLayerQuery( q + sonLayer.getLayerQuery());
647
        			 *
648
        			 */
649
        			} else if (parser.getName().compareTo(WebMapContextTags.ABSTRACT) == 0) {
650
        				if (layerAbstracts == null) {
651
							layerAbstracts = new Hashtable();
652
						}
653
        				layerAbstracts.put(layer, parser.nextText());
654
        			} else if (parser.getName().compareTo(WebMapContextTags.SRS) == 0) {
655
        				layer.setSRS(parser.nextText());
656
        			} else if (parser.getName().compareTo(WebMapContextTags.FORMAT_LIST) == 0) {
657
        				int formatsTag = parser.nextTag();
658
        				boolean bFormatsEnd = false;
659
        				ArrayList formats = new ArrayList();
660
        				while (!bFormatsEnd) {
661
        					switch (formatsTag) {
662
        					case KXmlParser.START_TAG:
663
        						if (parser.getName().compareTo(WebMapContextTags.FORMAT) == 0) {
664
        							String current = parser.getAttributeValue("", WebMapContextTags.CURRENT);
665
        							String format = parser.nextText();
666
        							if (current!=null && current.equals("1")) {
667
										layer.setFormat(format);
668
									}
669
        							formats.add(format);
670
        						} else {
671
        							System.out.println("Unrecognized "+parser.getName());
672
        						}
673
        						break;
674
        					case KXmlParser.END_TAG:
675
        						if (parser.getName().compareTo(WebMapContextTags.FORMAT_LIST) == 0) {
676
									bFormatsEnd = true;
677
								}
678
        						break;
679
        					case KXmlParser.TEXT:
680
        						if (parser.getName()!=null) {
681
									System.out.println("[TAG]["+parser.getName()+"]\n[TEXT]["+parser.getText().trim()+"]");
682
								}
683
        						break;
684
        					}
685
        					formatsTag = parser.next();
686
        				}
687
        				if (layerFormats == null) {
688
							layerFormats = new Hashtable();
689
						}
690
        				layerFormats.put(layer, formats.toArray(new String[0]));
691

  
692
        			} else if (parser.getName().compareTo(WebMapContextTags.STYLE_LIST) == 0) {
693
        				int stylesTag = parser.nextTag();
694
        				boolean bStylesEnd = false;
695
        				ArrayList styles = new ArrayList();
696
        				while (!bStylesEnd) {
697
        					switch (stylesTag) {
698
        					case KXmlParser.START_TAG:
699
        						if (parser.getName().compareTo(WebMapContextTags.STYLE) == 0) {
700
        							String current = parser.getAttributeValue("", WebMapContextTags.CURRENT);
701
        							FMapWMSStyle style = parseStyle1_1_0(parser);
702

  
703
        							if (current!=null && current.equals("1") && !style.name.equals("default")) {
704
        								Vector v = new Vector();
705
        								v.add(style.name);
706
        								layer.setStyles(v);
707
        							}
708
        							styles.add(style);
709
        						} else {
710
        							System.out.println("Unrecognized "+parser.getName());
711
        						}
712
        						break;
713
        					case KXmlParser.END_TAG:
714
        						if (parser.getName().compareTo(WebMapContextTags.STYLE_LIST) == 0) {
715
									bStylesEnd = true;
716
								}
717
        						break;
718
        					case KXmlParser.TEXT:
719
        						if (parser.getName()!=null) {
720
									System.out.println("[TAG]["+parser.getName()+"]\n[TEXT]["+parser.getText().trim()+"]");
721
								}
722
        						break;
723
        					}
724
        					stylesTag = parser.next();
725
        				}
726
        				if (layerStyles == null) {
727
							layerStyles = new Hashtable();
728
						}
729

  
730
        				layerStyles.put(layer, styles.toArray(new FMapWMSStyle[0]));
731
        			} else {
732
        				System.out.println("Unrecognized "+parser.getName());
733
        			}
734
        			break;
735
        		case KXmlParser.END_TAG:
736
        			if (parser.getName().compareTo(WebMapContextTags.LAYER) == 0) {
737
						end = true;
738
					}
739
        			break;
740
        		case KXmlParser.TEXT:
741
        			if (parser.getName()!=null) {
742
						System.out.println("[TAG]["+parser.getName()+"]\n[TEXT]["+parser.getText().trim()+"]");
743
					}
744
        			break;
745
    		}
746
    		tag = parser.next();
747
    	}
748
    	return layer;
749
	}
750

  
751
	private FMapWMSStyle parseStyle1_1_0(KXmlParser parser) throws XmlPullParserException, IOException {
752
		boolean end = false;
753
		String styleName = null;
754
		String styleAbstract = null;
755
		String styleTitle = null;
756
		int legendWidth = -1;
757
		int legendHeight = -1;
758
		String legendType = null;
759
		String legendHref = null;
760

  
761
    	int tag = parser.next();
762
    	while (!end) {
763
    		switch(tag) {
764
        		case KXmlParser.START_TAG:
765
        			if (parser.getName().compareTo(WebMapContextTags.NAME) == 0) {
766
        				styleName = parser.nextText();
767
        			} else if (parser.getName().compareTo(WebMapContextTags.ABSTRACT) == 0) {
768
        				styleAbstract = parser.nextText();
769
        			} else if (parser.getName().compareTo(WebMapContextTags.TITLE) == 0) {
770
        				styleTitle = parser.nextText();
771
        			} else if (parser.getName().compareTo(WebMapContextTags.LEGEND_URL) == 0){
772
        				legendWidth = Integer.parseInt(parser.getAttributeValue("", WebMapContextTags.WIDTH));
773
        				legendHeight = Integer.parseInt(parser.getAttributeValue("", WebMapContextTags.HEIGHT));
774
        				parser.nextTag();
775
        				if (parser.getName().compareTo(WebMapContextTags.ONLINE_RESOURCE) == 0 ) {
776
							legendType = parser.getAttributeValue("", WebMapContextTags.XLINK_TYPE);
777
							legendHref = parser.getAttributeValue("", WebMapContextTags.XLINK_HREF);
778
        				}
779
        			} else {
780
                    	System.out.println("Unrecognized "+parser.getName());
781
					}
782
        			break;
783
         		case KXmlParser.END_TAG:
784
        			if (parser.getName().compareTo(WebMapContextTags.STYLE) == 0) {
785
						end = true;
786
					}
787
        			break;
788
        		case KXmlParser.TEXT:
789
        			if (parser.getName()!=null) {
790
						System.out.println("[TAG]["+parser.getName()+"]\n[TEXT]["+parser.getText().trim()+"]");
791
					}
792
        			break;
793
    		}
794
    		tag = parser.next();
795
    	}
796
    	WMSLayerNode n = new WMSLayerNode();
797
    	FMapWMSStyle sty = n.new FMapWMSStyle();
798
    	sty.name = styleName;
799
    	sty.title = styleTitle;
800
    	sty.styleAbstract = styleAbstract;
801
    	sty.legendWidth = legendWidth;
802
    	sty.legendHeight = legendHeight;
803
    	sty.type = legendType;
804
    	sty.href = legendHref;
805
    	sty.parent = null;
806
    	return sty;
807
	}
808

  
809
	/**
810
	 * Creates the Web Map Context (WMC) XML according on the version 1.1.0. Since Web
811
	 * WMC 1.0.0 is a subset of WMC 1.1.0 and this method does not produce tags for
812
	 * unset values, it can be used to produce WMC 1.0.0, or even to produce
813
	 * the deprecated (but still alive) WMC 0.1.4.
814
	 * @param v
815
	 * @return
816
	 */
817
	@SuppressWarnings("unchecked")
818
	private String createMapContext1_1_0(DefaultViewDocument v) {
819
		ArrayList layersToExport = getExportableLayers(v);
820

  
821
		XmlBuilder xml = new XmlBuilder();
822
		HashMap xmlAttrs = new HashMap();
823
		xml.setEncoding("ISO-8859-1"); // TODO make it customizable???
824
		xml.writeHeader();
825

  
826
		// <ViewContext>
827
		String viewContextTag;
828
		if (fileVersion.equals("0.1.4")) {
829
			viewContextTag = WebMapContextTags.VIEW_CONTEXT_0_1_4;
830
		} else {
831
			viewContextTag = WebMapContextTags.VIEW_CONTEXT;
832
		}
833

  
834
		xml.writeRaw("<!-- "+PluginServices.getText(this, "created_with")+" gvSIG "+ ApplicationLocator.getManager().getVersion() +" -->");
835

  
836
		xmlAttrs.put(WebMapContextTags.VERSION, fileVersion);
837
		xmlAttrs.put(WebMapContextTags.ID, id);
838
		xmlAttrs.put(WebMapContextTags.XMLNS, WebMapContextTags.XMLNS_VALUE);
839
		xmlAttrs.put(WebMapContextTags.XMLNS_XLINK, WebMapContextTags.XMLNS_XLINK_VALUE);
840
		xmlAttrs.put(WebMapContextTags.XMLNS_XSI, WebMapContextTags.XMLNS_XSI_VALUE);
841
		xmlAttrs.put(WebMapContextTags.XSI_SCHEMA_LOCATION, WebMapContextTags.XSI_SCHEMA_LOCATION_VALUE);
842
		xml.openTag(viewContextTag,  xmlAttrs);
843
		xmlAttrs.clear();
844

  
845
			// <General>
846
			xml.openTag(WebMapContextTags.GENERAL);
847

  
848
				// <Window>
849
				if (windowSize!=null) {
850
					xmlAttrs.put(WebMapContextTags.WIDTH, ((int) windowSize.getWidth())+"");
851
					xmlAttrs.put(WebMapContextTags.HEIGHT, ((int) windowSize.getHeight())+"");
852
					xml.writeTag(WebMapContextTags.WINDOW, xmlAttrs);
853
					xmlAttrs.clear();
854
				}
855
				// </Window>
856

  
857
				// <BoundingBox>
858
				xmlAttrs.put(WebMapContextTags.SRS, v.getProjection().getAbrev());
859
				xmlAttrs.put(WebMapContextTags.X_MIN, bBox.getMinX()+"");
860
				xmlAttrs.put(WebMapContextTags.Y_MIN, bBox.getMinY()+"");
861
				xmlAttrs.put(WebMapContextTags.X_MAX, bBox.getMaxX()+"");
862
				xmlAttrs.put(WebMapContextTags.Y_MAX, bBox.getMaxY()+"");
863
				xml.writeTag(WebMapContextTags.BOUNDING_BOX, xmlAttrs);
864
				xmlAttrs.clear();
865
				// </BoundingBox>
866

  
867
				// <Title>
868
				xml.writeTag(WebMapContextTags.TITLE, title.trim());
869
				// </Title>
870

  
871
				// <Abstract>
872
				if (_abstract != null) {
873
					xml.writeTag(WebMapContextTags.ABSTRACT, _abstract.trim());
874
				// </Abstract>
875
				}
876

  
877
				// <LogoURL>
878
				if (logoURL != null) {
879
					xml.writeTag(WebMapContextTags.LOGO_URL, logoURL.trim());
880
				// </LogoURL>
881
				}
882

  
883
				// <DescriptionURL>
884
				if (descriptionURL != null) {
885
					xml.writeTag(WebMapContextTags.DESCRIPTION_URL, descriptionURL.trim());
886
				// </DescriptionURL>
887
				}
888

  
889
				if (contactInfo) {
890

  
891
					// <ContactInformation>
892
					xml.openTag(WebMapContextTags.CONTACT_INFORMATION);
893
					if (contactPerson != null || contactOrganization != null) {
894

  
895
						// <ContactPersonPrimary>
896
						xml.openTag(WebMapContextTags.CONTACT_PERSON_PRIMARY);
897

  
898
							// <ContactPerson>
899
							if (contactPerson != null) {
900
								xml.writeTag(WebMapContextTags.CONTACT_PERSON, contactPerson.trim());
901
							// </ContactPerson>
902
							}
903

  
904
							// <ContactOrganization>
905
							if (contactOrganization != null) {
906
								xml.writeTag(WebMapContextTags.CONTACT_ORGANIZATION, contactOrganization.trim());
907
							// </ContactOrganization>
908
							}
909

  
910
						xml.closeTag();
911
						// </ContactPersonPrimary>
912
					}
913
					xml.closeTag();
914
					// </ContactInformation>
915

  
916
					// <ContactPosition>
917
					if (contactPosition != null) {
918
						xml.writeTag(WebMapContextTags.CONTACT_POSITION, contactPosition.trim());
919
					// </ContactPosition>
920
					}
921

  
922
					// <ContactAddress>
923
					if (address != null || city != null || stateOrProvince != null || postCode != null || country != null) {
924
						xml.openTag(WebMapContextTags.CONTACT_ADDRESS);
925

  
926
							// <AddressType>
927
							xml.writeTag(WebMapContextTags.ADDRESS_TYPE, "Postal");
928
							// </AddressType>
929

  
930
							// <Address>
931
							if (address != null) {
932
								xml.writeTag(WebMapContextTags.ADDRESS, address.trim());
933
							// </Address>
934
							}
935

  
936
							// <City>
937
							if (city != null) {
938
								xml.writeTag(WebMapContextTags.CITY, city.trim());
939
							// </City>
940
							}
941

  
942
							// <StateOrProvince>
943
							if (stateOrProvince != null) {
944
								xml.writeTag(WebMapContextTags.STATE_OR_PROVINCE, stateOrProvince.trim());
945
							// </StateOrProvince>
946
							}
947

  
948
							// <PostCode>
949
							if (postCode != null) {
950
								xml.writeTag(WebMapContextTags.POSTCODE, postCode.trim());
951
							// </PostCode>
952
							}
953

  
954
							// <Country>
955
							if (country != null) {
956
								xml.writeTag(WebMapContextTags.COUNTRY, country.trim());
957
							}
958
							// </Country>
959
						xml.closeTag();
960
					}
961
					// </ContactAddress>
962

  
963
					// <ContactVoiceTelephone>
964
					if (telephone != null) {
965
						xml.writeTag(WebMapContextTags.CONTACT_VOICE_TELEPHONE, telephone.trim());
966
					// </ContactVoiceTelephone>
967
					}
968

  
969
					// <ContactFacsimileTelephone>
970
					if (fax != null) {
971
						xml.writeTag(WebMapContextTags.CONTACT_FACSIMILE_TELEPHONE, fax.trim());
972
					// </ContactFacsimileTelephone>
973
					}
974

  
975
					// <ContactElectronicMailAddress>
976
					if (email != null) {
977
						xml.writeTag(WebMapContextTags.CONTACT_ELECTRONIC_MAIL_ADDRESS, email.trim());
978
					// </ContactElectronicMailAddress>
979
					}
980
				}
981
				// <KeywordList>
982
				xml.openTag(WebMapContextTags.KEYWORD_LIST);
983
				if (keywordList != null) {
984
					for (int i = 0; i < keywordList.size(); i++) {
985
						xml.writeTag(WebMapContextTags.KEYWORD, ((String) keywordList.get(i)).trim());
986
					}
987
				} else {
988
					xml.writeTag(WebMapContextTags.KEYWORD, "");
989
				}
990
				xml.closeTag();
991

  
992
				// </KeywordList>
993
			xml.closeTag();
994
			// </General>
995

  
996
			// <LayerList>
997
			xml.openTag(WebMapContextTags.LAYER_LIST);
998
			for (int i = 0; i < layersToExport.size(); i++) {
999
				xml.writeRaw(((FLyrWMS) layersToExport.get(i)).toMapContext(fileVersion));
1000
			}
1001
			xml.closeTag();
1002
			// </LayerList>
1003
		xml.closeTag();
1004
		// </ViewContext>
1005

  
1006
		return xml.getXML();
1007
	}
1008

  
1009
	/**
1010
	 * Exports the ProjectView passed as parameter to Web Map Context XML compliant
1011
	 * with the defined specifications for the version set in the fileVersion field.
1012
	 * @param DefaultViewDocument to be exported
1013
	 * @return String containing the XML
1014
	 */
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff