| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| BeanModelUtils |
|
| 0.0;0 |
| 1 | // Copyright 2007, 2008, 2009 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.tapestry5.internal.beaneditor; | |
| 16 | ||
| 17 | import org.apache.tapestry5.beaneditor.BeanModel; | |
| 18 | import org.apache.tapestry5.ioc.internal.util.InternalUtils; | |
| 19 | ||
| 20 | /** | |
| 21 | * Utilities used in a few places to modify an existing {@link BeanModel}. | |
| 22 | */ | |
| 23 | 0 | public final class BeanModelUtils |
| 24 | { | |
| 25 | ||
| 26 | /** | |
| 27 | * Performs standard set of modifications to a {@link org.apache.tapestry5.beaneditor.BeanModel}. First new | |
| 28 | * properties may be added, then properties removed, then properties reordered. | |
| 29 | * | |
| 30 | * @param model to modifiy | |
| 31 | * @param addPropertyNames comma seperated list of property names to add, or null | |
| 32 | * @param includePropertyNames comma seperated list of property names to include | |
| 33 | * @param excludePropertyNames comma seperated list of property names to exclude, or null | |
| 34 | * @param reorderPropertyNames comma seperated list of property names to reorder, or null | |
| 35 | */ | |
| 36 | public static void modify(BeanModel model, String addPropertyNames, String includePropertyNames, | |
| 37 | String excludePropertyNames, | |
| 38 | String reorderPropertyNames) | |
| 39 | { | |
| 40 | 402 | if (addPropertyNames != null) add(model, addPropertyNames); |
| 41 | ||
| 42 | 402 | if (includePropertyNames != null) include(model, join(includePropertyNames, addPropertyNames)); |
| 43 | ||
| 44 | 402 | if (excludePropertyNames != null) exclude(model, excludePropertyNames); |
| 45 | ||
| 46 | 402 | if (reorderPropertyNames != null) reorder(model, reorderPropertyNames); |
| 47 | 402 | } |
| 48 | ||
| 49 | private static final String join(String firstList, String optionalSecondList) | |
| 50 | { | |
| 51 | 4 | if (InternalUtils.isBlank(optionalSecondList)) return firstList; |
| 52 | ||
| 53 | 2 | return firstList + "," + optionalSecondList; |
| 54 | } | |
| 55 | ||
| 56 | /** | |
| 57 | * Adds empty properties to the bean model. New properties are added with a <em>null</em> {@link | |
| 58 | * org.apache.tapestry5.PropertyConduit}. ` | |
| 59 | * | |
| 60 | * @param model to be modified | |
| 61 | * @param propertyNames comma-separated list of property names | |
| 62 | */ | |
| 63 | public static void add(BeanModel model, String propertyNames) | |
| 64 | { | |
| 65 | 112 | for (String name : split(propertyNames)) |
| 66 | { | |
| 67 | 58 | model.add(name, null); |
| 68 | } | |
| 69 | 54 | } |
| 70 | ||
| 71 | /** | |
| 72 | * Removes properties from the bean model. | |
| 73 | * | |
| 74 | * @param model | |
| 75 | * @param propertyNames comma-separated list of property names | |
| 76 | * @see BeanModel#exclude(String...) | |
| 77 | */ | |
| 78 | public static void exclude(BeanModel model, String propertyNames) | |
| 79 | { | |
| 80 | 10 | model.exclude(split(propertyNames)); |
| 81 | 10 | } |
| 82 | ||
| 83 | /** | |
| 84 | * Selects a subset of the properties to keep, and reorders them. | |
| 85 | */ | |
| 86 | public static void include(BeanModel model, String propertyNames) | |
| 87 | { | |
| 88 | 4 | model.include(split(propertyNames)); |
| 89 | 4 | } |
| 90 | ||
| 91 | /** | |
| 92 | * Reorders properties within the bean model. | |
| 93 | * | |
| 94 | * @param model | |
| 95 | * @param propertyNames comma-separated list of property names | |
| 96 | * @see BeanModel#reorder(String...) | |
| 97 | */ | |
| 98 | public static void reorder(BeanModel model, String propertyNames) | |
| 99 | { | |
| 100 | 112 | model.reorder(split(propertyNames)); |
| 101 | 112 | } |
| 102 | ||
| 103 | static String[] split(String propertyNames) | |
| 104 | { | |
| 105 | 188 | String trimmed = propertyNames.trim(); |
| 106 | ||
| 107 | 188 | if (trimmed.length() == 0) return new String[0]; |
| 108 | ||
| 109 | 186 | return trimmed.split("\\s*,\\s*"); |
| 110 | } | |
| 111 | } |