001    /*
002    // $Id: //open/util/resgen/src/org/eigenbase/xom/WrapperElementDef.java#3 $
003    // Package org.eigenbase.xom is an XML Object Mapper.
004    // Copyright (C) 2005-2005 The Eigenbase Project
005    // Copyright (C) 2005-2005 Disruptive Tech
006    // Copyright (C) 2005-2005 LucidEra, Inc.
007    // Portions Copyright (C) 2001-2005 Kana Software, Inc. and others.
008    //
009    // This library is free software; you can redistribute it and/or modify it
010    // under the terms of the GNU Lesser General Public License as published by the
011    // Free Software Foundation; either version 2 of the License, or (at your
012    // option) any later version approved by The Eigenbase Project.
013    //
014    // This library is distributed in the hope that it will be useful, 
015    // but WITHOUT ANY WARRANTY; without even the implied warranty of
016    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017    // GNU Lesser General Public License for more details.
018    // 
019    // You should have received a copy of the GNU Lesser General Public License
020    // along with this library; if not, write to the Free Software
021    // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
022    //
023    // jhyde, 31 October, 2001
024    */
025    
026    package org.eigenbase.xom;
027    import java.io.PrintWriter;
028    
029    /**
030     * <code>WrapperElementDef</code> is an {@link ElementDef} which retains the
031     * underlying XML {@link DOMWrapper}. It is used when there is no specific
032     * class for this tag.
033     *
034     * @author jhyde
035     * @since 31 October, 2001
036     * @version $Id: //open/util/resgen/src/org/eigenbase/xom/WrapperElementDef.java#3 $
037     **/
038    public class WrapperElementDef extends ElementDef
039    {
040        DOMWrapper _def;
041        Class enclosure;
042        String prefix;
043    
044        public WrapperElementDef(
045            DOMWrapper def, Class enclosure, String prefix)
046        {
047            this._def = def;
048            this.enclosure = enclosure;
049            this.prefix = prefix;
050        }
051    
052        // implement NodeDef
053        public void display(PrintWriter out, int indent)
054        {
055            out.print("<");
056            out.print(_def.getTagName());
057            String[] attributeKeys = _def.getAttributeNames();
058            for (int i = 0; i < attributeKeys.length; i++) {
059                String key = attributeKeys[i];
060                Object value = _def.getAttribute(key);
061                XOMUtil.printAtt(out, key, value.toString());
062            }
063            NodeDef[] children = getChildren();
064            if (children.length == 0) {
065                out.print("/>");
066            } else {
067                for (int i = 0, count = children.length; i < count; i++) {
068                    children[i].display(out, indent + 1);
069                }
070                out.print("</");
071                out.print(_def.getTagName());
072                out.print(">");
073            }
074        }
075    
076        // implement NodeDef
077        public void displayXML(XMLOutput out, int indent)
078        {
079            out.beginNode();
080            String tagName = _def.getTagName();
081            out.beginBeginTag(tagName);
082            String[] attributeKeys = _def.getAttributeNames();
083            for (int i = 0; i < attributeKeys.length; i++) {
084                String key = attributeKeys[i];
085                Object value = _def.getAttribute(key);
086                out.attribute(key, value.toString());
087            }
088            out.endBeginTag(tagName);
089            NodeDef[] children = getChildren();
090            for (int i = 0, count = children.length; i < count; i++) {
091                NodeDef child = children[i];
092                child.displayXML(out, indent + 1);
093            }
094            out.endTag(tagName);
095        }
096    
097        // implement NodeDef
098        public int getType()
099        {
100            return DOMWrapper.ELEMENT;
101        }
102    
103        // implement NodeDef
104        public String getName()
105        {
106            return _def.getTagName();
107        }
108    
109        // implement NodeDef
110        public NodeDef[] getChildren()
111        {
112            try {
113                DOMWrapper[] children = _def.getChildren();
114                NodeDef[] a = new NodeDef[children.length];
115                for (int i = 0; i < a.length; i++) {
116                    a[i] = ElementDef.constructElement(
117                        children[i], enclosure, prefix);
118                }
119                return a;
120            } catch (XOMException e) {
121                throw new AssertFailure(e, "in WrapperElementDef.getChildren");
122            }
123        }
124    
125        // implement NodeDef
126        public DOMWrapper getWrapper()
127        {
128            return _def;
129        }
130    }
131    
132    // End WrapperElementDef.java