Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: c04aebd59551d4a3c0fa82d229153177456080da (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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*******************************************************************************
 * Copyright (c) 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.xsd.ui.internal.editor;

import java.util.List;

import org.eclipse.xsd.XSDAttributeDeclaration;
import org.eclipse.xsd.XSDAttributeGroupDefinition;
import org.eclipse.xsd.XSDConcreteComponent;
import org.eclipse.xsd.XSDElementDeclaration;
import org.eclipse.xsd.XSDIdentityConstraintDefinition;
import org.eclipse.xsd.XSDModelGroupDefinition;
import org.eclipse.xsd.XSDSchema;
import org.eclipse.xsd.XSDSchemaDirective;
import org.eclipse.xsd.XSDSimpleTypeDefinition;
import org.eclipse.xsd.XSDTypeDefinition;
import org.eclipse.xsd.XSDVariety;
import org.eclipse.xsd.util.XSDConstants;
import org.eclipse.xsd.util.XSDSwitch;

/**
 * A custom XSDSwitch used to locate the "referenced" component. Used by the
 * hyperlink/F3 navigation mechanism. Made a separate class because it is used
 * from the WSDL editor as well.
 */
public class XSDHyperlinkTargetLocator extends XSDSwitch
{
  /**
   * Holds the attribute name if the cursor/mouse is over an attribute.
   */
  private String attributeName;

  /*
   * (non-Javadoc)
   * 
   * @see org.eclipse.xsd.util.XSDSwitch#caseXSDAttributeDeclaration(org.eclipse.xsd.XSDAttributeDeclaration)
   */
  public Object caseXSDAttributeDeclaration(XSDAttributeDeclaration attributeDeclaration)
  {
    XSDConcreteComponent target = null;

    if (attributeDeclaration.isAttributeDeclarationReference())
    {
      target = attributeDeclaration.getResolvedAttributeDeclaration();
    }
    else if (attributeDeclaration.getAnonymousTypeDefinition() == null)
    {
      target = attributeDeclaration.getTypeDefinition();

      // Avoid navigating to built in data types.

      if (isFromSchemaForSchema(target))
      {
        target = null;
      }
    }
    return target;
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.eclipse.xsd.util.XSDSwitch#caseXSDAttributeGroupDefinition(org.eclipse.xsd.XSDAttributeGroupDefinition)
   */
  public Object caseXSDAttributeGroupDefinition(XSDAttributeGroupDefinition attributeGroupDefinition)
  {
    XSDConcreteComponent target = null;

    if (attributeGroupDefinition.isAttributeGroupDefinitionReference())
    {
      target = attributeGroupDefinition.getResolvedAttributeGroupDefinition();
    }

    return target;
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.eclipse.xsd.util.XSDSwitch#caseXSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration)
   */
  public Object caseXSDElementDeclaration(XSDElementDeclaration elementDeclaration)
  {
    XSDConcreteComponent target = null;

    if (elementDeclaration.isElementDeclarationReference())
    {
      target = elementDeclaration.getResolvedElementDeclaration();
    }
    else
    {
      XSDConcreteComponent typeDefinition = null;

      if (elementDeclaration.getAnonymousTypeDefinition() == null)
      {
        typeDefinition = elementDeclaration.getTypeDefinition();
      }

      XSDConcreteComponent substitutionGroupAffiliation = elementDeclaration.getSubstitutionGroupAffiliation();

      if (typeDefinition != null && substitutionGroupAffiliation != null)
      {
        // There are 2 things we can navigate to: if the cursor is anywhere on
        // the
        // substitution attribute then jump to that, otherwise just go to the
        // base type.

        if (XSDConstants.SUBSTITUTIONGROUP_ATTRIBUTE.equals(attributeName))
        {
          target = substitutionGroupAffiliation;
        }
        else
        {
          target = typeDefinition;
        }
      }
      else
      {
        target = typeDefinition != null ? typeDefinition : substitutionGroupAffiliation;
      }

      if (isFromSchemaForSchema(target))
      {
        target = null;
      }
    }

    return target;
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.eclipse.xsd.util.XSDSwitch#caseXSDIdentityConstraintDefinition(org.eclipse.xsd.XSDIdentityConstraintDefinition)
   */
  public Object caseXSDIdentityConstraintDefinition(XSDIdentityConstraintDefinition idConstraintDefinition)
  {
    Object target = null;

    XSDIdentityConstraintDefinition referencedKey = idConstraintDefinition.getReferencedKey();

    if (referencedKey != null)
    {
      target = referencedKey;
    }

    return target;
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.eclipse.xsd.util.XSDSwitch#caseXSDModelGroupDefinition(org.eclipse.xsd.XSDModelGroupDefinition)
   */
  public Object caseXSDModelGroupDefinition(XSDModelGroupDefinition modelGroupDefinition)
  {
    XSDConcreteComponent target = null;

    if (modelGroupDefinition.isModelGroupDefinitionReference())
    {
      target = modelGroupDefinition.getResolvedModelGroupDefinition();
    }

    return target;
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.eclipse.xsd.util.XSDSwitch#caseXSDSchemaDirective(org.eclipse.xsd.XSDSchemaDirective)
   */
  public Object caseXSDSchemaDirective(XSDSchemaDirective directive)
  {
    XSDSchema schema = directive.getResolvedSchema();

    return schema;
  }

  public Object caseXSDSimpleTypeDefinition(XSDSimpleTypeDefinition typeDefinition)
  {
    XSDConcreteComponent target = null;

    // Simple types can be one of: atomic, list or union.

    XSDVariety variety = typeDefinition.getVariety();
    int varietyType = variety.getValue();

    switch (varietyType)
    {
      case XSDVariety.ATOMIC:
        {
          target = typeDefinition.getBaseTypeDefinition();
        }
        break;
      case XSDVariety.LIST:
        {
          target = typeDefinition.getItemTypeDefinition();
        }
        break;
      case XSDVariety.UNION:
        {
          List memberTypes = typeDefinition.getMemberTypeDefinitions();
          if (memberTypes != null && memberTypes.size() > 0)
          {
            // ISSUE: What if there are more than one type?
            // This could be a case for multiple hyperlinks at the same
            // location.
            target = (XSDConcreteComponent) memberTypes.get(0);
          }
        }
        break;
    }

    // Avoid navigating to built in data types.

    if (isFromSchemaForSchema(target))
    {
      target = null;
    }

    return target;
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.eclipse.xsd.util.XSDSwitch#caseXSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition)
   */
  public Object caseXSDTypeDefinition(XSDTypeDefinition typeDefinition)
  {
    XSDConcreteComponent target = null;

    XSDTypeDefinition baseType = typeDefinition.getBaseType();

    if (baseType != null)
    {
      target = baseType;
    }

    // Avoid navigating to built in data types.

    if (isFromSchemaForSchema(target))
    {
      target = null;
    }

    return target;
  }

  /**
   * Detects if a given schema component is from the schema for schema (built in
   * data types). Used to avoid navigating to this type of components as they
   * don't have an accessible physical location.
   * 
   * @param component the component to check.
   * @return true if the component is from the schema for schema namespace,
   *         false otherwise.
   */
  public boolean isFromSchemaForSchema(XSDConcreteComponent component)
  {
    if (component == null)
    {
      return false;
    }

    XSDSchema schema = component.getSchema();

    if (schema != null && schema.equals(schema.getSchemaForSchema()))
    {
      return true;
    }

    return false;
  }

  /**
   * Locates the target component - for example the element declaration pointed
   * to by an element reference, etc.
   * 
   * @param component the current component.
   * @param attributeName the attribute name if the cursor/mouse is over an
   *        attribute. This is used to provide fine grained navigation for
   *        components with more than one "active" attribute.
   * @return the referenced XSD concrete component or null if none is found.
   */
  public XSDConcreteComponent locate(XSDConcreteComponent component, String attributeName)
  {
    this.attributeName = attributeName;

    return (XSDConcreteComponent) doSwitch(component);
  }
}

Back to the top