001    /*
002    // $Id: //open/util/resgen/src/org/eigenbase/xom/TextDef.java#4 $
003    // Package org.eigenbase.xom is an XML Object Mapper.
004    // Copyright (C) 2005-2008 The Eigenbase Project
005    // Copyright (C) 2005-2008 Disruptive Tech
006    // Copyright (C) 2005-2008 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, 5 October, 2001
024    */
025    
026    package org.eigenbase.xom;
027    import java.io.PrintWriter;
028    
029    /**
030     * A <code>TextDef</code> represents piece of textual data in an XML document.
031     * Free text (such as <code>Some text</code>) is represented by an actual
032     * <code>TextDef</code>; comments (such as <code>&lt-- a comment --&gt;</code>)
033     * by derived class {@link CommentDef}; and CDATA sections (such as
034     * <code>&lt;![CDATA[Some text]]&gt;</code>) by derived class {@link CdataDef}.
035     *
036     * @author jhyde
037     * @since 5 October, 2001
038     * @version $Id: //open/util/resgen/src/org/eigenbase/xom/TextDef.java#4 $
039     **/
040    public class TextDef implements NodeDef {
041    
042        public String s;
043    
044        /**
045         * Whether to print the data as is -- never quote as a CDATA
046         * section. Useful if the fragment contains a valid XML string.
047         **/
048        boolean asIs;
049    
050        private Location location;
051    
052        public TextDef()
053        {
054            this(null, false, null);
055        }
056    
057        public TextDef(String s)
058        {
059            this(s, false, null);
060        }
061    
062        public TextDef(String s, boolean asIs)
063        {
064            this(s, asIs, null);
065        }
066    
067        public TextDef(String s, boolean asIs, Location location)
068        {
069            this.s = s;
070            this.asIs = asIs;
071            this.location = location;
072        }
073    
074        public TextDef(org.eigenbase.xom.DOMWrapper _def)
075            throws org.eigenbase.xom.XOMException
076        {
077            switch (_def.getType()) {
078            case DOMWrapper.FREETEXT:
079            case DOMWrapper.CDATA:
080            case DOMWrapper.COMMENT:
081                break;
082            default:
083                throw new XOMException(
084                    "cannot make CDATA/PCDATA element from a " + _def.getType());
085            }
086            this.s = _def.getText();
087            this.location = _def.getLocation();
088        }
089    
090        // override ElementDef
091        public String getName()
092        {
093            return null;
094        }
095    
096        // override ElementDef
097        public String getText()
098        {
099            return s;
100        }
101    
102        // implement NodeDef
103        public NodeDef[] getChildren()
104        {
105            return XOMUtil.emptyNodeArray;
106        }
107    
108        // implement NodeDef
109        public DOMWrapper getWrapper()
110        {
111            return null;
112        }
113    
114        // implement NodeDef
115        public int getType()
116        {
117            return DOMWrapper.FREETEXT;
118        }
119    
120        // implement NodeDef
121        public void display(PrintWriter pw, int indent)
122        {
123            pw.print(s);
124        }
125    
126        // override NodeDef
127        public void displayXML(XMLOutput out, int indent)
128        {
129            if (out.getIgnorePcdata()) {
130                return;
131            }
132            out.beginNode();
133            if (asIs) {
134                out.print(s);
135            } else {
136                boolean quote = true;
137                out.cdata(s, quote);
138            }
139        }
140    
141        // implement NodeDef
142        public Location getLocation()
143        {
144            return location;
145        }
146    }
147    
148    // End TextDef.java