View Javadoc

1   /*
2    * $Id: TextNode.java,v 1.11 2005/06/01 14:36:03 jlerner Exp $
3    *
4    * Copyright (c) 1999-2004, BBN Technologies, LLC.
5    * All rights reserved.
6    * http://www.daml.org/legal/opensource/bbn_license.html
7    */
8    
9   package com.bbn.swede.core.dom;
10  
11  import org.eclipse.jface.text.IRegion;
12  import org.eclipse.jface.text.Region;
13  
14  /***
15   * Common implementation for text nodes.  A text node is any node that has no
16   * special meaning in OWL.  Primarily, this means XML prolog elements, literals,
17   * XML comments, and unparseable XML errors caught by SWeDE.
18   * @author jlerner
19   */
20  public abstract class TextNode extends OASTNode
21  {
22     /***
23      * The text of the text node.  Generally, other classes should modify the
24      * node text using the OAST API so an event is generated.  This member is
25      * package visible for the parser, which needs to be able to modify the
26      * node text without triggering any offset changes or events.
27      * @see #setText(String)
28      */
29     /*package*/ String _sText/package-summary.html">class="comment">package*/ String _sText;
30     
31     /***
32      * Creates a text node.
33      * @param sText The full text for the node.
34      */
35     public TextNode(String sText)
36     {
37        _sText = sText;
38        _iLength = sText.length();
39     }
40     
41     /* (non-Javadoc)
42      * @see com.bbn.swede.core.dom.OASTNode#generateNodeText()
43      */
44     public String generateNodeText()
45     {
46        return _sText;
47     }
48  
49     /***
50      * Factory method for creating text nodes.  Returns a node of the 
51      * specified type with no parent and no children.
52      * @param iType One of the OASTNode type constants specifying a text
53      *              node type.  This includes LITERAL, DOCTYPE, 
54      *              PROCESSING_INSTRUCTION, XML_VERSION, UNPARSEABLE,
55      *              and COMMENT.
56      * @param sText The text for the new text node
57      * @return A node of the specified type, or <code>null</code> if an unknown
58      *         or non-text node type is specified. 
59      */
60     public static TextNode create(int iType, String sText)
61     {
62        switch (iType)
63        {
64           case OASTNode.LITERAL:
65              return new Literal(sText);
66           case OASTNode.DOCTYPE:
67              return new Doctype(sText);
68           case OASTNode.PROCESSING_INSTRUCTION:
69              return new ProcessingInstruction(sText);
70           case OASTNode.XML_VERSION:
71              return new XMLVersion(sText);
72           case OASTNode.UNPARSEABLE:
73              return new UnparseableNode(sText);
74           case OASTNode.COMMENT:
75              return new XMLComment(sText);
76           default:
77              return null;
78        }
79     }
80     
81     /***
82      * Sets the full text of the text node.
83      * @param sText The new node text.
84      * @throws OASTException if this node is attached to a read-only OAST
85      */
86     public void setText(String sText) throws OASTException
87     {
88        if (getOWLAbstractSyntaxTree() != null)
89        {
90           //Swap in a new text node so an OAST event gets created and dispatched
91           OASTNode parent = getParent();
92           TextNode textNew = create(this.getNodeType(), sText);
93           textNew.accept(new NodeDisplacementVisitor(0, getOffset()));
94           parent.replace(this, textNew);
95        }
96        else
97        {
98           //Nobody's listening - change text and length directly
99           int iDisplacement = sText.length() - _sText.length();
100          getRoot().accept(new NodeDisplacementVisitor(getOffset() + 1, iDisplacement));
101          _sText = sText;
102       }
103    }
104 
105    /***
106     * Retrieves the full text of the node.
107     * @return The node text.
108     */
109    public String getText()
110    {
111       return _sText;
112    }
113 
114    /*
115     *  (non-Javadoc)
116     * @see java.lang.Object#toString()
117     */
118    public String toString()
119    {
120       return _sText;
121    }
122 
123    /* (non-Javadoc)
124     * @see com.bbn.swede.core.dom.OASTNode#simplePartitioning()
125     */
126    public IRegion[] simplePartitioning()
127    {
128       return new IRegion[]{new Region(getOffset(), getLength())};
129    }
130 
131    /*
132     *  (non-Javadoc)
133     * @see com.bbn.swede.core.dom.OASTNode#getName()
134     */
135    public String getName()
136    {
137       return "";
138    }
139    
140    /*
141     *  (non-Javadoc)
142     * @see com.bbn.swede.core.dom.OASTNode#getQName()
143     */
144    public String getQName()
145    {
146       return "";
147    }
148    
149    /*
150     *  (non-Javadoc)
151     * @see com.bbn.swede.core.dom.OASTNode#getNamespace()
152     */
153    public String getNamespace()
154    {
155       return "";
156    }
157    
158    /*
159     *  (non-Javadoc)
160     * @see com.bbn.swede.core.dom.OASTNode#duplicateType()
161     */
162    protected OASTNode duplicateType()
163    {
164       return TextNode.create(getNodeType(), getText());
165    }
166    
167    /*
168     *  (non-Javadoc)
169     * @see java.lang.Object#clone()
170     */
171    public Object clone()
172    {
173       TextNode newText = (TextNode)super.clone();
174       newText._sText = _sText;
175       return newText;
176    }
177 }