use of org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery in project webtools.sourceediting by eclipse.
the class DOMPropertyDescriptorFactory method createAttributePropertyDescriptor.
public IPropertyDescriptor createAttributePropertyDescriptor(Attr attr) {
IPropertyDescriptor result = null;
String attributeName = attr.getName();
ModelQuery mq = ModelQueryUtil.getModelQuery(attr.getOwnerDocument());
if (mq != null) {
CMAttributeDeclaration ad = mq.getCMAttributeDeclaration(attr);
if (ad != null) {
String[] valuesArray = mq.getPossibleDataTypeValues(attr.getOwnerElement(), ad);
if ((valuesArray != null) && (valuesArray.length > 0)) {
result = new EnumeratedStringPropertyDescriptor(attributeName, attributeName, valuesArray);
}
}
}
if (result == null) {
result = createDefaultPropertyDescriptor(attributeName);
}
return result;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery in project webtools.sourceediting by eclipse.
the class ElementImplTests method testCMAttrWithNullImpliedValue.
public void testCMAttrWithNullImpliedValue() {
IDOMModel model = null;
try {
model = (IDOMModel) getModelForRead("testfiles/time.xml");
if (model != null) {
IDOMDocument document = model.getDocument();
final String ATTR_NAME = "second";
// Setup a ModelQueryAdapter whose sole purpose it to provide a attribute declaration with a null implied value
document.addAdapter(new ModelQueryAdapter() {
public boolean isAdapterForType(Object type) {
return type.equals(ModelQueryAdapter.class);
}
public void notifyChanged(INodeNotifier notifier, int eventType, Object changedFeature, Object oldValue, Object newValue, int pos) {
}
public CMDocumentCache getCMDocumentCache() {
return null;
}
public org.eclipse.wst.common.uriresolver.internal.provisional.URIResolver getIdResolver() {
return null;
}
public ModelQuery getModelQuery() {
return new ModelQueryImpl(null) {
/* (non-Javadoc)
* @see org.eclipse.wst.xml.core.internal.contentmodel.modelqueryimpl.ModelQueryImpl#getCMElementDeclaration(org.w3c.dom.Element)
*/
public CMElementDeclaration getCMElementDeclaration(Element element) {
final CMElementDeclaration decl = new CMElementDeclarationImpl(null, null);
CMNamedNodeMapImpl map = (CMNamedNodeMapImpl) decl.getAttributes();
map.put(new CMAttributeDeclarationImpl(ATTR_NAME, CMAttributeDeclaration.OPTIONAL, new CMDataTypeImpl(ATTR_NAME, (String) null)));
return decl;
}
};
}
public void release() {
}
public void setIdResolver(org.eclipse.wst.common.uriresolver.internal.provisional.URIResolver newIdResolver) {
}
});
Element element = document.getDocumentElement();
assertNotNull(element);
// Default value should be 0
assertEquals("", element.getAttribute(ATTR_NAME));
}
} finally {
if (model != null) {
model.releaseFromRead();
}
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery in project webtools.sourceediting by eclipse.
the class TestAttributesOrder method ensureDocumentHasGrammar.
/**
* Reusable test to make sure the XML model for the given file has a grammar.
*
* @param file
* the file containing the XML document.
*/
private void ensureDocumentHasGrammar(IFile file) throws IOException, CoreException {
IStructuredModel model = null;
try {
IModelManager modelManager = StructuredModelManager.getModelManager();
model = modelManager.getModelForRead(file);
// $NON-NLS-1$
assertNotNull("failure loading model", model);
IDOMModel domModel = (IDOMModel) model;
IDOMDocument document = domModel.getDocument();
// $NON-NLS-1$
assertNotNull("failure getting document", document);
ModelQuery modelQuery = ModelQueryUtil.getModelQuery(document);
// $NON-NLS-1$
assertNotNull("ModelQuery is missing", modelQuery);
IDOMElement documentElement = (IDOMElement) document.getDocumentElement();
// $NON-NLS-1$
assertNotNull("missing document element", documentElement);
CMElementDeclaration cmElementDeclaration = modelQuery.getCMElementDeclaration(documentElement);
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertNotNull("No element declaration for" + documentElement.getNodeName() + " (" + documentElement.getNamespaceURI() + ")", cmElementDeclaration);
assertNotNull("No content assist available for" + documentElement.getNodeName() + " (" + documentElement.getNamespaceURI() + ")", modelQuery.getAvailableContent(documentElement, cmElementDeclaration, ModelQuery.INCLUDE_CHILD_NODES));
} finally {
if (model != null) {
model.releaseFromRead();
}
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery in project webtools.sourceediting by eclipse.
the class TestCyclicGroupReferences method testCyclicGroupReferences.
/**
* Test CMVisitor for cyclic group references.
*/
public void testCyclicGroupReferences() {
// $NON-NLS-1$
IFile file = getFile("Test.xml");
CMVisitor cmVisitor = new CMVisitor();
IStructuredModel model = null;
try {
IModelManager modelManager = StructuredModelManager.getModelManager();
model = modelManager.getModelForRead(file);
// $NON-NLS-1$
assertNotNull("failure loading model", model);
IDOMModel domModel = (IDOMModel) model;
IDOMDocument document = domModel.getDocument();
// $NON-NLS-1$
assertNotNull("failure getting document", document);
ModelQuery modelQuery = ModelQueryUtil.getModelQuery(model);
// $NON-NLS-1$
assertNotNull("ModelQuery is missing", modelQuery);
IDOMElement documentElement = (IDOMElement) document.getDocumentElement();
// $NON-NLS-1$
assertNotNull("missing document element", documentElement);
CMElementDeclaration cmElementDeclaration = modelQuery.getCMElementDeclaration(documentElement);
assertNotNull("No element declaration for" + documentElement.getNodeName() + " (" + documentElement.getNamespaceURI() + ")", // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
cmElementDeclaration);
cmVisitor.visitCMElementDeclaration(cmElementDeclaration);
} catch (Throwable th) {
fail("Test failed :" + th.getClass().getName());
} finally {
if (model != null) {
model.releaseFromRead();
}
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery in project webtools.sourceediting by eclipse.
the class AttributeShowingLabelProvider method getText.
/*
* (non-Javadoc)
*
* @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
*/
@Override
public String getText(Object o) {
StringBuffer text = null;
if (o instanceof Node) {
Node node = (Node) o;
if ((node.getNodeType() == Node.ELEMENT_NODE) && fShowAttributes) {
text = new StringBuffer(super.getText(o));
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=88444
if (node.hasAttributes()) {
Element element = (Element) node;
NamedNodeMap attributes = element.getAttributes();
Node idTypedAttribute = null;
Node requiredAttribute = null;
boolean hasId = false;
boolean hasName = false;
Node shownAttribute = null;
// try to get content model element
// declaration
CMElementDeclaration elementDecl = null;
ModelQuery mq = ModelQueryUtil.getModelQuery(element.getOwnerDocument());
if (mq != null) {
elementDecl = mq.getCMElementDeclaration(element);
}
// ID
if (elementDecl != null) {
int i = 0;
while ((i < attributes.getLength()) && (idTypedAttribute == null)) {
Node attr = attributes.item(i);
String attrName = attr.getNodeName();
CMNamedNodeMap attributeDeclarationMap = elementDecl.getAttributes();
CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(attributeDeclarationMap);
List nodes = ModelQueryUtil.getModelQuery(node.getOwnerDocument()).getAvailableContent(element, 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);
}
}
attributeDeclarationMap = allAttributes;
CMAttributeDeclaration attrDecl = (CMAttributeDeclaration) attributeDeclarationMap.getNamedItem(attrName);
if (attrDecl != null) {
if ((attrDecl.getAttrType() != null) && (CMDataType.ID.equals(attrDecl.getAttrType().getDataTypeName()))) {
idTypedAttribute = attr;
} else if ((attrDecl.getUsage() == CMAttributeDeclaration.REQUIRED) && (requiredAttribute == null)) {
// as a backup, keep tabs on
// any required
// attributes
requiredAttribute = attr;
} else {
hasId = hasId || attrName.equals(ATTR_ID);
hasName = hasName || attrName.equals(ATTR_NAME);
}
}
++i;
}
}
/*
* If no suitable attribute was found, try using a
* required attribute, if none, then prefer "id" or
* "name", otherwise just use first attribute
*/
if (idTypedAttribute != null) {
shownAttribute = idTypedAttribute;
} else if (requiredAttribute != null) {
shownAttribute = requiredAttribute;
} else if (hasId) {
shownAttribute = attributes.getNamedItem(ATTR_ID);
} else if (hasName) {
shownAttribute = attributes.getNamedItem(ATTR_NAME);
}
if (shownAttribute == null) {
shownAttribute = attributes.item(0);
}
// display the attribute and value (without quotes)
String attributeName = shownAttribute.getNodeName();
if ((attributeName != null) && (attributeName.length() > 0)) {
// $NON-NLS-1$
text.append(" ");
text.append(attributeName);
String attributeValue = shownAttribute.getNodeValue();
if ((attributeValue != null) && (attributeValue.length() > 0)) {
// $NON-NLS-1$
text.append("=");
text.append(StringUtils.strip(attributeValue));
}
}
// if (XSLCore.XSL_NAMESPACE_URI.equals(node.getNamespaceURI())) {
// Element el = (Element) node;
// Attr attr = el.getAttributeNode("mode"); //$NON-NLS-1$
// if (attr != null) {
// text.append(" "); //$NON-NLS-1$
// text.append(attr.getName());
// text.append("="); //$NON-NLS-1$
// text.append(StringUtils.strip(attr.getNodeValue()));
// }
// }
}
} else {
text = new StringBuffer(super.getText(o));
}
} else {
return super.toString();
}
return text.toString();
}
Aggregations