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: c813dac7f75329cfa9ca07ce1f68d35049eee828 (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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*
* Copyright (c) 2002 IBM Corporation and others.
* All rights reserved.   This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
* 
* Contributors:
*   IBM - Initial API and implementation
*   Jens Lukowski/Innoopract - initial renaming/restructuring
* 
*/
package org.eclipse.wst.xml.core.internal.contentmodel.modelqueryimpl;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration;
import org.eclipse.wst.xml.core.internal.contentmodel.CMDocument;
import org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration;
import org.eclipse.wst.xml.core.internal.contentmodel.CMGroup;
import org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap;
import org.eclipse.wst.xml.core.internal.contentmodel.CMNode;
import org.eclipse.wst.xml.core.internal.contentmodel.CMNodeList;
import org.eclipse.wst.xml.core.internal.contentmodel.internal.util.CMValidator;
import org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery;
import org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQueryAction;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


/**
 *
 */
public class ModelQueryActionHelper
{
  protected ModelQueryImpl modelQuery;

  protected static class Action implements ModelQueryAction
  {
    public int kind;
    public int startIndex;
    public int endIndex;
    public Node parent;
    public CMNode cmNode;
    public Object userData;

    public Action(int kind, Node parent, CMNode cmNode)
    {
      this.kind = kind;
      this.parent = parent;
      this.cmNode = cmNode;
    }

    public Action(int kind, Node parent, CMNode cmNode, int startIndex, int endIndex)
    {
      this.kind = kind;
      this.parent = parent;
      this.cmNode = cmNode;
      this.startIndex = startIndex;
      this.endIndex = endIndex;
    }

    public int getKind()
    {
      return kind;
    }

    public int getStartIndex()
    {
      return startIndex;
    }

    public int getEndIndex()
    {
      return endIndex;
    }

    public Node getParent()
    {
      return parent;
    }

    public CMNode getCMNode()
    {
      return cmNode;
    }

    public Object getUserData()
    {
      return userData;
    }

    public void setUserData(Object object)
    {
      userData = object;
    }

    public void performAction()
    {
    }
  }


  public ModelQueryActionHelper(ModelQueryImpl modelQuery)
  {
    this.modelQuery = modelQuery;
  }


  public void getAllActions(Element parent, CMElementDeclaration ed, int validityChecking, List actionList)
  {
  }


  // insert actions
  //
  public void getInsertActions(Element parent, CMElementDeclaration ed, int index, int includeOptions, int validityChecking, List actionList)
  {
    if ((includeOptions & ModelQuery.INCLUDE_ATTRIBUTES) != 0)
    {
      getInsertAttributeActions(parent, ed, validityChecking, actionList);
    }
    includeOptions &= ~ModelQuery.INCLUDE_ATTRIBUTES;
    if ((includeOptions & ModelQuery.INCLUDE_CHILD_NODES) != 0)
    {
      if (index != -1)
      {
        getInsertChildNodeActionsAtIndex(parent, ed, index, includeOptions, validityChecking, actionList);
      }
      else
      {
        getInsertChildNodeActions(parent, ed, includeOptions, validityChecking, actionList);
      }
    }
  }



  protected void getInsertAttributeActions(Element parent, CMElementDeclaration ed, int validityChecking, List actionList)
  {
    // get actions for each insertable attribute
    //
    List availableAttributeList = modelQuery.getAvailableContent(parent, ed, ModelQuery.INCLUDE_ATTRIBUTES);

    for (Iterator i = availableAttributeList.iterator(); i.hasNext(); )
    {
      CMAttributeDeclaration ad = (CMAttributeDeclaration)i.next();
      if (modelQuery.canInsert(parent, ed, ad, 0, validityChecking))
      {
        Action action = new Action(ModelQueryAction.INSERT, parent, ad);
        actionList.add(action);
      }
    }
  }


  protected void getInsertChildNodeActionsAtIndex(Element parent, CMElementDeclaration ed, int index, int includeOptions, int validityChecking, List actionList)
  {                       
    // get actions for each insertable attribute
    //
    int size = parent.getChildNodes().getLength();
    if (index <= size)
    {                                                                                          
      List contentSpecificationList = modelQuery.getValidator().createContentSpecificationList(parent, ed); 
      List availableChildNodeList = modelQuery.getAvailableContent(parent, ed, includeOptions);

      boolean isSimpleChoice = isSimpleChoiceGroupContentModel(ed);
     
      for (Iterator i = availableChildNodeList.iterator(); i.hasNext(); )
      {
        CMNode cmNode = (CMNode)i.next();      
        if (isSimpleChoice || modelQuery.canInsert(parent, ed, cmNode, index, validityChecking, contentSpecificationList))
        {
          Action action = new Action(ModelQueryAction.INSERT, parent, cmNode, index, index);
          actionList.add(action);
        }
      }
    }
  }
                  
 
  protected boolean isSimpleChoiceGroupContentModel(CMElementDeclaration ed)
  {       
    boolean result = false;
    CMNode cmNode = ed.getContent();
    if (cmNode != null && cmNode.getNodeType() == CMNode.GROUP)
    {
      CMGroup cmGroup = (CMGroup)cmNode;
      if (cmGroup.getOperator() == CMGroup.CHOICE && cmGroup.getMaxOccur() == -1)
      {                   
        result = true;
        CMNodeList list = cmGroup.getChildNodes();
        for (int i = list.getLength() - 1; i >= 0; i--)
        {
          if (list.item(i).getNodeType() != CMNode.ELEMENT_DECLARATION)
          {
            result = false;
            break;
          }
        }
      }
    }
    return result;
  }


  protected void getInsertChildNodeActions(Element parent, CMElementDeclaration ed, int includeOptions, int validityChecking, List actionList)
  {
    int size = parent.getChildNodes().getLength();
    List contentSpecificationList = modelQuery.getValidator().createContentSpecificationList(parent, ed);
    List availableChildNodeList = modelQuery.getAvailableContent(parent, ed, includeOptions);

    boolean isSimpleChoice = isSimpleChoiceGroupContentModel(ed);

    for (Iterator iterator = availableChildNodeList.iterator(); iterator.hasNext(); )
    {
      CMNode cmNode = (CMNode)iterator.next();
      for (int i = size; i >= 0; i--)
      {
        if (isSimpleChoice || modelQuery.canInsert(parent, ed, cmNode, i, validityChecking, contentSpecificationList))
        {
          Action action = new Action(ModelQueryAction.INSERT, parent, cmNode, i, i);
          actionList.add(action);
          break;
        }
      }
    }
  }

  public void getInsertActions(Document parent, CMDocument cmDocument, int index, int includeOptions, int validityChecking, List actionList)
  {
    // get the root element and doctype index (if any)
    //
    int doctypeIndex = -1;
    DocumentType doctype = null;
    Element rootElement = null;
    NodeList nodeList = parent.getChildNodes();
    int nodeListLength = nodeList.getLength();
    for (int i = 0; i < nodeListLength; i++)
    {
      Node childNode = nodeList.item(i);
      if (childNode.getNodeType() == Node.ELEMENT_NODE)
      {
        rootElement = (Element)childNode;
        break;
      }
      else if (childNode.getNodeType() == Node.DOCUMENT_TYPE_NODE)
      {
        doctype = (DocumentType)childNode;
        doctypeIndex = i;
      }
    }

    // make sure that root elements are only added after the doctype (if any)
    if (rootElement == null && index > doctypeIndex)
    {
      CMNamedNodeMap map = cmDocument.getElements();
      int mapLength = map.getLength();
      for (int i = 0; i < mapLength; i++)
      {
        CMNode cmNode = map.item(i);

        boolean canAdd = true;
        if (validityChecking == ModelQuery.VALIDITY_STRICT)
        {
          canAdd = doctype == null || doctype.getName().equals(cmNode.getNodeName());
        }

        if (canAdd)
        {
          Action action = new Action(ModelQueryAction.INSERT, parent, cmNode, index, index);
          actionList.add(action);
        }
      }
    }
  }



  public void getInsertChildNodeActionTable(Element parent, CMElementDeclaration ed, int validityChecking, Hashtable actionTable)
  {
  }


  public void getReplaceActions(Element parent, CMElementDeclaration ed, int includeOptions, int validityChecking, List actionList)
  {
    CMValidator.MatchModelNode matchModelNode = modelQuery.getValidator().getMatchModel(ed, parent);
    if (matchModelNode != null)
    {
      MatchModelVisitor visitor = new MatchModelVisitor(parent, actionList);
      visitor.visitMatchModelNode(matchModelNode);
    }     
  }

  public void getReplaceActions(Element parent, CMElementDeclaration ed, List selectedChildren, int includeOptions, int validityChecking, List actionList)
  {
    int[] range = getRange(parent, selectedChildren);
    if (range != null)
    {                
      if (isContiguous(parent, range, selectedChildren))
      {
        List tempList = new Vector();
        getReplaceActions(parent, ed, includeOptions, validityChecking, tempList);
        if ((includeOptions & ModelQuery.INCLUDE_ENCLOSING_REPLACE_ACTIONS) != 0)
        {
          removeActionsNotContainingRange(tempList, range[0], range[1]);            
        }
        else
        {
          removeActionsNotMatchingRange(tempList, range[0], range[1]);    
        }
        actionList.addAll(tempList);
      }
    }   
    
    if (selectedChildren.size() == 1)
    {
      Node node = (Node)selectedChildren.get(0);
      if (node.getNodeType() == Node.ELEMENT_NODE)
      {        
        Element childElement = (Element)node;       
        CMNode childEd = modelQuery.getCMElementDeclaration(childElement);
        if (childEd != null)
        {         

          CMNode childOrigin= modelQuery.getOrigin(childElement);

          CMNodeList cmNodeList = childOrigin != null ? 
                                    (CMNodeList)childOrigin.getProperty("SubstitutionGroup") : //$NON-NLS-1$
                                    (CMNodeList)childEd.getProperty("SubstitutionGroup"); //$NON-NLS-1$

          if (cmNodeList != null && cmNodeList.getLength() > 1)
          {                                                 
            int replaceIndex = getIndex(parent, childElement);
            String childEdName = childEd.getNodeName();
            for (int i = 0; i < cmNodeList.getLength(); i++)
            {         
              CMNode substitution = cmNodeList.item(i);
              if (!substitution.getNodeName().equals(childEdName) && !Boolean.TRUE.equals(substitution.getProperty("Abstract"))) //$NON-NLS-1$
              {
                Action action = new Action(ModelQueryAction.REPLACE, parent, cmNodeList.item(i), replaceIndex, replaceIndex);
                actionList.add(action);
              }
            }
          }
        }
      }   
    }
  }     
           
  
  // returns true if the selected nodes are contiguous
  //  
  protected boolean isContiguous(Element parent, int[] range, List selectedNodeList)
  {         
    boolean result = true;
    NodeList nodeList = parent.getChildNodes();
	// issue: nodeListLength was never read, but in theory, 
	// nodelList.getLength() might cause some clearing of cached 
	// data, or something, so leaving in a potential meaningless call, for now.
    //int nodeListLength = nodeList.getLength();
	nodeList.getLength();
    for (int i = range[0]; i < range[1]; i++)
    {       
      Node node = nodeList.item(i);    
      if (!isWhitespaceNode(node) && !selectedNodeList.contains(node))
      {             
        result = false;
        break;
      }                       
    }         
    return result;
  }
 
 
  protected int[] getRange(Element parent, List list)
  {
    int[] result = null;
    int first = -1;
    int last = -1;                     

    NodeList nodeList = parent.getChildNodes();
    int nodeListLength = nodeList.getLength();
    for (int i = 0; i < nodeListLength; i++)
    {       
      Node node = nodeList.item(i);    
      if (list.contains(node))
      {             
        first = (first == -1) ? i : Math.min(first, i);        
        last = Math.max(last, i);
      }    
    }
   
    if (first != -1 && last!= -1)
    {             
      result = new int[2];
      result[0] = first;
      result[1] = last;
    }   
    return result;
  } 


  protected boolean isWhitespaceNode(Node node)
  {
    return node.getNodeType() == Node.TEXT_NODE &&
           node.getNodeValue().trim().length() == 0;
  } 


  protected int getIndex(Node parentNode, Node child)
  {
    NodeList nodeList = parentNode.getChildNodes();
    int index = -1;
    int size = nodeList.getLength();
    for (int i = 0; i < size; i++)
    {
      if (nodeList.item(i) == child)
      {
        index = i;
        break;
      }
    }
    return index;
  }                    


  protected boolean isActionContainingRange(ModelQueryAction action, int startIndex, int endIndex)
  {
    int actionStartIndex = action.getStartIndex();
    int actionEndIndex = action.getEndIndex();

    return (actionStartIndex <= startIndex &&
            actionEndIndex >= endIndex);
  } 
           

  protected boolean isActionMatchingRange(ModelQueryAction action, int startIndex, int endIndex)
  {
    int actionStartIndex = action.getStartIndex();
    int actionEndIndex = action.getEndIndex();
    return (actionStartIndex == startIndex &&        
            actionEndIndex == endIndex);
  } 
           

  protected void removeActionsNotContainingRange(List actionList, int startIndex, int endIndex)
  {
    for (int i = actionList.size() - 1; i >= 0; i--)
    {
      ModelQueryAction action = (ModelQueryAction)actionList.get(i);
      if (!isActionContainingRange(action, startIndex, endIndex))
      {
        actionList.remove(i);
      }
    }
  }


  protected void removeActionsNotMatchingRange(List actionList, int startIndex, int endIndex)
  {
    for (int i = actionList.size() - 1; i >= 0; i--)
    {
      ModelQueryAction action = (ModelQueryAction)actionList.get(i);
      if (!isActionMatchingRange(action, startIndex, endIndex))
      {
        actionList.remove(i);
      }
    }
  }


  public static class MatchModelVisitor
  {
    int indent;
    int elementIndex;
    Node parent;
    List actionList;

    public MatchModelVisitor(Node parent, List actionList)
    {
      this.parent = parent;
      this.actionList = actionList;
    }

    public int indexOfNextElement(int start)
    {
      NodeList nodeList = parent.getChildNodes();
      int length = nodeList.getLength();
      int result = length;
      for (int i = start; i < length; i++)
      {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE)
        {
          result = i;
          break;
        }
      }
      return result;
    }

    public void visitMatchModelNode(CMValidator.MatchModelNode matchModelNode)
    {
      int startIndex = indexOfNextElement(elementIndex);

      //String cmNodeName = matchModelNode.cmNode != null ? matchModelNode.cmNode.getNodeName() : "null";
      //printIndented(indent, "+MatchModelNode : " + cmNodeName +  " " + startIndex);

      indent += 2;
      for (Iterator iterator = matchModelNode.children.iterator(); iterator.hasNext(); )
      {
        CMValidator.MatchModelNode child = (CMValidator.MatchModelNode)iterator.next();
        visitMatchModelNode(child);
      }
      indent -= 2;

      if (matchModelNode.cmNode != null)
      {
        int nodeType = matchModelNode.cmNode.getNodeType();
        if (nodeType == CMNode.GROUP)
        {
          CMGroup group = (CMGroup)matchModelNode.cmNode;
          if (group.getOperator() == CMGroup.CHOICE)
          {
            addReplaceActions(matchModelNode, group, startIndex, elementIndex - 1);
          }
        }
        else if (nodeType == CMNode.ELEMENT_DECLARATION)
        {
          elementIndex = startIndex + 1;
        }
        //printIndented(indent, "-MatchModelNode : " + cmNodeName +  " " + (elementIndex - 1));
      }
    }

    public void addReplaceActions(CMValidator.MatchModelNode matchModelNode, CMGroup group, int startIndex, int endIndex)
    {
      CMNode excludeCMNode = null;
      if (matchModelNode.children.size() > 0)
      {
        CMValidator.MatchModelNode child = (CMValidator.MatchModelNode)matchModelNode.children.get(0);
        excludeCMNode = child.cmNode;
      }

      CMNodeList nodeList = group.getChildNodes();
      int size = nodeList.getLength();
      for (int i = 0; i < size; i++)
      {
        CMNode alternative = nodeList.item(i);
        if (alternative != excludeCMNode)
        {
          Action action = new Action(ModelQueryAction.REPLACE, parent, alternative, startIndex, endIndex);
          actionList.add(action);
        }
      }
    }
  }

  //public static void printIndented(int indent, String string)
  //{
  //  for (int i = 0; i < indent; i++)
  //  {
  //    System.out.print(" ");
  //  }
  //  System.out.println(string);
  //}
}

Back to the top