/*
* (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.IdReference;
import cookxml.core.exception.CreatorException;
import cookxml.core.interfaces.Creator;
/**
* This creator is used for idref tag. It requires the constructor to be the id of the object
* to be referenced.
*
* @cxdoc
* This tag is used to obtain an earlier object that has the id.
*
* @cxexample
* <codeblock type="xml">
* <gridbaglayout>
* <gridbagconstraint id="gc_1">
* <button/>
* </gridbagconstraint>
* <idref ctor="gc_1"> <!-- refers to the same gridbagconstraint object above -->
* <button/>
* </idref>
* </gridbaglayout>
* </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">
* <gridbaglayout>
* <gridbagconstraint id="gc_1">
* <button/>
* </gridbagconstraint>
* <gridbagconstraint idref="gc_1"> <!-- refers to the same gridbagconstraint object above -->
* <button/>
* </gridbagconstraint>
* </gridbaglayout>
* </codeblock>
*
* @cxattr ctor
* Specifies the id name.
*
* @author Heng Yuan
* @version $Id: IdReferenceCreator.java 249 2007-06-07 07:18:23Z coconut $
* @since CookXml 1.0
*/
public class IdReferenceCreator implements Creator
{
public Object create (String parentNS, String parentTag, Element elm, Object parentObj, DecodeEngine decodeEngine)
{
Attr attr = elm.getAttributeNode ("ctor");
IdReference idref = attr == null ? null : decodeEngine.getCookXml ().getId (attr.getNodeValue ());
if (idref != null)
{
decodeEngine.setCurrentTag (idref.tag);
decodeEngine.addCurrentSkipList (attr);
return idref.object;
}
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;
}
}