001    /**
002     * <copyright>
003     * </copyright>
004     *
005     * $Id$
006     */
007    package com.hammurapi.config.impl;
008    
009    import java.net.MalformedURLException;
010    import java.net.URL;
011    import java.util.Collection;
012    import java.util.logging.Logger;
013    
014    import org.eclipse.emf.common.notify.Notification;
015    import org.eclipse.emf.common.notify.NotificationChain;
016    import org.eclipse.emf.common.util.BasicEList;
017    import org.eclipse.emf.common.util.EList;
018    import org.eclipse.emf.ecore.EClass;
019    import org.eclipse.emf.ecore.InternalEObject;
020    import org.eclipse.emf.ecore.impl.ENotificationImpl;
021    import org.eclipse.emf.ecore.impl.EObjectImpl;
022    import org.eclipse.emf.ecore.util.EDataTypeUniqueEList;
023    import org.eclipse.emf.ecore.util.EObjectContainmentEList;
024    import org.eclipse.emf.ecore.util.InternalEList;
025    
026    import com.hammurapi.config.ConfigPackage;
027    import com.hammurapi.config.Path;
028    import com.hammurapi.config.bootstrap.ConfigurationException;
029    import com.hammurapi.config.bootstrap.TokenExpander;
030    import com.hammurapi.config.bootstrap.TokenExpander.TokenSource;
031    
032    /**
033     * <!-- begin-user-doc -->
034     * An implementation of the model object '<em><b>Path</b></em>'.
035     * <!-- end-user-doc -->
036     * <p>
037     * The following features are implemented:
038     * <ul>
039     *   <li>{@link com.hammurapi.config.impl.PathImpl#getPathElement <em>Path Element</em>}</li>
040     *   <li>{@link com.hammurapi.config.impl.PathImpl#getPath <em>Path</em>}</li>
041     *   <li>{@link com.hammurapi.config.impl.PathImpl#getBaseUrl <em>Base Url</em>}</li>
042     * </ul>
043     * </p>
044     *
045     * @generated
046     */
047    public class PathImpl extends EObjectImpl implements Path {
048            private static final Logger logger = Logger.getLogger(PathImpl.class.getName());
049            /**
050             * The cached value of the '{@link #getPathElement() <em>Path Element</em>}' attribute list.
051             * <!-- begin-user-doc -->
052             * <!-- end-user-doc -->
053             * @see #getPathElement()
054             * @generated
055             * @ordered
056             */
057            protected EList<String> pathElement;
058    
059            /**
060             * The cached value of the '{@link #getPath() <em>Path</em>}' containment reference list.
061             * <!-- begin-user-doc -->
062             * <!-- end-user-doc -->
063             * @see #getPath()
064             * @generated
065             * @ordered
066             */
067            protected EList<Path> path;
068    
069            /**
070             * The default value of the '{@link #getBaseUrl() <em>Base Url</em>}' attribute.
071             * <!-- begin-user-doc -->
072             * <!-- end-user-doc -->
073             * @see #getBaseUrl()
074             * @generated
075             * @ordered
076             */
077            protected static final String BASE_URL_EDEFAULT = null;
078    
079            /**
080             * The cached value of the '{@link #getBaseUrl() <em>Base Url</em>}' attribute.
081             * <!-- begin-user-doc -->
082             * <!-- end-user-doc -->
083             * @see #getBaseUrl()
084             * @generated
085             * @ordered
086             */
087            protected String baseUrl = BASE_URL_EDEFAULT;
088    
089            /**
090             * <!-- begin-user-doc -->
091             * <!-- end-user-doc -->
092             * @generated
093             */
094            protected PathImpl() {
095                    super();
096            }
097    
098            /**
099             * <!-- begin-user-doc -->
100             * <!-- end-user-doc -->
101             * @generated
102             */
103            @Override
104            protected EClass eStaticClass() {
105                    return ConfigPackage.Literals.PATH;
106            }
107    
108            /**
109             * <!-- begin-user-doc -->
110             * <!-- end-user-doc -->
111             * @generated
112             */
113            public EList<String> getPathElement() {
114                    if (pathElement == null) {
115                            pathElement = new EDataTypeUniqueEList<String>(String.class, this, ConfigPackage.PATH__PATH_ELEMENT);
116                    }
117                    return pathElement;
118            }
119    
120            /**
121             * <!-- begin-user-doc -->
122             * <!-- end-user-doc -->
123             * @generated
124             */
125            public EList<Path> getPath() {
126                    if (path == null) {
127                            path = new EObjectContainmentEList<Path>(Path.class, this, ConfigPackage.PATH__PATH);
128                    }
129                    return path;
130            }
131    
132            /**
133             * <!-- begin-user-doc -->
134             * <!-- end-user-doc -->
135             * @generated
136             */
137            public String getBaseUrl() {
138                    return baseUrl;
139            }
140    
141            /**
142             * <!-- begin-user-doc -->
143             * <!-- end-user-doc -->
144             * @generated
145             */
146            public void setBaseUrl(String newBaseUrl) {
147                    String oldBaseUrl = baseUrl;
148                    baseUrl = newBaseUrl;
149                    if (eNotificationRequired())
150                            eNotify(new ENotificationImpl(this, Notification.SET, ConfigPackage.PATH__BASE_URL, oldBaseUrl, baseUrl));
151            }
152    
153            /**
154             * <!-- begin-user-doc -->
155             * <!-- end-user-doc -->
156             * @generated NOT
157             */
158            public EList<URL> getUrls(URL baseURL, TokenSource tokens) throws ConfigurationException {
159                    URL theBaseURL = baseURL;
160                    if (getBaseUrl()!=null && getBaseUrl().trim().length()>0) {
161                            try {
162                                    theBaseURL = baseURL==null ? new URL(getBaseUrl()) : new URL(baseURL, getBaseUrl());
163                            } catch (MalformedURLException e) {
164                                    throw new ConfigurationException(e);
165                            }
166                    }
167                    
168                    TokenExpander tokenExpander = tokens==null ? null : new TokenExpander(tokens);
169                    
170                    EList<URL> ret = new BasicEList<URL>();
171                    
172                    for (String pe: getPathElement()) {
173                            try {
174                                    ret.add(theBaseURL==null ? new URL(pe) : new URL(theBaseURL, tokenExpander==null ? pe : tokenExpander.expand(pe)));
175                            } catch (MalformedURLException e) {
176                                    throw new ConfigurationException(e);
177                            }
178                    }
179                    
180                    for (Path child: getPath()) {
181                            ret.addAll(child.getUrls(theBaseURL, tokens));
182                    }
183    
184                    logger.fine("Path URL's: "+ret);                
185                    return ret;
186            }
187    
188            /**
189             * <!-- begin-user-doc -->
190             * <!-- end-user-doc -->
191             * @generated
192             */
193            @Override
194            public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
195                    switch (featureID) {
196                            case ConfigPackage.PATH__PATH:
197                                    return ((InternalEList<?>)getPath()).basicRemove(otherEnd, msgs);
198                    }
199                    return super.eInverseRemove(otherEnd, featureID, msgs);
200            }
201    
202            /**
203             * <!-- begin-user-doc -->
204             * <!-- end-user-doc -->
205             * @generated
206             */
207            @Override
208            public Object eGet(int featureID, boolean resolve, boolean coreType) {
209                    switch (featureID) {
210                            case ConfigPackage.PATH__PATH_ELEMENT:
211                                    return getPathElement();
212                            case ConfigPackage.PATH__PATH:
213                                    return getPath();
214                            case ConfigPackage.PATH__BASE_URL:
215                                    return getBaseUrl();
216                    }
217                    return super.eGet(featureID, resolve, coreType);
218            }
219    
220            /**
221             * <!-- begin-user-doc -->
222             * <!-- end-user-doc -->
223             * @generated
224             */
225            @SuppressWarnings("unchecked")
226            @Override
227            public void eSet(int featureID, Object newValue) {
228                    switch (featureID) {
229                            case ConfigPackage.PATH__PATH_ELEMENT:
230                                    getPathElement().clear();
231                                    getPathElement().addAll((Collection<? extends String>)newValue);
232                                    return;
233                            case ConfigPackage.PATH__PATH:
234                                    getPath().clear();
235                                    getPath().addAll((Collection<? extends Path>)newValue);
236                                    return;
237                            case ConfigPackage.PATH__BASE_URL:
238                                    setBaseUrl((String)newValue);
239                                    return;
240                    }
241                    super.eSet(featureID, newValue);
242            }
243    
244            /**
245             * <!-- begin-user-doc -->
246             * <!-- end-user-doc -->
247             * @generated
248             */
249            @Override
250            public void eUnset(int featureID) {
251                    switch (featureID) {
252                            case ConfigPackage.PATH__PATH_ELEMENT:
253                                    getPathElement().clear();
254                                    return;
255                            case ConfigPackage.PATH__PATH:
256                                    getPath().clear();
257                                    return;
258                            case ConfigPackage.PATH__BASE_URL:
259                                    setBaseUrl(BASE_URL_EDEFAULT);
260                                    return;
261                    }
262                    super.eUnset(featureID);
263            }
264    
265            /**
266             * <!-- begin-user-doc -->
267             * <!-- end-user-doc -->
268             * @generated
269             */
270            @Override
271            public boolean eIsSet(int featureID) {
272                    switch (featureID) {
273                            case ConfigPackage.PATH__PATH_ELEMENT:
274                                    return pathElement != null && !pathElement.isEmpty();
275                            case ConfigPackage.PATH__PATH:
276                                    return path != null && !path.isEmpty();
277                            case ConfigPackage.PATH__BASE_URL:
278                                    return BASE_URL_EDEFAULT == null ? baseUrl != null : !BASE_URL_EDEFAULT.equals(baseUrl);
279                    }
280                    return super.eIsSet(featureID);
281            }
282    
283            /**
284             * <!-- begin-user-doc -->
285             * <!-- end-user-doc -->
286             * @generated
287             */
288            @Override
289            public String toString() {
290                    if (eIsProxy()) return super.toString();
291    
292                    StringBuffer result = new StringBuffer(super.toString());
293                    result.append(" (pathElement: ");
294                    result.append(pathElement);
295                    result.append(", baseUrl: ");
296                    result.append(baseUrl);
297                    result.append(')');
298                    return result.toString();
299            }
300    
301    } //PathImpl