Search in sources :

Example 41 with CMDocument

use of org.eclipse.wst.xml.core.internal.contentmodel.CMDocument in project webtools.sourceediting by eclipse.

the class TestCatalogRetrivalAndModelCreation method doCM_directURITest_checkElementCount.

private void doCM_directURITest_checkElementCount(String EXPECTED_URI, int count) throws MalformedURLException, IOException {
    ContentModelManager contentModelManager = ContentModelManager.getInstance();
    CMDocument contentModel = contentModelManager.createCMDocument(EXPECTED_URI, null);
    assertNotNull("expected to create content model for " + EXPECTED_URI, contentModel);
    int actualCount = contentModel.getElements().getLength();
    assertEquals("count of element declarations found for content model create from " + EXPECTED_URI, contentModel.getElements().getLength(), actualCount);
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) ContentModelManager(org.eclipse.wst.xml.core.internal.contentmodel.ContentModelManager)

Example 42 with CMDocument

use of org.eclipse.wst.xml.core.internal.contentmodel.CMDocument in project webtools.sourceediting by eclipse.

the class TestCatalogRetrivalAndModelCreation method doURI_CMTest.

void doURI_CMTest(String EXPECTED_URI) throws MalformedURLException, IOException {
    ICatalog xmlCatalog = XMLCorePlugin.getDefault().getDefaultXMLCatalog();
    String resolved = xmlCatalog.resolveURI(EXPECTED_URI);
    assertNotNull("expected to find " + EXPECTED_URI, resolved);
    ContentModelManager contentModelManager = ContentModelManager.getInstance();
    CMDocument contentModel = contentModelManager.createCMDocument(resolved, null);
    assertNotNull("expected to create content model for " + EXPECTED_URI, contentModel);
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) ICatalog(org.eclipse.wst.xml.core.internal.catalog.provisional.ICatalog) ContentModelManager(org.eclipse.wst.xml.core.internal.contentmodel.ContentModelManager)

Example 43 with CMDocument

use of org.eclipse.wst.xml.core.internal.contentmodel.CMDocument 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 44 with CMDocument

use of org.eclipse.wst.xml.core.internal.contentmodel.CMDocument in project webtools.sourceediting by eclipse.

the class AbstractContentAssistProcessor method computeEntityReferenceProposals.

/**
 * return all possible EntityReferenceProposals (according to current
 * position in doc)
 */
protected ICompletionProposal[] computeEntityReferenceProposals(int documentPosition, ITextRegion completionRegion, IDOMNode treeNode) {
    // only handle XML content for now
    // ICompletionProposals
    Vector proposals = new Vector();
    IStructuredDocumentRegion sdRegion = getStructuredDocumentRegion(documentPosition);
    if ((completionRegion != null) && (completionRegion.getType() == DOMRegionContext.XML_CONTENT)) {
        int nodeOffset = documentPosition - sdRegion.getStartOffset(completionRegion);
        String regionText = sdRegion.getFullText(completionRegion);
        // the previous region...there might be a better way to do this
        if ((regionText != null) && regionText.trim().equals("") && (documentPosition > 0)) {
            // $NON-NLS-1$
            IStructuredDocumentRegion prev = treeNode.getStructuredDocument().getRegionAtCharacterOffset(documentPosition - 1);
            if ((prev != null) && prev.getText().equals("&")) {
                // $NON-NLS-1$
                // https://bugs.eclipse.org/bugs/show_bug.cgi?id=206680
                // examine previous region
                sdRegion = prev;
                completionRegion = prev.getLastRegion();
                regionText = prev.getFullText();
                nodeOffset = 1;
            }
        }
        // string must start w/ &
        if ((regionText != null) && regionText.startsWith("&")) {
            // $NON-NLS-1$
            // $NON-NLS-1$
            String key = (nodeOffset > 0) ? regionText.substring(1, nodeOffset) : "";
            // get entity proposals, passing in the appropriate start
            // string
            ModelQuery mq = ModelQueryUtil.getModelQuery(((Node) treeNode).getOwnerDocument());
            if (mq != null) {
                CMDocument xmlDoc = mq.getCorrespondingCMDocument(treeNode);
                CMNamedNodeMap cmmap = null;
                Properties entities = null;
                if (xmlDoc != null) {
                    cmmap = xmlDoc.getEntities();
                }
                if (cmmap != null) {
                    entities = mapToProperties(cmmap);
                } else // 224787 in absence of content model, just use
                // minimal 5 entities
                {
                    entities = new Properties();
                    // $NON-NLS-1$ //$NON-NLS-2$
                    entities.put("quot", "\"");
                    // $NON-NLS-1$ //$NON-NLS-2$
                    entities.put("apos", "'");
                    // $NON-NLS-1$ //$NON-NLS-2$
                    entities.put("amp", "&");
                    // $NON-NLS-1$ //$NON-NLS-2$
                    entities.put("lt", "<");
                    // $NON-NLS-1$ //$NON-NLS-2$
                    entities.put("gt", ">");
                    // $NON-NLS-1$ //$NON-NLS-2$
                    entities.put("nbsp", " ");
                }
                addEntityProposals(proposals, entities, key, nodeOffset, sdRegion, completionRegion);
            }
        }
    }
    return (ICompletionProposal[]) ((proposals.size() > 0) ? proposals.toArray(new ICompletionProposal[proposals.size()]) : null);
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) Properties(java.util.Properties) Vector(java.util.Vector)

