Search in sources :

Example 1 with ITaglibRecord

use of org.eclipse.jst.jsp.core.taglib.ITaglibRecord 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 ITaglibRecord

use of org.eclipse.jst.jsp.core.taglib.ITaglibRecord in project webtools.sourceediting by eclipse.

the class JSPJavaTranslatorCoreTest method test_432978.

// http://bugs.eclipse.org/432978
public void test_432978() throws Exception {
    String testName = "bug_432978";
    // Create new project
    IProject project = BundleResourceUtil.createJavaWebProject(testName);
    assertTrue(project.exists());
    BundleResourceUtil.copyBundleEntriesIntoWorkspace("/testfiles/" + testName, "/" + testName);
    // TEI class needs to already be compiled
    // waitForBuildAndValidation(project);
    // project.build(IncrementalProjectBuilder.FULL_BUILD, "org.eclipse.jdt.internal.core.builder.JavaBuilder", null, null);
    project.getWorkspace().checkpoint(true);
    IFile file1 = project.getFile("/WebContent/test.jsp");
    IFile file2 = project.getFile("/WebContent/test2.jsp");
    IDOMModel structuredModel1 = null;
    IDOMModel structuredModel2 = null;
    try {
        ITaglibRecord tld = TaglibIndex.resolve(file1.getFullPath().toString(), "http://eclipse.org/testbug_432978", false);
        structuredModel1 = (IDOMModel) StructuredModelManager.getModelManager().getModelForRead(file1);
        ModelHandlerForJSP.ensureTranslationAdapterFactory(structuredModel1);
        JSPTranslationAdapter translationAdapter = (JSPTranslationAdapter) structuredModel1.getDocument().getAdapterFor(IJSPTranslation.class);
        final String translation = translationAdapter.getJSPTranslation().getJavaText();
        assertTrue("The 'extra' integer declared by a TEI class was not found, taglib was: " + tld, translation.indexOf("java.lang.Integer extra") > 0);
        /*
			 * the extra variable should only be declared once in the
			 * translated text
			 */
        assertEquals(2, translation.split("java.lang.Integer extra").length);
        structuredModel2 = (IDOMModel) StructuredModelManager.getModelManager().getModelForRead(file2);
        ModelHandlerForJSP.ensureTranslationAdapterFactory(structuredModel2);
        JSPTranslationAdapter translationAdapter2 = (JSPTranslationAdapter) structuredModel2.getDocument().getAdapterFor(IJSPTranslation.class);
        final String translation2 = translationAdapter2.getJSPTranslation().getJavaText();
        assertTrue(translation2.indexOf("extra") != -1);
        // the extra variable should be declared twice because of the nested atbegin tags
        assertEquals(3, translation2.split("java.lang.Integer extra").length);
    } finally {
        if (structuredModel1 != null)
            structuredModel1.releaseFromRead();
        if (structuredModel2 != null)
            structuredModel2.releaseFromRead();
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) ITaglibRecord(org.eclipse.jst.jsp.core.taglib.ITaglibRecord) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) JSPTranslationAdapter(org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter) IProject(org.eclipse.core.resources.IProject)

Example 3 with ITaglibRecord

use of org.eclipse.jst.jsp.core.taglib.ITaglibRecord 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 4 with ITaglibRecord

use of org.eclipse.jst.jsp.core.taglib.ITaglibRecord 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)

Example 5 with ITaglibRecord

use of org.eclipse.jst.jsp.core.taglib.ITaglibRecord in project webtools.sourceediting by eclipse.

the class TestIndex method testWebXMLTaglibMappingsToJARs.

public void testWebXMLTaglibMappingsToJARs() throws Exception {
    IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject("bug_148717");
    if (!project.exists()) {
        // Create new project
        project = BundleResourceUtil.createSimpleProject("bug_148717", null, null);
        assertTrue(project.exists());
        BundleResourceUtil.copyBundleEntriesIntoWorkspace("/testfiles/bug_148717", "/bug_148717");
    }
    IFile file = project.getFile("/WebContent/WEB-INF/lib/internal.jar");
    assertTrue(file.exists());
    String uri = "http://example.com/external-uri";
    ITaglibRecord taglibRecord = TaglibIndex.resolve("/bug_148717/WebContent/", uri, false);
    assertNotNull("record not found for " + uri, taglibRecord);
    assertEquals(ITaglibRecord.JAR, taglibRecord.getRecordType());
    assertEquals(uri, ((IJarRecord) taglibRecord).getDescriptor().getURI());
    ITaglibRecord taglibRecord2 = null;
    ITaglibRecord[] records = TaglibIndex.getAvailableTaglibRecords(new Path("/bug_148717/WebContent/"));
    for (int i = 0; i < records.length; i++) {
        int type = records[i].getRecordType();
        switch(type) {
            case ITaglibRecord.JAR:
                {
                    taglibRecord2 = records[i];
                }
                break;
        }
    }
    assertNotNull("record not returned for " + uri, taglibRecord2);
    assertEquals(ITaglibRecord.JAR, taglibRecord2.getRecordType());
    assertEquals(uri, ((IJarRecord) taglibRecord2).getDescriptor().getURI());
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) ITaglibRecord(org.eclipse.jst.jsp.core.taglib.ITaglibRecord) IProject(org.eclipse.core.resources.IProject) IJarRecord(org.eclipse.jst.jsp.core.taglib.IJarRecord)

Aggregations

ITaglibRecord (org.eclipse.jst.jsp.core.taglib.ITaglibRecord)16 IProject (org.eclipse.core.resources.IProject)10 Path (org.eclipse.core.runtime.Path)10 IPath (org.eclipse.core.runtime.IPath)5 IFile (org.eclipse.core.resources.IFile)4 URL (java.net.URL)3 CMElementDeclarationImpl (org.eclipse.jst.jsp.core.internal.contentmodel.tld.CMElementDeclarationImpl)3 ITLDRecord (org.eclipse.jst.jsp.core.taglib.ITLDRecord)3 IURLRecord (org.eclipse.jst.jsp.core.taglib.IURLRecord)3 CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)3 CoreException (org.eclipse.core.runtime.CoreException)2 CMDocumentFactoryTLD (org.eclipse.jst.jsp.core.internal.contentmodel.tld.CMDocumentFactoryTLD)2 IJarRecord (org.eclipse.jst.jsp.core.taglib.IJarRecord)2 ITagDirRecord (org.eclipse.jst.jsp.core.taglib.ITagDirRecord)2 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)2 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)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 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)2