use of org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery in project webtools.sourceediting by eclipse.
the class ContextInfoModelUtil method getModelQuery.
public ModelQuery getModelQuery() {
ModelQuery mq = null;
IStructuredModel xmlModel = null;
try {
xmlModel = StructuredModelManager.getModelManager().getExistingModelForRead(getDocument());
mq = ModelQueryUtil.getModelQuery(xmlModel);
} finally {
if (xmlModel != null) {
xmlModel.releaseFromRead();
}
}
return mq;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery in project webtools.sourceediting by eclipse.
the class CMValidatorValidationTest method verify.
private void verify(Element parentElement, String[] childElementsAvailable) {
List list = new ArrayList();
ModelQuery modelQuery = ModelQueryUtil.getModelQuery(parentElement.getOwnerDocument());
CMElementDeclaration cmElementDeclaration = modelQuery.getCMElementDeclaration(parentElement);
modelQuery.getInsertActions(parentElement, cmElementDeclaration, -1, ModelQuery.INCLUDE_CHILD_NODES, ModelQuery.VALIDITY_STRICT, list);
assertEquals(childElementsAvailable.length, list.size());
List availableNodeNameList = new ArrayList();
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
availableNodeNameList.add(((ModelQueryAction) iterator.next()).getCMNode().getNodeName());
}
for (int i = 0; i < childElementsAvailable.length; i++) {
if (availableNodeNameList.indexOf(childElementsAvailable[i]) == -1) {
fail();
}
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery in project webtools.sourceediting by eclipse.
the class XSITypeTest method testXSIType.
public void testXSIType() {
IStructuredModel structuredModel = null;
try {
// $NON-NLS-1$
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(PROJECT_NAME + "/test.xml"));
IModelManager modelManager = StructuredModelManager.getModelManager();
structuredModel = modelManager.getModelForRead(file);
Document document = ((IDOMModel) structuredModel).getDocument();
ModelQuery modelQuery = ModelQueryUtil.getModelQuery(document);
NodeList nodeList = null;
CMElementDeclaration cmElementDeclaration = null;
// Check for local XSI type
// $NON-NLS-1$
nodeList = document.getElementsByTagName("a");
cmElementDeclaration = modelQuery.getCMElementDeclaration((Element) nodeList.item(0));
// $NON-NLS-1$
assertNotNull("Local XSI type failed to be resolved", cmElementDeclaration);
// Check for external XSI type
// $NON-NLS-1$
nodeList = document.getElementsByTagName("b");
cmElementDeclaration = modelQuery.getCMElementDeclaration((Element) nodeList.item(0));
// $NON-NLS-1$
assertNotNull("External XSI type failed to be resolved", cmElementDeclaration);
} catch (Exception exception) {
exception.printStackTrace();
} finally {
if (structuredModel != null) {
structuredModel.releaseFromRead();
}
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery in project webtools.sourceediting by eclipse.
the class AbstractContentAssistProcessor method getAvailableRootChildren.
// returns a list of CMElementDeclarations
protected List getAvailableRootChildren(Document document, int childIndex) {
List list = null;
// extract the valid 'root' node name from the DocumentType Node
DocumentType docType = document.getDoctype();
String rootName = null;
if (docType != null) {
rootName = docType.getNodeName();
}
if (rootName == null) {
return new ArrayList(0);
}
for (Node child = document.getFirstChild(); child != null; child = child.getNextSibling()) {
// is it required to be an Element?
if ((child.getNodeType() == Node.ELEMENT_NODE) && stringsEqual(child.getNodeName(), rootName)) {
// count it as present
if ((child instanceof IDOMNode) && ((((IDOMNode) child).getStartStructuredDocumentRegion() == null) || (((IDOMNode) child).getEndStructuredDocumentRegion() == null))) {
continue;
}
if (Debug.displayInfo) {
// $NON-NLS-1$
System.out.println(rootName + " already present!");
}
setErrorMessage(NLS.bind(XMLUIMessages.The_document_element__, (new Object[] { rootName })));
return new ArrayList(0);
}
}
list = new ArrayList(1);
ModelQuery modelQuery = ModelQueryUtil.getModelQuery(document);
if (modelQuery != null) {
CMDocument cmdoc = modelQuery.getCorrespondingCMDocument(document);
if (cmdoc != null) {
if (rootName != null) {
CMElementDeclaration rootDecl = (CMElementDeclaration) cmdoc.getElements().getNamedItem(rootName);
if (rootDecl != null) {
list.add(rootDecl);
} else {
// supply the given document name anyway, even if it
// is an error
list.add(new SimpleCMElementDeclaration(rootName));
if (Debug.displayInfo || Debug.displayWarnings) {
// $NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$
System.out.println("No definition found for " + rootName + " in " + docType.getPublicId() + "/" + docType.getSystemId());
}
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
String location = "" + (docType.getPublicId() != null ? docType.getPublicId() + "/" : "") + (docType.getSystemId() != null ? docType.getSystemId() : "");
// $NON-NLS-4$//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$
if (location.length() > 0) {
setErrorMessage(NLS.bind(XMLUIMessages.No_definition_for_in, (new Object[] { rootName, location })));
} else {
setErrorMessage(NLS.bind(XMLUIMessages.No_definition_for, (new Object[] { rootName })));
}
}
}
} else {
if (Debug.displayInfo || Debug.displayWarnings) {
// $NON-NLS-1$
System.out.println("No content model found.");
}
// $NON-NLS-1$
// $NON-NLS-1$
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
String location = "" + (docType.getPublicId() != null ? docType.getPublicId() + "/" : "") + (docType.getSystemId() != null ? docType.getSystemId() : "");
// $NON-NLS-4$//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$
if (location.length() > 0) {
setErrorMessage(NLS.bind(XMLUIMessages.No_content_model_for, (new Object[] { location })));
} else {
setErrorMessage(XMLUIMessages.No_content_model_found_UI_);
}
}
}
return list;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery in project webtools.sourceediting by eclipse.
the class AbstractContentAssistProcessor method addEndTagNameProposals.
/**
* Add the proposals for the name in an end tag
*/
protected void addEndTagNameProposals(ContentAssistRequest contentAssistRequest) {
if (contentAssistRequest.getStartOffset() + contentAssistRequest.getRegion().getTextLength() < contentAssistRequest.getReplacementBeginPosition()) {
CustomCompletionProposal proposal = new // $NON-NLS-1$
CustomCompletionProposal(// $NON-NLS-1$
">", // $NON-NLS-1$
contentAssistRequest.getReplacementBeginPosition(), // $NON-NLS-1$
contentAssistRequest.getReplacementLength(), // $NON-NLS-1$
1, // $NON-NLS-1$
XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_TAG_GENERIC), // $NON-NLS-1$
NLS.bind(XMLUIMessages.Close_with__, (new Object[] { " '>'" })), null, null, XMLRelevanceConstants.R_END_TAG_NAME);
contentAssistRequest.addProposal(proposal);
} else {
IDOMNode node = (IDOMNode) contentAssistRequest.getNode();
ModelQuery modelQuery = ModelQueryUtil.getModelQuery(node.getOwnerDocument());
Node aNode = contentAssistRequest.getNode();
String matchString = contentAssistRequest.getMatchString();
if (matchString.startsWith("</")) {
matchString = matchString.substring(2);
}
while (aNode != null) {
if (aNode.getNodeType() == Node.ELEMENT_NODE) {
if (aNode.getNodeName().startsWith(matchString)) {
IDOMNode aXMLNode = (IDOMNode) aNode;
CMElementDeclaration ed = modelQuery.getCMElementDeclaration((Element) aNode);
if ((aXMLNode.getEndStructuredDocumentRegion() == null) && ((ed == null) || (ed.getContentType() != CMElementDeclaration.EMPTY))) {
String replacementText = aNode.getNodeName();
String displayText = replacementText;
String proposedInfo = (ed != null) ? getAdditionalInfo(null, ed) : null;
if (!contentAssistRequest.getDocumentRegion().isEnded()) {
// $NON-NLS-1$
replacementText += ">";
}
CustomCompletionProposal proposal = null;
// double check to see if the region acted upon is
// a tag name; replace it if so
Image image = CMImageUtil.getImage(ed);
if (image == null) {
image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_TAG_GENERIC);
}
if (contentAssistRequest.getRegion().getType() == DOMRegionContext.XML_TAG_NAME) {
proposal = new CustomCompletionProposal(replacementText, contentAssistRequest.getStartOffset(), contentAssistRequest.getRegion().getTextLength(), replacementText.length(), image, displayText, null, proposedInfo, XMLRelevanceConstants.R_END_TAG_NAME);
} else {
proposal = new // $NON-NLS-1$ //$NON-NLS-2$
CustomCompletionProposal(// $NON-NLS-1$ //$NON-NLS-2$
replacementText, // $NON-NLS-1$ //$NON-NLS-2$
contentAssistRequest.getReplacementBeginPosition(), // $NON-NLS-1$ //$NON-NLS-2$
contentAssistRequest.getReplacementLength(), // $NON-NLS-1$ //$NON-NLS-2$
replacementText.length(), // $NON-NLS-1$ //$NON-NLS-2$
image, // $NON-NLS-1$ //$NON-NLS-2$
NLS.bind(XMLUIMessages.Close_with__, (new Object[] { "'" + displayText + "'" })), null, proposedInfo, XMLRelevanceConstants.R_END_TAG_NAME);
}
contentAssistRequest.addProposal(proposal);
}
}
}
aNode = aNode.getParentNode();
}
}
}
Aggregations