1 57 58 package com.sun.org.apache.xerces.internal.impl.xs.models; 59 60 import com.sun.org.apache.xerces.internal.impl.dtd.models.CMNode; 61 import com.sun.org.apache.xerces.internal.impl.xs.SchemaSymbols; 62 import com.sun.org.apache.xerces.internal.impl.xs.XSComplexTypeDecl; 63 import com.sun.org.apache.xerces.internal.impl.xs.XSDeclarationPool; 64 import com.sun.org.apache.xerces.internal.impl.xs.XSElementDecl; 65 import com.sun.org.apache.xerces.internal.impl.xs.XSModelGroupImpl; 66 import com.sun.org.apache.xerces.internal.impl.xs.XSParticleDecl; 67 68 76 public class CMBuilder { 77 78 private XSDeclarationPool fDeclPool = null; 80 81 private static XSEmptyCM fEmptyCM = new XSEmptyCM(); 83 84 private int fLeafCount; 86 private int fParticleCount; 88 private CMNodeFactory fNodeFactory ; 90 91 public CMBuilder(CMNodeFactory nodeFactory) { 92 fDeclPool = null; 93 fNodeFactory = nodeFactory ; 94 } 95 96 public void setDeclPool(XSDeclarationPool declPool) { 97 fDeclPool = declPool; 98 } 99 100 106 public XSCMValidator getContentModel(XSComplexTypeDecl typeDecl) { 107 108 short contentType = typeDecl.getContentType(); 111 if (contentType == XSComplexTypeDecl.CONTENTTYPE_SIMPLE || 112 contentType == XSComplexTypeDecl.CONTENTTYPE_EMPTY) { 113 return null; 114 } 115 116 XSParticleDecl particle = (XSParticleDecl)typeDecl.getParticle(); 117 118 if (particle == null) 121 return fEmptyCM; 122 123 XSCMValidator cmValidator = null; 126 if (particle.fType == XSParticleDecl.PARTICLE_MODELGROUP && 127 ((XSModelGroupImpl)particle.fValue).fCompositor == XSModelGroupImpl.MODELGROUP_ALL) { 128 cmValidator = createAllCM(particle); 129 } 130 else { 131 cmValidator = createDFACM(particle); 132 } 133 134 fNodeFactory.resetNodeCount() ; 137 138 if (cmValidator == null) 141 cmValidator = fEmptyCM; 142 143 return cmValidator; 144 } 145 146 XSCMValidator createAllCM(XSParticleDecl particle) { 147 if (particle.fMaxOccurs == 0) 148 return null; 149 150 XSModelGroupImpl group = (XSModelGroupImpl)particle.fValue; 152 XSAllCM allContent = new XSAllCM(particle.fMinOccurs == 0, group.fParticleCount); 155 for (int i = 0; i < group.fParticleCount; i++) { 156 allContent.addElement((XSElementDecl)group.fParticles[i].fValue, 158 group.fParticles[i].fMinOccurs == 0); 159 } 160 return allContent; 161 } 162 163 XSCMValidator createDFACM(XSParticleDecl particle) { 164 fLeafCount = 0; 165 fParticleCount = 0; 166 CMNode node = buildSyntaxTree(particle); 168 if (node == null) 169 return null; 170 return new XSDFACM(node, fLeafCount); 172 } 173 174 private CMNode buildSyntaxTree(XSParticleDecl particle) { 181 182 int maxOccurs = particle.fMaxOccurs; 183 int minOccurs = particle.fMinOccurs; 184 short type = particle.fType; 185 CMNode nodeRet = null; 186 187 if ((type == XSParticleDecl.PARTICLE_WILDCARD) || 188 (type == XSParticleDecl.PARTICLE_ELEMENT)) { 189 nodeRet = fNodeFactory.getCMLeafNode(particle.fType, particle.fValue, fParticleCount++, fLeafCount++); 196 nodeRet = expandContentModel(nodeRet, minOccurs, maxOccurs); 198 } 199 else if (type == XSParticleDecl.PARTICLE_MODELGROUP) { 200 XSModelGroupImpl group = (XSModelGroupImpl)particle.fValue; 202 CMNode temp = null; 203 boolean twoChildren = false; 214 for (int i = 0; i < group.fParticleCount; i++) { 215 temp = buildSyntaxTree(group.fParticles[i]); 217 if (temp != null) { 219 if (nodeRet == null) { 220 nodeRet = temp; 221 } 222 else { 223 nodeRet = fNodeFactory.getCMBinOpNode(group.fCompositor, nodeRet, temp); 224 twoChildren = true; 226 } 227 } 228 } 229 if (nodeRet != null) { 231 if (group.fCompositor == XSModelGroupImpl.MODELGROUP_CHOICE && 236 !twoChildren && group.fParticleCount > 1) { 237 nodeRet = fNodeFactory.getCMUniOpNode(XSParticleDecl.PARTICLE_ZERO_OR_ONE, nodeRet); 238 } 239 nodeRet = expandContentModel(nodeRet, minOccurs, maxOccurs); 240 } 241 } 242 243 return nodeRet; 244 } 245 246 private CMNode expandContentModel(CMNode node, 250 int minOccurs, int maxOccurs) { 251 252 CMNode nodeRet = null; 253 254 if (minOccurs==1 && maxOccurs==1) { 255 nodeRet = node; 256 } 257 else if (minOccurs==0 && maxOccurs==1) { 258 nodeRet = fNodeFactory.getCMUniOpNode(XSParticleDecl.PARTICLE_ZERO_OR_ONE, node); 260 } 261 else if (minOccurs == 0 && maxOccurs==SchemaSymbols.OCCURRENCE_UNBOUNDED) { 262 nodeRet = fNodeFactory.getCMUniOpNode(XSParticleDecl.PARTICLE_ZERO_OR_MORE, node); 264 } 265 else if (minOccurs == 1 && maxOccurs==SchemaSymbols.OCCURRENCE_UNBOUNDED) { 266 nodeRet = fNodeFactory.getCMUniOpNode(XSParticleDecl.PARTICLE_ONE_OR_MORE, node); 268 } 269 else if (maxOccurs == SchemaSymbols.OCCURRENCE_UNBOUNDED) { 270 nodeRet = fNodeFactory.getCMUniOpNode(XSParticleDecl.PARTICLE_ONE_OR_MORE, node); 275 nodeRet = fNodeFactory.getCMBinOpNode(XSModelGroupImpl.MODELGROUP_SEQUENCE, 280 multiNodes(node, minOccurs-1, true), nodeRet); 281 } 282 else { 283 if (minOccurs > 0) { 287 nodeRet = multiNodes(node, minOccurs, false); 288 } 289 if (maxOccurs > minOccurs) { 290 node = fNodeFactory.getCMUniOpNode(XSParticleDecl.PARTICLE_ZERO_OR_ONE, node); 291 if (nodeRet == null) { 292 nodeRet = multiNodes(node, maxOccurs-minOccurs, false); 293 } 294 else { 295 nodeRet = fNodeFactory.getCMBinOpNode(XSModelGroupImpl.MODELGROUP_SEQUENCE, 296 nodeRet, multiNodes(node, maxOccurs-minOccurs, true)); 297 } 298 } 299 } 300 301 return nodeRet; 302 } 303 304 private CMNode multiNodes(CMNode node, int num, boolean copyFirst) { 305 if (num == 0) { 306 return null; 307 } 308 if (num == 1) { 309 return copyFirst ? copyNode(node) : node; 310 } 311 int num1 = num/2; 312 return fNodeFactory.getCMBinOpNode(XSModelGroupImpl.MODELGROUP_SEQUENCE, 313 multiNodes(node, num1, copyFirst), 314 multiNodes(node, num-num1, true)); 315 } 316 317 private CMNode copyNode(CMNode node) { 319 int type = node.type(); 320 if (type == XSModelGroupImpl.MODELGROUP_CHOICE || 322 type == XSModelGroupImpl.MODELGROUP_SEQUENCE) { 323 XSCMBinOp bin = (XSCMBinOp)node; 324 node = fNodeFactory.getCMBinOpNode(type, copyNode(bin.getLeft()), 325 copyNode(bin.getRight())); 326 } 327 else if (type == XSParticleDecl.PARTICLE_ZERO_OR_MORE || 329 type == XSParticleDecl.PARTICLE_ONE_OR_MORE || 330 type == XSParticleDecl.PARTICLE_ZERO_OR_ONE) { 331 XSCMUniOp uni = (XSCMUniOp)node; 332 node = fNodeFactory.getCMUniOpNode(type, copyNode(uni.getChild())); 333 } 334 else if (type == XSParticleDecl.PARTICLE_ELEMENT || 337 type == XSParticleDecl.PARTICLE_WILDCARD) { 338 XSCMLeaf leaf = (XSCMLeaf)node; 339 node = fNodeFactory.getCMLeafNode(leaf.type(), leaf.getLeaf(), leaf.getParticleId(), fLeafCount++); 340 } 341 342 return node; 343 } 344 } 345 | Popular Tags |