use of org.eclipse.wst.xml.core.internal.contentmodel.CMNode in project webtools.sourceediting by eclipse.
the class XPathElementContentAssist method createXPathXMLProposals.
private void createXPathXMLProposals(Node ancestorNode, Iterator<CMNode> cmNodeIt) {
while (cmNodeIt.hasNext()) {
CMNode cmNode = cmNodeIt.next();
String proposedText = getRequiredName(ancestorNode, cmNode);
if (!(proposedText.contains("xsl:") || proposedText.contains("xslt:"))) {
// $NON-NLS-1$ //$NON-NLS-2$
int offset = getReplacementBeginPosition();
Image image = getCMNodeImage(cmNode);
int startLength = getCursorPosition() - offset;
String additionalInfo = getInfoProvider().getInfo(cmNode);
if (matchString.length() > 0) {
if (proposedText.startsWith(matchString)) {
CustomCompletionProposal proposal = createProposal(proposedText, additionalInfo, offset, image, startLength);
addProposal(proposal);
}
} else {
CustomCompletionProposal proposal = createProposal(proposedText, additionalInfo, offset, image, startLength);
addProposal(proposal);
}
}
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMNode in project webtools.sourceediting by eclipse.
the class CMValidatorTest method main.
public static void main(String[] arg) {
if (arg.length > 1) {
try {
// CMDocumentFactoryRegistry.getInstance().registerCMDocumentBuilderWithClassName("org.eclipse.wst.xml.core.internal.contentmodel.mofimpl.CMDocumentBuilderImpl");
String grammarFileName = arg[0];
String elementName = arg[1];
CMDocument cmDocument = ContentModelManager.getInstance().createCMDocument(grammarFileName, null);
CMNamedNodeMap elementMap = cmDocument.getElements();
CMElementDeclaration element = (CMElementDeclaration) elementMap.getNamedItem(elementName);
if (element != null) {
/*
println("found element [" + elementName + "] contentType = " + element.getContentType());
GraphNode graphNode = createGraph(element);
printGraph(graphNode);
*/
// $NON-NLS-1$
println("-------------- begin validate ---------------");
StringElementContentComparator comparator = new StringElementContentComparator();
CMValidator validator = new CMValidator();
ElementPathRecordingResult result = new ElementPathRecordingResult();
validator.getOriginArray(element, CMValidator.createStringList(arg, 2), comparator, result);
if (result.isValid) {
CMNode[] nodeMapping = result.getOriginArray();
// $NON-NLS-1$
println("Validation Success!");
// $NON-NLS-1$
print(" ");
for (int i = 0; i < nodeMapping.length; i++) {
// $NON-NLS-1$
String name = nodeMapping[i] != null ? nodeMapping[i].getNodeName() : "null";
// $NON-NLS-1$ //$NON-NLS-2$
print("[" + name + "]");
}
// $NON-NLS-1$
println("");
} else {
// $NON-NLS-1$
println("Validation Failed! ");
if (result.errorMessage != null) {
// $NON-NLS-1$
println(" " + result.errorMessage);
}
}
// $NON-NLS-1$
println("-------------- end validate ---------------");
} else {
// $NON-NLS-1$ //$NON-NLS-2$
println("element [" + elementName + "] can not be found");
}
} catch (Exception e) {
// $NON-NLS-1$
println("CMValidator error");
e.printStackTrace();
}
} else {
// $NON-NLS-1$ //$NON-NLS-2$
println("2 args required... only " + arg.length + " provided");
// $NON-NLS-1$
println("usage java org.eclipse.wst.newxml.util.XMLUtil grammarFileName rootElementName pattern");
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMNode in project webtools.sourceediting by eclipse.
the class AttributeShowingLabelProvider method getText.
/*
* (non-Javadoc)
*
* @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
*/
@Override
public String getText(Object o) {
StringBuffer text = null;
if (o instanceof Node) {
Node node = (Node) o;
if ((node.getNodeType() == Node.ELEMENT_NODE) && fShowAttributes) {
text = new StringBuffer(super.getText(o));
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=88444
if (node.hasAttributes()) {
Element element = (Element) node;
NamedNodeMap attributes = element.getAttributes();
Node idTypedAttribute = null;
Node requiredAttribute = null;
boolean hasId = false;
boolean hasName = false;
Node shownAttribute = null;
// try to get content model element
// declaration
CMElementDeclaration elementDecl = null;
ModelQuery mq = ModelQueryUtil.getModelQuery(element.getOwnerDocument());
if (mq != null) {
elementDecl = mq.getCMElementDeclaration(element);
}
// ID
if (elementDecl != null) {
int i = 0;
while ((i < attributes.getLength()) && (idTypedAttribute == null)) {
Node attr = attributes.item(i);
String attrName = attr.getNodeName();
CMNamedNodeMap attributeDeclarationMap = elementDecl.getAttributes();
CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(attributeDeclarationMap);
List nodes = ModelQueryUtil.getModelQuery(node.getOwnerDocument()).getAvailableContent(element, elementDecl, ModelQuery.INCLUDE_ATTRIBUTES);
for (int k = 0; k < nodes.size(); k++) {
CMNode cmnode = (CMNode) nodes.get(k);
if (cmnode.getNodeType() == CMNode.ATTRIBUTE_DECLARATION) {
allAttributes.put(cmnode);
}
}
attributeDeclarationMap = allAttributes;
CMAttributeDeclaration attrDecl = (CMAttributeDeclaration) attributeDeclarationMap.getNamedItem(attrName);
if (attrDecl != null) {
if ((attrDecl.getAttrType() != null) && (CMDataType.ID.equals(attrDecl.getAttrType().getDataTypeName()))) {
idTypedAttribute = attr;
} else if ((attrDecl.getUsage() == CMAttributeDeclaration.REQUIRED) && (requiredAttribute == null)) {
// as a backup, keep tabs on
// any required
// attributes
requiredAttribute = attr;
} else {
hasId = hasId || attrName.equals(ATTR_ID);
hasName = hasName || attrName.equals(ATTR_NAME);
}
}
++i;
}
}
/*
* If no suitable attribute was found, try using a
* required attribute, if none, then prefer "id" or
* "name", otherwise just use first attribute
*/
if (idTypedAttribute != null) {
shownAttribute = idTypedAttribute;
} else if (requiredAttribute != null) {
shownAttribute = requiredAttribute;
} else if (hasId) {
shownAttribute = attributes.getNamedItem(ATTR_ID);
} else if (hasName) {
shownAttribute = attributes.getNamedItem(ATTR_NAME);
}
if (shownAttribute == null) {
shownAttribute = attributes.item(0);
}
// display the attribute and value (without quotes)
String attributeName = shownAttribute.getNodeName();
if ((attributeName != null) && (attributeName.length() > 0)) {
// $NON-NLS-1$
text.append(" ");
text.append(attributeName);
String attributeValue = shownAttribute.getNodeValue();
if ((attributeValue != null) && (attributeValue.length() > 0)) {
// $NON-NLS-1$
text.append("=");
text.append(StringUtils.strip(attributeValue));
}
}
// if (XSLCore.XSL_NAMESPACE_URI.equals(node.getNamespaceURI())) {
// Element el = (Element) node;
// Attr attr = el.getAttributeNode("mode"); //$NON-NLS-1$
// if (attr != null) {
// text.append(" "); //$NON-NLS-1$
// text.append(attr.getName());
// text.append("="); //$NON-NLS-1$
// text.append(StringUtils.strip(attr.getNodeValue()));
// }
// }
}
} else {
text = new StringBuffer(super.getText(o));
}
} else {
return super.toString();
}
return text.toString();
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMNode in project webtools.sourceediting by eclipse.
the class AbstractXMLModelQueryCompletionProposalComputer method getAvailableChildElementDeclarations.
/**
* returns a list of CMNodes that are available within this parent context
* Given the grammar shown below and a snippet of XML code (where the '|'
* indicated the cursor position)
* the list would return all of the element declarations that are
* potential child elements of Foo.
*
* grammar : Foo -> (A, B, C)
* snippet : <Foo><A>|
* result : {A, B, C}
*
* TODO cs... do we need to pass in the 'kindOfAction'? Seems to me we
* could assume it's always an insert.
*
* @param parent
* @param childPosition
* @param kindOfAction
* @return
*/
private List getAvailableChildElementDeclarations(Element parent, int childPosition, int kindOfAction) {
List modelQueryActions = getAvailableModelQueryActionsAtIndex(parent, childPosition, ModelQuery.VALIDITY_NONE);
Iterator iterator = modelQueryActions.iterator();
List cmnodes = new Vector();
while (iterator.hasNext()) {
ModelQueryAction action = (ModelQueryAction) iterator.next();
if ((childPosition < 0) || (((action.getStartIndex() <= childPosition) && (childPosition <= action.getEndIndex())) && (action.getKind() == kindOfAction))) {
CMNode actionCMNode = action.getCMNode();
if ((actionCMNode != null) && !cmnodes.contains(actionCMNode)) {
cmnodes.add(actionCMNode);
}
}
}
return cmnodes;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMNode in project webtools.sourceediting by eclipse.
the class AbstractXMLModelQueryCompletionProposalComputer method getValidChildElementDeclarations.
/**
* returns a list of CMNodes that can be validly inserted at this
* childPosition
* Given the grammar shown below and a snippet of XML code (where the '|'
* indicated the cursor position)
* the list would return only the element declarations can be inserted
* while maintaing validity of the content.
*
* grammar : Foo -> (A, B, C)
* snippet : <Foo><A>|
* result : {B}
*
* @param parent
* @param childPosition
* @param kindOfAction
* @return
*/
private List getValidChildElementDeclarations(Element parent, int childPosition, int kindOfAction) {
List modelQueryActions = getAvailableModelQueryActionsAtIndex(parent, childPosition, ModelQuery.VALIDITY_STRICT);
Iterator iterator = modelQueryActions.iterator();
List cmnodes = new Vector();
while (iterator.hasNext()) {
ModelQueryAction action = (ModelQueryAction) iterator.next();
if ((childPosition < 0) || (((action.getStartIndex() <= childPosition) && (childPosition <= action.getEndIndex())) && (action.getKind() == kindOfAction))) {
CMNode actionCMNode = action.getCMNode();
if ((actionCMNode != null) && !cmnodes.contains(actionCMNode)) {
cmnodes.add(actionCMNode);
}
}
}
return cmnodes;
}
Aggregations