Search in sources :

Example 21 with JSPTranslationAdapter

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

the class JSPTranslatorPersistenceTest method testCreateTranslationAdapter.

/**
 * <p>This test case follows the general pattern of how a translation is created, then can be
 * externalized when the workspace is closed, then reloaded when its opened again.</p>
 *
 * @throws Exception
 */
public void testCreateTranslationAdapter() throws Exception {
    String outFileName = "testCreateTranslationAdapter.obj";
    IStructuredModel structModel = null;
    try {
        structModel = getModelForRead("Test1.jsp");
        // verify there is not already an existing translation adapter
        IDOMDocument domDoc = ((IDOMModel) structModel).getDocument();
        INodeAdapter existingAdapter = domDoc.getAdapterFor(IJSPTranslation.class);
        assertNull("There should be no existing adapter for IJSPTranslation", existingAdapter);
        // create a translator and externalize it, then load the externalized translator
        JSPTranslator originalTranslator = writeTranslator(structModel, outFileName);
        JSPTranslator externalizedTranslator = (JSPTranslator) readObject(outFileName);
        // create an adaptr from the loaded externalized translator and add it to the doc
        JSPTranslationAdapter restoredAdapter = new JSPTranslationAdapter((IDOMModel) structModel, externalizedTranslator);
        domDoc.addAdapter(restoredAdapter);
        // verify we can retrieve the adapter we just set
        existingAdapter = domDoc.getAdapterFor(IJSPTranslation.class);
        assertNotNull("There should now be an existing adapter for IJSPTranslation", existingAdapter);
        assertTrue("Expected " + existingAdapter + " to be an instance of JSPTranslationAdapter", existingAdapter instanceof JSPTranslationAdapter);
        JSPTranslationAdapter retrievedAdapter = (JSPTranslationAdapter) existingAdapter;
        JSPTranslationExtension jspTranslationExtension = retrievedAdapter.getJSPTranslation();
        /* verify that the original translation is equal to that of the
			 * retrieved adapter created from the previously externalized translator
			 */
        assertEquals("The original translation should be the same as the restored externalized translation", originalTranslator.getTranslation().toString(), jspTranslationExtension.getJavaText());
    } finally {
        if (structModel != null) {
            structModel.releaseFromRead();
        }
    }
}
Also used : INodeAdapter(org.eclipse.wst.sse.core.internal.provisional.INodeAdapter) JSPTranslationExtension(org.eclipse.jst.jsp.core.internal.java.JSPTranslationExtension) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) JSPTranslationAdapter(org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter) JSPTranslator(org.eclipse.jst.jsp.core.internal.java.JSPTranslator)

Example 22 with JSPTranslationAdapter

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

the class TestModelIncludes method testTranslateMultiLineIncludedFileWithSpacesAndScriptletInInclude.

/**
 * Verify included files are translated properly when they contain a
 * multiple lines
 *
 * @throws Exception
 */
public void testTranslateMultiLineIncludedFileWithSpacesAndScriptletInInclude() throws Exception {
    String projectName = "prj119576_c";
    BundleResourceUtil.createSimpleProject(projectName, null, null);
    BundleResourceUtil.copyBundleEntriesIntoWorkspace("/testfiles/" + projectName, "/" + projectName);
    assertTrue("project could not be created", ResourcesPlugin.getWorkspace().getRoot().getProject(projectName).exists());
    IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path("/" + projectName + "/WebContent/body3.jsp"));
    IDOMModel model = null;
    try {
        model = (IDOMModel) StructuredModelManager.getModelManager().getModelForRead(file);
        assertTrue("model has no content", model.getStructuredDocument().getLength() > 0);
        ModelHandlerForJSP.ensureTranslationAdapterFactory(model);
        JSPTranslationAdapter adapter = (JSPTranslationAdapter) model.getDocument().getAdapterFor(IJSPTranslation.class);
        String source = adapter.getJSPTranslation().getJavaText();
        assertTrue("scriptlet with variable declaration not found\n" + source, source.indexOf("java.util.Date headerDate") > -1);
    } finally {
        if (model != null)
            model.releaseFromRead();
    }
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) 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)

Example 23 with JSPTranslationAdapter

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

the class JSPFindOccurrencesProcessor method getJavaElementsForCurrentSelection.

/**
 * uses JSPTranslation to get currently selected Java elements.
 *
 * @return currently selected IJavaElements
 */
