Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  // Copyright 2004, 2005 The Apache Software Foundation
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //     http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
 // distributed under the License is distributed on an "AS IS" BASIS,
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
 package org.apache.tapestry.spec;
 
 import  org.apache.hivemind.ApplicationRuntimeException;
 import  org.apache.hivemind.Location;
 import  org.apache.hivemind.Resource;
 import  org.apache.hivemind.util.ToStringBuilder;
 
 import java.util.*;

Specification for a library. org.apache.tapestry.spec.ApplicationSpecificationis a specialized kind of library.

Author(s):
Howard Lewis Ship
Since:
2.2bv
 
 
 {

    
Map of page name to page specification path.
 
 
     private Map _pages;

    
Map of component alias to component specification path.
 
     private Map _components;

    
Map of library id to library specification path.
 
 
     private Map _libraries;
 
     private String _description;

    
Map of extension name to IExtensionSpecification.
 
 
     private Map _extensions;

    
Map of extension name to Object for instantiated extensions.
 
 
     private Map _instantiatedExtensions;

    
The XML Public Id used when the library specification was read (if applicable).

Since:
2.2
 
 
     private String _publicId;

    
The location of the specification.
 
 
     private Resource _specificationLocation;
 
     {
         return (Stringget(id);
     }

    
Sets the specification path for an embedded library.

Throws:
IllegalArgumentException if a library with the given id already exists
 
 
     public void setLibrarySpecificationPath(String idString path)
     {
         if ( == null = new HashMap();
 
         if (.containsKey(id))
            throw new IllegalArgumentException(Tapestry.format("LibrarySpecification.duplicate-child-namespace-id"id));
        .put(idpath);
    }
    public List getLibraryIds()
    {
        return sortedKeys();
    }
    public String getPageSpecificationPath(String name)
    {
        return (Stringget(name);
    }
    public void setPageSpecificationPath(String nameString path)
    {
        if ( == null)
             = new HashMap();
        if (.containsKey(name))
            throw new IllegalArgumentException(Tapestry.format("LibrarySpecification.duplicate-page-name"name));
        .put(namepath);
    }
    public List getPageNames()
    {
        return sortedKeys();
    }
    public void setComponentSpecificationPath(String aliasString path)
    {
        if ( == null)
             = new HashMap();
        if (.containsKey(alias))
            throw new IllegalArgumentException(Tapestry.format("LibrarySpecification.duplicate-component-alias"alias));
        .put(aliaspath);
    }
    {
        return (Stringget(alias);
    }

    

Since:
3.0
    public List getComponentTypes()
    {
        return sortedKeys();
    }
    private List sortedKeys(Map map)
    {
        if (map == null)
            return .;
        List result = new ArrayList(map.keySet());
        Collections.sort(result);
        return result;
    }
    private Object get(Map mapObject key)
    {
        if (map == null)
            return null;
        return map.get(key);
    }

    
Returns the documentation for this library..
    public String getDescription()
    {
        return ;
    }

    
Sets the documentation for this library.
    public void setDescription(String description)
    {
         = description;
    }

    
Returns a Map of extensions; key is extension name, value is org.apache.tapestry.spec.IExtensionSpecification. May return null. The returned Map is immutable.
    {
        if ( == null)
            return null;
        return Collections.unmodifiableMap();
    }

    
Adds another extension specification.

Throws:
IllegalArgumentException if an extension with the given name already exists.
    public void addExtensionSpecification(String nameIExtensionSpecification extension)
    {
        if ( == null)
             = new HashMap();
        if (.containsKey(name))
            throw new IllegalArgumentException(Tapestry.format("LibrarySpecification.duplicate-extension-name",
                                                               thisname));
        .put(nameextension);
    }

    
Returns a sorted List of the names of all extensions. May return the empty list, but won't return null.
    public synchronized List getExtensionNames()
    {
        return sortedKeys();
    }

    
Returns the named IExtensionSpecification, or null if it doesn't exist.
    {
        if ( == null)
            return null;
        return (IExtensionSpecification.get(name);
    }

    
Returns true if this library specification has a specification for the named extension.
    public boolean checkExtension(String name)
    {
        if ( == null)
            return false;
        return .containsKey(name);
    }

    
Returns an instantiated extension. Extensions are created as needed and cached for later use.

Throws:
IllegalArgumentException if no extension specification exists for the given name.
    public synchronized Object getExtension(String name)
    {
        return getExtension(namenull);
    }

    

Since:
3.0 *
    public synchronized Object getExtension(String nameClass typeConstraint)
    {
        if ( == null)
             = new HashMap();
        Object result = .get(name);
        IExtensionSpecification spec = getExtensionSpecification(name);
        if (spec == null)
            throw new IllegalArgumentException(Tapestry.format("LibrarySpecification.no-such-extension"name));
        if (result == null)
        {
            result = spec.instantiateExtension();
            .put(nameresult);
        }
        if (typeConstraint != null)
            applyTypeConstraint(nameresulttypeConstraintspec.getLocation());
        return result;
    }

    
Checks that an extension conforms to the supplied type constraint.

Parameters:
name Name of the extension to apply constraint check to.
extension Object extension.
typeConstraint Constraint to check.
location Location of specified extension.
Throws:
IllegalArgumentException if the extension fails the check.
Since:
3.0
    protected void applyTypeConstraint(String nameObject extension,
                                       Class typeConstraint, Location location)
    {
        Class extensionClass = extension.getClass();
        // Can you assign an instance of the extension to a variable
        // of type typeContraint legally?
        if (typeConstraint.isAssignableFrom(extensionClass))
            return;
        String key = typeConstraint.isInterface()
                     ? "LibrarySpecification.extension-does-not-implement-interface"
                     : "LibrarySpecification.extension-not-a-subclass";
        throw new ApplicationRuntimeException(
          Tapestry.format(keynameextensionClass.getName(), typeConstraint.getName()), locationnull);
    }

    
Invoked after the entire specification has been constructed to instantiate any extensions marked immediate.
    public synchronized void instantiateImmediateExtensions()
    {
        if ( == null)
            return;
        Iterator i = .entrySet().iterator();
        while(i.hasNext())
        {
            Map.Entry entry = (Map.Entryi.next();
            IExtensionSpecification spec = (IExtensionSpecificationentry.getValue();
            if (!spec.isImmediate())
                continue;
            String name = (Stringentry.getKey();
            getExtension(name);
        }
    }

    
Returns the extensions map.

Returns:
Map of objects.
    protected Map getExtensions()
    {
        return ;
    }

    
Updates the extension map.

Parameters:
extension A Map of extension specification paths keyed on extension id.

The map is retained, not copied.

    protected void setExtensions(Map extension)
    {
         = extension;
    }

    
Returns the libraries map.

Returns:
Map of LibrarySpecification.
    protected Map getLibraries()
    {
        return ;
    }

    
Updates the library map.

Parameters:
libraries A Map of library specification paths keyed on library id.

The map is retained, not copied.

    protected void setLibraries(Map libraries)
    {
         = libraries;
    }

    
Returns the pages map.

Returns:
Map of IComponentSpecification.
    protected Map getPages()
    {
        return ;
    }

    
Updates the page map.

Parameters:
pages A Map of page specification paths keyed on page id.

The map is retained, not copied.

    protected void setPages(Map pages)
    {
         = pages;
    }

    
Returns the components map.

Returns:
Map of IContainedComponent.
    protected Map getComponents()
    {
        return ;
    }

    
Updates the components map.

Parameters:
components A Map of IContainedComponentkeyed on component id. The map is retained, not copied.
    protected void setComponents(Map components)
    {
         = components;
    }

    
Returns the XML Public Id for the library file, or null if not applicable.

This method exists as a convienience for the Spindle plugin. A previous method used an arbitrary version string, the public id is more useful and less ambiguous.

    public String getPublicId()
    {
        return ;
    }
    public void setPublicId(String publicId)
    {
         = publicId;
    }

    

Since:
3.0 *
    public Resource getSpecificationLocation()
    {
        return ;
    }

    

Since:
3.0 *
    public void setSpecificationLocation(Resource specificationLocation)
    {
         = specificationLocation;
    }

    

Since:
3.0 *
    public synchronized String toString()
    {
        ToStringBuilder builder = new ToStringBuilder(this);
        builder.append("components");
        builder.append("description");
        builder.append("instantiatedExtensions");
        builder.append("libraries");
        builder.append("pages");
        builder.append("publicId");
        builder.append("specificationLocation");
        extendDescription(builder);
        return builder.toString();
    }

    
Does nothing, subclasses may override to add additional description.

Since:
3.0
See also:
toString()
    protected void extendDescription(ToStringBuilder builder)
    {
    }
New to GrepCode? Check out our FAQ X