View Javadoc

1   /*
2    * $Id: DocumentRoot.java,v 1.18 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.core.resources.IFile;
12  import org.eclipse.jface.text.IRegion;
13  import org.eclipse.jface.text.Region;
14  
15  import com.bbn.swede.core.IOWLDocument;
16  import com.bbn.swede.core.resources.SWResourceManager;
17  
18  /***
19   * Custom node representing the entire document.  DocumentRoot is used as the
20   * root of OASTs rather than Rdf so that the tree may contain XML prolog 
21   * elements in addition to the nodes that make up the RDF graph.
22   * @author jlerner
23   */
24  public class DocumentRoot extends OASTNode
25  {
26     private IFile _file;
27     /***
28      * Creates a DocumentRoot element for a file.  This does not cause the file
29      * to be parsed, it is simply stored for reference purposes.
30      * @param file The file the root element represents.
31      * @param tree The OWL abstract syntax tree for which this node is the root
32      */
33     public DocumentRoot(IFile file, IOWLAbstractSyntaxTree tree)
34     {
35        _file = file;
36        _tree = tree;
37     }
38  
39     /***
40      * Retrieves the file the document root represents.
41      * @return The associated file.
42      */
43     public IFile getFile()
44     {
45        return _file;
46     }
47  
48     /*
49      *  (non-Javadoc)
50      * @see com.bbn.swede.core.dom.OASTNode#allowedChildren()
51      */
52     protected int[] allowedChildren()
53     {
54        return null;
55     }
56  
57     /*
58      *  (non-Javadoc)
59      * @see com.bbn.swede.core.dom.OASTNode#getNodeType()
60      */
61     public int getNodeType()
62     {
63        return DOCUMENT;
64     }
65  
66     /*
67      *  (non-Javadoc)
68      * @see com.bbn.swede.core.dom.OASTNode#getName()
69      */
70     public String getName()
71     {
72        return "";
73     }
74  
75     /*
76      *  (non-Javadoc)
77      * @see com.bbn.swede.core.dom.OASTNode#getNamespace()
78      */
79     public String getNamespace()
80     {
81        return "";
82     }
83  
84     /*
85      *  (non-Javadoc)
86      * @see com.bbn.swede.core.dom.OASTNode#getQName()
87      */
88     public String getQName()
89     {
90        return "";
91     }
92  
93     /*
94      *  (non-Javadoc)
95      * @see java.lang.Object#toString()
96      */
97     public String toString()
98     {
99        return "Document root";
100    }
101    
102    /*
103     *  (non-Javadoc)
104     * @see com.bbn.swede.core.dom.OASTNode#generateNodeText()
105     */
106    public String generateNodeText()
107    {
108       //TODO: do this right
109       return "";
110    }
111 
112    /***
113     * Returns the XML base for the associated file, as stored in the project
114     * metadata.  This override of the standard findXmlBase() functionality
115     * ensures that the recursion in OASTNode.getXmlBase() always returns a 
116     * value.
117     * @return A new Base node constructed based on the base URI stored in the
118     *         project metadata.
119     * @see com.bbn.swede.core.dom.OASTNode#getXmlBase()
120     * @see com.bbn.swede.core.dom.OASTNode#findXmlBase()
121     */
122    protected Base findXmlBase()
123    {
124       IOWLDocument doc =
125          (IOWLDocument) SWResourceManager.getModel().getCorrespondingElement(_file);
126       if (doc == null)
127       {
128          return null;
129       }
130       return new Base(doc.getURI());
131    }
132 
133    /*
134     *  (non-Javadoc)
135     * @see com.bbn.swede.core.dom.OASTNode#simplePartitioning()
136     */
137    public IRegion[] simplePartitioning()
138    {
139       return new IRegion[] {new Region(getOffset(), getLength())};
140    }
141    
142    /***
143     * Lengthens the root node to accomodate a character displacement.  This
144     * is different than the standard displace behavior, which would adjust the
145     * node's starting position if the displacement offset is 0.  The root must
146     * always start at offset 0, so all displacements must be handled by
147     * adjusting the length.
148     * @param offset The location of the displacement.
149     * @param length The magnitude of the displacement.  May be negative.
150     * @return <code>true</code> to ensure that the appropriate child nodes are
151     *         displaced.
152     * @see com.bbn.swede.core.dom.OASTNode#displace(int, int)
153     */
154    public boolean displace(int offset, int length)
155    {
156       //The document root offset must always be zero, so displacements
157       //that affect the root should just adjust its length
158       setLength(getLength() + length);
159       return true;
160    }
161 
162    /* (non-Javadoc)
163     * @see com.bbn.swede.core.dom.OASTNode#duplicateType()
164     */
165    protected OASTNode duplicateType()
166    {
167       return new DocumentRoot(_file, _tree);
168    }
169 
170    /*
171     *  (non-Javadoc)
172     * @see java.lang.Object#clone()
173     */
174    public Object clone()
175    {
176       DocumentRoot newRoot = (DocumentRoot)super.clone();
177       newRoot._file = _file;
178       return newRoot;
179    }
180 }