Example 45 with CMDocument

use of org.eclipse.wst.xml.core.internal.contentmodel.CMDocument in project webtools.sourceediting by eclipse.

the class BaseNodeActionManager method contributeAddSiblingActions.

protected void contributeAddSiblingActions(IMenuManager menu, Node node, int ic, int vc) {
    IMenuManager addBeforeMenu = new MyMenuManager(XMLUIMessages._UI_MENU_ADD_BEFORE);
    IMenuManager addAfterMenu = new MyMenuManager(XMLUIMessages._UI_MENU_ADD_AFTER);
    menu.add(addBeforeMenu);
    menu.add(addAfterMenu);
    Node parentNode = node.getParentNode();
    if (parentNode != null) {
        int index = getIndex(parentNode, node);
        if (parentNode.getNodeType() == Node.ELEMENT_NODE) {
            Element parentElement = (Element) parentNode;
            CMElementDeclaration parentED = modelQuery.getCMElementDeclaration(parentElement);
            if (parentED != null) {
                // 'Add Before...' and 'Add After...' actions
                // 
                List modelQueryActionList = new ArrayList();
                modelQuery.getInsertActions(parentElement, parentED, index, ic, vc, modelQueryActionList);
                addActionHelper(addBeforeMenu, modelQueryActionList);
                modelQueryActionList = new ArrayList();
                modelQuery.getInsertActions(parentElement, parentED, index + 1, ic, vc, modelQueryActionList);
                addActionHelper(addAfterMenu, modelQueryActionList);
            }
            // add COMMENT and PI before and after
            contributePIAndCommentActions(addBeforeMenu, parentElement, parentED, index);
            contributePIAndCommentActions(addAfterMenu, parentElement, parentED, index + 1);
            // add PCDATA, CDATA_SECTION before and after
            contributeTextNodeActions(addBeforeMenu, parentElement, parentED, index);
            contributeTextNodeActions(addAfterMenu, parentElement, parentED, index + 1);
            // add NEW ELEMENT before and after
            contributeUnconstrainedAddElementAction(addBeforeMenu, parentElement, parentED, index);
            contributeUnconstrainedAddElementAction(addAfterMenu, parentElement, parentED, index + 1);
        } else if (parentNode.getNodeType() == Node.DOCUMENT_NODE) {
            Document document = (Document) parentNode;
            CMDocument cmDocument = modelQuery.getCorrespondingCMDocument(parentNode);
            if (cmDocument != null) {
                // add possible root element insertions
                // 
                List modelQueryActionList = new ArrayList();
                modelQuery.getInsertActions(document, cmDocument, index, ic, vc, modelQueryActionList);
                addActionHelper(addAfterMenu, modelQueryActionList);
                modelQueryActionList = new ArrayList();
                modelQuery.getInsertActions(document, cmDocument, index + 1, ic, vc, modelQueryActionList);
                addActionHelper(addAfterMenu, modelQueryActionList);
            }
            // add COMMENT and PI before and after
            contributePIAndCommentActions(addBeforeMenu, document, index);
            contributePIAndCommentActions(addAfterMenu, document, index + 1);
            // add ELEMENT before and after
            contributeUnconstrainedAddElementAction(addBeforeMenu, document, index);
            contributeUnconstrainedAddElementAction(addAfterMenu, document, index + 1);
        }
    }
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) Node(org.w3c.dom.Node) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) IMenuManager(org.eclipse.jface.action.IMenuManager) Document(org.w3c.dom.Document) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)

Aggregations

CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)83 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)33 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)26 CMNode (org.eclipse.wst.xml.core.internal.contentmodel.CMNode)20 List (java.util.List)15 ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)14 Document (org.w3c.dom.Document)12 Element (org.w3c.dom.Element)11 ArrayList (java.util.ArrayList)10 Path (org.eclipse.core.runtime.Path)8 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)8 JSPCMDocument (org.eclipse.wst.html.core.internal.contentmodel.JSPCMDocument)7 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)7 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)7 URL (java.net.URL)6 TLDCMDocumentManager (org.eclipse.jst.jsp.core.internal.contentmodel.tld.TLDCMDocumentManager)6 Node (org.w3c.dom.Node)6 NodeList (org.w3c.dom.NodeList)6 Iterator (java.util.Iterator)5 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)5