Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6f256a1437e7e63aebb79da908402e72997cf172 (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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*******************************************************************************
 * Copyright (c) 2001, 2009 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.navigation;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.GraphicalViewer;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.INavigationLocation;
import org.eclipse.ui.NavigationLocation;
import org.eclipse.wst.xsd.ui.internal.adapters.XSDAdapterFactory;
import org.eclipse.wst.xsd.ui.internal.adapters.XSDVisitor;
import org.eclipse.wst.xsd.ui.internal.adt.design.DesignViewGraphicalViewer;
import org.eclipse.wst.xsd.ui.internal.adt.facade.IADTObject;
import org.eclipse.xsd.XSDAttributeGroupDefinition;
import org.eclipse.xsd.XSDConcreteComponent;
import org.eclipse.xsd.XSDElementDeclaration;
import org.eclipse.xsd.XSDModelGroupDefinition;
import org.eclipse.xsd.XSDRedefine;
import org.eclipse.xsd.XSDSchema;
import org.eclipse.xsd.XSDTypeDefinition;
import com.ibm.icu.util.StringTokenizer;

/**
 * This class exists to support navigation in a context where there is no text 
 * editor page.  In these cases we can't rely on the TextSelectionNavigationLocations
 * so we this class which is designed to work with just the design view.
 */
public class DesignViewNavigationLocation extends NavigationLocation
{
  protected Path path;

  private static final String PATH_TAG = "path"; //$NON-NLS-1$
  
  public DesignViewNavigationLocation(IEditorPart part)
  {
    super(part);
    this.path = new Path();
  }
  
  public DesignViewNavigationLocation(IEditorPart part, XSDConcreteComponent component)
  {
    super(part);   
    this.path = Path.computePath(component);  
  }

  public boolean mergeInto(INavigationLocation currentLocation)
  {
    boolean result = false;
    if (currentLocation instanceof DesignViewNavigationLocation)
    {
      DesignViewNavigationLocation loc = (DesignViewNavigationLocation) currentLocation;
      result = loc.path.toString().equals(path.toString());
    }
    else
    {
    }
    return result;
  }

  public void restoreLocation()
  {
    XSDSchema schema = (XSDSchema) getEditorPart().getAdapter(XSDSchema.class);
    Object viewer = getEditorPart().getAdapter(GraphicalViewer.class);
    if (viewer instanceof DesignViewGraphicalViewer)
    {
      DesignViewGraphicalViewer graphicalViewer = (DesignViewGraphicalViewer) viewer;
      XSDConcreteComponent component = Path.computeComponent(schema, path);
      if (component != null)
      {
        Adapter adapter = XSDAdapterFactory.getInstance().adapt(component);
        if (adapter instanceof IADTObject)
        {
          graphicalViewer.setInput((IADTObject)adapter);
        }
      }
      else if (path.segments.isEmpty())
      {
        Adapter adapter = XSDAdapterFactory.getInstance().adapt(schema);
        if (adapter instanceof IADTObject)
        {
          graphicalViewer.setInput((IADTObject)adapter);
        }
      }
    }   
  }

  public void restoreState(IMemento memento)
  {
    String string = memento.getString(PATH_TAG);
    path = Path.createPath(string);
  }

  public void saveState(IMemento memento)
  {
    memento.putString(PATH_TAG, path.toString());
  }

  public void update()
  {
    // TODO (cs) not sure what needs to be done here
  }
  static class PathSegment
  {
    final static int ELEMENT = 1;
    final static int TYPE = 2;
    final static int MODEL_GROUP = 3;
    final static int ATTRIBUTE_GROUP = 4;
    final static int REDEFINE = 5;
    int kind;
    String name;

    PathSegment()
    {
    }

    PathSegment(int kind, String name)
    {
      this.kind = kind;
      this.name = name;
    }
  }
  protected static class Path
  {
    private static final String REDEFINE_TOKEN = "redefine"; //$NON-NLS-1$
    private static final String MODEL_GROUP_TOKEN = "modelGroup"; //$NON-NLS-1$
    private static final String ATTRIBUTE_GROUP_TOKEN = "attributeGroup"; //$NON-NLS-1$
    private static final String TYPE_TOKEN = "type"; //$NON-NLS-1$
    private static final String ELEMENT_TOKEN = "element"; //$NON-NLS-1$

    List segments = new ArrayList();

    public static XSDConcreteComponent computeComponent(XSDSchema schema, Path path)
    {
      PathResolvingXSDVisitor visitor = new PathResolvingXSDVisitor(path);
      visitor.visitSchema(schema);
      if (visitor.isDone())
      {
        return visitor.result;
      }
      return null;
    }

    static Path createPath(String pathString)
    {
      Path path = new Path();
      PathSegment segment = null;
      for (StringTokenizer st = new StringTokenizer(pathString, "/"); st.hasMoreTokens();)
      {
        String token = st.nextToken();
        int kind = -1;
        if (token.startsWith(ELEMENT_TOKEN))
        {
          kind = PathSegment.ELEMENT;
        }
        else if (token.startsWith(TYPE_TOKEN))
        {
          kind = PathSegment.TYPE;
        }
        else if (token.startsWith(ATTRIBUTE_GROUP_TOKEN))
        {
        	kind = PathSegment.ATTRIBUTE_GROUP;
        }
        else if (token.startsWith(MODEL_GROUP_TOKEN))
        {
        	kind = PathSegment.MODEL_GROUP;
        }
        else if (token.startsWith(REDEFINE_TOKEN))
        {
        	kind = PathSegment.REDEFINE;
        }
        if (kind != -1)
        {
          segment = new PathSegment();
          segment.kind = kind;
          path.segments.add(segment);
          String namePattern = "[@name='";
          int startIndex = token.indexOf(namePattern);
          if (startIndex != -1)
          {
            startIndex += namePattern.length();
            int endIndex = token.indexOf("']");
            if (endIndex != -1)
            {
              segment.name = token.substring(startIndex, endIndex);
            }
          }
        }
      }
      return path;
    }

    public static Path computePath(XSDConcreteComponent component)
    {
      Path path = new Path();
      for (EObject c = component; c != null; c = c.eContainer())
      {
        if (c instanceof XSDConcreteComponent)
        {
          PathSegment segment = computePathSegment((XSDConcreteComponent) c);
          if (segment != null)
          {
            path.segments.add(0, segment);
          }
        }
      }
      return path;
    }

    static PathSegment computePathSegment(XSDConcreteComponent component)
    {
    	PathSegment pathSegment = null;
    	if (component instanceof XSDElementDeclaration)
    	{
    		XSDElementDeclaration elementDeclaration = (XSDElementDeclaration) component;
    		pathSegment = new PathSegment(PathSegment.ELEMENT, elementDeclaration.getResolvedElementDeclaration().getName());
    	}
    	else if (component instanceof XSDTypeDefinition)
    	{
    		XSDTypeDefinition typeDefinition = (XSDTypeDefinition) component;
    		pathSegment = new PathSegment(PathSegment.TYPE, typeDefinition.getName());
    	}
    	else if (component instanceof XSDModelGroupDefinition)
    	{
    		XSDModelGroupDefinition modelGroupDefinition = (XSDModelGroupDefinition) component;
    		pathSegment = new PathSegment(PathSegment.MODEL_GROUP, modelGroupDefinition.getName());
    	}
    	else if (component instanceof XSDAttributeGroupDefinition)
    	{
    		XSDAttributeGroupDefinition attributeGroupDefinition = (XSDAttributeGroupDefinition) component;
    		pathSegment = new PathSegment(PathSegment.ATTRIBUTE_GROUP, attributeGroupDefinition.getResolvedAttributeGroupDefinition().getName());
    	}
    	else if (component instanceof XSDRedefine)
    	{
    		XSDRedefine redefine = (XSDRedefine) component;
    		pathSegment = new PathSegment(PathSegment.REDEFINE, redefine.getSchemaLocation());
    	}
    	return pathSegment;
    }

    public String toString()
    {
      StringBuffer b = new StringBuffer();
      for (Iterator i = segments.iterator(); i.hasNext();)
      {
        PathSegment segment = (PathSegment) i.next();
        String kind = "";
        if (segment.kind == PathSegment.ELEMENT)
        {
          kind = ELEMENT_TOKEN;
        }
        else if (segment.kind == PathSegment.TYPE)
        {
          kind = TYPE_TOKEN;
        }
        else if (segment.kind == PathSegment.MODEL_GROUP)
        {
        	kind = MODEL_GROUP_TOKEN;
        }
        else if (segment.kind == PathSegment.ATTRIBUTE_GROUP)
        {
        	kind = ATTRIBUTE_GROUP_TOKEN;
        }
        else if (segment.kind == PathSegment.REDEFINE)
        {
        	kind = REDEFINE_TOKEN;
        }
        b.append(kind);
        if (segment.name != null)
        {
          b.append("[@name='" + segment.name + "']");
        }
        if (i.hasNext())
        {
          b.append("/");
        }
      }
      return b.toString();
    }
  }
  
  
  static class PathResolvingXSDVisitor extends XSDVisitor
  {
    Path path;
    int index = -1;
    PathSegment segment;
    XSDConcreteComponent result = null;

    PathResolvingXSDVisitor(Path path)
    {
      this.path = path;
      incrementSegment();
    }

    boolean isDone()
    {
      return index >= path.segments.size();
    }

    void incrementSegment()
    {
      index++;
      if (index < path.segments.size())
      {
        segment = (PathSegment) path.segments.get(index);
      }
      else
      {
        segment = null;
      }
    }

    public void visitSchema(XSDSchema schema)
    {
      if (segment != null)
      {
        if (segment.kind == PathSegment.ELEMENT)
        {
          XSDElementDeclaration elementDeclaration = schema.resolveElementDeclaration(segment.name);
          if (elementDeclaration != null)
          {
            visitElementDeclaration(elementDeclaration);
          }
        }
        else if (segment.kind == PathSegment.TYPE)
        {
          XSDTypeDefinition typeDefinition = schema.resolveTypeDefinition(segment.name);
          if (typeDefinition != null)
          {
            visitTypeDefinition(typeDefinition);
          }
        }
        else if (segment.kind == PathSegment.MODEL_GROUP)
        {
        	XSDModelGroupDefinition modelGroupDefinition = schema.resolveModelGroupDefinition(segment.name);
        	if (modelGroupDefinition != null)
        	{
        		visitModelGroupDefinition(modelGroupDefinition);
        	}
        }
        else if (segment.kind == PathSegment.ATTRIBUTE_GROUP)
        {
        	XSDAttributeGroupDefinition attributeGroupDefinition = schema.resolveAttributeGroupDefinition(segment.name);
        	if (attributeGroupDefinition != null)
        	{
        		visitAttributeGroupDefinition(attributeGroupDefinition);
        	}
        }
        else if (segment.kind == PathSegment.REDEFINE)
        {
        	Iterator iterator = schema.getContents().iterator();
        	while (iterator.hasNext())
        	{
        		Object object = iterator.next();
        		if (object instanceof XSDRedefine)
        		{        			
        			XSDRedefine redefine = (XSDRedefine)object;
        			visitRedefine(redefine);
        		}
        	}
        }
      }
    }

    public void visitElementDeclaration(XSDElementDeclaration element)
    {
      if (segment != null)
      {
        String name = element.getResolvedElementDeclaration().getName();
        if (segment.kind == PathSegment.ELEMENT && isMatch(segment.name, name))
        {
          result = element;
          incrementSegment();
          if (!isDone())
          {
            super.visitElementDeclaration(element);
          }
        }
      }
    }

    public void visitTypeDefinition(XSDTypeDefinition type)
    {
      if (segment != null)
      {
        String name = type.getName();
        if (segment.kind == PathSegment.TYPE && isMatch(segment.name, name))
        {
          result = type;
          incrementSegment();
          if (!isDone())
          {
            super.visitTypeDefinition(type);
          }
        }
      }
    }
    
    public void visitModelGroupDefinition(XSDModelGroupDefinition modelGroup)
    {
    	if (segment != null)
    	{
    		String name = modelGroup.getName();
    		if (segment.kind == PathSegment.MODEL_GROUP && isMatch(segment.name, name))
    		{
    			result = modelGroup;
    			incrementSegment();
    			if (!isDone())
    			{
    				super.visitModelGroupDefinition(modelGroup);
    			}
    		}
    	}
    }

    public void visitAttributeGroupDefinition(XSDAttributeGroupDefinition attributeGroup)
    {
    	if (segment != null)
    	{
    		String name = attributeGroup.getName();
    		if (segment.kind == PathSegment.ATTRIBUTE_GROUP && isMatch(segment.name, name))
    		{
    			result = attributeGroup;
    			incrementSegment();
    			if (!isDone())
    			{
    				super.visitAttributeGroupDefinition(attributeGroup);
    			}
    		}
    	}
    }
    
    public void visitRedefine(XSDRedefine redefine)
    {
    	if (segment != null)
    	{
    		String name = redefine.getSchemaLocation();
    		if (segment.kind == PathSegment.REDEFINE && isMatch(segment.name, name))
    		{
    			result = redefine;
    			incrementSegment();
    			if (!isDone())
    			{
    				visitSchema(redefine.getSchema());
    			}
    		}
    	}
    	
    }
    protected boolean isMatch(String name1, String name2)
    {
      return name1 != null ? name1.equals(name2) : name1 == name2;
    }
  }
}

Back to the top