Search in sources :

Example 1 with CMElementDeclarationImpl

use of org.eclipse.jst.jsp.core.internal.contentmodel.tld.CMElementDeclarationImpl in project webtools.sourceediting by eclipse.

the class TaglibHyperlinkDetector method detectHyperlinks.

public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
    IHyperlink hyperlink = null;
    if (textViewer != null && region != null) {
        IDocument doc = textViewer.getDocument();
        if (doc != null) {
            try {
                // check if jsp tag/directive first
                ITypedRegion partition = TextUtilities.getPartition(doc, IStructuredPartitioning.DEFAULT_STRUCTURED_PARTITIONING, region.getOffset(), false);
                if (partition != null && partition.getType() == IJSPPartitions.JSP_DIRECTIVE) {
                    IStructuredModel sModel = null;
                    try {
                        sModel = StructuredModelManager.getModelManager().getExistingModelForRead(doc);
                        // check if jsp taglib directive
                        Node currentNode = getCurrentNode(sModel, region.getOffset());
                        if (currentNode != null && currentNode.getNodeType() == Node.ELEMENT_NODE) {
                            String baseLocationForTaglib = getBaseLocationForTaglib(doc);
                            if (baseLocationForTaglib != null && JSP11Namespace.ElementName.DIRECTIVE_TAGLIB.equalsIgnoreCase(currentNode.getNodeName())) {
                                /**
                                 * The taglib directive itself
                                 */
                                // get the uri attribute
                                Attr taglibURINode = ((Element) currentNode).getAttributeNode(JSP11Namespace.ATTR_NAME_URI);
                                if (taglibURINode != null) {
                                    ITaglibRecord reference = TaglibIndex.resolve(baseLocationForTaglib, taglibURINode.getValue(), false);
                                    // there's nothing to link to
                                    if (reference != null) {
                                        // handle taglibs
                                        switch(reference.getRecordType()) {
                                            case (ITaglibRecord.TLD):
                                                {
                                                    ITLDRecord record = (ITLDRecord) reference;
                                                    String uriString = record.getPath().toString();
                                                    IRegion hyperlinkRegion = getHyperlinkRegion(taglibURINode, region);
                                                    if (hyperlinkRegion != null) {
                                                        hyperlink = createHyperlink(uriString, hyperlinkRegion, doc, null);
                                                    }
                                                }
                                                break;
                                            case (ITaglibRecord.JAR):
                                            case (ITaglibRecord.URL):
                                                {
                                                    IRegion hyperlinkRegion = getHyperlinkRegion(taglibURINode, region);
                                                    if (hyperlinkRegion != null) {
                                                        hyperlink = new TaglibJarUriHyperlink(hyperlinkRegion, reference);
                                                    }
                                                }
                                        }
                                    }
                                }
                            } else if (baseLocationForTaglib != null && JSP12Namespace.ElementName.ROOT.equalsIgnoreCase(currentNode.getNodeName())) {
                                /**
                                 * The jsp:root element
                                 */
                                NamedNodeMap attrs = currentNode.getAttributes();
                                for (int i = 0; i < attrs.getLength(); i++) {
                                    Attr attr = (Attr) attrs.item(i);
                                    if (attr.getNodeName().startsWith(XMLNS)) {
                                        String uri = StringUtils.strip(attr.getNodeValue());
                                        if (uri.startsWith(URN_TLD)) {
                                            uri = uri.substring(URN_TLD.length());
                                        }
                                        ITaglibRecord reference = TaglibIndex.resolve(baseLocationForTaglib, uri, false);
                                        // there's nothing to link to
                                        if (reference != null) {
                                            // handle taglibs
                                            switch(reference.getRecordType()) {
                                                case (ITaglibRecord.TLD):
                                                    {
                                                        ITLDRecord record = (ITLDRecord) reference;
                                                        String uriString = record.getPath().toString();
                                                        IRegion hyperlinkRegion = getHyperlinkRegion(attr, region);
                                                        if (hyperlinkRegion != null) {
                                                            hyperlink = createHyperlink(uriString, hyperlinkRegion, doc, null);
                                                        }
                                                    }
                                                    break;
                                                case (ITaglibRecord.JAR):
                                                case (ITaglibRecord.URL):
                                                    {
                                                        IRegion hyperlinkRegion = getHyperlinkRegion(attr, region);
                                                        if (hyperlinkRegion != null) {
                                                            hyperlink = new TaglibJarUriHyperlink(hyperlinkRegion, reference);
                                                        }
                                                    }
                                            }
                                        }
                                    }
                                }
                            } else {
                                /**
                                 * Hyperlink custom tag to its TLD or tag file
                                 */
                                TLDCMDocumentManager documentManager = TaglibController.getTLDCMDocumentManager(doc);
                                if (documentManager != null) {
                                    List documentTrackers = documentManager.getCMDocumentTrackers(currentNode.getPrefix(), region.getOffset());
                                    for (int i = 0; i < documentTrackers.size(); i++) {
                                        TaglibTracker tracker = (TaglibTracker) documentTrackers.get(i);
                                        CMElementDeclaration decl = (CMElementDeclaration) tracker.getElements().getNamedItem(currentNode.getNodeName());
                                        if (decl != null) {
                                            decl = (CMElementDeclaration) ((CMNodeWrapper) decl).getOriginNode();
                                            if (decl instanceof CMElementDeclarationImpl) {
                                                String base = ((CMElementDeclarationImpl) decl).getLocationString();
                                                IRegion hyperlinkRegion = getHyperlinkRegion(currentNode, region);
                                                if (hyperlinkRegion != null) {
                                                    hyperlink = createHyperlink(base, hyperlinkRegion, doc, currentNode);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    } finally {
                        if (sModel != null)
                            sModel.releaseFromRead();
                    }
                }
            } catch (BadLocationException e) {
                Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e);
            }
        }
    }
    if (hyperlink != null)
        return new IHyperlink[] { hyperlink };
    return null;
}
Also used : TLDCMDocumentManager(org.eclipse.jst.jsp.core.internal.contentmodel.tld.TLDCMDocumentManager) NamedNodeMap(org.w3c.dom.NamedNodeMap) ITaglibRecord(org.eclipse.jst.jsp.core.taglib.ITaglibRecord) ITLDRecord(org.eclipse.jst.jsp.core.taglib.ITLDRecord) TaglibTracker(org.eclipse.jst.jsp.core.internal.contentmodel.tld.TaglibTracker) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Attr(org.w3c.dom.Attr) IDOMAttr(org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr) IRegion(org.eclipse.jface.text.IRegion) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) ITypedRegion(org.eclipse.jface.text.ITypedRegion) CMElementDeclarationImpl(org.eclipse.jst.jsp.core.internal.contentmodel.tld.CMElementDeclarationImpl) List(java.util.List) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) NodeList(org.w3c.dom.NodeList) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 2 with CMElementDeclarationImpl

use of org.eclipse.jst.jsp.core.internal.contentmodel.tld.CMElementDeclarationImpl in project webtools.sourceediting by eclipse.

the class TestTaglibCMTests method testDynamicAttributes.

public void testDynamicAttributes() throws Exception {
    final String testName = "testDynamicAttributes";
    IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(testName);
    if (!project.exists()) {
        project = BundleResourceUtil.createSimpleProject(testName, null, null);
        BundleResourceUtil.copyBundleEntriesIntoWorkspace(TESTFILES_PATHSTRING + "testDynamicAttributes", "/testDynamicAttributes");
    }
    project.refreshLocal(IResource.DEPTH_INFINITE, null);
    CMDocumentFactoryTLD factory = new CMDocumentFactoryTLD();
    ITaglibRecord[] records = TaglibIndex.getAvailableTaglibRecords(new Path("/" + testName + "/"));
    assertEquals("There should only be one taglib record", 1, records.length);
    CMDocument document = factory.createCMDocument(records[0]);
    CMNamedNodeMap elements = document.getElements();
    assertNotNull("No elements for the CM Document", elements);
    CMNode node = elements.getNamedItem("bar");
    assertTrue("Node must be a CMElementDeclarationImpl", node instanceof CMElementDeclarationImpl);
    assertEquals("Dynamic attributes must be set to 'true'", "true", ((CMElementDeclarationImpl) node).getDynamicAttributes());
}
Also used : Path(org.eclipse.core.runtime.Path) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) ITaglibRecord(org.eclipse.jst.jsp.core.taglib.ITaglibRecord) CMElementDeclarationImpl(org.eclipse.jst.jsp.core.internal.contentmodel.tld.CMElementDeclarationImpl) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) CMDocumentFactoryTLD(org.eclipse.jst.jsp.core.internal.contentmodel.tld.CMDocumentFactoryTLD) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) IProject(org.eclipse.core.resources.IProject)

