1 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 30 public class TapestryUtils 31 { 32 45 46 public static void storeUniqueAttribute(IRequestCycle cycle, String key, Object object) 47 { 48 Defense.notNull(cycle, "cycle"); 49 Defense.notNull(key, "key"); 50 Defense.notNull(object, "object"); 51 52 Object existing = cycle.getAttribute(key); 53 if (existing != null) 54 throw new IllegalStateException (TapestryMessages.nonUniqueAttribute( 55 object, 56 key, 57 existing)); 58 59 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 69 70 public static void storePageRenderSupport(IRequestCycle cycle, PageRenderSupport support) 71 { 72 storeUniqueAttribute(cycle, PAGE_RENDER_SUPPORT_ATTRIBUTE, support); 73 } 74 75 78 79 public static void storeForm(IRequestCycle cycle, IForm form) 80 { 81 storeUniqueAttribute(cycle, FORM_ATTRIBUTE, form); 82 } 83 84 94 95 public static PageRenderSupport getPageRenderSupport(IRequestCycle cycle, IComponent component) 96 { 97 Defense.notNull(cycle, "cycle"); 98 Defense.notNull(component, "component"); 99 100 PageRenderSupport result = (PageRenderSupport) cycle 101 .getAttribute(PAGE_RENDER_SUPPORT_ATTRIBUTE); 102 103 if (result == null) 104 throw new ApplicationRuntimeException(TapestryMessages.noPageRenderSupport(component), 105 component.getLocation(), null); 106 107 return result; 108 } 109 110 120 public static IForm getForm(IRequestCycle cycle, IComponent component) 121 { 122 Defense.notNull(cycle, "cycle"); 123 Defense.notNull(component, "component"); 124 125 IForm result = (IForm) cycle.getAttribute(FORM_ATTRIBUTE); 126 127 if (result == null) 128 throw new ApplicationRuntimeException(TapestryMessages.noForm(component), component 129 .getLocation(), null); 130 131 return result; 132 } 133 134 public static void removePageRenderSupport(IRequestCycle cycle) 135 { 136 cycle.removeAttribute(PAGE_RENDER_SUPPORT_ATTRIBUTE); 137 } 138 139 public static void removeForm(IRequestCycle cycle) 140 { 141 cycle.removeAttribute(FORM_ATTRIBUTE); 142 } 143 144 150 151 public static PageRenderSupport getOptionalPageRenderSupport(IRequestCycle cycle) 152 { 153 return (PageRenderSupport) cycle.getAttribute(PAGE_RENDER_SUPPORT_ATTRIBUTE); 154 } 155 156 159 160 public static String [] split(String input) 161 { 162 return split(input, ','); 163 } 164 165 168 169 public static String [] split(String input, char delimiter) 170 { 171 if (HiveMind.isBlank(input)) 172 return new String [0]; 173 174 List strings = new ArrayList (); 175 176 char[] buffer = input.toCharArray(); 177 178 int start = 0; 179 int length = 0; 180 181 for (int i = 0; i < buffer.length; i++) 182 { 183 if (buffer[i] != delimiter) 184 { 185 length++; 186 continue; 187 } 188 189 192 String token = new String (buffer, start, length); 193 strings.add(token); 194 195 start = i + 1; 196 length = 0; 197 } 198 199 202 if (start == 0 && length == buffer.length) 203 { 204 return new String [] 205 { input }; 206 } 207 208 String token = new String (buffer, start, length); 210 strings.add(token); 211 212 return (String []) strings.toArray(new String [strings.size()]); 213 } 214 } | Popular Tags |