com.hfg.util.AttributeMgr Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.util;
import java.io.ByteArrayInputStream;
import java.lang.reflect.Constructor;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.hfg.exception.ProgrammingException;
import com.hfg.xml.HfgXML;
import com.hfg.xml.HfgXMLSerializable;
import com.hfg.xml.XMLNode;
import com.hfg.xml.XMLTag;
import com.hfg.xml.XMLizable;
//------------------------------------------------------------------------------
/**
Generic attribute manager.
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
public class AttributeMgr implements Cloneable, HfgXMLSerializable
{
private Map mAttributes;
//--------------------------------------------------------------------------
public AttributeMgr()
{
}
//--------------------------------------------------------------------------
public AttributeMgr(XMLNode inXML)
{
inXML.verifyTagName(HfgXML.ATTRIBUTES);
List extends XMLNode> attributeTags = inXML.getSubtagsByName(HfgXML.ATTRIBUTE);
for (XMLNode attrTag : attributeTags)
{
String name = attrTag.getAttributeValue(HfgXML.NAME_ATT);
String className = attrTag.getAttributeValue(HfgXML.CLASS_ATT);
if (attrTag.getAttributeValue(HfgXML.IS_NULL_ATT) != null)
{
setAttribute(name, null);
}
else
{
String value = attrTag.getUnescapedContent();
try
{
Class clazz = Class.forName(className);
if (String.class.getName().equals(className))
{
setAttribute(name, value);
}
else if (Integer.class.getName().equals(className))
{
setAttribute(name, Integer.valueOf(value));
}
else if (Long.class.getName().equals(className))
{
setAttribute(name, Long.valueOf(value));
}
else if (Float.class.getName().equals(className))
{
setAttribute(name, Float.valueOf(value));
}
else if (Double.class.getName().equals(className))
{
setAttribute(name, Double.valueOf(value));
}
else if (Boolean.class.getName().equals(className))
{
setAttribute(name, Boolean.valueOf(value));
}
else
{
try
{
Constructor constructor = clazz.getConstructor(XMLTag.class);
setAttribute(name, constructor.newInstance(new XMLTag(new ByteArrayInputStream(value.getBytes()))));
}
catch (NoSuchMethodException e)
{
Constructor constructor = clazz.getConstructor(String.class);
setAttribute(name, constructor.newInstance(value));
}
}
}
catch (Exception e)
{
throw new RuntimeException("Problem extracting the value for attribute " + StringUtil.singleQuote(name) + "!", e);
}
}
}
}
//--------------------------------------------------------------------------
public AttributeMgr setAttribute(String inName, Object inValue)
{
if (null == mAttributes)
{
mAttributes = new HashMap<>();
}
mAttributes.put(inName, inValue);
return this;
}
//--------------------------------------------------------------------------
public boolean hasAttribute(String inName)
{
return mAttributes != null && mAttributes.containsKey(inName);
}
//--------------------------------------------------------------------------
public Object getAttribute(String inName)
{
Object attr = null;
if (mAttributes != null)
{
attr = mAttributes.get(inName);
}
return attr;
}
//--------------------------------------------------------------------------
public Collection getAttributeNames()
{
Collection attrNames = null;
if (mAttributes != null)
{
attrNames = mAttributes.keySet();
}
return attrNames;
}
//--------------------------------------------------------------------------
public void clearAttributes()
{
if (mAttributes != null)
{
mAttributes.clear();
}
}
//--------------------------------------------------------------------------
public Object removeAttribute(String inName)
{
Object attr = null;
if (mAttributes != null)
{
attr = mAttributes.remove(inName);
}
return attr;
}
//--------------------------------------------------------------------------
public AttributeMgr clone()
{
AttributeMgr cloneObj;
try
{
cloneObj = (AttributeMgr) super.clone();
}
catch (CloneNotSupportedException e)
{
throw new ProgrammingException(e);
}
if (mAttributes != null)
{
cloneObj.mAttributes = new HashMap<>(mAttributes.size());
for (String key : mAttributes.keySet())
{
Object value = mAttributes.get(key);
cloneObj.mAttributes.put(key, value != null ? value : null);
}
}
return cloneObj;
}
//--------------------------------------------------------------------------
public XMLNode toXMLNode()
{
XMLNode node = new XMLTag(HfgXML.ATTRIBUTES);
for (String attrName : mAttributes.keySet())
{
XMLNode attributeTag = new XMLTag(HfgXML.ATTRIBUTE);
attributeTag.setAttribute(HfgXML.NAME_ATT, attrName);
Object value = mAttributes.get(attrName);
if (value != null)
{
attributeTag.setAttribute(HfgXML.CLASS_ATT, value.getClass().getName());
if (value instanceof XMLizable)
{
attributeTag.addContentWithoutEscaping(((XMLizable)value).toXML());
}
else
{
attributeTag.setContent(value.toString());
}
}
else
{
attributeTag.setAttribute(HfgXML.IS_NULL_ATT, 1);
}
node.addSubtag(attributeTag);
}
return node;
}
}