Clover coverage report - Code Coverage for tapestry release 4.0-beta-8
Coverage timestamp: Sat Sep 24 2005 11:50:34 EDT
file stats: LOC: 383   Methods: 25
NCLOC: 219   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Namespace.java 67.6% 71.1% 84% 72.5%
coverage coverage
 1    // Copyright 2004, 2005 The Apache Software Foundation
 2    //
 3    // Licensed under the Apache License, Version 2.0 (the "License");
 4    // you may not use this file except in compliance with the License.
 5    // You may obtain a copy of the License at
 6    //
 7    // http://www.apache.org/licenses/LICENSE-2.0
 8    //
 9    // Unless required by applicable law or agreed to in writing, software
 10    // distributed under the License is distributed on an "AS IS" BASIS,
 11    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12    // See the License for the specific language governing permissions and
 13    // limitations under the License.
 14   
 15    package org.apache.tapestry.engine;
 16   
 17    import java.util.ArrayList;
 18    import java.util.Collections;
 19    import java.util.HashMap;
 20    import java.util.HashSet;
 21    import java.util.List;
 22    import java.util.Map;
 23    import java.util.Set;
 24   
 25    import org.apache.hivemind.ApplicationRuntimeException;
 26    import org.apache.hivemind.Location;
 27    import org.apache.hivemind.Resource;
 28    import org.apache.tapestry.INamespace;
 29    import org.apache.tapestry.Tapestry;
 30    import org.apache.tapestry.services.NamespaceResources;
 31    import org.apache.tapestry.spec.IComponentSpecification;
 32    import org.apache.tapestry.spec.ILibrarySpecification;
 33   
 34    /**
 35    * Implementation of {@link org.apache.tapestry.INamespace} that works with a
 36    * {@link org.apache.tapestry.services.NamespaceResources} to obtain page and component
 37    * specifications as needed.
 38    *
 39    * @author Howard Lewis Ship
 40    * @since 2.2
 41    */
 42   
 43    public class Namespace implements INamespace
 44    {
 45    private final ILibrarySpecification _specification;
 46   
 47    private final String _id;
 48   
 49    private String _extendedId;
 50   
 51    private final INamespace _parent;
 52   
 53    private final boolean _frameworkNamespace;
 54   
 55    private final boolean _applicationNamespace;
 56   
 57    /** @since 4.0 */
 58   
 59    private final NamespaceResources _resources;
 60   
 61    /**
 62    * Map of {@link org.apache.tapestry.spec.ComponentSpecification}keyed on page name. The map is
 63    * synchronized because different threads may try to update it simultaneously (due to dynamic
 64    * page discovery in the application namespace).
 65    */
 66   
 67    private final Map _pages = Collections.synchronizedMap(new HashMap());
 68   
 69    /**
 70    * Map of {@link org.apache.tapestry.spec.ComponentSpecification}keyed on component alias.
 71    */
 72   
 73    private final Map _components = Collections.synchronizedMap(new HashMap());
 74   
 75    /**
 76    * Map, keyed on id, of {@link INamespace}.
 77    */
 78   
 79    private final Map _children = Collections.synchronizedMap(new HashMap());
 80   
 81  116 public Namespace(String id, INamespace parent, ILibrarySpecification specification,
 82    NamespaceResources resources)
 83    {
 84  116 _id = id;
 85  116 _parent = parent;
 86  116 _specification = specification;
 87  116 _resources = resources;
 88   
 89  116 _applicationNamespace = (_id == null);
 90  116 _frameworkNamespace = FRAMEWORK_NAMESPACE.equals(_id);
 91    }
 92   
 93  0 public String toString()
 94    {
 95  0 StringBuffer buffer = new StringBuffer("Namespace@");
 96  0 buffer.append(Integer.toHexString(hashCode()));
 97  0 buffer.append('[');
 98   
 99  0 if (_applicationNamespace)
 100  0 buffer.append("<application>");
 101    else
 102  0 buffer.append(getExtendedId());
 103   
 104  0 buffer.append(']');
 105   
 106  0 return buffer.toString();
 107    }
 108   
 109  118 public String getId()
 110    {
 111  118 return _id;
 112    }
 113   
 114  212 public String getExtendedId()
 115    {
 116  212 if (_applicationNamespace)
 117  151 return null;
 118   
 119  61 if (_extendedId == null)
 120  29 _extendedId = buildExtendedId();
 121   
 122  61 return _extendedId;
 123    }
 124   
 125  0 public INamespace getParentNamespace()
 126    {
 127  0 return _parent;
 128    }
 129   
 130  161 public INamespace getChildNamespace(String id)
 131    {
 132  161 String firstId = id;
 133  161 String nextIds = null;
 134   
 135    // Split the id into first and next if it is a dot separated sequence
 136  161 int index = id.indexOf('.');
 137  161 if (index >= 0)
 138    {
 139  0 firstId = id.substring(0, index);
 140  0 nextIds = id.substring(index + 1);
 141    }
 142   
 143    // Get the first namespace
 144  161 INamespace result = (INamespace) _children.get(firstId);
 145   
 146  161 if (result == null)
 147    {
 148  17 result = createNamespace(firstId);
 149   
 150  17 _children.put(firstId, result);
 151    }
 152   
 153    // If the id is a dot separated sequence, recurse to find
 154    // the needed namespace
 155  161 if (result != null && nextIds != null)
 156  0 result = result.getChildNamespace(nextIds);
 157   
 158  161 return result;
 159    }
 160   
 161  0 public List getChildIds()
 162    {
 163  0 return _specification.getLibraryIds();
 164    }
 165   
 166  67 public IComponentSpecification getPageSpecification(String name)
 167    {
 168  67 IComponentSpecification result = (IComponentSpecification) _pages.get(name);
 169   
 170  67 if (result == null)
 171    {
 172  45 result = locatePageSpecification(name);
 173   
 174  45 _pages.put(name, result);
 175    }
 176   
 177  67 return result;
 178    }
 179   
 180  0 public List getPageNames()
 181    {
 182  0 Set names = new HashSet();
 183   
 184  0 names.addAll(_pages.keySet());
 185  0 names.addAll(_specification.getPageNames());
 186   
 187  0 List result = new ArrayList(names);
 188   
 189  0 Collections.sort(result);
 190   
 191  0 return result;
 192    }
 193   
 194  1473 public IComponentSpecification getComponentSpecification(String alias)
 195    {
 196  1473 IComponentSpecification result = (IComponentSpecification) _components.get(alias);
 197   
 198  1473 if (result == null)
 199    {
 200  304 result = locateComponentSpecification(alias);
 201  304 _components.put(alias, result);
 202    }
 203   
 204  1473 return result;
 205    }
 206   
 207  70 public ILibrarySpecification getSpecification()
 208    {
 209  70 return _specification;
 210    }
 211   
 212  29 private String buildExtendedId()
 213    {
 214  29 if (_parent == null)
 215  18 return _id;
 216   
 217  11 String parentId = _parent.getExtendedId();
 218   
 219    // If immediate child of application namespace
 220   
 221  11 if (parentId == null)
 222  11 return _id;
 223   
 224  0 return parentId + "." + _id;
 225    }
 226   
 227    /**
 228    * Returns a string identifying the namespace, for use in error messages. I.e., "Application
 229    * namespace" or "namespace 'foo'".
 230    */
 231   
 232  1 public String getNamespaceId()
 233    {
 234  1 if (_frameworkNamespace)
 235  0 return Tapestry.getMessage("Namespace.framework-namespace");
 236   
 237  1 if (_applicationNamespace)
 238  1 return Tapestry.getMessage("Namespace.application-namespace");
 239   
 240  0 return Tapestry.format("Namespace.nested-namespace", getExtendedId());
 241    }
 242   
 243    /**
 244    * Gets the specification from the specification source.
 245    *
 246    * @throws ApplicationRuntimeException
 247    * if the named page is not defined.
 248    */
 249   
 250  45 private IComponentSpecification locatePageSpecification(String name)
 251    {
 252  45 String path = _specification.getPageSpecificationPath(name);
 253   
 254  45 if (path == null)
 255  0 throw new ApplicationRuntimeException(Tapestry.format(
 256    "Namespace.no-such-page",
 257    name,
 258    getNamespaceId()));
 259   
 260    // We don't record line-precise data about <page> elements
 261    // so use the location for the specification as a whole (at least identifying
 262    // the right file)
 263   
 264  45 return _resources.getPageSpecification(getSpecificationLocation(), path, getLocation());
 265    }
 266   
 267  304 private IComponentSpecification locateComponentSpecification(String type)
 268    {
 269  304 String path = _specification.getComponentSpecificationPath(type);
 270   
 271  304 if (path == null)
 272  0 throw new ApplicationRuntimeException(Tapestry.format(
 273    "Namespace.no-such-alias",
 274    type,
 275    getNamespaceId()));
 276   
 277    // We don't record line-precise data about <component-type> elements
 278    // so use the location for the specification as a whole (at least identifying
 279    // the right file)
 280   
 281  304 return _resources
 282    .getComponentSpecification(getSpecificationLocation(), path, getLocation());
 283    }
 284   
 285  17 private INamespace createNamespace(String id)
 286    {
 287  17 String path = _specification.getLibrarySpecificationPath(id);
 288   
 289  17 if (path == null)
 290  0 throw new ApplicationRuntimeException(Tapestry.format(
 291    "Namespace.library-id-not-found",
 292    id,
 293    getNamespaceId()));
 294   
 295    // We don't record line-precise data about <library> elements
 296    // so use the location for the specification as a whole (at least identifying
 297    // the right file)
 298   
 299  17 ILibrarySpecification ls = _resources.findChildLibrarySpecification(
 300    getSpecificationLocation(),
 301    path,
 302    getLocation());
 303   
 304  17 return new Namespace(id, this, ls, _resources);
 305    }
 306   
 307  161 public synchronized boolean containsPage(String name)
 308    {
 309  161 return _pages.containsKey(name) || (_specification.getPageSpecificationPath(name) != null);
 310    }
 311   
 312    /** @since 2.3 * */
 313   
 314  131 public String constructQualifiedName(String pageName)
 315    {
 316  131 String prefix = getExtendedId();
 317   
 318  131 if (prefix == null)
 319  99 return pageName;
 320   
 321  32 return prefix + SEPARATOR + pageName;
 322    }
 323   
 324    /** @since 3.0 * */
 325   
 326  992 public Resource getSpecificationLocation()
 327    {
 328  992 return _specification.getSpecificationLocation();
 329    }
 330   
 331    /** @since 3.0 * */
 332   
 333  260 public boolean isApplicationNamespace()
 334    {
 335  260 return _applicationNamespace;
 336    }
 337   
 338    /** @since 3.0 * */
 339   
 340  64 public synchronized void installPageSpecification(String pageName,
 341    IComponentSpecification specification)
 342    {
 343  64 _pages.put(pageName, specification);
 344    }
 345   
 346    /** @since 3.0 * */
 347   
 348  159 public synchronized void installComponentSpecification(String type,
 349    IComponentSpecification specification)
 350    {
 351  159 _components.put(type, specification);
 352    }
 353   
 354    /** @since 3.0 * */
 355   
 356  1632 public synchronized boolean containsComponentType(String type)
 357    {
 358  1632 return _components.containsKey(type)
 359    || (_specification.getComponentSpecificationPath(type) != null);
 360    }
 361   
 362    /** @since 3.0 * */
 363   
 364  366 public Location getLocation()
 365    {
 366  366 if (_specification == null)
 367  0 return null;
 368   
 369  366 return _specification.getLocation();
 370    }
 371   
 372    /**
 373    * Returns property values defined in the namespace's library specification.
 374    *
 375    * @return the property, or null if not provided in the specification.
 376    * @since 4.0
 377    */
 378   
 379  323 public String getPropertyValue(String propertyName)
 380    {
 381  323 return _specification.getProperty(propertyName);
 382    }
 383    }