001: /*
002: * uDig - User Friendly Desktop Internet GIS client http://udig.refractions.net (C) 2004,
003: * Refractions Research Inc. This library is free software; you can redistribute it and/or modify it
004: * under the terms of the GNU Lesser General Public License as published by the Free Software
005: * Foundation; version 2.1 of the License. This library is distributed in the hope that it will be
006: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
007: * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
008: */
009: package net.refractions.udig.project.internal;
010:
011: import java.io.Serializable;
012: import java.io.UnsupportedEncodingException;
013: import java.net.URL;
014: import java.net.URLDecoder;
015: import java.util.Collections;
016: import java.util.HashMap;
017: import java.util.Map;
018:
019: import net.refractions.udig.core.internal.CorePlugin;
020:
021: public class OldCatalogRef {
022: protected Layer layer;
023:
024: protected static final String ID_SEPERATOR = "-ID_SEP-"; //$NON-NLS-1$
025:
026: protected static final String CONNECTION_PARAM = "-ENTRY_SEP-"; //$NON-NLS-1$
027:
028: protected static final String KEY_VALUE_SEPERATOR = "-KEY_VALUE_SEP-"; //$NON-NLS-1$
029:
030: protected static final String ENCODING = ProjectPlugin.Implementation.ENCODING;
031:
032: protected static final String SERVICE_TAG = "-SERVICE_TAG-:"; //$NON-NLS-1$
033:
034: /**
035: * @uml.property name="connectionParams"
036: * @uml.associationEnd qualifier="key:java.lang.Object java.util.Map<String,Serializable>"
037: */
038: protected Map<URL, Map<String, Serializable>> connectionParams = Collections
039: .synchronizedMap(new HashMap<URL, Map<String, Serializable>>());
040:
041: /**
042: * Construct <code>LayerRef</code>.
043: *
044: * @param layer
045: */
046: public OldCatalogRef(Layer layer) {
047: this .layer = layer;
048: }
049:
050: /**
051: * Construct <code>LayerRef</code>.
052: */
053: public OldCatalogRef() {
054: // do nothing
055: }
056:
057: /**
058: * @param string
059: */
060: public void parseResourceParameters(String string) {
061:
062: String[] resources = string.split(SERVICE_TAG);
063: for (int i = 0; i < resources.length; i++) {
064: decodeConnectionParams(resources[i]);
065: }
066:
067: }
068:
069: /**
070: * @return A list of services identified by the string encodedService
071: */
072: protected void decodeConnectionParams(String encodedService) {
073: String[] tmp = encodedService.split(ID_SEPERATOR);
074: String id = tmp[0];
075:
076: String[] entries = tmp[1].split(CONNECTION_PARAM);
077: Map<String, Serializable> map = decodeConnectionParams(entries);
078: URL url = decodeServiceURL(id, map);
079: connectionParams.put(url, map);
080: }
081:
082: /**
083: * @return a map of the connection parameters obtained from parsing the encodedParams field
084: */
085: private Map<String, Serializable> decodeConnectionParams(
086: String[] encodedParams) {
087: Map<String, Serializable> map = new HashMap<String, Serializable>();
088: for (int j = 0; j < encodedParams.length; j++) {
089: String[] entry = encodedParams[j]
090: .split(KEY_VALUE_SEPERATOR);
091: if (entry[0].equalsIgnoreCase("URL")) //$NON-NLS-1$
092: try {
093: entry[1] = decodeURL(entry[1]);
094: } catch (UnsupportedEncodingException e) {
095: // do nothing
096: }
097: if (entry.length == 2)
098: map.put(entry[0], entry[1]);
099: }
100: return map;
101: }
102:
103: private URL decodeServiceURL(String id,
104: Map<String, Serializable> map) {
105: URL url = null;
106: try {
107: url = new URL(null, decodeURL(id),
108: CorePlugin.RELAXED_HANDLER);
109: } catch (Exception e) {
110: try {
111: url = new URL(decodeURL((String) map.get("url"))); //$NON-NLS-1$
112: } catch (Exception e2) {
113: e.printStackTrace();
114: return null;
115: }
116: }
117: return url;
118: }
119:
120: /**
121: * @return a url from the string url
122: * @throws UnsupportedEncodingException
123: */
124: private String decodeURL(String url)
125: throws UnsupportedEncodingException {
126: return URLDecoder.decode(url, ENCODING);
127: }
128:
129: }
|