View Javadoc

1   /*
2    * $Id: OASTStringHandler.java,v 1.25 2005/05/31 23:28:07 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   package com.bbn.swede.core.dom;
9   
10  import com.bbn.swede.core.OWLCore;
11  
12  /***
13   * A customized OAST handler for String-based SAX parses.  Because random access
14   * against strings in memory is efficient, this handler locates nodes as they
15   * are parsed, rather than relying on the caller to locate all nodes en masse
16   * after the fact.
17   * @author jlerner
18   */
19  public class OASTStringHandler extends OASTHandler
20  {
21     /***
22      * The text being parsed by the SAX parser.  This should be the full text
23      * handed to the parser, including any shennanigans required to fool it into
24      * doing a partial parse.
25      */
26     protected String _sText;
27     /***
28      * A value of <code>true</code> indicates that the parser was given a dummy
29      * root tag.
30      */
31     protected boolean _bDummy;
32     
33     /***
34      * Creates an OAST string handler.  The text provided at construction should
35      * be the same node text handed to the parser.  The location code assumes
36      * there is no dummy node to ignore.
37      * @param root The node to serve as the root for the string parse.
38      * @param sText The node text being parsed.
39      */
40     public OASTStringHandler(OASTNode root, String sText)
41     {
42        super(root);
43        _sText = sText;
44        _bDummy = false;
45     }
46  
47     /***
48      * Creates an OAST string handler that optionally ignores the first tag
49      * found by the parser.  This constructor must be used when the text given
50      * to SAX had &lt;a&gt;&lt;/a&gt; wrapped around it to ensure the presence of
51      * a root tag.  In this case, the text provided to the handler should be the
52      * original text being parsed, not including the dummy root tag.
53      * @param root The node to serve as the root for the string parse.
54      * @param sText The node text being parsed.
55      * @param bDummy <code>true</code> if the parser was given a dummy root tag,
56      *               <code>false</code> if not.  A <code>true</code> value has
57      *               the effect of making the handler ignore the first tag it is
58      *               told to handle.
59      */
60     public OASTStringHandler(OASTNode root, String sText, boolean bDummy)
61     {
62        super(root);
63        _sText = sText;
64        _bDummy = bDummy;
65     }
66  
67     /*
68      *  (non-Javadoc)
69      * @see com.bbn.swede.core.dom.OASTHandler#locateNode(
70      *       com.bbn.swede.core.dom.OASTNode, com.bbn.swede.core.dom.Literal)
71      */
72     protected void locateNode(OASTNode parent, Literal lit)
73     {
74        OASTNode prev = parent.getPreviousChild(lit);
75        int iOffset;
76        if (parent.getParent() == _root && prev == null)
77        {
78           iOffset = 0;
79           //            lit.setOffset(0);
80           //            lit.setLength(_sText.length());
81           //            return;
82        }
83        else if (prev != null)
84        {
85           iOffset = prev.getOffset() + prev.getLength();
86           if (prev instanceof AttributeNode)
87           {
88              iOffset = _sText.indexOf(">", iOffset) + 1;
89           }
90        }
91        else
92        {
93           iOffset = _sText.indexOf(">", parent.getOffset()) + 1;
94        }
95        lit.setOffset(iOffset);
96        iOffset = _sText.indexOf("<", iOffset);
97        if (iOffset < 0)
98        {
99           iOffset = _sText.length();
100       }
101       lit.setLength(iOffset - lit.getOffset());
102       lit._sText = _sText.substring(lit.getOffset(), iOffset);
103 
104    }
105 
106    /*
107     *  (non-Javadoc)
108     * @see com.bbn.swede.core.dom.OASTHandler#locateNode(
109     *       com.bbn.swede.core.dom.OASTNode, com.bbn.swede.core.dom.AttributeNode)
110     */
111    protected void locateNode(OASTNode parent, AttributeNode node)
112    {
113       OASTNode prev = parent.getPreviousChild(node);
114       int iOffset =
115          _sText.indexOf(
116             node.getQName(),
117             prev == null
118                ? parent.getOffset()
119                : prev.getOffset() + prev.getLength());
120       node.setOffset(iOffset);
121 
122       int iPosSingle = _sText.indexOf("\'", iOffset);
123       int iPosDouble = _sText.indexOf("\"", iOffset);
124       if (iPosDouble >= 0 && (iPosSingle < 0 || iPosDouble < iPosSingle))
125       {
126          iOffset = _sText.indexOf("\"", iPosDouble + 1) + 1;
127       }
128       else if (iPosSingle >= 0)
129       {
130          iOffset = _sText.indexOf("\'", iPosSingle + 1) + 1;
131       }
132       else
133       {
134          OWLCore.logWarning(
135             OWLCore.getID(),
136             "The impossible has happened.",
137             new Exception());
138       }
139       node.setLength(iOffset - node.getOffset());
140 
141       iOffset = _sText.indexOf("=", node.getOffset());
142       node.setValueRegion(iOffset + 1);
143    }
144 
145    /*
146     *  (non-Javadoc)
147     * @see com.bbn.swede.core.dom.OASTHandler#locateNodeBegin(com.bbn.swede.core.dom.OASTNode)
148     */
149    protected void locateNodeBegin(OASTNode node)
150    {
151       if (_bDummy && node.getParent() == _root)
152       {
153          node.setOffset(0);
154          return;
155       }
156       if (node instanceof AttributeNode)
157       {
158          locateNode(node.getParent(), (AttributeNode) node);
159          return;
160       }
161 
162       int iOffset;
163       OASTNode nodePrev = node.getParent().getPreviousChild(node);
164       if (nodePrev != null)
165       {
166          iOffset = nodePrev.getOffset() + nodePrev.getLength();
167       }
168       else
169       {
170          OASTNode parent = node.getParent();
171          iOffset = parent.getOffset();
172          if (parent.getParent() != null && parent.getParent().getParent() != null)
173 //          if (parent.getQName().equals(tag.getQName()))
174          {
175             iOffset += parent.getQName().length() + 1;
176          }
177       }
178       iOffset = _sText.indexOf("<" + node.getQName(), iOffset);
179       node.setOffset(iOffset);
180    }
181 
182    /*
183     *  (non-Javadoc)
184     * @see com.bbn.swede.core.dom.OASTHandler#locateNodeEnd(com.bbn.swede.core.dom.OASTNode)
185     */
186    protected void locateNodeEnd(OASTNode node)
187    {
188       if (_bDummy && node.getParent() == _root)
189       {
190          node.setLength(_sText.length());
191          return;
192       }
193       int iOffset;
194       OASTNode[] children = node.getChildren();
195       if (children != null && children.length > 0)
196       {
197          OASTNode nodeLast = children[children.length - 1];
198          iOffset = nodeLast.getOffset() + nodeLast.getLength();
199       }
200       else
201       {
202          iOffset = node.getOffset() + node.getQName().length();
203       }
204       int iPosSingleton = _sText.indexOf("/>", iOffset);
205       int iPosEnd = _sText.indexOf("</" + node.getQName(), iOffset);
206       if (iPosEnd >= 0 && (iPosSingleton < 0 || iPosEnd < iPosSingleton))
207       {
208          iOffset = _sText.indexOf(">", iPosEnd) + 1;
209       }
210       else
211       {
212          iOffset = iPosSingleton + 2;
213          node.setLength(iOffset - node.getOffset());
214          if (node instanceof TagNode)
215          {
216             TagNode tag = (TagNode) node;
217             tag.setBeginRegion(iOffset - tag.getOffset());
218             tag.setEndRegion(iOffset); //should force null middle/end regions
219          }
220          return;
221       }
222       node.setLength(iOffset - node.getOffset());
223 
224       if (node instanceof TagNode)
225       {
226          TagNode tag = (TagNode) node;
227          AttributeNode att = tag.getLastAttribute();
228          iOffset =
229             (att == null
230                ? tag.getOffset() + tag.getQName().length() + 1
231                : att.getOffset() + att.getLength());
232          iOffset = _sText.indexOf(">", iOffset) + 1;
233          tag.setBeginRegion(iOffset - tag.getOffset());
234          iOffset =
235             _sText
236                .substring(tag.getOffset(), tag.getOffset() + tag.getLength())
237                .lastIndexOf("<");
238          tag.setEndRegion(tag.getOffset() + iOffset);
239       }
240    }
241 
242    /*
243     *  (non-Javadoc)
244     * @see com.bbn.swede.core.dom.OASTHandler#locateRootNode(com.bbn.swede.core.dom.OASTNode)
245     */
246    protected void locateRootNode(OASTNode root)
247    {
248       root.setOffset(0);
249       root.setLength(_sText.length());
250    }
251 
252 }