Skip to main content
summaryrefslogtreecommitdiffstats
blob: acf7b06703c97fc3dddd6501ff25b2eca71e24b5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*******************************************************************************
 * Copyright (c) 2001, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.wsdl.ui.internal.text;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.hyperlink.IHyperlink;
import org.eclipse.jface.text.hyperlink.IHyperlinkDetector;
import org.eclipse.wst.sse.core.StructuredModelManager;
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion;
import org.eclipse.wst.sse.core.utils.StringUtils;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

/**
 * Base class for hyperlinks detectors. Provides a framework and common code for
 * hyperlink detectors. TODO: Can we pull this class further up the inheritance
 * hierarchy?
 */
public abstract class BaseHyperlinkDetector implements IHyperlinkDetector
{
  /*
   * (non-Javadoc)
   */
  public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks)
  {
    if (region == null || textViewer == null)
    {
      return null;
    }

    List hyperlinks = new ArrayList(0);
    IDocument document = textViewer.getDocument();
    int offset = region.getOffset();

    IDOMNode node = getCurrentNode(document, offset);

    // This call allows us to determine whether an attribute is linkable,
    // without incurring the cost of asking for the target component.

    if (!isLinkable(node))
    {
      return null;
    }

    IRegion hyperlinkRegion = getHyperlinkRegion(node);

    // createHyperlink is a template method. Derived classes, should override.

    IHyperlink hyperlink = createHyperlink(document, node, hyperlinkRegion);

    if (hyperlink != null)
    {
      hyperlinks.add(hyperlink);
    }

    if (hyperlinks.size() == 0)
    {
      return null;
    }

    return (IHyperlink[]) hyperlinks.toArray(new IHyperlink[0]);
  }

  /**
   * Determines whether a node is "linkable" that is, the component it refers to
   * can be the target of a "go to definition" navigation.
   * 
   * @param node the node to test, must not be null;
   * @return true if the node is linkable, false otherwise.
   */
  private boolean isLinkable(IDOMNode node)
  {
    if (node == null)
    {
      return false;
    }

    short nodeType = node.getNodeType();

    boolean isLinkable = false;

    if (nodeType == Node.ATTRIBUTE_NODE)
    {
      IDOMAttr attr = (IDOMAttr) node;
      String name = attr.getName();

      // isLinkableAttribute is a template method. Derived classes should
      // override.

      isLinkable = isLinkableAttribute(name);
    }

    return isLinkable;
  }

  /**
   * Determines whether an attribute is "linkable" that is, the component it
   * points to can be the target of a "go to definition" navigation. Derived
   * classes should override.
   * 
   * @param name the attribute name. Must not be null.
   * @return true if the attribute is linkable, false otherwise.
   */
  protected abstract boolean isLinkableAttribute(String name);

  /**
   * Creates a hyperlink based on the selected node. Derived classes should
   * override.
   * 
   * @param document the source document.
   * @param node the node under the cursor.
   * @param region the text region to use to create the hyperlink.
   * @return a new IHyperlink for the node or null if one cannot be created.
   */
  protected abstract IHyperlink createHyperlink(IDocument document, IDOMNode node, IRegion region);

  /**
   * Locates the attribute node under the cursor.
   * 
   * @param offset the cursor offset.
   * @param parent the parent node
   * @return an IDOMNode representing the attribute if one is found at the
   *         offset or null otherwise.
   */
  protected IDOMNode getAttributeNode(int offset, IDOMNode parent)
  {
    IDOMAttr attrNode = null;
    NamedNodeMap map = parent.getAttributes();

    for (int index = 0; index < map.getLength(); index++)
    {
      attrNode = (IDOMAttr) map.item(index);
      boolean located = attrNode.contains(offset);
      if (located)
      {
        if (attrNode.hasNameOnly())
        {
          attrNode = null;
        }
        break;
      }
    }

    if (attrNode == null)
    {
      return parent;
    }
    return attrNode;
  }

  /**
   * Returns the node the cursor is currently on in the document or null if no
   * node is selected
   * 
   * @param offset the current cursor offset.
   * @return IDOMNode either element, doctype, text, attribute or null
   */
  private IDOMNode getCurrentNode(IDocument document, int offset)
  {
    IndexedRegion inode = null;
    IStructuredModel sModel = null;

    try
    {
      sModel = StructuredModelManager.getModelManager().getExistingModelForRead(document);
      inode = sModel.getIndexedRegion(offset);
      if (inode == null)
        inode = sModel.getIndexedRegion(offset - 1);
    }
    finally
    {
      if (sModel != null)
        sModel.releaseFromRead();
    }

    if (inode instanceof IDOMNode)
    {
      IDOMNode node = (IDOMNode) inode;

      if (node.hasAttributes())
      {
        node = getAttributeNode(offset, node);
      }
      return node;
    }

    return null;
  }

  /**
   * Get the text region corresponding to an IDOMNode.
   * 
   * @param node the node for which we want the text region. Must not be null.
   * @return an IRegion for the node, or null if the node is not recognized.
   */
  protected IRegion getHyperlinkRegion(IDOMNode node)
  {
    if (node == null)
    {
      return null;
    }

    IRegion hyperRegion = null;
    short nodeType = node.getNodeType();

    switch (nodeType)
    {
      case Node.ELEMENT_NODE : 
        {
          hyperRegion = new Region(node.getStartOffset(), node.getEndOffset() - node.getStartOffset());
        }
      break;
      case Node.ATTRIBUTE_NODE : 
        {
          IDOMAttr att = (IDOMAttr) node;
  
          int regOffset = att.getValueRegionStartOffset();
  
          // ISSUE: We are using a deprecated method here. Is there
          // a better way to get what we need?
  
          ITextRegion valueRegion = att.getValueRegion();
          if (valueRegion != null)
          {
            int regLength = valueRegion.getTextLength();
            String attValue = att.getValueRegionText();
  
            // Do not include quotes in attribute value region.
            if (StringUtils.isQuoted(attValue))
            {
              regOffset = ++regOffset;
              regLength = regLength - 2;
            }
            hyperRegion = new Region(regOffset, regLength);
          }
        }
        break;
      default :
        // Do nothing.
        break;
    }
 
    return hyperRegion;
  }
}

Back to the top