Clover coverage report - Code Coverage for tapestry release 4.0-beta-3
Coverage timestamp: Sun Jul 24 2005 08:26:33 EDT
file stats: LOC: 211   Methods: 10
NCLOC: 95   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
TapestryUtils.java 100% 100% 100% 100%
coverage
 1    // Copyright 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;
 16   
 17    import java.util.ArrayList;
 18    import java.util.List;
 19   
 20    import org.apache.hivemind.ApplicationRuntimeException;
 21    import org.apache.hivemind.HiveMind;
 22    import org.apache.hivemind.util.Defense;
 23   
 24    /**
 25    * Constants and static methods.
 26    *
 27    * @author Howard M. Lewis Ship
 28    * @since 4.0
 29    */
 30    public class TapestryUtils
 31    {
 32    /**
 33    * Stores an attribute into the request cycle, verifying that no object with that key is already
 34    * present.
 35    *
 36    * @param cycle
 37    * the cycle to store the attribute into
 38    * @param key
 39    * the key to store the attribute as
 40    * @param object
 41    * the attribute value to store
 42    * @throws IllegalStateException
 43    * if a non-null value has been stored into the cycle with the provided key.
 44    */
 45   
 46  149 public static void storeUniqueAttribute(IRequestCycle cycle, String key, Object object)
 47    {
 48  149 Defense.notNull(cycle, "cycle");
 49  149 Defense.notNull(key, "key");
 50  149 Defense.notNull(object, "object");
 51   
 52  149 Object existing = cycle.getAttribute(key);
 53  149 if (existing != null)
 54  1 throw new IllegalStateException(TapestryMessages.nonUniqueAttribute(
 55    object,
 56    key,
 57    existing));
 58   
 59  148 cycle.setAttribute(key, object);
 60    }
 61   
 62    public static final String PAGE_RENDER_SUPPORT_ATTRIBUTE = "org.apache.tapestry.PageRenderSupport";
 63   
 64    public static final String FORM_ATTRIBUTE = "org.apache.tapestry.Form";
 65   
 66    /**
 67    * Stores the support object using {@link #storeUniqueAttribute(IRequestCycle, String, Object)}.
 68    */
 69   
 70  76 public static void storePageRenderSupport(IRequestCycle cycle, PageRenderSupport support)
 71    {
 72  76 storeUniqueAttribute(cycle, PAGE_RENDER_SUPPORT_ATTRIBUTE, support);
 73    }
 74   
 75    /**
 76    * Store the IForm instance using {@link #storeUniqueAttribute(IRequestCycle, String, Object)}.
 77    */
 78   
 79  71 public static void storeForm(IRequestCycle cycle, IForm form)
 80    {
 81  71 storeUniqueAttribute(cycle, FORM_ATTRIBUTE, form);
 82    }
 83   
 84    /**
 85    * Gets the previously stored {@link org.apache.tapestry.PageRenderSupport} object.
 86    *
 87    * @param cycle
 88    * the request cycle storing the support object
 89    * @param component
 90    * the component which requires the support (used to report exceptions)
 91    * @throws ApplicationRuntimeException
 92    * if no support object has been stored
 93    */
 94   
 95  12 public static PageRenderSupport getPageRenderSupport(IRequestCycle cycle, IComponent component)
 96    {
 97  12 Defense.notNull(component, "component");
 98   
 99  12 PageRenderSupport result = getOptionalPageRenderSupport(cycle);
 100  12 if (result == null)
 101  2 throw new ApplicationRuntimeException(TapestryMessages.noPageRenderSupport(component),
 102    component.getLocation(), null);
 103   
 104  10 return result;
 105    }
 106   
 107    /**
 108    * Gets the previously stored {@link IForm} object.
 109    *
 110    * @param cycle
 111    * the request cycle storing the support object
 112    * @param component
 113    * the component which requires the form (used to report exceptions)
 114    * @throws ApplicationRuntimeException
 115    * if no form object has been stored
 116    */
 117  116 public static IForm getForm(IRequestCycle cycle, IComponent component)
 118    {
 119  116 Defense.notNull(cycle, "cycle");
 120  116 Defense.notNull(component, "component");
 121   
 122  116 IForm result = (IForm) cycle.getAttribute(FORM_ATTRIBUTE);
 123   
 124  116 if (result == null)
 125  2 throw new ApplicationRuntimeException(TapestryMessages.noForm(component), component
 126    .getLocation(), null);
 127   
 128  114 return result;
 129    }
 130   
 131  77 public static void removePageRenderSupport(IRequestCycle cycle)
 132    {
 133  77 cycle.removeAttribute(PAGE_RENDER_SUPPORT_ATTRIBUTE);
 134    }
 135   
 136  72 public static void removeForm(IRequestCycle cycle)
 137    {
 138  72 cycle.removeAttribute(FORM_ATTRIBUTE);
 139    }
 140   
 141    /**
 142    * Returns the {@link PageRenderSupport} object if previously stored, or null otherwise.
 143    * This is used in the rare case that a component wishes to adjust its behavior based on whether
 144    * the page render support services are avaiable (typically, adjust for whether enclosed by a
 145    * Body component, or not).
 146    */
 147   
 148  102 public static PageRenderSupport getOptionalPageRenderSupport(IRequestCycle cycle)
 149    {
 150  102 return (PageRenderSupport) cycle.getAttribute(PAGE_RENDER_SUPPORT_ATTRIBUTE);
 151    }
 152   
 153    /**
 154    * Splits a string using the default delimiter of ','.
 155    */
 156   
 157  214 public static String[] split(String input)
 158    {
 159  214 return split(input, ',');
 160    }
 161   
 162    /**
 163    * Splits a single string into an array of strings, using a specific delimiter character.
 164    */
 165   
 166  268 public static String[] split(String input, char delimiter)
 167    {
 168  268 if (HiveMind.isBlank(input))
 169  158 return new String[0];
 170   
 171  110 List strings = new ArrayList();
 172   
 173  110 char[] buffer = input.toCharArray();
 174   
 175  110 int start = 0;
 176  110 int length = 0;
 177   
 178  110 for (int i = 0; i < buffer.length; i++)
 179    {
 180  849 if (buffer[i] != delimiter)
 181    {
 182  788 length++;
 183  788 continue;
 184    }
 185   
 186    // Consecutive delimiters will result in a sequence
 187    // of empty strings.
 188   
 189  61 String token = new String(buffer, start, length);
 190  61 strings.add(token);
 191   
 192  61 start = i + 1;
 193  61 length = 0;
 194    }
 195   
 196    // If the string contains no delimiters, then
 197    // wrap it an an array and return it.
 198   
 199  110 if (start == 0 && length == buffer.length)
 200    {
 201  80 return new String[]
 202    { input };
 203    }
 204   
 205    // The final token.
 206  30 String token = new String(buffer, start, length);
 207  30 strings.add(token);
 208   
 209  30 return (String[]) strings.toArray(new String[strings.size()]);
 210    }
 211    }