use of org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration in project webtools.sourceediting by eclipse.
the class AttrImpl method getDeclaration.
/**
*/
protected CMAttributeDeclaration getDeclaration() {
ElementImpl element = (ElementImpl) getOwnerElement();
if (element == null)
return null;
CMElementDeclaration elementDecl = element.getDeclaration();
if (elementDecl == null)
return null;
List nodes = ModelQueryUtil.getModelQuery(getOwnerDocument()).getAvailableContent(getOwnerElement(), elementDecl, ModelQuery.INCLUDE_ATTRIBUTES);
String name = getName();
for (int k = 0; k < nodes.size(); k++) {
CMNode cmnode = (CMNode) nodes.get(k);
if (cmnode.getNodeType() == CMNode.ATTRIBUTE_DECLARATION && name.equals(cmnode.getNodeName())) {
return (CMAttributeDeclaration) cmnode;
}
}
return null;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration in project webtools.sourceediting by eclipse.
the class AbstractContentAssistProcessor method addAttributeValueProposals.
protected void addAttributeValueProposals(ContentAssistRequest contentAssistRequest) {
IDOMNode node = (IDOMNode) contentAssistRequest.getNode();
// Find the attribute region and name for which this position should
// have a value proposed
IStructuredDocumentRegion open = node.getFirstStructuredDocumentRegion();
ITextRegionList openRegions = open.getRegions();
int i = openRegions.indexOf(contentAssistRequest.getRegion());
if (i < 0) {
return;
}
ITextRegion nameRegion = null;
while (i >= 0) {
nameRegion = openRegions.get(i--);
if (nameRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME) {
break;
}
}
// the name region is REQUIRED to do anything useful
if (nameRegion != null) {
// Retrieve the declaration
CMElementDeclaration elementDecl = getCMElementDeclaration(node);
// String attributeName = nameRegion.getText();
String attributeName = open.getText(nameRegion);
CMAttributeDeclaration attrDecl = null;
// declaration for the attribute otherwise
if (elementDecl != null) {
CMNamedNodeMap attributes = elementDecl.getAttributes();
CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(attributes) {
private Map caseInsensitive;
private Map getCaseInsensitiveMap() {
if (caseInsensitive == null)
caseInsensitive = new HashMap();
return caseInsensitive;
}
public CMNode getNamedItem(String name) {
CMNode node = super.getNamedItem(name);
if (node == null) {
node = (CMNode) getCaseInsensitiveMap().get(name.toLowerCase(Locale.US));
}
return node;
}
public void put(CMNode cmNode) {
super.put(cmNode);
getCaseInsensitiveMap().put(cmNode.getNodeName().toLowerCase(Locale.US), cmNode);
}
};
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 noprefixName = DOMNamespaceHelper.getUnprefixedName(attributeName);
if (attributes != null) {
attrDecl = (CMAttributeDeclaration) attributes.getNamedItem(noprefixName);
if (attrDecl == null) {
attrDecl = (CMAttributeDeclaration) attributes.getNamedItem(attributeName);
}
}
if (attrDecl == null) {
setErrorMessage(UNKNOWN_ATTR, attributeName);
}
}
String currentValue = node.getAttributes().getNamedItem(attributeName).getNodeValue();
String proposedInfo = null;
Image image = CMImageUtil.getImage(attrDecl);
if (image == null) {
if ((attrDecl != null) && (attrDecl.getUsage() == CMAttributeDeclaration.REQUIRED)) {
image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ATT_REQ_OBJ);
} else {
image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ATTRIBUTE);
}
}
if ((attrDecl != null) && (attrDecl.getAttrType() != null)) {
// attribute is known, prompt with values from the declaration
proposedInfo = getAdditionalInfo(elementDecl, attrDecl);
List possibleValues = getPossibleDataTypeValues(node, attrDecl);
String defaultValue = attrDecl.getAttrType().getImpliedValue();
if (possibleValues.size() > 0 || defaultValue != null) {
// ENUMERATED VALUES
String matchString = contentAssistRequest.getMatchString();
if (matchString == null) {
// $NON-NLS-1$
matchString = "";
}
if ((matchString.length() > 0) && (matchString.startsWith("\"") || matchString.startsWith("'"))) {
matchString = matchString.substring(1);
}
boolean currentValid = false;
// d210858, if the region's a container, don't suggest the
// enumerated values as they probably won't help
boolean existingComplicatedValue = (contentAssistRequest.getRegion() != null) && (contentAssistRequest.getRegion() instanceof ITextRegionContainer);
if (!existingComplicatedValue) {
int rOffset = contentAssistRequest.getReplacementBeginPosition();
int rLength = contentAssistRequest.getReplacementLength();
for (Iterator j = possibleValues.iterator(); j.hasNext(); ) {
String possibleValue = (String) j.next();
if (!possibleValue.equals(defaultValue)) {
currentValid = currentValid || possibleValue.equals(currentValue);
if ((matchString.length() == 0) || possibleValue.startsWith(matchString)) {
// $NON-NLS-2$//$NON-NLS-1$
String rString = "\"" + possibleValue + "\"";
CustomCompletionProposal proposal = new CustomCompletionProposal(rString, rOffset, rLength, possibleValue.length() + 1, XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ENUM), rString, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_VALUE);
contentAssistRequest.addProposal(proposal);
}
}
}
if (defaultValue != null && ((matchString.length() == 0) || defaultValue.startsWith(matchString))) {
// $NON-NLS-2$//$NON-NLS-1$
String rString = "\"" + defaultValue + "\"";
CustomCompletionProposal proposal = new CustomCompletionProposal(rString, rOffset, rLength, defaultValue.length() + 1, XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_DEFAULT), rString, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_VALUE);
contentAssistRequest.addProposal(proposal);
}
}
} else if (((attrDecl.getUsage() == CMAttributeDeclaration.FIXED) || (attrDecl.getAttrType().getImpliedValueKind() == CMDataType.IMPLIED_VALUE_FIXED)) && (attrDecl.getAttrType().getImpliedValue() != null)) {
// FIXED values
String value = attrDecl.getAttrType().getImpliedValue();
if ((value != null) && (value.length() > 0)) {
// $NON-NLS-2$//$NON-NLS-1$
String rValue = "\"" + value + "\"";
CustomCompletionProposal proposal = new CustomCompletionProposal(rValue, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), rValue.length() + 1, image, rValue, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_VALUE);
contentAssistRequest.addProposal(proposal);
if ((currentValue.length() > 0) && !value.equals(currentValue)) {
// $NON-NLS-2$//$NON-NLS-1$
rValue = "\"" + currentValue + "\"";
proposal = new CustomCompletionProposal(rValue, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), rValue.length() + 1, image, rValue, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_VALUE);
contentAssistRequest.addProposal(proposal);
}
}
}
} else {
// unknown attribute, so supply nice empty values
proposedInfo = getAdditionalInfo(null, elementDecl);
CustomCompletionProposal proposal = null;
if ((currentValue != null) && (currentValue.length() > 0)) {
// $NON-NLS-2$//$NON-NLS-1$
String rValue = "\"" + currentValue + "\"";
proposal = new CustomCompletionProposal(rValue, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), 1, image, rValue, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_VALUE);
contentAssistRequest.addProposal(proposal);
}
}
} else {
setErrorMessage(UNKNOWN_CONTEXT);
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration in project webtools.sourceediting by eclipse.
the class XMLQuickAssistProcessor method getInsertRequiredAttrs.
private void getInsertRequiredAttrs(List proposals, ISourceViewer viewer, int offset) {
IDOMNode node = (IDOMNode) getNodeAt(viewer, offset);
if ((node != null) && (node.getNodeType() == Node.ELEMENT_NODE)) {
IStructuredDocumentRegion startStructuredDocumentRegion = node.getStartStructuredDocumentRegion();
if ((startStructuredDocumentRegion != null) && startStructuredDocumentRegion.containsOffset(offset)) {
IDOMNode cursorNode = (IDOMNode) getNodeAt(viewer, offset);
List requiredAttrs = getRequiredAttrs(cursorNode);
if (requiredAttrs.size() > 0) {
NamedNodeMap currentAttrs = node.getAttributes();
List insertAttrs = new ArrayList();
if (currentAttrs.getLength() == 0) {
insertAttrs.addAll(requiredAttrs);
} else {
for (int i = 0; i < requiredAttrs.size(); i++) {
String requiredAttrName = ((CMAttributeDeclaration) requiredAttrs.get(i)).getAttrName();
boolean found = false;
for (int j = 0; j < currentAttrs.getLength(); j++) {
String currentAttrName = currentAttrs.item(j).getNodeName();
if (requiredAttrName.compareToIgnoreCase(currentAttrName) == 0) {
found = true;
break;
}
}
if (!found) {
insertAttrs.add(requiredAttrs.get(i));
}
}
}
if (insertAttrs.size() > 0) {
proposals.add(new InsertRequiredAttrsQuickAssistProposal(insertAttrs));
}
}
}
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration in project webtools.sourceediting by eclipse.
the class XMLQuickAssistProcessor method getRequiredAttrs.
private List getRequiredAttrs(Node node) {
List result = new ArrayList();
ModelQuery modelQuery = getModelQuery(node);
if (modelQuery != null) {
CMElementDeclaration elementDecl = modelQuery.getCMElementDeclaration((Element) node);
if (elementDecl != null) {
CMNamedNodeMap attrMap = elementDecl.getAttributes();
CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(attrMap);
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);
}
}
attrMap = allAttributes;
Iterator it = attrMap.iterator();
CMAttributeDeclaration attr = null;
while (it.hasNext()) {
attr = (CMAttributeDeclaration) it.next();
if (attr.getUsage() == CMAttributeDeclaration.REQUIRED) {
result.add(attr);
}
}
}
}
return result;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration in project webtools.sourceediting by eclipse.
the class XMLTableTreePropertyDescriptorFactory method createAttributePropertyDescriptor.
public IPropertyDescriptor createAttributePropertyDescriptor(Attr attr) {
IPropertyDescriptor result = null;
String attributeName = attr.getName();
ModelQuery mq = ModelQueryUtil.getModelQuery(attr.getOwnerDocument());
CMAttributeDeclaration ad = null;
if (mq != null) {
ad = mq.getCMAttributeDeclaration(attr);
}
if (ad != null) {
result = createPropertyDescriptorHelper(attributeName, attr.getOwnerElement(), ad);
}
if (result == null) {
result = new TextPropertyDescriptor(attributeName, attributeName);
}
return result;
}
Aggregations