Example 3 with CMElementDeclarationImpl

use of org.eclipse.jst.jsp.core.internal.contentmodel.tld.CMElementDeclarationImpl in project webtools.sourceediting by eclipse.

the class TestTaglibCMTests method testTagRuntimeExpressionValues.

public void testTagRuntimeExpressionValues() throws Exception {
    final String testName = "testLoadTagFiles";
    IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(testName);
    if (!project.exists()) {
        project = BundleResourceUtil.createSimpleProject(testName, null, null);
        BundleResourceUtil.copyBundleEntriesIntoWorkspace(TESTFILES_PATHSTRING + testName, "/" + testName);
    }
    project.refreshLocal(IResource.DEPTH_INFINITE, null);
    CMDocumentFactoryTLD factory = new CMDocumentFactoryTLD();
    ITaglibRecord[] records = TaglibIndex.getAvailableTaglibRecords(new Path("/" + testName + "/"));
    assertEquals("There should only be one taglib record", 1, records.length);
    CMDocument document = factory.createCMDocument(records[0]);
    CMNamedNodeMap elements = document.getElements();
    assertNotNull("No elements for the CM Document", elements);
    CMNode node = elements.getNamedItem("test");
    assertTrue("Node must be a CMElementDeclarationImpl", node instanceof CMElementDeclarationImpl);
    CMNamedNodeMap attributes = ((CMElementDeclaration) node).getAttributes();
    assertNotNull("No attributes", attributes);
    node = attributes.getNamedItem("myAttr");
    assertTrue("Node must be a CMAttributeDeclarationImpl", node instanceof CMAttributeDeclarationImpl);
    assertEquals("Default rtexprvalue for tags should be true", JSP11Namespace.ATTR_VALUE_TRUE, ((CMAttributeDeclarationImpl) node).getRtexprvalue());
    node = attributes.getNamedItem("noRuntimeAttr");
    assertTrue("Node must be a CMAttributeDeclarationImpl", node instanceof CMAttributeDeclarationImpl);
    assertEquals("rtexprvalue for should be false since explicitly set", JSP11Namespace.ATTR_VALUE_FALSE, ((CMAttributeDeclarationImpl) node).getRtexprvalue());
}
Also used : Path(org.eclipse.core.runtime.Path) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) CMAttributeDeclarationImpl(org.eclipse.jst.jsp.core.internal.contentmodel.tld.CMAttributeDeclarationImpl) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) ITaglibRecord(org.eclipse.jst.jsp.core.taglib.ITaglibRecord) CMElementDeclarationImpl(org.eclipse.jst.jsp.core.internal.contentmodel.tld.CMElementDeclarationImpl) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) CMDocumentFactoryTLD(org.eclipse.jst.jsp.core.internal.contentmodel.tld.CMDocumentFactoryTLD) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) IProject(org.eclipse.core.resources.IProject)

