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;
}
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();
}
}
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());
}
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());
}
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());
}
Aggregations