Clover coverage report - Code Coverage for tapestry release 3.1-alpha-1
Coverage timestamp: Mon Feb 21 2005 09:16:14 EST
file stats: LOC: 258   Methods: 7
NCLOC: 119   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
ComponentSpecificationResolverImpl.java 89.3% 94.3% 100% 93.2%
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.resolver;
 16   
 
 17   
 import org.apache.commons.logging.Log;
 18   
 import org.apache.hivemind.ApplicationRuntimeException;
 19   
 import org.apache.hivemind.Location;
 20   
 import org.apache.hivemind.Resource;
 21   
 import org.apache.tapestry.INamespace;
 22   
 import org.apache.tapestry.IRequestCycle;
 23   
 import org.apache.tapestry.spec.IComponentSpecification;
 24   
 
 25   
 /**
 26   
  *  Utility class that understands the rules of component types (which
 27   
  *  may optionally have a library prefix) and can resolve 
 28   
  *  the type to a {@link org.apache.tapestry.INamespace} and a 
 29   
  *  {@link org.apache.tapestry.spec.IComponentSpecification}.
 30   
  * 
 31   
  *  <p>Like {@link org.apache.tapestry.resolver.PageSpecificationResolver},
 32   
  *  if the component is not defined explicitly in the namespace, a search
 33   
  *  may occur:
 34   
  * 
 35   
  *  Performs the tricky work of resolving a page name to a page specification.
 36   
  *  The search for pages in the application namespace is the most complicated,
 37   
  *  since Tapestry searches for pages that aren't explicitly defined in the
 38   
  *  application specification.  The search, based on the <i>simple-name</i>
 39   
  *  of the page, goes as follows:
 40   
  * 
 41   
  *  <ul>
 42   
  *  <li>As declared in the application specification
 43   
  *  <li><i>type</i>.jwc in the same folder as the application specification
 44   
  *  <li><i>type</i> jwc in the WEB-INF/<i>servlet-name</i> directory of the context root
 45   
  *  <li><i>type</i>.jwc in WEB-INF
 46   
  *  <li><i>type</i>.jwc in the application root (within the context root)
 47   
  *  <li>By searching the framework namespace
 48   
  *  </ul> 
 49   
  * 
 50   
  *  The search for components in library namespaces is more abbreviated:
 51   
  *  <li>As declared in the library specification
 52   
  *  <li><i>type</i>.jwc in the same folder as the library specification
 53   
  *  <li>By searching the framework namespace
 54   
  *  </ul>
 55   
  *
 56   
  * 
 57   
  *  @author Howard Lewis Ship
 58   
  *  @since 3.0
 59   
  *
 60   
  */
 61   
 
 62   
 public class ComponentSpecificationResolverImpl
 63   
     extends AbstractSpecificationResolver
 64   
     implements ComponentSpecificationResolver
 65   
 {
 66   
     /** Set by container */
 67   
     private Log _log;
 68   
 
 69   
     /** Set by resolve() */
 70   
     private String _type;
 71   
 
 72  1650
     protected void reset()
 73   
     {
 74  1650
         _type = null;
 75   
 
 76  1650
         super.reset();
 77   
     }
 78   
 
 79   
     /**
 80   
      *  Passed the namespace of a container (to resolve the type in)
 81   
      *  and the type to resolve, performs the processing.  A "bare type"
 82   
      *  (without a library prefix) may be in the containerNamespace,
 83   
      *  or the framework namespace
 84   
      *  (a search occurs in that order).
 85   
      * 
 86   
      *  @param cycle current request cycle
 87   
      *  @param containerNamespace namespace that may contain
 88   
      *  a library referenced in the type
 89   
      *  @param type the component specification
 90   
      *  to  find, either a simple name, or prefixed with a library id
 91   
      *  (defined for the container namespace)
 92   
      * 
 93   
      *  @see #getNamespace()
 94   
      *  @see #getSpecification()
 95   
      * 
 96   
      */
 97   
 
 98  1171
     public void resolve(
 99   
         IRequestCycle cycle,
 100   
         INamespace containerNamespace,
 101   
         String type,
 102   
         Location location)
 103   
     {
 104  1171
         int colonx = type.indexOf(':');
 105   
 
 106  1171
         if (colonx > 0)
 107   
         {
 108  103
             String libraryId = type.substring(0, colonx);
 109  103
             String simpleType = type.substring(colonx + 1);
 110   
 
 111  103
             resolve(cycle, containerNamespace, libraryId, simpleType, location);
 112   
         }
 113   
         else
 114  1068
             resolve(cycle, containerNamespace, null, type, location);
 115   
     }
 116   
 
 117   
     /**
 118   
      *  Like {@link #resolve(org.apache.tapestry.IRequestCycle, org.apache.tapestry.INamespace, java.lang.String, org.apache.tapestry.ILocation)},
 119   
      *  but used when the type has already been parsed into a library id and a simple type.
 120   
      * 
 121   
      *  @param cycle current request cycle
 122   
      *  @param containerNamespace namespace that may contain
 123   
      *  a library referenced in the type
 124   
      *  @param libraryId the library id within the container namespace, or null
 125   
      *  @param type the component specification
 126   
      *  to  find as a simple name (without a library prefix)
 127   
      *  @param location of reference to be resolved
 128   
      *  @throws ApplicationRuntimeException if the type cannot be resolved
 129   
      * 
 130   
      */
 131   
 
 132  1650
     public void resolve(
 133   
         IRequestCycle cycle,
 134   
         INamespace containerNamespace,
 135   
         String libraryId,
 136   
         String type,
 137   
         Location location)
 138   
     {
 139  1650
         reset();
 140  1650
         _type = type;
 141   
 
 142  1650
         INamespace namespace = null;
 143   
 
 144  1650
         if (libraryId != null)
 145  181
             namespace = containerNamespace.getChildNamespace(libraryId);
 146   
         else
 147  1469
             namespace = containerNamespace;
 148   
 
 149  1650
         setNamespace(namespace);
 150   
 
 151  1650
         if (namespace.containsComponentType(type))
 152  809
             setSpecification(namespace.getComponentSpecification(type));
 153   
         else
 154  841
             searchForComponent(cycle);
 155   
 
 156   
         // If not found after search, check to see if it's in
 157   
         // the framework instead.
 158   
 
 159  1650
         if (getSpecification() == null)
 160   
         {
 161   
 
 162  1
             throw new ApplicationRuntimeException(
 163   
                 ResolverMessages.noSuchComponentType(type, namespace),
 164   
                 location,
 165   
                 null);
 166   
 
 167   
         }
 168   
     }
 169   
 
 170  841
     private void searchForComponent(IRequestCycle cycle)
 171   
     {
 172  841
         INamespace namespace = getNamespace();
 173   
 
 174  841
         if (_log.isDebugEnabled())
 175  0
             _log.debug("Resolving unknown component '" + _type + "' in " + namespace);
 176   
 
 177  841
         String expectedName = _type + ".jwc";
 178  841
         Resource namespaceLocation = namespace.getSpecificationLocation();
 179   
 
 180   
         // Look for appropriate file in same folder as the library (or application)
 181   
         // specificaiton.
 182   
 
 183  841
         if (found(namespaceLocation.getRelativeResource(expectedName)))
 184  10
             return;
 185   
 
 186  831
         if (namespace.isApplicationNamespace())
 187   
         {
 188   
 
 189   
             // The application namespace gets some extra searching.
 190   
 
 191  711
             if (found(getWebInfAppLocation().getRelativeResource(expectedName)))
 192  1
                 return;
 193   
 
 194  710
             if (found(getWebInfLocation().getRelativeResource(expectedName)))
 195  1
                 return;
 196   
 
 197  709
             if (found(getContextRoot().getRelativeResource(expectedName)))
 198  1
                 return;
 199   
         }
 200   
 
 201   
         // Not in the library or app spec; does it match a component
 202   
         // provided by the Framework?
 203   
 
 204  828
         INamespace framework = getSpecificationSource().getFrameworkNamespace();
 205   
 
 206  828
         if (framework.containsComponentType(_type))
 207   
         {
 208  827
             setSpecification(framework.getComponentSpecification(_type));
 209  827
             return;
 210   
         }
 211   
 
 212  1
         IComponentSpecification specification =
 213   
             getDelegate().findComponentSpecification(cycle, namespace, _type);
 214   
 
 215  1
         setSpecification(specification);
 216   
 
 217   
         // If not found by here, an exception will be thrown.
 218   
     }
 219   
 
 220  2971
     private boolean found(Resource resource)
 221   
     {
 222  2971
         if (_log.isDebugEnabled())
 223  0
             _log.debug("Checking: " + resource);
 224   
 
 225  2971
         if (resource.getResourceURL() == null)
 226  2958
             return false;
 227   
 
 228  13
         setSpecification(getSpecificationSource().getComponentSpecification(resource));
 229   
 
 230  13
         install();
 231   
 
 232  13
         return true;
 233   
     }
 234   
 
 235  13
     private void install()
 236   
     {
 237  13
         INamespace namespace = getNamespace();
 238  13
         IComponentSpecification specification = getSpecification();
 239   
 
 240  13
         if (_log.isDebugEnabled())
 241  0
             _log.debug(
 242   
                 "Installing component type "
 243   
                     + _type
 244   
                     + " into "
 245   
                     + namespace
 246   
                     + " as "
 247   
                     + specification);
 248   
 
 249  13
         namespace.installComponentSpecification(_type, specification);
 250   
     }
 251   
 
 252  140
     public void setLog(Log log)
 253   
     {
 254  140
         _log = log;
 255   
     }
 256   
 
 257   
 }
 258