Package cookxml.common.creator

Source Code of cookxml.common.creator.ObjectCreator

/*
* (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}:
* <codeblock type="xml">
*   <object ctor="id:name"/>
*   <object ctor="var:name"/>
* </codeblock>
*
* @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.
* <p>
* Create a JPanel object:
* <p>
* <codeblock type="xml">
*   <object ctor="javax.swing.JPanel"/>
* </codeblock>
*
* @cxexample
* With the cookxml library (namespace <code>http://cookxml.sf.net/</code>) 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.
* <p>
* 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 <code>http://cookxml.sf.net/cookswing/</code>) which uses the common tag
* library:
* <p>
* <codeblock type="xml">
*   <object ctor="foo.MyPanel" tag="panel" background="blue"/>
* </codeblock>
*
* @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;
  }
}
TOP

Related Classes of cookxml.common.creator.ObjectCreator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.