use of org.eclipse.wst.xml.core.internal.contentmodel.CMNode in project webtools.sourceediting by eclipse.
the class AbstractContentAssistProcessor 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.
protected List getAvailableChildElementDeclarations(Element parent, int childPosition, int kindOfAction) {
List modelQueryActions = getAvailableChildrenAtIndex(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 AbstractContentAssistProcessor method addAttributeNameProposals.
protected void addAttributeNameProposals(ContentAssistRequest contentAssistRequest) {
IDOMNode node = (IDOMNode) contentAssistRequest.getNode();
IStructuredDocumentRegion sdRegion = contentAssistRequest.getDocumentRegion();
// retrieve the list of attributes
CMElementDeclaration elementDecl = getCMElementDeclaration(node);
if (elementDecl != null) {
CMNamedNodeMap attributes = elementDecl.getAttributes();
CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(attributes);
if (node.getNodeType() == Node.ELEMENT_NODE) {
List nodes = ModelQueryUtil.getModelQuery(node.getOwnerDocument()).getAvailableContent((Element) node, 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);
}
}
}
attributes = allAttributes;
String matchString = contentAssistRequest.getMatchString();
// check whether an attribute really exists for the replacement
// offsets AND if it possesses a value
boolean attrAtLocationHasValue = false;
NamedNodeMap attrs = node.getAttributes();
for (int i = 0; i < attrs.getLength(); i++) {
AttrImpl existingAttr = (AttrImpl) attrs.item(i);
ITextRegion name = existingAttr.getNameRegion();
if ((sdRegion.getStartOffset(name) <= contentAssistRequest.getReplacementBeginPosition()) && (sdRegion.getStartOffset(name) + name.getLength() >= contentAssistRequest.getReplacementBeginPosition() + contentAssistRequest.getReplacementLength()) && (existingAttr.getValueRegion() != null)) {
attrAtLocationHasValue = true;
break;
}
}
// the matchstring
if (attributes != null) {
for (int i = 0; i < attributes.getLength(); i++) {
CMAttributeDeclaration attrDecl = (CMAttributeDeclaration) attributes.item(i);
int isRequired = 0;
if (attrDecl.getUsage() == CMAttributeDeclaration.REQUIRED) {
isRequired = XMLRelevanceConstants.R_REQUIRED;
}
boolean showAttribute = true;
showAttribute = showAttribute && beginsWith(getRequiredName(node, attrDecl), matchString.trim());
AttrImpl attr = (AttrImpl) node.getAttributes().getNamedItem(getRequiredName(node, attrDecl));
ITextRegion nameRegion = attr != null ? attr.getNameRegion() : null;
// nameRegion.getEndOffset() + 1 is required to allow for
// matches against the full name of an existing Attr
showAttribute = showAttribute && (attr == null || nameRegion == null || (nameRegion != null && (sdRegion.getStartOffset(nameRegion) <= contentAssistRequest.getReplacementBeginPosition()) && (sdRegion.getStartOffset(nameRegion) + nameRegion.getLength() >= contentAssistRequest.getReplacementBeginPosition() + contentAssistRequest.getReplacementLength())));
if (showAttribute) {
Image attrImage = CMImageUtil.getImage(attrDecl);
if (attrImage == null) {
if (isRequired > 0) {
attrImage = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ATT_REQ_OBJ);
} else {
attrImage = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ATTRIBUTE);
}
}
String proposedText = null;
String proposedInfo = getAdditionalInfo(elementDecl, attrDecl);
CustomCompletionProposal proposal = null;
// attribute is at this location and already exists
if (attrAtLocationHasValue) {
// only propose the name
proposedText = getRequiredName(node, attrDecl);
proposal = new CustomCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), proposedText.length(), attrImage, proposedText, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_NAME + isRequired, true);
} else // no attribute exists or is elsewhere, generate
// minimally
{
Attr existingAttrNode = (Attr) node.getAttributes().getNamedItem(getRequiredName(node, attrDecl));
String value = null;
if (existingAttrNode != null && existingAttrNode.getSpecified()) {
value = existingAttrNode.getNodeValue();
}
if ((value != null) && (value.length() > 0)) {
proposedText = getRequiredName(node, attrDecl);
} else {
proposedText = getRequiredText(node, attrDecl);
}
proposal = new CustomCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), attrDecl.getNodeName().length() + 2, attrImage, // and there is no single quote that may be encasing a double quote
(showValues && (proposedText.lastIndexOf('\"') - proposedText.indexOf('\"') == 1 && proposedText.indexOf('\'') == -1)) ? getRequiredName(node, attrDecl) : proposedText, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_NAME + isRequired);
}
contentAssistRequest.addProposal(proposal);
}
}
}
} else {
setErrorMessage(NLS.bind(XMLUIMessages.Element__is_unknown, (new Object[] { node.getNodeName() })));
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMNode in project webtools.sourceediting by eclipse.
the class AbstractContentAssistProcessor method attributeInList.
/**
* This method determines if any of the attributes in the proposed XMLNode
* node, are possible values of attributes from possible Elements at this
* point in the document according to the Content Model.
*
* @param node
* the element with attributes that you would like to test if
* are possible for possible Elements at this point
* @param cmnode
* possible element at this point in the document (depending on
* what 'node' is) true if any attributes of 'node' match any
* possible attributes from 'cmnodes' list.
*/
protected boolean attributeInList(IDOMNode node, Node parent, CMNode cmnode) {
if ((node == null) || (parent == null) || (cmnode == null)) {
return false;
}
String elementMatchString = node.getNodeName();
// cmnode.getNodeName();
String cmnodeName = getRequiredName(parent, cmnode);
if (node instanceof Element) {
NamedNodeMap map = ((Element) node).getAttributes();
// $NON-NLS-1$
String attrMatchString = "";
CMNamedNodeMap cmattrMap = null;
// iterate attribute possibilities for partially started node
for (int i = 0; (map != null) && (i < map.getLength()); i++) {
attrMatchString = map.item(i).getNodeName();
// filter on whatever user typed for element name already
if (beginsWith(cmnodeName, elementMatchString)) {
if (cmnode.getNodeType() == CMNode.ELEMENT_DECLARATION) {
cmattrMap = ((CMElementDeclaration) cmnode).getAttributes();
CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(cmattrMap);
List nodes = ModelQueryUtil.getModelQuery(node.getOwnerDocument()).getAvailableContent((Element) node, (CMElementDeclaration) cmnode, ModelQuery.INCLUDE_ATTRIBUTES);
for (int k = 0; k < nodes.size(); k++) {
CMNode adnode = (CMNode) nodes.get(k);
if (adnode.getNodeType() == CMNode.ATTRIBUTE_DECLARATION) {
allAttributes.put(adnode);
}
}
cmattrMap = allAttributes;
// proposal list
for (int k = 0; (cmattrMap != null) && (k < cmattrMap.getLength()); k++) {
// check if name matches
if (cmattrMap.item(k).getNodeName().equals(attrMatchString)) {
return true;
}
}
}
}
}
}
return false;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMNode in project webtools.sourceediting by eclipse.
the class AttributeContextInformationProvider method getInfoForElement.
/**
* @param node
*/
private IContextInformation[] getInfoForElement(IDOMNode node) {
IContextInformation[] results = EMPTY_CONTEXT_INFO;
CMElementDeclaration decl = fModelUtil.getModelQuery().getCMElementDeclaration((Element) node);
if (decl != null) {
CMNamedNodeMap attributes = decl.getAttributes();
CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(attributes);
List nodes = ModelQueryUtil.getModelQuery(node.getOwnerDocument()).getAvailableContent((Element) node, decl, 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);
}
}
attributes = allAttributes;
String attrContextString = node.getNodeName();
// $NON-NLS-1$
StringBuffer attrInfo = new StringBuffer(" ");
// $NON-NLS-1$
String name = "";
HashMap attrPosMap = new HashMap();
int pos = 0;
int length = 0;
int numPerLine = 8;
CMAttributeDeclaration[] sortedAttrs = getSortedAttributes(attributes);
for (int i = 0; i < sortedAttrs.length; i++) {
name = sortedAttrs[i].getAttrName();
length = name.length();
pos = attrInfo.length();
attrInfo.append(name);
if (sortedAttrs[i].getUsage() == CMAttributeDeclaration.REQUIRED) {
// $NON-NLS-1$
attrInfo.append("*");
length++;
}
if (i < attributes.getLength() - 1) {
// $NON-NLS-1$
attrInfo.append(" ");
if ((i != 0) && (i % numPerLine == 0)) {
// $NON-NLS-1$
attrInfo.append("\n ");
}
}
attrPosMap.put(name, new Position(pos, length));
}
if (!attrInfo.toString().trim().equals("")) {
return new IContextInformation[] { new AttributeContextInformation(attrContextString, attrInfo.toString(), attrPosMap) };
}
}
return results;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMNode in project webtools.sourceediting by eclipse.
the class AttributeContextInformationProvider method getInfoForText.
/**
* @param node
*/
IContextInformation[] getInfoForText(IDOMNode node) {
Node parent = node.getParentNode();
String contextString = node.getNodeName();
// $NON-NLS-1$
StringBuffer info = new StringBuffer(" ");
if ((parent != null) && (parent.getNodeType() == Node.ELEMENT_NODE)) {
CMElementDeclaration decl = fModelUtil.getModelQuery().getCMElementDeclaration((Element) parent);
CMContent content = decl.getContent();
if (content instanceof CMGroup) {
CMGroup cmGroup = (CMGroup) content;
CMNodeList children = cmGroup.getChildNodes();
CMNode cmNode = null;
for (int i = 0; i < children.getLength(); i++) {
cmNode = children.item(i);
contextString = cmNode.getNodeName();
if (contextString != null) {
// $NON-NLS-1$ //$NON-NLS-2$
info.append("<" + cmNode.getNodeName() + ">");
if (i < children.getLength() - 1) {
// $NON-NLS-1$
info.append(" ");
}
}
}
}
}
if (!info.toString().trim().equals("")) {
return new IContextInformation[] { new ContextInformation(contextString, info.toString()) };
} else {
return EMPTY_CONTEXT_INFO;
}
}
Aggregations