use of org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl in project webtools.sourceediting by eclipse.
the class XMLHyperlinkDetector method isLinkableAttr.
/**
* Checks to see if the given attribute is openable. Attribute is openable
* if it is a namespace declaration attribute or if the attribute value is
* of type URI.
*
* @param attr
* cannot be null
* @param cmElement
* CMElementDeclaration associated with the attribute (can be
* null)
* @return true if this attribute is "openOn-able" false otherwise
*/
private boolean isLinkableAttr(Attr attr, CMElementDeclaration cmElement) {
String attrName = attr.getName();
String prefix = DOMNamespaceHelper.getPrefix(attrName);
String unprefixedName = DOMNamespaceHelper.getUnprefixedName(attrName);
// determine if attribute is namespace declaration
if ((XMLNS.equals(prefix)) || (XMLNS.equals(unprefixedName))) {
return true;
}
// determine if attribute contains schema location
if ((XSI_NAMESPACE_URI.equals(DOMNamespaceHelper.getNamespaceURI(attr))) && ((SCHEMA_LOCATION.equals(unprefixedName)) || (NO_NAMESPACE_SCHEMA_LOCATION.equals(unprefixedName)))) {
return true;
}
// determine if attribute value is of type URI
if (cmElement != null) {
CMNamedNodeMap attrDecls = cmElement.getAttributes();
CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(attrDecls);
List nodes = ModelQueryUtil.getModelQuery(attr.getOwnerDocument()).getAvailableContent(attr.getOwnerElement(), cmElement, 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);
}
}
attrDecls = allAttributes;
CMAttributeDeclaration attrDecl = (CMAttributeDeclaration) attrDecls.getNamedItem(attrName);
if ((attrDecl != null) && (attrDecl.getAttrType() != null) && (CMDataType.URI.equals(attrDecl.getAttrType().getDataTypeName()))) {
return true;
}
}
return false;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl in project webtools.sourceediting by eclipse.
the class XMLPropertySource method resetPropertyValue.
/**
* Resets the specified property's value to its default value.
*/
public void resetPropertyValue(Object propertyObject) {
String property = propertyObject.toString();
CMNamedNodeMap attrDecls = null;
CMElementDeclaration ed = getDeclaration();
if (ed != null) {
attrDecls = ed.getAttributes();
CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(attrDecls);
List nodes = ModelQueryUtil.getModelQuery(fNode.getOwnerDocument()).getAvailableContent((Element) fNode, ed, 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);
}
}
attrDecls = allAttributes;
}
NamedNodeMap attrMap = fNode.getAttributes();
if (attrDecls != null) {
CMAttributeDeclaration attrDecl = (CMAttributeDeclaration) attrDecls.getNamedItem(property);
String defValue = null;
if (attrDecl != null) {
if (attrDecl.getAttrType() != null) {
CMDataType helper = attrDecl.getAttrType();
if ((helper.getImpliedValueKind() != CMDataType.IMPLIED_VALUE_NONE) && (helper.getImpliedValue() != null)) {
defValue = helper.getImpliedValue();
}
}
}
if ((defValue != null) && (defValue.length() > 0)) {
// implied values will be in the DOM, but not the source
attrMap.removeNamedItem(property);
} else {
// $NON-NLS-1$
((Attr) attrMap.getNamedItem(property)).setValue("");
}
} else {
// remember, this method is for reset, not remove
// $NON-NLS-1$
((Attr) attrMap.getNamedItem(property)).setValue("");
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl 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.basic.CMNamedNodeMapImpl 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.basic.CMNamedNodeMapImpl in project webtools.sourceediting by eclipse.
the class AbstractXMLModelQueryCompletionProposalComputer method addAttributeNameProposals.
protected void addAttributeNameProposals(ContentAssistRequest contentAssistRequest, CompletionProposalInvocationContext context) {
IDOMNode node = (IDOMNode) contentAssistRequest.getNode();
IStructuredDocumentRegion sdRegion = contentAssistRequest.getDocumentRegion();
// retrieve the list of attributes
CMElementDeclaration elementDecl = getCMElementDeclaration(node);
if (elementDecl != null) {
CMNamedNodeMapImpl attributes = new CMNamedNodeMapImpl(elementDecl.getAttributes());
addModelQueryAttributeDeclarations(node, elementDecl, attributes);
String matchString = contentAssistRequest.getMatchString();
int cursorOffset = context.getInvocationOffset();
// check whether an attribute really exists for the replacement
// offsets AND if it possesses a value
boolean attrAtLocationHasValue = false;
boolean proposalNeedsSpace = false;
NamedNodeMap attrs = node.getAttributes();
for (int i = 0; i < attrs.getLength(); i++) {
AttrImpl existingAttr = (AttrImpl) attrs.item(i);
ITextRegion name = existingAttr.getNameRegion();
if (name != null && (sdRegion.getStartOffset(name) <= contentAssistRequest.getReplacementBeginPosition()) && (sdRegion.getStartOffset(name) + name.getLength() >= contentAssistRequest.getReplacementBeginPosition() + contentAssistRequest.getReplacementLength()) && (existingAttr.getValueRegion() != null)) {
// selected region is attribute name
if (cursorOffset >= sdRegion.getStartOffset(name) && contentAssistRequest.getReplacementLength() != 0)
attrAtLocationHasValue = true;
else // propose new attribute, cursor is at the start of another attribute name
if (cursorOffset == sdRegion.getStartOffset(name))
proposalNeedsSpace = true;
break;
}
}
// only add proposals for the attributes whose names begin with the matchstring
if (attributes != null) {
for (int i = 0; i < attributes.getLength(); i++) {
CMAttributeDeclaration attrDecl = (CMAttributeDeclaration) attributes.item(i);
if (validModelQueryNode(attrDecl)) {
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) {
// get the proposal image
Image attrImage = CMImageUtil.getImage(attrDecl);
if (attrImage == null) {
if (isRequired > 0) {
attrImage = this.getRequiredAttributeImage();
} else {
attrImage = this.getNotRequiredAttributeImage();
}
}
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 MarkupCompletionProposal(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();
}
int cursorPosition = 0;
if ((value != null) && (value.length() > 0)) {
proposedText = getRequiredName(node, attrDecl);
cursorPosition = proposedText.length() + 2;
} else {
proposedText = getRequiredText(node, attrDecl);
// skip the cursor past a fixed value
if (attrDecl.getAttrType() != null && attrDecl.getAttrType().getImpliedValueKind() == CMDataType.IMPLIED_VALUE_FIXED)
cursorPosition = proposedText.length();
else
cursorPosition = getRequiredName(node, attrDecl).length() + 2;
}
if (proposalNeedsSpace)
// $NON-NLS-1$
proposedText += " ";
proposal = new MarkupCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), cursorPosition, attrImage, // and there is no single quote that may be encasing a double quote
((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() })));
}
}
Aggregations