Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 035cd00ca072c54934b61defbc51a9afc869a429 (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
/*******************************************************************************
 * Copyright (c) 2002 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
 *     Jens Lukowski/Innoopract - initial renaming/restructuring
 *     
 *******************************************************************************/
package org.eclipse.wst.xml.core.internal.contentmodel.util;

import java.util.Vector;

import org.eclipse.wst.xml.core.internal.contentmodel.CMAnyElement;
import org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration;
import org.eclipse.wst.xml.core.internal.contentmodel.CMContent;
import org.eclipse.wst.xml.core.internal.contentmodel.CMDataType;
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.CMNodeList;

                   
/**
 * This class lets you traverse a 'CM' model providing callbacks to build content.
 */
public class ContentBuilder extends CMVisitor
{
  public static final int BUILD_ALL_CONTENT = 1;
  public static final int BUILD_ONLY_REQUIRED_CONTENT = 2;
  protected int buildPolicy = BUILD_ALL_CONTENT;

  protected boolean alwaysVisit;
  protected Vector visitedCMElementDeclarationList = new Vector();

  public ContentBuilder()
  {
  }
                
  public void setBuildPolicy(int buildPolicy)
  {
    this.buildPolicy = buildPolicy;
  }

  public int getBuildPolicy()
  {
    return buildPolicy;
  }                
           
  protected void createAnyElementNode(CMAnyElement anyElement)
  {
  }

  protected void createElementNodeStart(CMElementDeclaration ed)
  {      
  }

  protected void createElementNodeEnd(CMElementDeclaration ed)
  {
  }

  protected void createTextNode(CMDataType dataType)
  {
  } 

  protected void createAttributeNode(CMAttributeDeclaration attribute)
  {
  } 
 
  public void visitCMElementDeclaration(CMElementDeclaration ed)
  {
    int forcedMin = (buildPolicy == BUILD_ALL_CONTENT || alwaysVisit) ? 1 : 0;
    int min = Math.max(ed.getMinOccur(), forcedMin);                          
    alwaysVisit = false;

    if (min > 0 && !visitedCMElementDeclarationList.contains(ed))
    {
      visitedCMElementDeclarationList.add(ed);
      for (int i = 1; i <= min; i++)
      {       
        createElementNodeStart(ed);       
        
        // instead of calling super.visitCMElementDeclaration()
        // we duplicate the code with some minor modifications
        CMNamedNodeMap nodeMap = ed.getAttributes();
        int size = nodeMap.getLength();
        for (int j = 0; j < size; j++)
        {
          visitCMNode(nodeMap.item(j));
        }

        CMContent content = ed.getContent();
        if (content != null)
        {
          visitCMNode(content);
        }

        if (ed.getContentType() == CMElementDeclaration.PCDATA)
        {
          CMDataType dataType = ed.getDataType();
          if (dataType != null)
          {
            visitCMDataType(dataType);
          }
        }
        // end duplication
        createElementNodeEnd(ed);  
      }
      int size = visitedCMElementDeclarationList.size();
      visitedCMElementDeclarationList.remove(size - 1);
    }
  }
    

  public void visitCMDataType(CMDataType dataType)
  {
    createTextNode(dataType);   
  }


  public void visitCMGroup(CMGroup e)
  { 
    int forcedMin = (buildPolicy == BUILD_ALL_CONTENT || alwaysVisit) ? 1 : 0;
    int min = Math.max(e.getMinOccur(), forcedMin);                          
    alwaysVisit = false;

    for (int i = 1; i <= min; i++)
    {   
      if (e.getOperator() == CMGroup.CHOICE)
      {
        // add only 1 element from the group
        // todo... perhaps add something other than the first one        
        CMNodeList nodeList = e.getChildNodes();
        if (nodeList.getLength() > 0)
        {
          visitCMNode(nodeList.item(0));
        }
      }
      else // SEQUENCE, ALL
      {
        // visit all of the content
        super.visitCMGroup(e);
      }
    }
  } 

  public void visitCMAttributeDeclaration(CMAttributeDeclaration ad)
  {
    if (alwaysVisit ||
        buildPolicy == BUILD_ALL_CONTENT ||
        ad.getUsage() == CMAttributeDeclaration.REQUIRED)
    {
      createAttributeNode(ad);      
    }
  }                     
  

  public void visitCMAnyElement(CMAnyElement anyElement)
  {  
    int forcedMin = (buildPolicy == BUILD_ALL_CONTENT || alwaysVisit) ? 1 : 0;
    alwaysVisit = false; 
    int min = Math.max(anyElement.getMinOccur(), forcedMin);                          
    for (int i = 1; i <= min; i++)
    {                                
      createAnyElementNode(anyElement);
    }
  }     
}

Back to the top