Search in sources :

Example 1 with JSPTranslation

use of org.eclipse.jst.jsp.core.internal.java.JSPTranslation in project webtools.sourceediting by eclipse.

the class JSPJavaHyperlinkDetector method detectHyperlinks.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.text.hyperlink.IHyperlinkDetector#detectHyperlinks(org.eclipse.jface.text.ITextViewer,
	 *      org.eclipse.jface.text.IRegion, boolean)
	 */
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
    List hyperlinks = new ArrayList(0);
    if (region != null && textViewer != null) {
        IDocument document = textViewer.getDocument();
        JSPTranslation jspTranslation = getJSPTranslation(document);
        if (jspTranslation != null) {
            // check if we are in JSP Java content
            int javaOffset = jspTranslation.getJavaOffset(region.getOffset());
            if (javaOffset > -1) {
                // included files)
                if (!jspTranslation.isIndirect(javaOffset)) {
                    // get Java elements
                    IJavaElement[] elements = jspTranslation.getElementsFromJspRange(region.getOffset(), region.getOffset() + region.getLength());
                    if (elements != null && elements.length > 0) {
                        // create a JSPJavaHyperlink for each Java element
                        for (int i = 0; i < elements.length; ++i) {
                            IJavaElement element = elements[i];
                            // find hyperlink range for Java element
                            IRegion hyperlinkRegion = selectWord(document, region.getOffset());
                            IHyperlink link = createHyperlink(element, hyperlinkRegion, document);
                            if (link != null) {
                                hyperlinks.add(link);
                            }
                        }
                    }
                }
            }
        }
    }
    if (hyperlinks.isEmpty())
        return null;
    return (IHyperlink[]) hyperlinks.toArray(new IHyperlink[0]);
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) JSPTranslation(org.eclipse.jst.jsp.core.internal.java.JSPTranslation) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion)

Example 2 with JSPTranslation

use of org.eclipse.jst.jsp.core.internal.java.JSPTranslation in project webtools.sourceediting by eclipse.

the class JSPSearchDocument method getPath.

/**
 * the path to the Java compilation unit
 *
 * @see org.eclipse.jdt.core.search.SearchDocument#getPath()
 */
public String getPath() {
    // important that isDirty() check is second to cache modification stamp
    if (this.fCUPath == null || isDirty()) {
        JSPTranslation trans = getJSPTranslation();
        if (trans != null) {
            this.fCUPath = trans.getJavaPath();
            // save since it's expensive to calculate again later
            fCachedCharContents = trans.getJavaText().toCharArray();
        }
    }
    return fCUPath;
}
Also used : IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) JSPTranslation(org.eclipse.jst.jsp.core.internal.java.JSPTranslation)

Example 3 with JSPTranslation

use of org.eclipse.jst.jsp.core.internal.java.JSPTranslation in project webtools.sourceediting by eclipse.

the class JSPSearchDocument method getCharContents.

/**
 * @see org.eclipse.jdt.core.search.SearchDocument#getCharContents()
 */
public char[] getCharContents() {
    if (fCachedCharContents == null || isDirty()) {
        JSPTranslation trans = getJSPTranslation();
        fCachedCharContents = trans != null ? trans.getJavaText().toCharArray() : new char[0];
        fCUPath = trans.getJavaPath();
    }
    return fCachedCharContents;
}
Also used : IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) JSPTranslation(org.eclipse.jst.jsp.core.internal.java.JSPTranslation)

Example 4 with JSPTranslation

use of org.eclipse.jst.jsp.core.internal.java.JSPTranslation in project webtools.sourceediting by eclipse.

the class JSPTranslationTest method testXMLJSPTranslationText.

/**
 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=86382
 */
public void testXMLJSPTranslationText() {
    IFile f = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path("INCLUDES_TESTS/xml-jsp/most-tags-xml-jsp.jsp"));
    DOMModelForJSP sModel = (DOMModelForJSP) getStructuredModelForRead(f);
    try {
        setupAdapterFactory(sModel);
        JSPTranslationAdapter adapter = (JSPTranslationAdapter) sModel.getDocument().getAdapterFor(IJSPTranslation.class);
        JSPTranslation translation = adapter.getJSPTranslation();
        String javaText = translation.getJavaText();
        // named as .bin so no line conversion occurs (\n is in use)
        InputStream in = getClass().getResourceAsStream("translated_xml_jsp.bin");
        String knownTranslationText = loadChars(in);
        assertEquals(knownTranslationText, javaText);
    } finally {
        if (sModel != null)
            sModel.releaseFromRead();
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) JSPTranslation(org.eclipse.jst.jsp.core.internal.java.JSPTranslation) DOMModelForJSP(org.eclipse.jst.jsp.core.internal.domdocument.DOMModelForJSP) InputStream(java.io.InputStream) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) JSPTranslationAdapter(org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter)

Example 5 with JSPTranslation

use of org.eclipse.jst.jsp.core.internal.java.JSPTranslation in project webtools.sourceediting by eclipse.

the class JSPTranslationTest method testTranslatePositions.

public void testTranslatePositions() {
    IDOMModel model = getIncludeTestModelForRead();
    JSPTranslationAdapter adapter = (JSPTranslationAdapter) model.getDocument().getAdapterFor(IJSPTranslation.class);
    JSPTranslation translation = adapter.getJSPTranslation();
    try {
        HashMap java2jsp = translation.getJava2JspMap();
        assertEquals("java2jsp map size:", 11, java2jsp.size());
        HashMap jsp2java = translation.getJsp2JavaMap();
        assertEquals("jsp2java map size:", 3, jsp2java.size());
        // some test positions (out.print("" + | );)
        // we need to ignore the classname length in our comparisons
        // with java offsets that we get from the translation
        // since it can vary depending on workspace location
        int classnameLength = translation.getClassname().length();
        int jspTestPosition = translation.getJspText().indexOf("<%= ") + 4;
        int javaOffset = translation.getJavaOffset(jspTestPosition) - classnameLength;
        assertEquals("JSPTranslation java offset:", 1277, javaOffset);
        // (<%= | %>)
        int javaTestPostition = translation.getJavaText().indexOf("out.print(   );") + 10;
        // dont' need to worry about classname length here because we are comparing
        // with a position in the JSP document (which doesn't contain classname)
        int jspOffset = translation.getJspOffset(javaTestPostition);
        assertEquals("JSPTranslation jsp offset:", 564, jspOffset);
    } finally {
        if (model != null)
            model.releaseFromRead();
    }
}
Also used : IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) JSPTranslation(org.eclipse.jst.jsp.core.internal.java.JSPTranslation) HashMap(java.util.HashMap) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) JSPTranslationAdapter(org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter)

Aggregations

JSPTranslation (org.eclipse.jst.jsp.core.internal.java.JSPTranslation)24 IJSPTranslation (org.eclipse.jst.jsp.core.internal.java.IJSPTranslation)21 JSPTranslationAdapter (org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter)14 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)12 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)9 IFile (org.eclipse.core.resources.IFile)6 IJavaElement (org.eclipse.jdt.core.IJavaElement)6 IPath (org.eclipse.core.runtime.IPath)5 Path (org.eclipse.core.runtime.Path)5 DOMModelForJSP (org.eclipse.jst.jsp.core.internal.domdocument.DOMModelForJSP)4 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)4 InputStream (java.io.InputStream)3 HashMap (java.util.HashMap)3 CoreException (org.eclipse.core.runtime.CoreException)3 IDocument (org.eclipse.jface.text.IDocument)3 Position (org.eclipse.jface.text.Position)3 ArrayList (java.util.ArrayList)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)2