kchong | d10b4e4 | 2005-06-15 20:14:51 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
kchong | f350a64 | 2008-11-12 21:33:29 +0000 | [diff] [blame] | 2 | * Copyright (c) 2001, 2008 IBM Corporation and others. |
kchong | d10b4e4 | 2005-06-15 20:14:51 +0000 | [diff] [blame] | 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | *******************************************************************************/ |
| 11 | package org.eclipse.wst.xsd.contentmodel.internal; |
| 12 | |
| 13 | import java.util.Iterator; |
kchong | f350a64 | 2008-11-12 21:33:29 +0000 | [diff] [blame] | 14 | import java.util.Stack; |
kchong | d10b4e4 | 2005-06-15 20:14:51 +0000 | [diff] [blame] | 15 | |
| 16 | import org.eclipse.xsd.XSDAttributeDeclaration; |
| 17 | import org.eclipse.xsd.XSDAttributeGroupDefinition; |
| 18 | import org.eclipse.xsd.XSDComplexTypeContent; |
| 19 | import org.eclipse.xsd.XSDComplexTypeDefinition; |
| 20 | import org.eclipse.xsd.XSDElementDeclaration; |
| 21 | import org.eclipse.xsd.XSDIdentityConstraintDefinition; |
| 22 | import org.eclipse.xsd.XSDModelGroup; |
| 23 | import org.eclipse.xsd.XSDModelGroupDefinition; |
| 24 | import org.eclipse.xsd.XSDNotationDeclaration; |
| 25 | import org.eclipse.xsd.XSDParticle; |
| 26 | import org.eclipse.xsd.XSDParticleContent; |
| 27 | import org.eclipse.xsd.XSDSchema; |
| 28 | import org.eclipse.xsd.XSDSimpleTypeDefinition; |
| 29 | import org.eclipse.xsd.XSDTypeDefinition; |
| 30 | import org.eclipse.xsd.XSDWildcard; |
| 31 | |
| 32 | public class XSDVisitor |
| 33 | { |
| 34 | public XSDVisitor() |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | protected XSDSchema schema; |
kchong | f350a64 | 2008-11-12 21:33:29 +0000 | [diff] [blame] | 39 | protected Stack particleStack = new Stack(); |
kchong | d10b4e4 | 2005-06-15 20:14:51 +0000 | [diff] [blame] | 40 | |
| 41 | public void visitSchema(XSDSchema schema) |
| 42 | { |
| 43 | this.schema = schema; |
| 44 | for (Iterator iterator = schema.getAttributeDeclarations().iterator(); iterator.hasNext();) |
| 45 | { |
| 46 | XSDAttributeDeclaration attr = (XSDAttributeDeclaration) iterator.next(); |
| 47 | visitAttributeDeclaration(attr); |
| 48 | } |
| 49 | for (Iterator iterator = schema.getTypeDefinitions().iterator(); iterator.hasNext();) |
| 50 | { |
| 51 | XSDTypeDefinition type = (XSDTypeDefinition) iterator.next(); |
| 52 | visitTypeDefinition(type); |
| 53 | } |
| 54 | for (Iterator iterator = schema.getElementDeclarations().iterator(); iterator.hasNext();) |
| 55 | { |
| 56 | XSDElementDeclaration element = (XSDElementDeclaration) iterator.next(); |
| 57 | visitElementDeclaration(element); |
| 58 | } |
| 59 | for (Iterator iterator = schema.getIdentityConstraintDefinitions().iterator(); iterator.hasNext();) |
| 60 | { |
| 61 | XSDIdentityConstraintDefinition identityConstraint = (XSDIdentityConstraintDefinition) iterator.next(); |
| 62 | visitIdentityConstraintDefinition(identityConstraint); |
| 63 | } |
| 64 | for (Iterator iterator = schema.getModelGroupDefinitions().iterator(); iterator.hasNext();) |
| 65 | { |
| 66 | XSDModelGroupDefinition modelGroup = (XSDModelGroupDefinition) iterator.next(); |
| 67 | visitModelGroupDefinition(modelGroup); |
| 68 | } |
| 69 | for (Iterator iterator = schema.getAttributeGroupDefinitions().iterator(); iterator.hasNext();) |
| 70 | { |
| 71 | XSDAttributeGroupDefinition attributeGroup = (XSDAttributeGroupDefinition) iterator.next(); |
| 72 | visitAttributeGroupDefinition(attributeGroup); |
| 73 | } |
| 74 | for (Iterator iterator = schema.getNotationDeclarations().iterator(); iterator.hasNext();) |
| 75 | { |
| 76 | XSDNotationDeclaration element = (XSDNotationDeclaration) iterator.next(); |
| 77 | visitNotationDeclaration(element); |
| 78 | } |
| 79 | |
| 80 | } |
| 81 | |
| 82 | public void visitAttributeDeclaration(XSDAttributeDeclaration attr) |
| 83 | { |
| 84 | } |
| 85 | |
| 86 | public void visitTypeDefinition(XSDTypeDefinition type) |
| 87 | { |
| 88 | if (type instanceof XSDSimpleTypeDefinition) |
| 89 | { |
| 90 | visitSimpleTypeDefinition((XSDSimpleTypeDefinition)type); |
| 91 | } |
| 92 | else if (type instanceof XSDComplexTypeDefinition) |
| 93 | { |
| 94 | visitComplexTypeDefinition((XSDComplexTypeDefinition)type); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | public void visitElementDeclaration(XSDElementDeclaration element) |
| 99 | { |
| 100 | if (element.isElementDeclarationReference()) |
| 101 | { |
| 102 | } |
| 103 | else if (element.getAnonymousTypeDefinition() != null) |
| 104 | { |
| 105 | visitTypeDefinition(element.getAnonymousTypeDefinition()); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | public void visitIdentityConstraintDefinition(XSDIdentityConstraintDefinition identityConstraint) |
| 110 | { |
| 111 | } |
| 112 | |
| 113 | public void visitModelGroupDefinition(XSDModelGroupDefinition modelGroupDef) |
| 114 | { |
| 115 | if (!modelGroupDef.isModelGroupDefinitionReference()) |
| 116 | { |
| 117 | if (modelGroupDef.getModelGroup() != null) |
| 118 | { |
| 119 | visitModelGroup(modelGroupDef.getModelGroup()); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | public void visitAttributeGroupDefinition(XSDAttributeGroupDefinition attributeGroup) |
| 125 | { |
| 126 | } |
| 127 | |
| 128 | public void visitNotationDeclaration(XSDNotationDeclaration notation) |
| 129 | { |
| 130 | } |
| 131 | |
| 132 | public void visitSimpleTypeDefinition(XSDSimpleTypeDefinition type) |
| 133 | { |
| 134 | } |
| 135 | |
| 136 | public void visitComplexTypeDefinition(XSDComplexTypeDefinition type) |
| 137 | { |
| 138 | if (type.getContentType() != null) |
| 139 | { |
| 140 | XSDComplexTypeContent complexContent = type.getContentType(); |
| 141 | if (complexContent instanceof XSDSimpleTypeDefinition) |
| 142 | { |
| 143 | visitSimpleTypeDefinition((XSDSimpleTypeDefinition)complexContent); |
| 144 | } |
| 145 | else if (complexContent instanceof XSDParticle) |
| 146 | { |
| 147 | visitParticle((XSDParticle) complexContent); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | public void visitParticle(XSDParticle particle) |
| 153 | { |
| 154 | visitParticleContent(particle.getContent()); |
| 155 | } |
| 156 | |
| 157 | public void visitParticleContent(XSDParticleContent particleContent) |
| 158 | { |
| 159 | if (particleContent instanceof XSDModelGroupDefinition) |
| 160 | { |
kchong | f350a64 | 2008-11-12 21:33:29 +0000 | [diff] [blame] | 161 | XSDModelGroupDefinition modelGroupDef = (XSDModelGroupDefinition) particleContent; |
| 162 | |
| 163 | if (particleStack.contains(modelGroupDef)) |
| 164 | { |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | particleStack.push(modelGroupDef); |
| 169 | |
| 170 | visitModelGroupDefinition(modelGroupDef); |
| 171 | |
| 172 | particleStack.pop(); |
kchong | d10b4e4 | 2005-06-15 20:14:51 +0000 | [diff] [blame] | 173 | } |
| 174 | else if (particleContent instanceof XSDModelGroup) |
| 175 | { |
| 176 | visitModelGroup((XSDModelGroup)particleContent); |
| 177 | } |
| 178 | else if (particleContent instanceof XSDElementDeclaration) |
| 179 | { |
| 180 | visitElementDeclaration((XSDElementDeclaration)particleContent); |
| 181 | } |
| 182 | else if (particleContent instanceof XSDWildcard) |
| 183 | { |
| 184 | visitWildcard((XSDWildcard)particleContent); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | public void visitModelGroup(XSDModelGroup modelGroup) |
| 189 | { |
| 190 | if (modelGroup.getContents() != null) |
| 191 | { |
| 192 | for (Iterator iterator = modelGroup.getContents().iterator(); iterator.hasNext();) |
| 193 | { |
| 194 | XSDParticle particle = (XSDParticle) iterator.next(); |
| 195 | visitParticle(particle); |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | public void visitWildcard(XSDWildcard wildcard) |
| 201 | { |
| 202 | } |
| 203 | } |