Coverage Report - org.apache.fulcrum.intake.model.XmlField
 
Classes in this File Line Coverage Branch Coverage Complexity
XmlField
53%
36/67
32%
9/28
1.625
 
 1  
 package org.apache.fulcrum.intake.model;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.io.IOException;
 23  
 import java.io.ObjectInputStream;
 24  
 import java.io.ObjectOutputStream;
 25  
 import java.io.Serializable;
 26  
 import java.util.ArrayList;
 27  
 import java.util.HashMap;
 28  
 import java.util.List;
 29  
 import java.util.Map;
 30  
 
 31  
 import javax.xml.bind.Unmarshaller;
 32  
 import javax.xml.bind.annotation.XmlAccessType;
 33  
 import javax.xml.bind.annotation.XmlAccessorType;
 34  
 import javax.xml.bind.annotation.XmlAttribute;
 35  
 import javax.xml.bind.annotation.XmlElement;
 36  
 import javax.xml.bind.annotation.XmlType;
 37  
 
 38  
 import org.apache.avalon.framework.logger.LogEnabled;
 39  
 import org.apache.avalon.framework.logger.Logger;
 40  
 import org.apache.commons.lang3.StringUtils;
 41  
 
 42  
 /**
 43  
  * A Class for holding data about a property used in an Application.
 44  
  *
 45  
  * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
 46  
  * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
 47  
  * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
 48  
  * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
 49  
  * @version $Id: XmlField.java 1851410 2019-01-15 20:02:05Z painter $
 50  
  */
 51  
 @XmlType(name="field")
 52  
 @XmlAccessorType(XmlAccessType.NONE)
 53  
 public class XmlField
 54  
         implements Serializable, LogEnabled
 55  
 {
 56  
     /**
 57  
      * Serial version id
 58  
      */
 59  
     private static final long serialVersionUID = -734309157828058007L;
 60  
 
 61  
     @XmlAttribute(required=true)
 62  
     private String key;
 63  
 
 64  
     @XmlAttribute(required=true)
 65  
     private String name;
 66  
 
 67  
     @XmlAttribute
 68  
     private String displayName;
 69  
 
 70  
     @XmlAttribute
 71  
     private String displaySize;
 72  
 
 73  28
     @XmlAttribute
 74  
     private FieldType type = FieldType.FIELD_STRING;
 75  
 
 76  28
     @XmlAttribute
 77  
     private boolean multiValued = false;
 78  
 
 79  
     @XmlAttribute
 80  
     private String fieldClass;
 81  
 
 82  
     @XmlAttribute
 83  
     private String mapToObject;
 84  
 
 85  
     @XmlAttribute
 86  
     private String mapToProperty;
 87  
 
 88  
     @XmlAttribute
 89  
     private String validator;
 90  
 
 91  
     @XmlAttribute
 92  
     private String defaultValue;
 93  
 
 94  
     @XmlAttribute
 95  
     private String emptyValue;
 96  
 
 97  
     private List<Rule> rules;
 98  
     private Map<String, Rule> ruleMap;
 99  
 
 100  
     private Group parent;
 101  
 
 102  
     private Logger log;
 103  
 
 104  
     /**
 105  
      * Default Constructor
 106  
      */
 107  
     public XmlField()
 108  28
     {
 109  28
         rules = new ArrayList<Rule>();
 110  28
         ruleMap = new HashMap<String, Rule>();
 111  28
     }
 112  
 
 113  
     /**
 114  
          * Enable Avalon Logging
 115  
          */
 116  
         @Override
 117  
         public void enableLogging(Logger logger)
 118  
         {
 119  28
                 this.log = logger;
 120  28
         }
 121  
 
 122  
         /**
 123  
          * Return Avalon logger
 124  
          *
 125  
          * @return the logger
 126  
          */
 127  
         public Logger getLogger()
 128  
         {
 129  28
                 return log;
 130  
         }
 131  
 
 132  
     /**
 133  
      * Get the name of the property
 134  
      *
 135  
      * @return the raw name of the property
 136  
      */
 137  
     public String getRawName()
 138  
     {
 139  0
         return name;
 140  
     }
 141  
 
 142  
     /**
 143  
      * Get the name of the property
 144  
      *
 145  
      * @return the name of the property with underscores removed
 146  
      */
 147  
     public String getName()
 148  
     {
 149  53
         return StringUtils.replace(name, "_", "");
 150  
     }
 151  
 
 152  
     /**
 153  
      * Get the display name of the property
 154  
      *
 155  
      * @return the display name of the property
 156  
      */
 157  
     public String getDisplayName()
 158  
     {
 159  28
         return displayName;
 160  
     }
 161  
 
 162  
     /**
 163  
      * Gets the display size of the field.  This is
 164  
      * useful for constructing the HTML input tag.
 165  
      *
 166  
      * @return the display size for the field
 167  
      */
 168  
     public String getDisplaySize()
 169  
     {
 170  28
         return this.displaySize;
 171  
     }
 172  
 
 173  
     /**
 174  
      * Get the parameter key of the property
 175  
      *
 176  
      * @return the key of the property
 177  
      */
 178  
     public String getKey()
 179  
     {
 180  28
         return key;
 181  
     }
 182  
 
 183  
     /**
 184  
      * Get the type of the property
 185  
      *
 186  
      * @return the type of the field
 187  
      */
 188  
     public FieldType getType()
 189  
     {
 190  28
         return type;
 191  
     }
 192  
 
 193  
     /**
 194  
      * Can this field have several values?
 195  
      *
 196  
      * @return true if the field can have multiple values
 197  
      */
 198  
     public boolean isMultiValued()
 199  
     {
 200  28
         return multiValued;
 201  
     }
 202  
 
 203  
     /**
 204  
      * Get the name of the object that takes this input
 205  
      *
 206  
      * @return the name of the mapped object
 207  
      */
 208  
     public String getMapToObject()
 209  
     {
 210  28
         return mapToObject;
 211  
     }
 212  
 
 213  
     /**
 214  
      * Get the property method that takes this input
 215  
      *
 216  
      * @return the property this field is mapped to
 217  
      */
 218  
     public String getMapToProperty()
 219  
     {
 220  28
         if (mapToProperty == null)
 221  
         {
 222  25
             return getName();
 223  
         }
 224  
         else
 225  
         {
 226  3
             return mapToProperty;
 227  
         }
 228  
     }
 229  
 
 230  
     /**
 231  
      * Get the className of the validator
 232  
      *
 233  
      * @return the validator class name
 234  
      */
 235  
     public String getValidator()
 236  
     {
 237  28
         return validator;
 238  
     }
 239  
 
 240  
     /**
 241  
      * Get the default Value.
 242  
      *
 243  
      * @return The default value for this field.
 244  
      */
 245  
     public String getDefaultValue()
 246  
     {
 247  28
         return defaultValue;
 248  
     }
 249  
 
 250  
     /**
 251  
      * Get the empty Value.
 252  
      *
 253  
      * @return The empty value for this field.
 254  
      */
 255  
     public String getEmptyValue()
 256  
     {
 257  28
         return emptyValue;
 258  
     }
 259  
 
 260  
     /**
 261  
      * Get the parent XmlGroup of the field
 262  
      *
 263  
      * @return the group this field belongs to
 264  
      */
 265  
     public Group getGroup()
 266  
     {
 267  28
         return this.parent;
 268  
     }
 269  
 
 270  
     /**
 271  
      * Get the value of fieldClass.
 272  
      *
 273  
      * @return value of fieldClass.
 274  
      */
 275  
     public String getFieldClass()
 276  
     {
 277  2
         return fieldClass;
 278  
     }
 279  
 
 280  
     /**
 281  
      * The collection of rules for this field.
 282  
      *
 283  
      * @return a <code>List</code> value
 284  
      */
 285  
     public List<Rule> getRules()
 286  
     {
 287  16
         return rules;
 288  
     }
 289  
 
 290  
     /**
 291  
      * Set the collection of rules for this field
 292  
      *
 293  
      * @param rules the rules to set
 294  
      */
 295  
     @XmlElement(name="rule")
 296  
     public void setRules(List<Rule> rules)
 297  
     {
 298  0
         this.rules = rules;
 299  0
     }
 300  
 
 301  
     /**
 302  
      * The collection of rules for this field keyed by
 303  
      * parameter name.
 304  
      *
 305  
      * @return a <code>Map</code> value
 306  
      */
 307  
     public Map<String, Rule> getRuleMap()
 308  
     {
 309  84
         return ruleMap;
 310  
     }
 311  
 
 312  
     /**
 313  
      * JAXB callback to set the parent object
 314  
      *
 315  
      * @param um the Unmarshaller
 316  
      * @param parent the parent object (an XmlGroup)
 317  
      */
 318  
     public void afterUnmarshal(Unmarshaller um, Object parent)
 319  
     {
 320  28
         this.parent = (Group)parent;
 321  
 
 322  
         // Build map
 323  28
         this.ruleMap.clear();
 324  28
         for (Rule rule : rules)
 325  
         {
 326  44
             ruleMap.put(rule.getName(), rule);
 327  44
         }
 328  
 
 329  
         // if a mapToProperty exists, set the object to this group's default
 330  28
         if (mapToObject == null && 
 331  
                         mapToProperty != null &&
 332  3
                         StringUtils.isNotEmpty(mapToProperty) &&
 333  3
                         this.parent.getDefaultMapToObject() != null)
 334  
         {
 335  3
                 mapToObject = this.parent.getDefaultMapToObject();
 336  
         }
 337  28
     }
 338  
 
 339  
     /**
 340  
      * String representation of the column. This
 341  
      * is an xml representation.
 342  
      *
 343  
      * @return the value of this field as an XML representation
 344  
      */
 345  
     @Override
 346  
     public String toString()
 347  
     {
 348  0
         StringBuilder result = new StringBuilder();
 349  0
         result.append(" <field name=\"").append(name).append("\"")
 350  0
             .append(" key=\"").append(key).append("\"")
 351  0
             .append(" type=\"").append(type.value()).append("\"");
 352  
 
 353  0
         if (displayName != null)
 354  
         {
 355  0
             result.append(" displayName=\"").append(displayName).append("\"");
 356  
         }
 357  0
         if (mapToObject != null)
 358  
         {
 359  0
             result.append(" mapToObject=\"").append(mapToObject).append("\"");
 360  
         }
 361  0
         if (mapToProperty != null)
 362  
         {
 363  0
             result.append(" mapToProperty=\"").append(mapToProperty).append("\"");
 364  
         }
 365  0
         if (validator != null)
 366  
         {
 367  0
             result.append(" validator=\"").append(validator).append("\"");
 368  
         }
 369  0
         if (defaultValue != null)
 370  
         {
 371  0
             result.append(" defaultValue=\"").append(defaultValue).append("\"");
 372  
         }
 373  
 
 374  0
         if (emptyValue != null)
 375  
         {
 376  0
             result.append(" emptyValue=\"").append(emptyValue).append("\"");
 377  
         }
 378  
 
 379  0
         if (rules.size() == 0)
 380  
         {
 381  0
             result.append(" />\n");
 382  
         }
 383  
         else
 384  
         {
 385  0
             result.append(">\n");
 386  0
             for (Rule rule : rules)
 387  
             {
 388  0
                 result.append(rule);
 389  0
             }
 390  0
             result.append("</field>\n");
 391  
         }
 392  
 
 393  0
         return result.toString();
 394  
     }
 395  
 
 396  
     // this methods are called during serialization
 397  
     private void writeObject(ObjectOutputStream stream)
 398  
             throws IOException
 399  
     {
 400  0
         stream.defaultWriteObject();
 401  0
     }
 402  
 
 403  
     private void readObject(ObjectInputStream stream)
 404  
             throws IOException, ClassNotFoundException
 405  
     {
 406  0
         stream.defaultReadObject();
 407  0
     }
 408  
 }