org.apache.fulcrum.intake.model.XmlField Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fulcrum-intake Show documentation
Show all versions of fulcrum-intake Show documentation
This Service provides rule-based input validation
package org.apache.fulcrum.intake.model;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
import org.apache.avalon.framework.logger.LogEnabled;
import org.apache.avalon.framework.logger.Logger;
import org.apache.commons.lang.StringUtils;
/**
* A Class for holding data about a property used in an Application.
*
* @author John McNally
* @author Henning P. Schmiedehausen
* @author Quinton McCombs
* @author Thomas Vandahl
* @version $Id: XmlField.java 1673447 2015-04-14 15:47:06Z tv $
*/
@XmlType(name="field")
@XmlAccessorType(XmlAccessType.NONE)
public class XmlField
implements Serializable, LogEnabled
{
/**
* Serial version id
*/
private static final long serialVersionUID = -734309157828058007L;
@XmlAttribute(required=true)
private String key;
@XmlAttribute(required=true)
private String name;
@XmlAttribute
private String displayName;
@XmlAttribute
private String displaySize;
@XmlAttribute
private String type = "String";
@XmlAttribute
private boolean multiValued = false;
@XmlAttribute
private String fieldClass;
@XmlAttribute
private String mapToObject;
@XmlAttribute
private String mapToProperty;
@XmlAttribute
private String validator;
@XmlAttribute
private String defaultValue;
@XmlAttribute
private String emptyValue;
private List rules;
private Map ruleMap;
private Group parent;
private Logger log;
/**
* Default Constructor
*/
public XmlField()
{
ruleMap = new HashMap();
}
/**
* Enable Avalon Logging
*/
@Override
public void enableLogging(Logger logger)
{
this.log = logger;
}
/**
* Return Avalon logger
*/
public Logger getLogger()
{
return log;
}
/**
* Get the name of the property
*/
public String getRawName()
{
return name;
}
/**
* Get the name of the property
*/
public String getName()
{
return StringUtils.replace(name, "_", "");
}
/**
* Get the display name of the property
*/
public String getDisplayName()
{
return displayName;
}
/**
* Gets the display size of the field. This is
* useful for constructing the HTML input tag.
*/
public String getDisplaySize()
{
return this.displaySize;
}
/**
* Get the parameter key of the property
*/
public String getKey()
{
return key;
}
/**
* Get the type of the property
*/
public String getType()
{
return type;
}
/**
* can this field have several values?
*/
public boolean isMultiValued()
{
return multiValued;
}
/**
* Get the name of the object that takes this input
*/
public String getMapToObject()
{
return mapToObject;
}
/**
* Get the property method that takes this input
*/
public String getMapToProperty()
{
if (mapToProperty == null)
{
return getName();
}
else
{
return mapToProperty;
}
}
/**
* Get the className of the validator
*/
public String getValidator()
{
return validator;
}
/**
* Get the default Value.
*
* @return The default value for this field.
*/
public String getDefaultValue()
{
return defaultValue;
}
/**
* Get the empty Value.
*
* @return The empty value for this field.
*/
public String getEmptyValue()
{
return emptyValue;
}
/**
* Get the parent XmlGroup of the field
*/
public Group getGroup()
{
return this.parent;
}
/**
* Get the value of fieldClass.
*
* @return value of fieldClass.
*/
public String getFieldClass()
{
return fieldClass;
}
/**
* The collection of rules for this field.
*
* @return a List
value
*/
public List getRules()
{
return rules;
}
/**
* Set the collection of rules for this field
*
* @param rules the rules to set
*/
@XmlElement(name="rule")
public void setRules(List rules)
{
this.rules = rules;
this.ruleMap.clear();
for (Rule rule : rules)
{
ruleMap.put(rule.getName(), rule);
}
}
/**
* The collection of rules for this field keyed by
* parameter name.
*
* @return a Map
value
*/
public Map getRuleMap()
{
return ruleMap;
}
/**
* JAXB callback to set the parent object
*
* @param um the Unmarshaller
* @param parent the parent object (an XmlGroup)
*/
public void afterUnmarshal(Unmarshaller um, Object parent)
{
this.parent = (Group)parent;
if (mapToObject == null)
{
// if a mapToProperty exists, set the object to this group's default
if (mapToProperty != null
&& !"".equals(mapToProperty)
&& this.parent.getDefaultMapToObject() != null)
{
mapToObject = this.parent.getDefaultMapToObject();
}
}
}
/**
* String representation of the column. This
* is an xml representation.
*/
@Override
public String toString()
{
StringBuilder result = new StringBuilder();
result.append(" \n");
}
else
{
result.append(">\n");
for (Rule rule : rules)
{
result.append(rule);
}
result.append("\n");
}
return result.toString();
}
// this methods are called during serialization
private void writeObject(ObjectOutputStream stream)
throws IOException
{
stream.defaultWriteObject();
}
private void readObject(ObjectInputStream stream)
throws IOException, ClassNotFoundException
{
stream.defaultReadObject();
}
}