common.src.cookxml.common.creator.ObjectCreator Maven / Gradle / Ivy
Show all versions of cookcc Show documentation
/*
* (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.creator;
import java.lang.reflect.Constructor;
import org.w3c.dom.Attr;
import org.w3c.dom.Element;
import cookxml.core.DecodeEngine;
import cookxml.core.converter.ObjectConverter;
import cookxml.core.creator.DefaultCreator;
import cookxml.core.exception.CreatorException;
import cookxml.core.interfaces.Creator;
/**
* This is a way inside XML to initiate a class that has a default constructor.
*
* @cxdoc
* It has two possible uses. See the first two examples.
*
* @cxexample
* This tag can retrieve an object using the id or the var name, just
* like {@tag idref} and {@tag varref}:
*
*
*
*
*
* @cxexample
* This tag can be used to create a new instance of arbitrary classes
* that has a default constructor. The class is looked up using the ClassLoader
* passed to CookXml.
*
* Create a JPanel object:
*
*
*
*
*
* @cxexample
* With the cookxml library (namespace http://cookxml.sf.net/
) inherited
* as the parent library of the common library, the default singleton library, one
* can "cast" the tag to a new tag to that meaningful things can be done.
*
* Create a custom JPanel object, and cast it to "panel" tag, then
* change its background color to Blue. This example uses CookSwing tag library
* (namespace http://cookxml.sf.net/cookswing/
) which uses the common tag
* library:
*
*
*
*
*
* @cxattr ctor
* Specifies the full class name or the reference to the object
* using "id:name" or "var:name" syntax.
*
* @see java.lang.Object
* @see cookxml.core.creator.DefaultCreator
* @author Heng Yuan
* @version $Id: ObjectCreator.java 249 2007-06-07 07:18:23Z coconut $
* @since CookXml 1.0
*/
public class ObjectCreator implements Creator
{
public Object create (String parentNS, String parentTag, Element elm, Object parentObj, DecodeEngine decodeEngine) throws Exception
{
Attr attr = elm.getAttributeNode ("ctor");
if (attr == null)
return null;
decodeEngine.addCurrentSkipList (attr);
String value = attr.getNodeValue ();
Object obj = null;
try
{
obj = ObjectConverter.getInstance ().convert (value, decodeEngine);
}
catch (Exception ex)
{
}
if (obj != null)
return obj;
ClassLoader loader = decodeEngine.getCookXml ().getClassLoader ();
Class cl = loader.loadClass (value);
if (cl == null)
return null;
Constructor ctor = cl.getConstructor (DefaultCreator.EMPTY_CLASS_PARAM);
if (ctor != null)
return ctor.newInstance (DefaultCreator.EMPTY_OBJECT_PARAM);
throw new CreatorException (decodeEngine, null, this, parentNS, parentTag, elm, parentObj);
}
public Object editFinished (String parentNS, String parentTag, Element elm, Object parentObj, Object obj, DecodeEngine decodeEngine)
{
return obj;
}
}