Here are the examples of the java api class com.sun.org.apache.xerces.internal.impl.dtd.models.CMNode taken from open source projects.
1. CMBuilder#buildCompactSyntaxTree2()
Project: openjdk
File: CMBuilder.java
File: CMBuilder.java
private CMNode buildCompactSyntaxTree2(XSParticleDecl particle, int minOccurs, int maxOccurs) { // Convert element and wildcard particles to leaf nodes. Wrap repeating particles in a CMUniOpNode. CMNode nodeRet = null; if (minOccurs == 1 && maxOccurs == 1) { nodeRet = fNodeFactory.getCMLeafNode(particle.fType, particle.fValue, fParticleCount++, fLeafCount++); } else if (minOccurs == 0 && maxOccurs == 1) { // zero or one nodeRet = fNodeFactory.getCMLeafNode(particle.fType, particle.fValue, fParticleCount++, fLeafCount++); nodeRet = fNodeFactory.getCMUniOpNode(XSParticleDecl.PARTICLE_ZERO_OR_ONE, nodeRet); } else if (minOccurs == 0 && maxOccurs == SchemaSymbols.OCCURRENCE_UNBOUNDED) { // zero or more nodeRet = fNodeFactory.getCMLeafNode(particle.fType, particle.fValue, fParticleCount++, fLeafCount++); nodeRet = fNodeFactory.getCMUniOpNode(XSParticleDecl.PARTICLE_ZERO_OR_MORE, nodeRet); } else if (minOccurs == 1 && maxOccurs == SchemaSymbols.OCCURRENCE_UNBOUNDED) { // one or more nodeRet = fNodeFactory.getCMLeafNode(particle.fType, particle.fValue, fParticleCount++, fLeafCount++); nodeRet = fNodeFactory.getCMUniOpNode(XSParticleDecl.PARTICLE_ONE_OR_MORE, nodeRet); } else { // {n,m}: Instead of expanding this out, create a compound leaf node which carries the // occurence information and wrap it in the appropriate CMUniOpNode. nodeRet = fNodeFactory.getCMRepeatingLeafNode(particle.fType, particle.fValue, minOccurs, maxOccurs, fParticleCount++, fLeafCount++); if (minOccurs == 0) { nodeRet = fNodeFactory.getCMUniOpNode(XSParticleDecl.PARTICLE_ZERO_OR_MORE, nodeRet); } else { nodeRet = fNodeFactory.getCMUniOpNode(XSParticleDecl.PARTICLE_ONE_OR_MORE, nodeRet); } } return nodeRet; }
2. CMBuilder#buildCompactSyntaxTree()
Project: openjdk
File: CMBuilder.java
File: CMBuilder.java
// A special version of buildSyntaxTree() which builds a compact syntax tree // containing compound leaf nodes which carry occurence information. This method // for building the syntax tree is chosen over buildSyntaxTree() when // useRepeatingLeafNodes() returns true. private CMNode buildCompactSyntaxTree(XSParticleDecl particle) { int maxOccurs = particle.fMaxOccurs; int minOccurs = particle.fMinOccurs; short type = particle.fType; CMNode nodeRet = null; if ((type == XSParticleDecl.PARTICLE_WILDCARD) || (type == XSParticleDecl.PARTICLE_ELEMENT)) { return buildCompactSyntaxTree2(particle, minOccurs, maxOccurs); } else if (type == XSParticleDecl.PARTICLE_MODELGROUP) { XSModelGroupImpl group = (XSModelGroupImpl) particle.fValue; if (group.fParticleCount == 1 && (minOccurs != 1 || maxOccurs != 1)) { return buildCompactSyntaxTree2(group.fParticles[0], minOccurs, maxOccurs); } else { CMNode temp = null; // when the model group is a choice of more than one particles, but // only one of the particle is not empty, (for example // <choice> // <sequence/> // <element name="e"/> // </choice> // ) we can't not return that one particle ("e"). instead, we should // treat such particle as optional ("e?"). // the following int variable keeps track of the number of non-empty children int count = 0; for (int i = 0; i < group.fParticleCount; i++) { // first convert each child to a CM tree temp = buildCompactSyntaxTree(group.fParticles[i]); // then combine them using binary operation if (temp != null) { ++count; if (nodeRet == null) { nodeRet = temp; } else { nodeRet = fNodeFactory.getCMBinOpNode(group.fCompositor, nodeRet, temp); } } } if (nodeRet != null) { // we need to create a zero-or-one (optional) node for the non-empty particles. if (group.fCompositor == XSModelGroupImpl.MODELGROUP_CHOICE && count < group.fParticleCount) { nodeRet = fNodeFactory.getCMUniOpNode(XSParticleDecl.PARTICLE_ZERO_OR_ONE, nodeRet); } } } } return nodeRet; }
3. CMBuilder#expandContentModel()
Project: openjdk
File: CMBuilder.java
File: CMBuilder.java
// 2. expand all occurrence values: a{n, unbounded} -> a, a, ..., a+ // a{n, m} -> a, a, ..., a?, a?, ... // 4. make sure each leaf node (XSCMLeaf) has a distinct position private CMNode expandContentModel(CMNode node, int minOccurs, int maxOccurs, boolean optimize) { CMNode nodeRet = null; if (minOccurs == 1 && maxOccurs == 1) { nodeRet = node; } else if (minOccurs == 0 && maxOccurs == 1) { //zero or one nodeRet = fNodeFactory.getCMUniOpNode(XSParticleDecl.PARTICLE_ZERO_OR_ONE, node); } else if (minOccurs == 0 && maxOccurs == SchemaSymbols.OCCURRENCE_UNBOUNDED) { //zero or more nodeRet = fNodeFactory.getCMUniOpNode(XSParticleDecl.PARTICLE_ZERO_OR_MORE, node); } else if (minOccurs == 1 && maxOccurs == SchemaSymbols.OCCURRENCE_UNBOUNDED) { //one or more nodeRet = fNodeFactory.getCMUniOpNode(XSParticleDecl.PARTICLE_ONE_OR_MORE, node); } else if (optimize && node.type() == XSParticleDecl.PARTICLE_ELEMENT || node.type() == XSParticleDecl.PARTICLE_WILDCARD) { // Only for elements and wildcards, subsume e{n,m} and e{n,unbounded} to e* // or e+ and, once the DFA reaches a final state, check if the actual number // of elements is between minOccurs and maxOccurs. This new algorithm runs // in constant space. // TODO: What is the impact of this optimization on the PSVI? nodeRet = fNodeFactory.getCMUniOpNode(minOccurs == 0 ? XSParticleDecl.PARTICLE_ZERO_OR_MORE : XSParticleDecl.PARTICLE_ONE_OR_MORE, node); nodeRet.setUserData(new int[] { minOccurs, maxOccurs }); } else if (maxOccurs == SchemaSymbols.OCCURRENCE_UNBOUNDED) { // => a,a,..,a+ // create a+ node first, then put minOccurs-1 a's in front of it // for the first time "node" is used, we don't need to make a copy // and for other references to node, we make copies nodeRet = fNodeFactory.getCMUniOpNode(XSParticleDecl.PARTICLE_ONE_OR_MORE, node); // (task 4) we need to call copyNode here, so that we append // an entire new copy of the node (a subtree). this is to ensure // all leaf nodes have distinct position // we know that minOccurs > 1 nodeRet = fNodeFactory.getCMBinOpNode(XSModelGroupImpl.MODELGROUP_SEQUENCE, multiNodes(node, minOccurs - 1, true), nodeRet); } else { // copyNode is called, for the same reason as above if (minOccurs > 0) { nodeRet = multiNodes(node, minOccurs, false); } if (maxOccurs > minOccurs) { node = fNodeFactory.getCMUniOpNode(XSParticleDecl.PARTICLE_ZERO_OR_ONE, node); if (nodeRet == null) { nodeRet = multiNodes(node, maxOccurs - minOccurs, false); } else { nodeRet = fNodeFactory.getCMBinOpNode(XSModelGroupImpl.MODELGROUP_SEQUENCE, nodeRet, multiNodes(node, maxOccurs - minOccurs, true)); } } } return nodeRet; }
4. CMBuilder#buildSyntaxTree()
Project: openjdk
File: CMBuilder.java
File: CMBuilder.java
// 1. convert particle tree to CM tree: // 2. expand all occurrence values: a{n, unbounded} -> a, a, ..., a+ // a{n, m} -> a, a, ..., a?, a?, ... // 3. convert model groups (a, b, c, ...) or (a | b | c | ...) to // binary tree: (((a,b),c),...) or (((a|b)|c)|...) // 4. make sure each leaf node (XSCMLeaf) has a distinct position private CMNode buildSyntaxTree(XSParticleDecl particle, boolean forUPA, boolean optimize) { int maxOccurs = particle.fMaxOccurs; int minOccurs = particle.fMinOccurs; boolean compactedForUPA = false; if (forUPA) { // processing the DFA faster. For UPA the exact values don't matter. if (minOccurs > 1) { if (maxOccurs > minOccurs || particle.getMaxOccursUnbounded()) { minOccurs = 1; compactedForUPA = true; } else { // maxOccurs == minOccurs minOccurs = 2; compactedForUPA = true; } } if (maxOccurs > 1) { maxOccurs = 2; compactedForUPA = true; } } short type = particle.fType; CMNode nodeRet = null; if ((type == XSParticleDecl.PARTICLE_WILDCARD) || (type == XSParticleDecl.PARTICLE_ELEMENT)) { // (task 1) element and wildcard particles should be converted to // leaf nodes // REVISIT: Make a clone of the leaf particle, so that if there // are two references to the same group, we have two different // leaf particles for the same element or wildcard decl. // This is useful for checking UPA. nodeRet = fNodeFactory.getCMLeafNode(particle.fType, particle.fValue, fParticleCount++, fLeafCount++); // (task 2) expand occurrence values nodeRet = expandContentModel(nodeRet, minOccurs, maxOccurs, optimize); if (nodeRet != null) { nodeRet.setIsCompactUPAModel(compactedForUPA); } } else if (type == XSParticleDecl.PARTICLE_MODELGROUP) { // (task 1,3) convert model groups to binary trees XSModelGroupImpl group = (XSModelGroupImpl) particle.fValue; CMNode temp = null; // when the model group is a choice of more than one particles, but // only one of the particle is not empty, (for example // <choice> // <sequence/> // <element name="e"/> // </choice> // ) we can't not return that one particle ("e"). instead, we should // treat such particle as optional ("e?"). // the following boolean variable is true when there are at least // 2 non-empty children. boolean twoChildren = false; for (int i = 0; i < group.fParticleCount; i++) { // first convert each child to a CM tree temp = buildSyntaxTree(group.fParticles[i], forUPA, optimize && minOccurs == 1 && maxOccurs == 1 && (group.fCompositor == XSModelGroupImpl.MODELGROUP_SEQUENCE || group.fParticleCount == 1)); // then combine them using binary operation if (temp != null) { compactedForUPA |= temp.isCompactedForUPA(); if (nodeRet == null) { nodeRet = temp; } else { nodeRet = fNodeFactory.getCMBinOpNode(group.fCompositor, nodeRet, temp); // record the fact that there are at least 2 children twoChildren = true; } } } // (task 2) expand occurrence values if (nodeRet != null) { // particle. if (group.fCompositor == XSModelGroupImpl.MODELGROUP_CHOICE && !twoChildren && group.fParticleCount > 1) { nodeRet = fNodeFactory.getCMUniOpNode(XSParticleDecl.PARTICLE_ZERO_OR_ONE, nodeRet); } nodeRet = expandContentModel(nodeRet, minOccurs, maxOccurs, false); nodeRet.setIsCompactUPAModel(compactedForUPA); } } return nodeRet; }
5. CMBuilder#createDFACM()
Project: openjdk
File: CMBuilder.java
File: CMBuilder.java
XSCMValidator createDFACM(XSParticleDecl particle, boolean forUPA) { fLeafCount = 0; fParticleCount = 0; // convert particle tree to CM tree CMNode node = useRepeatingLeafNodes(particle) ? buildCompactSyntaxTree(particle) : buildSyntaxTree(particle, forUPA, true); if (node == null) return null; // build DFA content model from the CM tree return new XSDFACM(node, fLeafCount); }
6. DTDGrammar#buildSyntaxTree()
Project: openjdk
File: DTDGrammar.java
File: DTDGrammar.java
// createChildModel(int):ContentModelValidator private final CMNode buildSyntaxTree(int startNode, XMLContentSpec contentSpec) { // We will build a node at this level for the new tree CMNode nodeRet = null; getContentSpec(startNode, contentSpec); if ((contentSpec.type & 0x0f) == XMLContentSpec.CONTENTSPECNODE_ANY) { //nodeRet = new CMAny(contentSpec.type, -1, fLeafCount++); nodeRet = new CMAny(contentSpec.type, (String) contentSpec.otherValue, fLeafCount++); } else if ((contentSpec.type & 0x0f) == XMLContentSpec.CONTENTSPECNODE_ANY_OTHER) { nodeRet = new CMAny(contentSpec.type, (String) contentSpec.otherValue, fLeafCount++); } else if ((contentSpec.type & 0x0f) == XMLContentSpec.CONTENTSPECNODE_ANY_LOCAL) { nodeRet = new CMAny(contentSpec.type, null, fLeafCount++); } else // if (contentSpec.type == XMLContentSpec.CONTENTSPECNODE_LEAF) { // // Create a new leaf node, and pass it the current leaf count, // which is its DFA state position. Bump the leaf count after // storing it. This makes the positions zero based since we // store first and then increment. // fQName.setValues(null, (String) contentSpec.value, (String) contentSpec.value, (String) contentSpec.otherValue); nodeRet = new CMLeaf(fQName, fLeafCount++); } else { // // Its not a leaf, so we have to recurse its left and maybe right // nodes. Save both values before we recurse and trash the node. final int leftNode = ((int[]) contentSpec.value)[0]; final int rightNode = ((int[]) contentSpec.otherValue)[0]; if ((contentSpec.type == XMLContentSpec.CONTENTSPECNODE_CHOICE) || (contentSpec.type == XMLContentSpec.CONTENTSPECNODE_SEQ)) { // // Recurse on both children, and return a binary op node // with the two created sub nodes as its children. The node // type is the same type as the source. // nodeRet = new CMBinOp(contentSpec.type, buildSyntaxTree(leftNode, contentSpec), buildSyntaxTree(rightNode, contentSpec)); } else if (contentSpec.type == XMLContentSpec.CONTENTSPECNODE_ZERO_OR_MORE) { nodeRet = new CMUniOp(contentSpec.type, buildSyntaxTree(leftNode, contentSpec)); } else if (contentSpec.type == XMLContentSpec.CONTENTSPECNODE_ZERO_OR_MORE || contentSpec.type == XMLContentSpec.CONTENTSPECNODE_ZERO_OR_ONE || contentSpec.type == XMLContentSpec.CONTENTSPECNODE_ONE_OR_MORE) { nodeRet = new CMUniOp(contentSpec.type, buildSyntaxTree(leftNode, contentSpec)); } else { throw new RuntimeException("ImplementationMessages.VAL_CST"); } } // And return our new node for this level return nodeRet; }
7. DTDGrammar#createChildModel()
Project: openjdk
File: DTDGrammar.java
File: DTDGrammar.java
// printAttribute(int) // content models /** * When the element has a 'CHILDREN' model, this method is called to * create the content model object. It looks for some special case simple * models and creates SimpleContentModel objects for those. For the rest * it creates the standard DFA style model. */ private synchronized ContentModelValidator createChildModel(int contentSpecIndex) { // // Get the content spec node for the element we are working on. // This will tell us what kind of node it is, which tells us what // kind of model we will try to create. // XMLContentSpec contentSpec = new XMLContentSpec(); getContentSpec(contentSpecIndex, contentSpec); if ((contentSpec.type & 0x0f) == XMLContentSpec.CONTENTSPECNODE_ANY || (contentSpec.type & 0x0f) == XMLContentSpec.CONTENTSPECNODE_ANY_OTHER || (contentSpec.type & 0x0f) == XMLContentSpec.CONTENTSPECNODE_ANY_LOCAL) { // let fall through to build a DFAContentModel } else if (contentSpec.type == XMLContentSpec.CONTENTSPECNODE_LEAF) { // if (contentSpec.value == null && contentSpec.otherValue == null) throw new RuntimeException("ImplementationMessages.VAL_NPCD"); // // Its a single leaf, so its an 'a' type of content model, i.e. // just one instance of one element. That one is definitely a // simple content model. // fQName.setValues(null, (String) contentSpec.value, (String) contentSpec.value, (String) contentSpec.otherValue); return new SimpleContentModel(contentSpec.type, fQName, null); } else if ((contentSpec.type == XMLContentSpec.CONTENTSPECNODE_CHOICE) || (contentSpec.type == XMLContentSpec.CONTENTSPECNODE_SEQ)) { // // Lets see if both of the children are leafs. If so, then it // it has to be a simple content model // XMLContentSpec contentSpecLeft = new XMLContentSpec(); XMLContentSpec contentSpecRight = new XMLContentSpec(); getContentSpec(((int[]) contentSpec.value)[0], contentSpecLeft); getContentSpec(((int[]) contentSpec.otherValue)[0], contentSpecRight); if ((contentSpecLeft.type == XMLContentSpec.CONTENTSPECNODE_LEAF) && (contentSpecRight.type == XMLContentSpec.CONTENTSPECNODE_LEAF)) { // // Its a simple choice or sequence, so we can do a simple // content model for it. // fQName.setValues(null, (String) contentSpecLeft.value, (String) contentSpecLeft.value, (String) contentSpecLeft.otherValue); fQName2.setValues(null, (String) contentSpecRight.value, (String) contentSpecRight.value, (String) contentSpecRight.otherValue); return new SimpleContentModel(contentSpec.type, fQName, fQName2); } } else if ((contentSpec.type == XMLContentSpec.CONTENTSPECNODE_ZERO_OR_ONE) || (contentSpec.type == XMLContentSpec.CONTENTSPECNODE_ZERO_OR_MORE) || (contentSpec.type == XMLContentSpec.CONTENTSPECNODE_ONE_OR_MORE)) { // // Its a repetition, so see if its one child is a leaf. If so // its a repetition of a single element, so we can do a simple // content model for that. // XMLContentSpec contentSpecLeft = new XMLContentSpec(); getContentSpec(((int[]) contentSpec.value)[0], contentSpecLeft); if (contentSpecLeft.type == XMLContentSpec.CONTENTSPECNODE_LEAF) { // // It is, so we can create a simple content model here that // will check for this repetition. We pass -1 for the unused // right node. // fQName.setValues(null, (String) contentSpecLeft.value, (String) contentSpecLeft.value, (String) contentSpecLeft.otherValue); return new SimpleContentModel(contentSpec.type, fQName, null); } } else { throw new RuntimeException("ImplementationMessages.VAL_CST"); } // // Its not a simple content model, so here we have to create a DFA // for this element. So we create a DFAContentModel object. He // encapsulates all of the work to create the DFA. // fLeafCount = 0; //int leafCount = countLeaves(contentSpecIndex); fLeafCount = 0; CMNode cmn = buildSyntaxTree(contentSpecIndex, contentSpec); // REVISIT: has to be fLeafCount because we convert x+ to x,x*, one more leaf return new DFAContentModel(cmn, fLeafCount, false); }