use of org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration in project webtools.sourceediting by eclipse.
the class DOMContentBuilderImpl method getParentCMElementDeclaration.
protected CMElementDeclaration getParentCMElementDeclaration() {
CMElementDeclaration ed = null;
int listSize = visitedCMElementDeclarationList.size();
if (listSize > 0) {
ed = (CMElementDeclaration) visitedCMElementDeclarationList.get(listSize - 1);
}
return ed;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration in project webtools.sourceediting by eclipse.
the class DOMContentBuilderImpl method visitCMAnyElement.
public void visitCMAnyElement(CMAnyElement anyElement) {
// ingnore buildPolicy for ANY elements... only create elements if
// absolutely needed
//
int forcedMin = alwaysVisit ? 1 : 0;
int min = Math.max(anyElement.getMinOccur(), forcedMin);
alwaysVisit = false;
String uri = anyElement.getNamespaceURI();
// $NON-NLS-1$
String targetNSProperty = "http://org.eclipse.wst/cm/properties/targetNamespaceURI";
// $NON-NLS-1$
CMDocument parentCMDocument = (CMDocument) anyElement.getProperty("CMDocument");
CMElementDeclaration ed = null;
// //$NON-NLS-1$
if (parentCMDocument != null) {
if (// $NON-NLS-1$
uri == null || uri.startsWith("##") || uri.equals(parentCMDocument.getProperty(targetNSProperty))) {
ed = getSuitableElement(getParentCMElementDeclaration(), parentCMDocument);
}
}
if (// $NON-NLS-1$
ed == null && externalCMDocumentSupport != null && uri != null && !uri.startsWith("##") && currentParent instanceof Element) {
CMDocument externalCMDocument = externalCMDocumentSupport.getCMDocument((Element) currentParent, uri);
if (externalCMDocument != null) {
ed = getSuitableElement(null, externalCMDocument);
}
}
for (int i = 1; i <= min; i++) {
if (ed != null) {
visitCMElementDeclaration(ed);
} else {
// $NON-NLS-1$
Element element = document.createElement("ANY-ELEMENT");
linkNode(element);
}
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration 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.CMElementDeclaration in project webtools.sourceediting by eclipse.
the class AbstractContentAssistProcessor method addTagInsertionProposals.
protected void addTagInsertionProposals(ContentAssistRequest contentAssistRequest, int childPosition) {
List cmnodes = null;
Node parent = contentAssistRequest.getParent();
String error = null;
// only valid if it's XML (check added 2/17/2004)
if ((parent != null) && (parent.getNodeType() == Node.DOCUMENT_NODE) && ((IDOMDocument) parent).isXMLType() && !isCursorAfterXMLPI(contentAssistRequest)) {
return;
}
// only want proposals if cursor is after doctype...
if (!isCursorAfterDoctype(contentAssistRequest)) {
return;
}
// have a content model (so can't propose any children..)
if ((parent != null) && (parent instanceof IDOMNode) && isCommentNode((IDOMNode) parent)) {
// loop and find non comment node?
while ((parent != null) && isCommentNode((IDOMNode) parent)) {
parent = parent.getParentNode();
}
}
if (parent.getNodeType() == Node.ELEMENT_NODE) {
CMElementDeclaration parentDecl = getCMElementDeclaration(parent);
if (parentDecl != null) {
// XSD-specific ability - no filtering
CMDataType childType = parentDecl.getDataType();
if (childType != null) {
String[] childStrings = childType.getEnumeratedValues();
String defaultValue = childType.getImpliedValue();
if (childStrings != null || defaultValue != null) {
// the content string is the sole valid child...so
// replace the rest
int begin = contentAssistRequest.getReplacementBeginPosition();
int length = contentAssistRequest.getReplacementLength();
if (parent instanceof IDOMNode) {
if (((IDOMNode) parent).getLastStructuredDocumentRegion() != ((IDOMNode) parent).getFirstStructuredDocumentRegion()) {
begin = ((IDOMNode) parent).getFirstStructuredDocumentRegion().getEndOffset();
length = ((IDOMNode) parent).getLastStructuredDocumentRegion().getStartOffset() - begin;
}
}
String proposedInfo = getAdditionalInfo(parentDecl, childType);
for (int i = 0; i < childStrings.length; i++) {
if (!childStrings[i].equals(defaultValue)) {
CustomCompletionProposal textProposal = new CustomCompletionProposal(childStrings[i], begin, length, childStrings[i].length(), XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ENUM), childStrings[i], null, proposedInfo, XMLRelevanceConstants.R_TAG_INSERTION);
contentAssistRequest.addProposal(textProposal);
}
}
if (defaultValue != null) {
CustomCompletionProposal textProposal = new CustomCompletionProposal(defaultValue, begin, length, defaultValue.length(), XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_DEFAULT), defaultValue, null, proposedInfo, XMLRelevanceConstants.R_TAG_INSERTION);
contentAssistRequest.addProposal(textProposal);
}
}
}
}
if ((parentDecl != null) && (parentDecl.getContentType() == CMElementDeclaration.PCDATA)) {
addPCDATAProposal(parentDecl.getNodeName(), contentAssistRequest);
} else {
// retrieve the list of all possible children within this
// parent context
cmnodes = getAvailableChildElementDeclarations((Element) parent, childPosition, ModelQueryAction.INSERT);
// retrieve the list of the possible children within this
// parent context and at this index
List strictCMNodeSuggestions = null;
if (XMLUIPreferenceNames.SUGGESTION_STRATEGY_VALUE_STRICT.equals(XMLUIPlugin.getInstance().getPreferenceStore().getString(XMLUIPreferenceNames.SUGGESTION_STRATEGY))) {
strictCMNodeSuggestions = getValidChildElementDeclarations((Element) parent, childPosition, ModelQueryAction.INSERT);
}
Iterator nodeIterator = cmnodes.iterator();
if (!nodeIterator.hasNext()) {
if (getCMElementDeclaration(parent) != null) {
error = NLS.bind(XMLUIMessages._Has_no_available_child, (new Object[] { parent.getNodeName() }));
} else {
error = NLS.bind(XMLUIMessages.Element__is_unknown, (new Object[] { parent.getNodeName() }));
}
}
String matchString = contentAssistRequest.getMatchString();
// matchstring
while ((matchString.length() > 0) && (Character.isWhitespace(matchString.charAt(0)) || beginsWith(matchString, "<"))) {
// $NON-NLS-1$
matchString = matchString.substring(1);
}
while (nodeIterator.hasNext()) {
Object o = nodeIterator.next();
if (o instanceof CMElementDeclaration) {
CMElementDeclaration elementDecl = (CMElementDeclaration) o;
// only add proposals for the child element's that
// begin with the matchstring
String tagname = getRequiredName(parent, elementDecl);
boolean isStrictCMNodeSuggestion = strictCMNodeSuggestions != null ? strictCMNodeSuggestions.contains(elementDecl) : false;
Image image = CMImageUtil.getImage(elementDecl);
if (image == null) {
if (strictCMNodeSuggestions != null) {
image = isStrictCMNodeSuggestion ? XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_TAG_GENERIC_EMPHASIZED) : XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_TAG_GENERIC_DEEMPHASIZED);
} else {
image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_TAG_GENERIC);
}
}
// elementDecl);
if (beginsWith(tagname, matchString)) {
String proposedText = getRequiredText(parent, elementDecl);
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=89811
// place cursor in first empty quotes
int markupAdjustment = getCursorPositionForProposedText(proposedText);
String proposedInfo = getAdditionalInfo(parentDecl, elementDecl);
int relevance = isStrictCMNodeSuggestion ? XMLRelevanceConstants.R_STRICTLY_VALID_TAG_INSERTION : XMLRelevanceConstants.R_TAG_INSERTION;
CustomCompletionProposal proposal = new CustomCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), markupAdjustment, image, tagname, null, proposedInfo, relevance);
contentAssistRequest.addProposal(proposal);
}
}
}
if (contentAssistRequest.getProposals().size() == 0) {
if (error != null) {
setErrorMessage(error);
} else if ((contentAssistRequest.getMatchString() != null) && (contentAssistRequest.getMatchString().length() > 0)) {
setErrorMessage(NLS.bind(XMLUIMessages.No_known_child_tag, (new Object[] { parent.getNodeName(), contentAssistRequest.getMatchString() })));
// $NON-NLS-1$ = "No known child tag names of <{0}> begin with \"{1}\"."
} else {
setErrorMessage(NLS.bind(XMLUIMessages.__Has_no_known_child, (new Object[] { parent.getNodeName() })));
}
}
}
} else if (parent.getNodeType() == Node.DOCUMENT_NODE) {
// Can only prompt with elements if the cursor position is past
// the XML processing
// instruction and DOCTYPE declaration
boolean xmlpiFound = false;
boolean doctypeFound = false;
int minimumOffset = -1;
for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
// $NON-NLS-1$
boolean xmlpi = ((child.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) && child.getNodeName().equals("xml"));
boolean doctype = child.getNodeType() == Node.DOCUMENT_TYPE_NODE;
if (xmlpi || (doctype && (minimumOffset < 0))) {
minimumOffset = ((IDOMNode) child).getFirstStructuredDocumentRegion().getStartOffset() + ((IDOMNode) child).getFirstStructuredDocumentRegion().getTextLength();
}
xmlpiFound = xmlpiFound || xmlpi;
doctypeFound = doctypeFound || doctype;
}
if (contentAssistRequest.getReplacementBeginPosition() >= minimumOffset) {
List childDecls = getAvailableRootChildren((Document) parent, childPosition);
for (int i = 0; i < childDecls.size(); i++) {
CMElementDeclaration ed = (CMElementDeclaration) childDecls.get(i);
if (ed != null) {
Image image = CMImageUtil.getImage(ed);
if (image == null) {
image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_TAG_GENERIC);
}
String proposedText = getRequiredText(parent, ed);
String tagname = getRequiredName(parent, ed);
// account for the < and >
int markupAdjustment = getContentGenerator().getMinimalStartTagLength(parent, ed);
String proposedInfo = getAdditionalInfo(null, ed);
CustomCompletionProposal proposal = new CustomCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), markupAdjustment, image, tagname, null, proposedInfo, XMLRelevanceConstants.R_TAG_INSERTION);
contentAssistRequest.addProposal(proposal);
}
}
}
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration 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);
}
}
Aggregations