Aggregations

CMElementDeclarationImpl (org.eclipse.jst.jsp.core.internal.contentmodel.tld.CMElementDeclarationImpl)3 ITaglibRecord (org.eclipse.jst.jsp.core.taglib.ITaglibRecord)3 IProject (org.eclipse.core.resources.IProject)2 Path (org.eclipse.core.runtime.Path)2 CMDocumentFactoryTLD (org.eclipse.jst.jsp.core.internal.contentmodel.tld.CMDocumentFactoryTLD)2 CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)2 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)2 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)2 CMNode (org.eclipse.wst.xml.core.internal.contentmodel.CMNode)2 List (java.util.List)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 IDocument (org.eclipse.jface.text.IDocument)1 IRegion (org.eclipse.jface.text.IRegion)1 ITypedRegion (org.eclipse.jface.text.ITypedRegion)1 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)1 CMAttributeDeclarationImpl (org.eclipse.jst.jsp.core.internal.contentmodel.tld.CMAttributeDeclarationImpl)1 TLDCMDocumentManager (org.eclipse.jst.jsp.core.internal.contentmodel.tld.TLDCMDocumentManager)1 TaglibTracker (org.eclipse.jst.jsp.core.internal.contentmodel.tld.TaglibTracker)1 ITLDRecord (org.eclipse.jst.jsp.core.taglib.ITLDRecord)1 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)1