/*
* (c) Copyright 2004 by Heng Yuan
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* ITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package cookxml.common;
import java.beans.ExceptionListener;
import java.beans.VetoableChangeListener;
import java.net.URL;
import java.text.ChoiceFormat;
import java.text.DecimalFormat;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.util.*;
import cookxml.common.adder.MapEntryAdder;
import cookxml.common.converter.DateConverter;
import cookxml.common.converter.LocaleConverter;
import cookxml.common.creator.*;
import cookxml.common.helper.*;
import cookxml.common.setter.PropertyChangeListenerSetter;
import cookxml.common.setter.ResourceBundleSetter;
import cookxml.common.util.Utils;
import cookxml.core.adder.DefaultAdder;
import cookxml.core.adder.DoNothingAdder;
import cookxml.core.converter.*;
import cookxml.core.creator.*;
import cookxml.core.exception.CookXmlException;
import cookxml.core.interfaces.TagLibrary;
import cookxml.core.setter.CallFunctionSetter;
import cookxml.core.setter.DefaultSetter;
import cookxml.core.setter.DoNothingSetter;
import cookxml.core.taglibrary.InheritableTagLibrary;
import cookxml.core.util.DocumentElement;
/**
* Setup common component tags.
*
* @author Heng Yuan
* @version $Id: CommonLib.java 267 2007-06-10 18:48:26Z coconut $
* @since CookXml 1.0
*/
public class CommonLib
{
/** this is the namespace for CookXml Common tags */
public static String NAMESPACE = "http://cookxml.sf.net/common/";
//
// internal tags
//
public final static String MAP = "map";
public final static String LISTENERS = "listeners";
//
// special tags
//
public final static String NOADD = "noadd";
public final static String BASE = "base";
/**
* Setup common component tags for a tag library. Call this function only if you
* intend to setup tags manually.
*
* @param tagLibrary
* the tag library to be setup
* @throws CookXmlException
* thrown if errors occurred.
* @since CookXml 1.0
*/
public static void setupTags (InheritableTagLibrary tagLibrary) throws CookXmlException
{
// converters
//
// primitive types
// misc
//
// default setter and adder if no setters or no adders are being specified
//
tagLibrary.setSetter (null, null, DefaultSetter.getInstance ());
tagLibrary.setSetter (null, "resourcebundle", new ResourceBundleSetter ());
tagLibrary.setSetter (null, "propertychangelistener", new PropertyChangeListenerSetter ());
tagLibrary.addAdder (null, DefaultAdder.getInstance ());
// noadd tag allows one to perform a list of actions such as setting up a
// list of objects without add anything subsequently
tagLibrary.setCreator (NOADD, DefaultCreator.getCreator (NoAddHelper.class));
tagLibrary.addAdder (NOADD, DoNothingAdder.getInstance ());
tagLibrary.setCreator (BASE, new BaseCreator ());
tagLibrary.addAdder (BASE, DoNothingAdder.getInstance ());
//
// generic loaders
//
tagLibrary.setCreator ("varref", new VarReferenceCreator ());
tagLibrary.setSetter ("varref", "ctor", DoNothingSetter.getInstance ());
tagLibrary.setCreator ("idref", new IdReferenceCreator ());
tagLibrary.setSetter ("idref", "ctor", DoNothingSetter.getInstance ());
tagLibrary.setCreator ("object", new ObjectCreator ());
tagLibrary.setSetter ("object", "ctor", DoNothingSetter.getInstance ());
tagLibrary.setCreator ("null", NullCreator.getInstance ());
tagLibrary.setCreator ("include", IncludeCreator.getInstance ());
tagLibrary.setSetter ("include", "ctor", DoNothingSetter.getInstance ());
tagLibrary.setConverter (DocumentElement.class, DocumentElementConverter.getInstance ());
//
// basic object types
//
// Boolean
tagLibrary.setCreator ("boolean", HelperCreator.getCreator (BooleanHelper.class));
tagLibrary.setConverter (Boolean.class, BooleanConverter.getInstance ());
tagLibrary.setConverter (boolean.class, BooleanConverter.getInstance ());
// Byte
tagLibrary.setCreator ("byte", HelperCreator.getCreator (ByteHelper.class));
tagLibrary.setConverter (Byte.class, ByteConverter.getInstance ());
tagLibrary.setConverter (byte.class, ByteConverter.getInstance ());
// Short
tagLibrary.setCreator ("short", HelperCreator.getCreator (ShortHelper.class));
tagLibrary.setConverter (Short.class, ShortConverter.getInstance ());
tagLibrary.setConverter (short.class, ShortConverter.getInstance ());
// Character
tagLibrary.setCreator ("char", HelperCreator.getCreator (CharHelper.class));
tagLibrary.setConverter (Character.class, CharConverter.getInstance ());
tagLibrary.setConverter (char.class, CharConverter.getInstance ());
// Integer
tagLibrary.setCreator ("int", HelperCreator.getCreator (IntegerHelper.class));
tagLibrary.setConverter (Integer.class, IntConverter.getInstance ());
tagLibrary.setConverter (int.class, IntConverter.getInstance ());
// Float
tagLibrary.setCreator ("float", HelperCreator.getCreator (FloatHelper.class));
tagLibrary.setConverter (Float.class, FloatConverter.getInstance ());
tagLibrary.setConverter (float.class, FloatConverter.getInstance ());
// Double
tagLibrary.setCreator ("double", HelperCreator.getCreator (DoubleHelper.class));
tagLibrary.setConverter (Double.class, DoubleConverter.getInstance ());
tagLibrary.setConverter (double.class, DoubleConverter.getInstance ());
// String
tagLibrary.setCreator ("string", HelperCreator.getCreator (StringHelper.class));
tagLibrary.setCreator ("text", new TextCreator ());
tagLibrary.setSetter ("text", "src", DoNothingSetter.getInstance ());
tagLibrary.setCreator ("html", new HtmlCreator ());
// Locale
tagLibrary.setCreator ("locale", HelperCreator.getCreator (LocaleHelper.class));
tagLibrary.setConverter (Locale.class, LocaleConverter.getInstance ());
// Date
tagLibrary.setCreator ("date", HelperCreator.getCreator (DateHelper.class));
tagLibrary.setConverter (Date.class, new DateConverter ());
// URL
tagLibrary.setCreator ("url", HelperCreator.getCreator (URLHelper.class));
tagLibrary.setConverter (URL.class, URLConverter.getInstance ());
// Object array
tagLibrary.setCreator ("array", new ArrayCreator ());
// simple containers
tagLibrary.setCreator ("vector", DefaultCreator.getCreator (Vector.class, true));
tagLibrary.setCreator ("stack", DefaultCreator.getCreator (Stack.class, true));
tagLibrary.setCreator ("linkedlist", DefaultCreator.getCreator (LinkedList.class, true));
// sets
tagLibrary.setCreator ("treeset", DefaultCreator.getCreator (TreeSet.class, true));
tagLibrary.setCreator ("hashset", DefaultCreator.getCreator (HashSet.class, true));
//
// maps
//
tagLibrary.addAdder (MAP, new MapEntryAdder ());
// helper object for maps
tagLibrary.setCreator ("mapentry", DefaultCreator.getCreator (MapEntryHelper.class, true));
// HashMap
tagLibrary.setCreator ("hashmap", DefaultCreator.getCreator (HashMap.class, true));
tagLibrary.inheritTag (MAP, "hashmap");
// Hashtable
tagLibrary.setCreator ("hashtable", DefaultCreator.getCreator (Hashtable.class, true));
tagLibrary.inheritTag (MAP, "hashtable");
// TreeMap
tagLibrary.setCreator ("treemap", DefaultCreator.getCreator (TreeMap.class, true));
tagLibrary.inheritTag (MAP, "treemap");
// WeakHashMap
tagLibrary.setCreator ("weakhashmap", DefaultCreator.getCreator (WeakHashMap.class, true));
tagLibrary.inheritTag (MAP, "weakhashmap");
// IdentityHashmap
tagLibrary.setCreator ("identityhashmap", DefaultCreator.getCreator (IdentityHashMap.class, true));
tagLibrary.inheritTag (MAP, "identityhashmap");
// Properties
tagLibrary.setCreator ("properties", DefaultCreator.getCreator (Properties.class, true));
tagLibrary.inheritTag (MAP, "properties");
// formatters
tagLibrary.setCreator ("dateformat", new DateFormatCreator ());
tagLibrary.setSetter ("dateformat", "datestyle", DoNothingSetter.getInstance ());
tagLibrary.setSetter ("dateformat", "timestyle", DoNothingSetter.getInstance ());
tagLibrary.setSetter ("dateformat", "type", DoNothingSetter.getInstance ());
tagLibrary.setCreator ("simpledateformat", HelperCreator.getCreator (SimpleDateFormatHelper.class));
tagLibrary.setCreator ("messageformat", new MessageFormatCreator ());
tagLibrary.setSetter ("messageformat", "pattern", new CallFunctionSetter ("applyPattern", MessageFormat.class, String.class));
tagLibrary.setCreator ("numberformat", GetInstanceCreator.getCreator (NumberFormat.class, "getInstance", false));
tagLibrary.setCreator ("decimalformat", DefaultCreator.getCreator (DecimalFormat.class, true));
tagLibrary.setSetter ("decimalformat", "pattern", new CallFunctionSetter ("applyPattern", DecimalFormat.class, String.class));
tagLibrary.setSetter ("decimalformat", "localizedpattern", new CallFunctionSetter ("applyLocalizedPattern", DecimalFormat.class, String.class));
tagLibrary.setCreator ("choiceformat", new ChoiceFormatCreator ());
tagLibrary.setSetter ("choiceformat", "pattern", new CallFunctionSetter ("applyPattern", ChoiceFormat.class, String.class));
//
// listeners
//
// bean listeners
Utils.addListenerSetter (tagLibrary, LISTENERS, ExceptionListener.class);
// PropertyChangeListener is been taken cared by PropertyChangeListenerSetter
//addListenerSetter (taglibrary, LISTENERS, PropertyChangeListener.class);
Utils.addListenerSetter (tagLibrary, LISTENERS, VetoableChangeListener.class);
}
/**
* Create a new instance of common tag library.
* @param parentTagLibrary
* the parent tag library
* @return the nwe instance of common tag library.
*/
public static InheritableTagLibrary createTagLibrary (TagLibrary parentTagLibrary)
{
InheritableTagLibrary tagLibrary = new InheritableTagLibrary (parentTagLibrary);
tagLibrary.setNameSpace (NAMESPACE);
setupTags (tagLibrary);
return tagLibrary;
}
/**
* Get the singleton instance of the Common tag library. Use this function
* if you want to conserve the memory in setting up multiple tag libraries that
* uses the common tag library.
* <p>
* This instance of library is not created until this functio is called.
*
* @return the global instance of the
*/
public static InheritableTagLibrary getSingletonTagLibrary ()
{
return CommonLibSingleton.getTagLibrary ();
}
private CommonLib ()
{
}
}