/*
* (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 org.w3c.dom.Attr;
import org.w3c.dom.Element;
import cookxml.core.DecodeEngine;
import cookxml.core.exception.CreatorException;
import cookxml.core.interfaces.Creator;
/**
* This creator is similar to IdReferenceCreator except that the constructor needs to be the
* variable name inside the root object.
*
* @cxdoc
* This tag is used to lookup an object using {@link cookxml.core.interfaces.VarLookup} inside CookXml.
* By default, CookXml uses {@link cookxml.core.DefaultVarLookup} which looks up a public variable
* inside the object passed to the VarLookup object.
*
* @cxexample
* Java code:
* <codeblock type="java">
* public class Foo
* {
* public String name = "Bar";
* public static void main (String[] args)
* {
* // an instance of Foo object is passed to CookSwing.
* // an VarLookup object which looks up variable in the CookSwing
* // instance is used by default.
* CookSwing cw = new CookSwing (new Foo ()).xmlDecode ("test.xml");
* }
* }
* </codeblock>
* <p>
* XML code:
* <codeblock type="xml">
* <vector>
* <varref ctor="name"/> <!-- looks up variable "name" and retrieve its value. -->
* </vector>
* </codeblock>
* <p>
* Note that if CookXml tag library (namespace http://cookxml.sf.net/) is installed, which is
* by default for Common, CookSwing and CookSwt tag libraries, then one can also use another
* approach:
* <p>
* <codeblock type="xml">
* <vector>
* <string varref="name"/> <!-- looks up variable "name" and retrieve its value. -->
* </vector>
* </codeblock>
* <p>
* It is also possible to retrieve private/protected/package scope variables.
* See {@link cookxml.core.CookXml#setAccessible(boolean)}.
*
* @cxattr ctor
* Specifies the variable name.
*
* @author Heng Yuan
* @version $Id: VarReferenceCreator.java 249 2007-06-07 07:18:23Z coconut $
* @since CookXml 1.0
*/
public class VarReferenceCreator implements Creator
{
public Object create (String parentNS, String parentTag, Element elm, Object parentObj, DecodeEngine decodeEngine)
{
Attr attr = elm.getAttributeNode ("ctor");
if (attr != null)
{
decodeEngine.addCurrentSkipList (attr);
return decodeEngine.getVariable (attr.getNodeValue ());
}
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;
}
}