Search in sources :

Example 36 with ModelQuery

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;
}
Also used : ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)

Example 37 with ModelQuery

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();
        }
    }
}
Also used : CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) ModelQueryAction(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQueryAction) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)

Example 38 with ModelQuery

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();
        }
    }
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) Document(org.w3c.dom.Document)

Example 39 with ModelQuery

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;
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) DocumentType(org.w3c.dom.DocumentType) CMNodeList(org.eclipse.wst.xml.core.internal.contentmodel.CMNodeList) List(java.util.List) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)

Example 40 with ModelQuery

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();
        }
    }
}
Also used : CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) Node(org.w3c.dom.Node) CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) Image(org.eclipse.swt.graphics.Image)

Aggregations

ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)76 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)38 List (java.util.List)22 ArrayList (java.util.ArrayList)19 CMNode (org.eclipse.wst.xml.core.internal.contentmodel.CMNode)18 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)16 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)15 CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)15 Element (org.w3c.dom.Element)15 Document (org.w3c.dom.Document)14 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)13 Node (org.w3c.dom.Node)12 Iterator (java.util.Iterator)10 CMAttributeDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration)10 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)10 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)10 NodeList (org.w3c.dom.NodeList)10 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)7 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)7 IFile (org.eclipse.core.resources.IFile)6