use of org.eclipse.wst.xml.core.internal.contentmodel.CMDataType 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.CMDataType in project webtools.sourceediting by eclipse.
the class BugFixesTest method testXSITypeVsTypeAttr.
public void testXSITypeVsTypeAttr() {
// See bug 225447, 225819
// Load the XSD file
String XSD_FILE_NAME = "XSITypeTest.xsd";
String fileURI = FILE_PROTOCOL + PLUGIN_ABSOLUTE_PATH + SAMPLES_DIR + XSD_FILE_NAME;
CMDocumentFactoryXSD factory = new CMDocumentFactoryXSD();
assertNotNull("Assert factory is not null", factory);
CMDocument cmDocument = factory.createCMDocument(fileURI);
assertNotNull("Assert CMDocument is not null", cmDocument);
// Check and obtain the two global elements (elementA and elementB)
CMNamedNodeMap elements = cmDocument.getElements();
assertEquals(elements.getLength(), 2);
CMElementDeclaration cmElementDeclaration = (CMElementDeclaration) elements.item(0);
CMElementDeclaration cmElementDeclarationA = null;
CMElementDeclaration cmElementDeclarationB = null;
if ("elementA".equals(cmElementDeclaration.getElementName())) {
cmElementDeclarationA = cmElementDeclaration;
cmElementDeclarationB = (CMElementDeclaration) elements.item(1);
} else {
cmElementDeclarationB = cmElementDeclaration;
cmElementDeclarationA = (CMElementDeclaration) elements.item(1);
}
// elementA has a "type" attribute with "X" enumerated value, make sure it appears in the model
CMNamedNodeMap attributesA = cmElementDeclarationA.getAttributes();
assertEquals(attributesA.getLength(), 1);
CMAttributeDeclaration cmAttributeDeclarationA = (CMAttributeDeclaration) attributesA.item(0);
assertEquals("type", cmAttributeDeclarationA.getAttrName());
CMDataType attrTypeA = cmAttributeDeclarationA.getAttrType();
String[] enumeratedValuesA = attrTypeA.getEnumeratedValues();
assertEquals(1, enumeratedValuesA.length);
assertEquals("X", enumeratedValuesA[0]);
// elementB does not have a "type" attribute, make sure the xsi:type appears in the model
CMNamedNodeMap attributesB = cmElementDeclarationB.getAttributes();
assertEquals(attributesB.getLength(), 1);
CMAttributeDeclaration cmAttributeDeclarationB = (CMAttributeDeclaration) attributesB.item(0);
assertEquals("type", cmAttributeDeclarationB.getAttrName());
CMDataType attrTypeB = cmAttributeDeclarationB.getAttrType();
assertEquals("typeNames", attrTypeB.getDataTypeName());
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMDataType in project webtools.sourceediting by eclipse.
the class DefaultXMLPartitionFormatter method updateFormattingConstraints.
/**
* Given the provided information (parentConstraints & currentDOMRegion),
* update the formatting constraints (for this & child)
*
* @param parentConstraints
* can be null
* @param thisConstraints
* can be null
* @param childConstraints
* can be null
* @param currentDOMRegion
* cannot be null
*/
protected void updateFormattingConstraints(XMLFormattingConstraints parentConstraints, XMLFormattingConstraints thisConstraints, XMLFormattingConstraints childConstraints, DOMRegion currentDOMRegion) {
IStructuredDocumentRegion currentRegion = currentDOMRegion.documentRegion;
IDOMNode currentNode = currentDOMRegion.domNode;
// default to whatever parent's constraint said to do
if (parentConstraints != null) {
if (thisConstraints != null) {
thisConstraints.copyConstraints(parentConstraints);
}
if (childConstraints != null) {
childConstraints.copyConstraints(parentConstraints);
// defaults are taken instead
if (parentConstraints.isWhitespaceStrategyAHint())
childConstraints.setWhitespaceStrategy(null);
}
}
// set up constraints for direct children of document root
Node parentNode = currentNode.getParentNode();
if (parentNode != null && parentNode.getNodeType() == Node.DOCUMENT_NODE) {
if (thisConstraints != null) {
thisConstraints.setWhitespaceStrategy(XMLFormattingConstraints.IGNORE);
thisConstraints.setIndentStrategy(XMLFormattingConstraints.NEW_LINE);
thisConstraints.setIndentLevel(0);
}
if (childConstraints != null) {
childConstraints.setWhitespaceStrategy(null);
childConstraints.setIndentStrategy(null);
childConstraints.setIndentLevel(0);
}
}
// other conditions to check when setting up child constraints
if (childConstraints != null) {
XMLFormattingPreferences preferences = getFormattingPreferences();
// on a new line and have an indent level of 0
if (currentNode.getNodeType() == Node.DOCUMENT_NODE) {
childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.IGNORE);
childConstraints.setIndentStrategy(XMLFormattingConstraints.NEW_LINE);
childConstraints.setIndentLevel(0);
} else {
// BUG108074 & BUG84688 - preserve whitespace in xsl:text &
// xsl:attribute
String nodeNamespaceURI = currentNode.getNamespaceURI();
if (XSL_NAMESPACE.equals(nodeNamespaceURI)) {
String nodeName = ((Element) currentNode).getLocalName();
if (XSL_ATTRIBUTE.equals(nodeName) || XSL_TEXT.equals(nodeName)) {
childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.PRESERVE);
}
} else {
// search within current tag for xml:space attribute
ITextRegionList textRegions = currentRegion.getRegions();
int i = 0;
boolean xmlSpaceFound = false;
boolean preserveFound = false;
while (i < textRegions.size() && !xmlSpaceFound) {
ITextRegion textRegion = textRegions.get(i);
if (DOMRegionContext.XML_TAG_ATTRIBUTE_NAME.equals(textRegion.getType())) {
String regionText = currentRegion.getText(textRegion);
if (XML_SPACE.equals(regionText)) {
if ((i + 1) < textRegions.size()) {
++i;
textRegion = textRegions.get(i);
if (DOMRegionContext.XML_TAG_ATTRIBUTE_EQUALS.equals(textRegion.getType()) && ((i + 1) < textRegions.size())) {
++i;
textRegion = textRegions.get(i);
regionText = currentRegion.getText(textRegion);
if (PRESERVE.equals(regionText) || PRESERVE_QUOTED.equals(regionText)) {
preserveFound = true;
}
}
}
xmlSpaceFound = true;
}
}
++i;
}
if (xmlSpaceFound) {
if (preserveFound) {
// preserve was found so set the strategy
childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.PRESERVE);
} else {
// xml:space was found but it was not collapse, so
// use default whitespace strategy
childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.DEFAULT);
}
} else {
// how to hande nodes that have nonwhitespace text
// content
NodeList nodeList = currentNode.getChildNodes();
int length = nodeList.getLength();
int index = 0;
boolean textNodeFound = false;
// still reflect the parent constraints
while (index < length && !textNodeFound && parentConstraints != null && !XMLFormattingConstraints.PRESERVE.equals(parentConstraints.getWhitespaceStrategy())) {
Node childNode = nodeList.item(index);
if (childNode.getNodeType() == Node.TEXT_NODE) {
textNodeFound = !((IDOMText) childNode).isElementContentWhitespace();
}
++index;
}
if (textNodeFound) {
if (length > 1) {
// more in here than just text, so consider
// this mixed content
childConstraints.setWhitespaceStrategy(preferences.getMixedWhitespaceStrategy());
childConstraints.setIndentStrategy(preferences.getMixedIndentStrategy());
} else {
// there's only text
childConstraints.setWhitespaceStrategy(preferences.getTextWhitespaceStrategy());
childConstraints.setIndentStrategy(preferences.getTextIndentStrategy());
}
childConstraints.setIsWhitespaceStrategyAHint(true);
childConstraints.setIsIndentStrategyAHint(true);
}
// try referring to content model for information on
// whitespace & indent strategy
ModelQueryAdapter adapter = (ModelQueryAdapter) ((IDOMDocument) currentNode.getOwnerDocument()).getAdapterFor(ModelQueryAdapter.class);
CMElementDeclaration elementDeclaration = (CMElementDeclaration) adapter.getModelQuery().getCMNode(currentNode);
if (elementDeclaration != null) {
// follow whitespace strategy preference for
// pcdata content
int contentType = elementDeclaration.getContentType();
String facetValue = null;
if (elementDeclaration.getDataType() != null)
facetValue = (String) elementDeclaration.getDataType().getProperty(PROPERTY_WHITESPACE_FACET);
if (facetValue != null) {
if (PRESERVE.equals(facetValue))
childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.PRESERVE);
else // For XSD types, "collapse" corresponds to the IGNOREANDTRIM strategy
if (COLLAPSE.equals(facetValue))
childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.IGNOREANDTRIM);
else if (REPLACE.equals(facetValue))
childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.REPLACE);
} else if (contentType == CMElementDeclaration.PCDATA && parentConstraints != null && !XMLFormattingConstraints.PRESERVE.equals(parentConstraints.getWhitespaceStrategy())) {
childConstraints.setWhitespaceStrategy(preferences.getPCDataWhitespaceStrategy());
} else if (contentType == CMElementDeclaration.ELEMENT && parentConstraints != null && !XMLFormattingConstraints.PRESERVE.equals(parentConstraints.getWhitespaceStrategy())) {
childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.IGNORE);
childConstraints.setIndentStrategy(XMLFormattingConstraints.INDENT);
childConstraints.setIsWhitespaceStrategyAHint(true);
childConstraints.setIsIndentStrategyAHint(true);
} else {
// look for xml:space in content model
CMNamedNodeMap cmAttributes = elementDeclaration.getAttributes();
// Not needed - we're looking for xml:space
// CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(cmAttributes);
// List nodes = ModelQueryUtil.getModelQuery(currentNode.getOwnerDocument()).getAvailableContent((Element) currentNode, elementDeclaration, 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);
// }
// }
// cmAttributes = allAttributes;
// Check implied values from the DTD way.
CMAttributeDeclaration attributeDeclaration = (CMAttributeDeclaration) cmAttributes.getNamedItem(XML_SPACE);
if (attributeDeclaration != null) {
// CMAttributeDeclaration found, check
// it
// out.
// BUG214516/196544 - Fixed NPE that was caused by an attr having
// a null attr type
String defaultValue = null;
CMDataType attrType = attributeDeclaration.getAttrType();
if (attrType != null) {
if ((attrType.getImpliedValueKind() != CMDataType.IMPLIED_VALUE_NONE) && attrType.getImpliedValue() != null)
defaultValue = attrType.getImpliedValue();
else if ((attrType.getEnumeratedValues() != null) && (attrType.getEnumeratedValues().length > 0)) {
defaultValue = attrType.getEnumeratedValues()[0];
}
}
// default.
if (PRESERVE.equals(defaultValue))
childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.PRESERVE);
else
childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.DEFAULT);
} else // If the node has no attributes, inherit the parents whitespace strategy
{
if (parentConstraints != null)
childConstraints.setWhitespaceStrategy(parentConstraints.getWhitespaceStrategy());
else
childConstraints.setWhitespaceStrategy(null);
}
}
}
}
}
}
// set default values according to preferences
if (childConstraints.getWhitespaceStrategy() == null) {
childConstraints.setWhitespaceStrategy(preferences.getElementWhitespaceStrategy());
}
if (childConstraints.getIndentStrategy() == null) {
childConstraints.setIndentStrategy(preferences.getElementIndentStrategy());
}
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMDataType in project webtools.sourceediting by eclipse.
the class AbstractXMLModelQueryCompletionProposalComputer method addTagInsertionProposals.
protected void addTagInsertionProposals(ContentAssistRequest contentAssistRequest, int childPosition, CompletionProposalInvocationContext context) {
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 MarkupCompletionProposal(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 MarkupCompletionProposal(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, context);
} 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();
// chop off any leading <'s and whitespace from the 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;
// get the proposal image
Image image = CMImageUtil.getImage(elementDecl);
if (image == null) {
if (strictCMNodeSuggestions != null) {
image = isStrictCMNodeSuggestion ? this.getEmphasizedTagImage() : this.getDeemphasizedTagImage();
} else {
image = this.getGenericTagImage();
}
}
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 MarkupCompletionProposal(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() })));
} 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 = this.getGenericTagImage();
}
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 MarkupCompletionProposal(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.CMDataType in project webtools.sourceediting by eclipse.
the class XMLContentModelGenerator method generateRequiredAttribute.
public void generateRequiredAttribute(Node ownerNode, CMAttributeDeclaration attrDecl, StringBuffer buffer) {
if ((attrDecl == null) || (buffer == null)) {
return;
}
// attribute name
String attributeName = getRequiredName(ownerNode, attrDecl);
CMDataType attrType = attrDecl.getAttrType();
String defaultValue = null;
// = sign
// $NON-NLS-1$
buffer.append(attributeName + "=");
// attribute value
if (attrType != null) {
// insert any value that is implied
if ((attrType.getImpliedValueKind() != CMDataType.IMPLIED_VALUE_NONE) && (attrType.getImpliedValue() != null)) {
defaultValue = attrType.getImpliedValue();
} else // first value
if ((attrType.getEnumeratedValues() != null) && (attrType.getEnumeratedValues().length > 0)) {
defaultValue = attrType.getEnumeratedValues()[0];
}
}
char attrQuote = '\"';
// Found a double quote, wrap the attribute in single quotes
if (defaultValue != null && defaultValue.indexOf(attrQuote) >= 0) {
attrQuote = '\'';
}
buffer.append(attrQuote);
// $NON-NLS-1$
buffer.append(((defaultValue != null) ? defaultValue : ""));
buffer.append(attrQuote);
return;
}
Aggregations