View Javadoc

1   /*
2    * $Id: OASTContentProvider.java,v 1.11 2005/05/19 21:01:23 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.ui;
9   
10  import java.util.ArrayList;
11  import java.util.Arrays;
12  import java.util.List;
13  
14  import org.eclipse.jface.viewers.ITreeContentProvider;
15  import org.eclipse.jface.viewers.Viewer;
16  
17  import com.bbn.swede.core.dom.IOWLAbstractSyntaxTree;
18  import com.bbn.swede.core.dom.OASTNode;
19  
20  /***
21   * Captures the tag and attribute structure of a document's OAST as a JFace
22   * content provider.  Nodes of type UNPARSEABLE are also included, but other 
23   * non-structural node types (comments and XML prolog elements) are omitted.
24   * @see OASTNodeLabelProvider
25   * @author jlerner
26   */
27  public class OASTContentProvider implements ITreeContentProvider
28  {
29     /*
30      *  (non-Javadoc)
31      * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
32      */
33     public Object[] getChildren(Object parentElement)
34     {
35        if (!(parentElement instanceof OASTNode))
36        {
37           return null;
38        }
39  
40        OASTNode node = (OASTNode) parentElement;
41        ArrayList al = new ArrayList();
42        al.addAll(Arrays.asList(node.getChildren()));
43        List lComments = Arrays.asList(node.getNodesOfType(OASTNode.COMMENT, 1));
44        al.removeAll(lComments);
45        lComments = Arrays.asList(node.getNodesOfType(OASTNode.PROCESSING_INSTRUCTION, 1));
46        al.removeAll(lComments);
47        lComments = Arrays.asList(node.getNodesOfType(OASTNode.XML_VERSION, 1));
48        al.removeAll(lComments);
49        lComments = Arrays.asList(node.getNodesOfType(OASTNode.DOCTYPE, 1));
50        al.removeAll(lComments);
51        return al.toArray();
52     }
53     /*
54      *  (non-Javadoc)
55      * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
56      */
57     public Object getParent(Object element)
58     {
59        if (!(element instanceof OASTNode))
60        {
61           return null;
62        }
63  
64        OASTNode node = (OASTNode) element;
65        return node.getParent();
66     }
67     /*
68      *  (non-Javadoc)
69      * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
70      */
71     public boolean hasChildren(Object element)
72     {
73        if (element instanceof IOWLAbstractSyntaxTree)
74        {
75           return true;
76        }
77        if (!(element instanceof OASTNode))
78        {
79           return false;
80        }
81        OASTNode node = (OASTNode) element;
82        return node.getChildren().length > 0;
83     }
84     /*
85      *  (non-Javadoc)
86      * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
87      */
88     public Object[] getElements(Object inputElement)
89     {
90        if (!(inputElement instanceof IOWLAbstractSyntaxTree))
91        {
92           return new Object[0];
93        }
94  
95        IOWLAbstractSyntaxTree tree = (IOWLAbstractSyntaxTree) inputElement;
96        ArrayList al = new ArrayList();
97        al.addAll(Arrays.asList(tree.getRoot().getChildren()));
98        List lComments = Arrays.asList(tree.getRoot().getNodesOfType(OASTNode.COMMENT, 1));
99        al.removeAll(lComments);
100       lComments = Arrays.asList(tree.getRoot().getNodesOfType(OASTNode.PROCESSING_INSTRUCTION, 1));
101       al.removeAll(lComments);
102       lComments = Arrays.asList(tree.getRoot().getNodesOfType(OASTNode.XML_VERSION, 1));
103       al.removeAll(lComments);
104       lComments = Arrays.asList(tree.getRoot().getNodesOfType(OASTNode.DOCTYPE, 1));
105       al.removeAll(lComments);
106       return al.toArray();
107    }
108    /*
109     *  (non-Javadoc)
110     * @see org.eclipse.jface.viewers.IContentProvider#dispose()
111     */
112    public void dispose()
113    {
114 
115    }
116    /*
117     *  (non-Javadoc)
118     * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(
119     *       org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
120     */
121    public void inputChanged(Viewer viewer, Object oldInput, Object newInput)
122    {
123 
124    }
125 
126 }