Skip to main content
summaryrefslogtreecommitdiffstats
blob: 346dfb3f296c4384d2f8564cc78a34e0951b8deb (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
/*******************************************************************************
 * Copyright (c) 2004 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.xml.core.tests.contentmodel;

import org.eclipse.wst.xml.core.internal.contentmodel.CMContent;
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.CMNode;
import org.eclipse.wst.xml.core.internal.contentmodel.util.CMVisitor;


public class CMUtility
{                                    
  /**
   * return true if the child or any of its ancestor group nodes are repeatable
   */
  public static boolean isNodeOrAncestorGroupRepeatable(CMContent content, CMNode child)
  {                                                                            
    // since we can't walk up the CMNode tree (why you ask? ... its a long story)
    // we walk down the tree to consider the child's parent node's
    //
    boolean result = isRepeatable(child);
    if (!result)
    {
      CanRepeatVisitor visitor = new CanRepeatVisitor(content, child);
      visitor.visitCMNode(content);
      result = visitor.getResult();
    }
    return result;
  }

  /**
   * return true if the child is repeatable
   */
  public static boolean isNodeRepeatable(CMContent content, CMNode child)
  {                                                                            
    // since we can't walk up the CMNode tree (why you ask? ... its a long story)
    // we walk down the tree to consider the child's parent node's
    //
    boolean result = isRepeatable(child);
    if (!result)
    {
      IsRepeatableVisitor visitor = new IsRepeatableVisitor(content, child);
      visitor.visitCMNode(content);
      result = visitor.getResult();
    }
    return result;
  }

  public static boolean isRepeatable(CMNode child)
  {                                
    boolean result = false;
    if (child instanceof CMContent)
    {
      CMContent content = (CMContent)child;
      result = content.getMaxOccur() > 1 || content.getMaxOccur() == -1;
    } 
    return result;
  }
    
   
  protected static class CanRepeatVisitor extends CMVisitor
  {                                             
    public boolean result;
    protected boolean isWithinRepeatableGroup;

    protected CMNode root;
    protected CMNode target;

    public CanRepeatVisitor(CMNode root, CMNode target)
    {
      this.root = root;
      this.target = target;
    }

    public void visitCMGroup(CMGroup group)
    {         
      boolean oldIsWithinRepeatableGroup = isWithinRepeatableGroup;
                                           
      isWithinRepeatableGroup = isRepeatable(group);
      
      super.visitCMGroup(group);

      isWithinRepeatableGroup = oldIsWithinRepeatableGroup;
    }                           

    public void visitCMElementDeclaration(CMElementDeclaration cmelement)
    {
      if (cmelement == target)
      {
        result = isWithinRepeatableGroup;
      }
    }

    public boolean getResult()
    {
      return result;
    }  
  }


  protected static class IsRepeatableVisitor extends CMVisitor
  {                                             
    public boolean result = false;
    protected CMGroup currentGroup;

    protected CMNode root;
    protected CMNode target;

    public IsRepeatableVisitor(CMNode root, CMNode target)
    {
      this.root = root;
      this.target = target;
    }

    public void visitCMGroup(CMGroup group)
    {         
      CMGroup previousGroup = currentGroup;
      currentGroup = group;
      super.visitCMGroup(group);
      currentGroup = previousGroup;
    }                           

    public void visitCMElementDeclaration(CMElementDeclaration cmelement)
    {
      if (cmelement == target)
      {
        if (currentGroup != null)
        {
          result = ((currentGroup.getOperator() == CMGroup.CHOICE) &&
                    (isRepeatable(currentGroup)));
        }
      }
    }

    public boolean getResult()
    {
      return result;
    }  
  }
}

Back to the top