use of org.eclipse.wst.xml.core.internal.contentmodel.CMContent in project webtools.sourceediting by eclipse.
the class HMQUtil method getInclusions.
/* gather all element declarations in inclusion of each parent of the target.
* the inclusion of the target is also gathered. */
public static Collection getInclusions(Element target) {
if (target == null)
return nullVector;
Collection inclusions = gatherInclusions(getAncestorDeclarations(target));
Hashtable availables = new Hashtable();
Iterator iter = inclusions.iterator();
while (iter.hasNext()) {
CMContent inclusion = (CMContent) iter.next();
switch(inclusion.getNodeType()) {
case CMNode.GROUP:
extractDeclarations(availables, (CMGroup) inclusion);
break;
case CMNode.ELEMENT_DECLARATION:
addInclusion(availables, (CMElementDeclaration) inclusion);
break;
}
}
return availables.values();
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMContent in project webtools.sourceediting by eclipse.
the class ModelQueryTester method testHtmlChildren.
/**
* Test the HTML HTML Element for its declared children
*/
public void testHtmlChildren() {
setUpHTML();
// set text
fModel.getStructuredDocument().set("<html></html>");
Element htmlElement = fModel.getDocument().getDocumentElement();
CMElementDeclaration htmlDecl = fModelQuery.getCMElementDeclaration(htmlElement);
// HTML's children are within a group
CMContent contents = htmlDecl.getContent();
assertTrue("content type is not a group", contents.getNodeType() == CMNode.GROUP);
CMGroup group = (CMGroup) contents;
int operator = group.getOperator();
CMNodeList childList = group.getChildNodes();
int max = contents.getMaxOccur();
int min = contents.getMinOccur();
// the group should be allowed once, with a sequence whose first entry
// is the declaration for HEAD
assertTrue("occurrance of group", min == 1 && max == 1);
assertTrue("relationship in group", operator == CMGroup.SEQUENCE);
assertTrue("content descriptor type, position 0", contents.getNodeType() == CMNode.GROUP);
assertTrue("child order (HEAD first)", childList.item(0).getNodeName().equalsIgnoreCase(HTML40Namespace.ElementName.HEAD));
assertTrue("content descriptor type, position 1", childList.item(1).getNodeType() == CMNode.GROUP);
// The second child should be a group as well, containing BODY and
// FRAMESET with an
// operator of CMGroup.CHOICE
assertTrue("content descriptor type, position 1 - relationship of group", ((CMGroup) childList.item(1)).getOperator() == CMGroup.CHOICE);
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMContent in project webtools.sourceediting by eclipse.
the class CMValidator method validatAllGroupContent.
private void validatAllGroupContent(List elementContent, ElementContentComparator comparator, CMGroup allGroup, Result result) {
boolean isValid = true;
boolean isPartiallyValid = true;
HashMap map = new HashMap();
CMNodeList list = allGroup.getChildNodes();
for (int j = list.getLength() - 1; j >= 0; j--) {
CMNode node = list.item(j);
if (map.get(node) == null) {
map.put(node, new ItemCount());
}
}
int validitionCount = 0;
for (Iterator i = elementContent.iterator(); i.hasNext(); validitionCount++) {
Object o = i.next();
if (comparator.isElement(o)) {
// test to see if the element is listed in the all group
//
CMNode matchingCMNode = null;
for (int j = list.getLength() - 1; j >= 0; j--) {
CMNode node = list.item(j);
if (comparator.matches(o, node)) {
matchingCMNode = node;
break;
}
}
if (matchingCMNode == null) {
isPartiallyValid = false;
isValid = false;
break;
} else {
// test to see that the element occurs only once
//
ItemCount itemCount = (ItemCount) map.get(matchingCMNode);
if (itemCount != null) {
if (itemCount.count > 0) {
// we don't want to allow too many elements!
// we consider 'not enough' to be partially valid... but not 'too many'
isPartiallyValid = false;
break;
} else {
itemCount.count++;
}
}
}
}
}
if (isValid) {
for (int j = list.getLength() - 1; j >= 0; j--) {
CMNode node = list.item(j);
if (node.getNodeType() == CMNode.ELEMENT_DECLARATION) {
CMContent content = (CMContent) node;
ItemCount itemCount = (ItemCount) map.get(node);
// System.out.print("content " + content.getNodeName() + " " + content.getMinOccur());
if (itemCount.count < content.getMinOccur()) {
isValid = false;
break;
}
}
}
}
if (result instanceof ElementPathRecordingResult && isPartiallyValid) {
((ElementPathRecordingResult) result).setPartialValidationCount(validitionCount);
}
result.isValid = isValid;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMContent in project webtools.sourceediting by eclipse.
the class CMValidator method collectNextSiblings.
private void collectNextSiblings(GraphNode node, List nextSiblingList, List namedArcList, List unamedArcList, int indent) {
// printlnIndented(indent, "GraphNode:" + node.name + decoration);
indent += 2;
for (Iterator i = node.arcList.iterator(); i.hasNext(); ) {
Arc arc = (Arc) i.next();
if (arc.kind == Arc.ELEMENT) {
if (!namedArcList.contains(arc)) {
if (arc.cmNode != null) {
nextSiblingList.add(arc.cmNode);
if (arc.cmNode.getNodeType() == CMNode.ELEMENT_DECLARATION || arc.cmNode.getNodeType() == CMNode.ANY_ELEMENT) {
namedArcList.add(arc);
CMContent cmNode = (CMContent) arc.cmNode;
if (cmNode.getMinOccur() == 0) {
unamedArcList = new ArrayList();
collectNextSiblings(arc.node, nextSiblingList, namedArcList, unamedArcList, indent + 2);
}
}
}
}
} else {
if (!unamedArcList.contains(arc)) {
unamedArcList.add(arc);
collectNextSiblings(arc.node, nextSiblingList, namedArcList, unamedArcList, indent + 2);
}
}
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMContent in project webtools.sourceediting by eclipse.
the class DOMContentBuilderImpl method visitCMElementDeclaration.
public void visitCMElementDeclaration(CMElementDeclaration ed) {
int forcedMin = (buildOptionalElements(buildPolicy) || alwaysVisit) ? 1 : 0;
int min = Math.max(ed.getMinOccur(), forcedMin);
// a group.
if (!cmGroupStack.isEmpty()) {
CMGroup group = (CMGroup) cmGroupStack.peek();
int gmin = group.getMinOccur();
if (gmin == 0)
if (buildOptionalElements(buildPolicy)) {
/* do nothing: min = min */
} else {
// min = 0
min = min * gmin;
}
else {
min = min * gmin;
}
}
int max = Math.min(ed.getMaxOccur(), getNumOfRepeatableElements());
if (max < min)
max = min;
alwaysVisit = false;
// involved.
if (// leave
buildFirstSubstitution(buildPolicy) || isAbstract(ed)) // this
// for
// backward
// compatibility
// for now
{
// Note - To change so that if ed is optional, we do not
// generate anything here.
ed = getSubstitution(ed);
// Note - the returned ed may be an abstract element in
// which case the xml will be invalid.
}
if (min > 0 && !visitedCMElementDeclarationList.contains(ed)) {
visitedCMElementDeclarationList.add(ed);
for (int i = 1; i <= max; i++) {
// create an Element for each
Element element = null;
if (rootElement != null) {
element = rootElement;
rootElement = null;
} else {
element = createElement(ed, computeName(ed, currentParent), currentParent);
}
// visit the children of the GrammarElement
Node oldParent = currentParent;
currentParent = element;
handlePushParent(element, ed);
namespaceTable.addElement(element);
boolean oldAttachNodesToParent = attachNodesToParent;
attachNodesToParent = true;
// 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
attachNodesToParent = oldAttachNodesToParent;
handlePopParent(element, ed);
currentParent = oldParent;
linkNode(element);
}
int size = visitedCMElementDeclarationList.size();
visitedCMElementDeclarationList.remove(size - 1);
}
}
Aggregations