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;
 
 import  org.apache.hivemind.ApplicationRuntimeException;
 import  org.apache.hivemind.Location;
 
 import java.util.*;

A placeholder for a number of (static) methods that don't belong elsewhere, as well as a global location for static constants.

Author(s):
Howard Lewis Ship
Since:
1.0.1
 
 
 public final class Tapestry
 {
    
The name ("direct") of a service that allows stateless behavior for an org.apache.tapestry.link.DirectLink component.

This service rolls back the state of the page but doesn't rewind the the dynamic state of the page the was the action service does, which is more efficient but less powerful.

An array of String parameters may be included with the service URL; these will be made available to the org.apache.tapestry.link.DirectLink component's listener.

 
 
     public static final String DIRECT_SERVICE = "direct";

    
Almost identical to the direct service, except specifically for handling browser level events.

Since:
4.1
 
 
     public static final String DIRECT_EVENT_SERVICE = "directevent";

    
The name ("external") of a service that a allows IExternalPage to be selected. Associated with a org.apache.tapestry.link.ExternalLink component.

This service enables IExternalPages to be accessed via a URL. External pages may be booked marked using their URL for future reference.

An array of Object parameters may be included with the service URL; these will be passed to the IExternalPage.activateExternalPage(Object[], IRequestCycle) method.

 
 
     public static final String EXTERNAL_SERVICE = "external";

    
The name ("page") of a service that allows a new page to be selected. Associated with a org.apache.tapestry.link.PageLink component.

The service requires a single parameter: the name of the target page.

 
 
     public static final String PAGE_SERVICE = "page";

    
The name ("home") of a service that jumps to the home page. A stand-in for when no service is provided, which is typically the entrypoint to the application.
 
 
     public static final String HOME_SERVICE = "home";

    
The name ("restart") of a service that invalidates the session and restarts the application. Typically used just to recover from an exception.
 
 
     public static final String RESTART_SERVICE = "restart";

    
The name ("asset") of a service used to access internal assets.
    public static final String ASSET_SERVICE = "asset";

    
The name ("reset") of a service used to clear cached template and specification data and remove all pooled pages. This is only used when debugging as a quick way to clear the out cached data, to allow updated versions of specifications and templates to be loaded (without stopping and restarting the servlet container).

This service is only available if the Java system property org.apache.tapestry.enable-reset-service is set to true.

    public static final String RESET_SERVICE = "reset";

    
Property name used to get the extension used for templates. This may be set in the page or component specification, or in the page (or component's) immediate container (library or application specification). Unlike most properties, value isn't inherited all the way up the chain. The default template extension is "html".

Since:
3.0
    public static final String TEMPLATE_EXTENSION_PROPERTY = "org.apache.tapestry.template-extension";

    
The name of an org.apache.tapestry.IRequestCycle attribute in which the currently rendering org.apache.tapestry.components.ILinkComponent is stored. Link components do not nest.
    public static final String LINK_COMPONENT_ATTRIBUTE_NAME = "org.apache.tapestry.active-link-component";

    
Suffix appended to a parameter name to form the name of a property that stores the binding for the parameter.

Since:
3.0
    public static final String PARAMETER_PROPERTY_NAME_SUFFIX = "Binding";

    
Key used to obtain an extension from the application specification. The extension, if it exists, implements org.apache.tapestry.request.IRequestDecoder.

Since:
2.2
    public static final String REQUEST_DECODER_EXTENSION_NAME = "org.apache.tapestry.request-decoder";

    
Name of optional application extension for the multipart decoder used by the application. The extension must implement org.apache.tapestry.multipart.IMultipartDecoder (and is generally a configured instance of IMultipartDecoder).

Since:
3.0
    public static final String MULTIPART_DECODER_EXTENSION_NAME = "org.apache.tapestry.multipart-decoder";

    
Method id used to check that IPage.validate(IRequestCycle) is invoked.

Since:
3.0
See also:
checkMethodInvocation(Object, String, Object)
    public static final String ABSTRACTPAGE_VALIDATE_METHOD_ID = "AbstractPage.validate()";

    
Method id used to check that IPage.detach() is invoked.

Since:
3.0
See also:
checkMethodInvocation(Object, String, Object)
    public static final String ABSTRACTPAGE_DETACH_METHOD_ID = "AbstractPage.detach()";

    
Regular expression defining a simple property name. Used by several different parsers. Simple property names match Java variable names; a leading letter (or underscore), followed by letters, numbers and underscores.

Since:
3.0
    public static final String SIMPLE_PROPERTY_NAME_PATTERN = "^_?[a-zA-Z]\\w*$";

    
Class name of an ognl.TypeConverterimplementing class to use as a type converter for org.apache.tapestry.binding.ExpressionBinding.
    public static final String OGNL_TYPE_CONVERTER = "org.apache.tapestry.ognl-type-converter";

    
The version of the framework; this is updated for major releases.
    public static final String VERSION = readVersion();
    private static final String UNKNOWN_VERSION = "Unknown";

    
Contains strings loaded from TapestryStrings.properties.

Since:
1.0.8
    private static ResourceBundle _strings;

    
A Mapthat links Locale names (as in Locale.toString()to Locale instances. This prevents needless duplication of Locales.
    private static final Map _localeMap = new HashMap();
    static
    {
        Locale[] locales = Locale.getAvailableLocales();
        for (int i = 0; i < locales.lengthi++)
        {
            .put(locales[i].toString(), locales[i]);
        }
    }

    
Used for tracking if a particular super-class method has been invoked.
    private static final ThreadLocal _invokedMethodIds = new ThreadLocal();


    
Prevent instantiation.
    private Tapestry()
    {
    }

    
Copys all informal bindingsfrom a source component to the destination component. Informal bindings are bindings for informal parameters. This will overwrite parameters (formal or informal) in the destination component if there is a naming conflict.
    public static void copyInformalBindings(IComponent sourceIComponent destination)
    {
        Collection names = source.getBindingNames();
        if (names == null)
            return;
        IComponentSpecification specification = source.getSpecification();
        Iterator i = names.iterator();
        while (i.hasNext())
        {
            String name = (Stringi.next();
            // If not a formal parameter, then copy it over.
            if (specification.getParameter(name) == null)
            {
                IBinding binding = source.getBinding(name);
                destination.setBinding(namebinding);
            }
        }
    }

    
Gets the Localefor the given string, which is the result of Locale.toString(). If no such locale is already registered, a new instance is created, registered and returned.
    public static Locale getLocale(String s)
    {
        Locale result = null;
        synchronized ()
        {
            result = (Locale.get(s);
        }
        if (result == null)
        {
            StringSplitter splitter = new StringSplitter('_');
            String[] terms = splitter.splitToArray(s);
            switch (terms.length)
            {
                case 1:
                    result = new Locale(terms[0], "");
                    break;
                case 2:
                    result = new Locale(terms[0], terms[1]);
                    break;
                case 3:
                    result = new Locale(terms[0], terms[1], terms[2]);
                    break;
                default:
                    throw new IllegalArgumentException("Unable to convert '" + s + "' to a Locale.");
            }
            synchronized ()
            {
                .put(sresult);
            }
        }
        return result;
    }

    
Closes the stream (if not null), ignoring any IOExceptionthrown.

Since:
1.0.2
    public static void close(InputStream stream)
    {
        if (stream != null)
        {
            try
            {
                stream.close();
            }
            catch (IOException ex)
            {
                // Ignore.
            }
        }
    }

    
Gets a string from the TapestryStrings resource bundle. The string in the bundle is treated as a pattern for MessageFormat.format(java.lang.String, java.lang.Object[]).

Since:
1.0.8
    public static String format(String keyObject[] args)
    {
        if ( == null)
             = ResourceBundle.getBundle("org.apache.tapestry.TapestryStrings");
        String pattern = .getString(key);
        if (args == null)
            return pattern;
        return MessageFormat.format(patternargs);
    }

    
Convienience method for invoking format(String, Object[]).

Since:
3.0
    public static String getMessage(String key)
    {
        return format(keynull);
    }

    
Convienience method for invoking format(String, Object[]).

Since:
3.0
    public static String format(String keyObject arg)
    {
        return format(keynew Object[]
          { arg });
    }

    
Convienience method for invoking format(String, Object[]).

Since:
3.0
    public static String format(String keyObject arg1Object arg2)
    {
        return format(keynew Object[]
          { arg1arg2 });
    }

    
Convienience method for invoking format(String, Object[]).

Since:
3.0
    public static String format(String keyObject arg1Object arg2Object arg3)
    {
        return format(keynew Object[]
          { arg1arg2arg3 });
    }

    
Invoked when the class is initialized to read the current version file.
    private static String readVersion()
    {
        Properties props = new Properties();
        try
        {
            InputStream in = Tapestry.class.getResourceAsStream("version.properties");
            if (in == null)
                return ;
            props.load(in);
            in.close();
            return props.getProperty("project.version");
        }
        catch (IOException ex)
        {
            return ;
        }
    }

    
Returns the size of a collection, or zero if the collection is null.

Since:
2.2
    public static int size(Collection c)
    {
        if (c == null)
            return 0;
        return c.size();
    }

    
Returns the length of the array, or 0 if the array is null.

Since:
2.2
    public static int size(Object[] array)
    {
        if (array == null)
            return 0;
        return array.length;
    }

    
Returns true if the Map is null or empty.

Since:
3.0
    public static boolean isEmpty(Map map)
    {
        return map == null || map.isEmpty();
    }

    
Returns true if the Collection is null or empty.

Since:
3.0
    public static boolean isEmpty(Collection c)
    {
        return c == null || c.isEmpty();
    }

    
Converts a Map to an even-sized array of key/value pairs. This may be useful when using a Map as service parameters (with org.apache.tapestry.link.DirectLink. Assuming the keys and values are simple objects (String, Boolean, Integer, etc.), then the representation as an array will encode more efficiently (via org.apache.tapestry.util.io.DataSqueezerImpl than serializing the Map and its contents.

Returns:
the array of keys and values, or null if the input Map is null or empty
Since:
2.2
    public static Object[] convertMapToArray(Map map)
    {
        if (isEmpty(map))
            return null;
        Set entries = map.entrySet();
        Object[] result = new Object[2 * entries.size()];
        int x = 0;
        Iterator i = entries.iterator();
        while (i.hasNext())
        {
            Map.Entry entry = (Map.Entryi.next();
            result[x++] = entry.getKey();
            result[x++] = entry.getValue();
        }
        return result;
    }

    
Converts an even-sized array of objects back into a Map.

Returns:
a Map, or null if the array is null or empty
Since:
2.2
See also:
convertMapToArray(Map)
    public static Map convertArrayToMap(Object[] array)
    {
        if (array == null || array.length == 0)
            return null;
        if (array.length % 2 != 0)
            throw new IllegalArgumentException(getMessage("Tapestry.even-sized-array"));
        Map result = new HashMap();
        int x = 0;
        while (x < array.length)
        {
            Object key = array[x++];
            Object value = array[x++];
            result.put(keyvalue);
        }
        return result;
    }

    
Creates an exception indicating the binding value is null.

Since:
3.0
    public static BindingException createNullBindingException(IBinding binding)
    {
        return new BindingException(getMessage("null-value-for-binding"), binding);
    }

    

Since:
3.0 *
    public static ApplicationRuntimeException createNoSuchComponentException(IComponent component,
                                                                             String id, Location location)
    {
        return new ApplicationRuntimeException(format("no-such-component"component.getExtendedId(), id),
                                               componentlocationnull);
    }

    

Since:
3.0 *
                                                                    String parameterName)
    {
        return new BindingException(format("required-parameter"parameterNamecomponent.getExtendedId()),
                                    componentnullcomponent.getBinding(parameterName), null);
    }

    

Since:
3.0 *
    public static ApplicationRuntimeException createRenderOnlyPropertyException(
      IComponent componentString propertyName)
    {
        return new ApplicationRuntimeException(format("render-only-property",
                                                      propertyName,
                                                      component.getExtendedId()), componentnullnull);
    }

    
Clears the list of method invocations.

Since:
3.0
See also:
checkMethodInvocation(Object, String, Object)
    public static void clearMethodInvocations()
    {
        .set(null);
    }

    
Adds a method invocation to the list of invocations. This is done in a super-class implementations.

Since:
3.0
See also:
checkMethodInvocation(Object, String, Object)
    public static void addMethodInvocation(Object methodId)
    {
        List methodIds = (List.get();
        if (methodIds == null)
        {
            methodIds = new ArrayList();
            .set(methodIds);
        }
        methodIds.add(methodId);
    }

    
Checks to see if a particular method has been invoked. The method is identified by a methodId (usually a String). The methodName and object are used to create an error message.

The caller should invoke clearMethodInvocations(), then invoke a method on the object. The super-class implementation should invoke addMethodInvocation(Object) to indicate that it was, in fact, invoked. The caller then invokes this method to validate that the super-class implementation was invoked.

The list of method invocations is stored in a ThreadLocal variable.

Since:
3.0
    public static void checkMethodInvocation(Object methodIdString methodNameObject object)
    {
        List methodIds = (List.get();
        if (methodIds != null && methodIds.contains(methodId))
            return;
        throw new ApplicationRuntimeException(Tapestry.format("Tapestry.missing-method-invocation",
                                                              object.getClass().getName(),
                                                              methodName));
    }

    
Method used by pages and components to send notifications about property changes.

Parameters:
component the component containing the property
propertyName the name of the property which changed
newValue the new value for the property
Since:
3.0
    public static void fireObservedChange(IComponent componentString propertyNameObject newValue)
    {
        ChangeObserver observer = component.getPage().getChangeObserver();
        if (observer == null)
            return;
        ObservedChangeEvent event = new ObservedChangeEvent(componentpropertyNamenewValue);
        observer.observeChange(event);
    }
New to GrepCode? Check out our FAQ X