private IJavaElement[] getJavaElementsForCurrentSelection(IDocument document, ITextSelection selection) {
    IJavaElement[] elements = new IJavaElement[0];
    // get JSP translation object for this viewer's document
    IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
    try {
        if (model != null && model instanceof IDOMModel) {
            IDOMDocument xmlDoc = ((IDOMModel) model).getDocument();
            JSPTranslationAdapter adapter = (JSPTranslationAdapter) xmlDoc.getAdapterFor(IJSPTranslation.class);
            if (adapter != null) {
                JSPTranslation translation = adapter.getJSPTranslation();
                // https://bugs.eclipse.org/bugs/show_bug.cgi?id=102211
                elements = translation.getElementsFromJspRange(selection.getOffset(), selection.getOffset() + selection.getLength());
            }
        }
    } finally {
        if (model != null)
            model.releaseFromRead();
    }
    return elements;
}
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) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) JSPTranslationAdapter(org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter)

Example 24 with JSPTranslationAdapter

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

the class JSPJavaJavadocHoverProcessor method getHoverInfo.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.text.ITextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer,
	 *      org.eclipse.jface.text.IRegion)
	 */
public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
    // get JSP translation object for this viewer's document
    IDOMModel xmlModel = (IDOMModel) StructuredModelManager.getModelManager().getExistingModelForRead(textViewer.getDocument());
    try {
        if (xmlModel != null) {
            IDOMDocument xmlDoc = xmlModel.getDocument();
            JSPTranslationAdapter adapter = (JSPTranslationAdapter) xmlDoc.getAdapterFor(IJSPTranslation.class);
            if (adapter != null) {
                JSPTranslation translation = adapter.getJSPTranslation();
                IJavaElement[] result = translation.getElementsFromJspRange(hoverRegion.getOffset(), hoverRegion.getOffset() + hoverRegion.getLength());
                return translation.fixupMangledName(getHoverInfo(result));
            }
        }
    } finally {
        if (xmlModel != null)
            xmlModel.releaseFromRead();
    }
    return null;
}
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) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) JSPTranslationAdapter(org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter)

Example 25 with JSPTranslationAdapter

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

the class JSPSearchTests method testSearchLocalVariable.

public void testSearchLocalVariable() {
    IDOMModel xmlModel = null;
    try {
        IPath jspTestFilePath = new Path("judo/SEARCH/searchTestJSP3.jsp");
        IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(jspTestFilePath);
        xmlModel = (IDOMModel) getStructuredModelForRead(file);
        ModelHandlerForJSP.ensureTranslationAdapterFactory(xmlModel);
        IDOMDocument doc = xmlModel.getDocument();
        JSPTranslationAdapter adapter = (JSPTranslationAdapter) doc.getAdapterFor(IJSPTranslation.class);
        JSPTranslation translation = adapter.getJSPTranslation();
        IJavaElement element = translation.getElementsFromJspRange(377, 384)[0];
        TestJspSearchRequestor requestor = new TestJspSearchRequestor();
        requestor.addCheckMatch("searchTestJSP3.jsp", 377, 384);
        JSPSearchSupport.getInstance().search(element, new JSPSearchScope(), requestor, new NullProgressMonitor());
        assertTrue("did not find all expected matches: search*", requestor.checkValid());
    } finally {
        if (xmlModel != null)
            xmlModel.releaseFromRead();
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IJavaElement(org.eclipse.jdt.core.IJavaElement) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) JSPTranslation(org.eclipse.jst.jsp.core.internal.java.JSPTranslation) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) JSPSearchScope(org.eclipse.jst.jsp.core.internal.java.search.JSPSearchScope) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) JSPTranslationAdapter(org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter)

Aggregations

IJSPTranslation (org.eclipse.jst.jsp.core.internal.java.IJSPTranslation)30 JSPTranslationAdapter (org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter)30 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)26 IFile (org.eclipse.core.resources.IFile)17 JSPTranslation (org.eclipse.jst.jsp.core.internal.java.JSPTranslation)14 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)10 IProject (org.eclipse.core.resources.IProject)9 Path (org.eclipse.core.runtime.Path)7 JSPTranslationExtension (org.eclipse.jst.jsp.core.internal.java.JSPTranslationExtension)6 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)6 IPath (org.eclipse.core.runtime.IPath)5 IJavaElement (org.eclipse.jdt.core.IJavaElement)5 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)4 InputStream (java.io.InputStream)3 List (java.util.List)3 IProblem (org.eclipse.jdt.core.compiler.IProblem)3 ISelection (org.eclipse.jface.viewers.ISelection)3 DOMModelForJSP (org.eclipse.jst.jsp.core.internal.domdocument.DOMModelForJSP)3 INodeAdapter (org.eclipse.wst.sse.core.internal.provisional.INodeAdapter)3 HashMap (java.util.HashMap)2