001// Copyright 2005 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry.services.impl;
016
017import org.apache.hivemind.Location;
018import org.apache.hivemind.Resource;
019import org.apache.hivemind.util.Defense;
020import org.apache.tapestry.IAsset;
021import org.apache.tapestry.asset.AssetSource;
022import org.apache.tapestry.engine.ISpecificationSource;
023import org.apache.tapestry.services.NamespaceResources;
024import org.apache.tapestry.spec.IComponentSpecification;
025import org.apache.tapestry.spec.ILibrarySpecification;
026
027/**
028 * Implementation of {@link org.apache.tapestry.services.NamespaceResources}.
029 * 
030 * @author Howard M. Lewis Ship
031 */
032public class NamespaceResourcesImpl implements NamespaceResources
033{
034    private final ISpecificationSource _specificationSource;
035
036    private final AssetSource _assetSource;
037
038    public NamespaceResourcesImpl(ISpecificationSource source, AssetSource assetSource)
039    {
040        Defense.notNull(source, "source");
041        Defense.notNull(assetSource, "assetSource");
042
043        _specificationSource = source;
044        _assetSource = assetSource;
045    }
046
047    public ILibrarySpecification findChildLibrarySpecification(Resource parentResource,
048            String path, Location location)
049    {
050        Resource childResource = findSpecificationResource(parentResource, path, location);
051
052        return _specificationSource.getLibrarySpecification(childResource);
053
054    }
055
056    private Resource findSpecificationResource(Resource libraryResource, String path,
057            Location location)
058    {
059        // TODO: This is where we'll play with assets and asset prefixes
060
061        IAsset childAsset = _assetSource.findAsset(libraryResource, path, null, location);
062
063        Resource childResource = childAsset.getResourceLocation();
064
065        return childResource;
066    }
067
068    public IComponentSpecification getPageSpecification(Resource resource,
069            String specificationPath, Location location)
070    {
071        Resource pageSpecificationResource = findSpecificationResource(
072                resource,
073                specificationPath,
074                location);
075
076        return _specificationSource.getPageSpecification(pageSpecificationResource);
077    }
078
079    public IComponentSpecification getComponentSpecification(Resource resource,
080            String specificationPath, Location location)
081    {
082        Resource componentSpecificationResource = findSpecificationResource(
083                resource,
084                specificationPath,
085                location);
086
087        return _specificationSource.getComponentSpecification(componentSpecificationResource);
088    }
089
090}