Search in sources :

Example 6 with JSPTranslationAdapter

use of org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter 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 7 with JSPTranslationAdapter

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

the class TestModelIncludes method testTranslateSingleLineIncludedFileWithNoSpacesButScriptletInInclude.

/**
 * Verify included files are translated properly when they contain a
 * single line and document region and no trailing white space.
 *
 * @throws Exception
 */
public void testTranslateSingleLineIncludedFileWithNoSpacesButScriptletInInclude() throws Exception {
    String projectName = "prj119576_b";
    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", 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 8 with JSPTranslationAdapter

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

the class ShowTranslationHandler method execute.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands
	 * .ExecutionEvent)
	 */
public Object execute(final ExecutionEvent event) throws ExecutionException {
    ISelection selection = HandlerUtil.getCurrentSelection(event);
    if (selection instanceof IStructuredSelection) {
        List list = ((IStructuredSelection) selection).toList();
        if (!list.isEmpty()) {
            if (list.get(0) instanceof IDOMNode) {
                final IDOMModel model = ((IDOMNode) list.get(0)).getModel();
                INodeAdapter adapter = model.getDocument().getAdapterFor(IJSPTranslation.class);
                if (adapter != null) {
                    Job opener = new UIJob("Opening JSP Java Translation") {

                        public IStatus runInUIThread(IProgressMonitor monitor) {
                            JSPTranslationAdapter translationAdapter = (JSPTranslationAdapter) model.getDocument().getAdapterFor(IJSPTranslation.class);
                            final JSPTranslationExtension translation = translationAdapter.getJSPTranslation();
                            // create an IEditorInput for the Java editor
                            final IStorageEditorInput input = new JSPTranslationEditorInput(model);
                            try {
                                IEditorPart editor = IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), input, JavaUI.ID_CU_EDITOR, true);
                                // Now add the problems we found
                                if (editor instanceof ITextEditor) {
                                    IAnnotationModel annotationModel = ((ITextEditor) editor).getDocumentProvider().getAnnotationModel(input);
                                    translation.reconcileCompilationUnit();
                                    List problemsList = translation.getProblems();
                                    IProblem[] problems = (IProblem[]) problemsList.toArray(new IProblem[problemsList.size()]);
                                    AnnotationTypeLookup lookup = new AnnotationTypeLookup();
                                    for (int i = 0; i < problems.length; i++) {
                                        if (problems[i] instanceof IJSPProblem)
                                            continue;
                                        int length = problems[i].getSourceEnd() - problems[i].getSourceStart() + 1;
                                        Position position = new Position(problems[i].getSourceStart(), length);
                                        Annotation annotation = null;
                                        String type = lookup.getAnnotationType(IMarker.PROBLEM, IMarker.SEVERITY_INFO);
                                        if (problems[i].isError()) {
                                            type = lookup.getAnnotationType(IMarker.PROBLEM, IMarker.SEVERITY_ERROR);
                                        } else if (problems[i].isWarning()) {
                                            type = lookup.getAnnotationType(IMarker.PROBLEM, IMarker.SEVERITY_WARNING);
                                        }
                                        annotation = new Annotation(type, false, problems[i].getMessage());
                                        if (annotation != null) {
                                            annotationModel.addAnnotation(annotation, position);
                                        }
                                    }
                                }
                            } catch (PartInitException e) {
                                e.printStackTrace();
                                Display.getCurrent().beep();
                            }
                            return Status.OK_STATUS;
                        }
                    };
                    opener.setSystem(false);
                    opener.setUser(true);
                    opener.schedule();
                }
            }
        }
    }
    return null;
}
Also used : INodeAdapter(org.eclipse.wst.sse.core.internal.provisional.INodeAdapter) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IJSPProblem(org.eclipse.jst.jsp.core.internal.java.IJSPProblem) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) JSPTranslationExtension(org.eclipse.jst.jsp.core.internal.java.JSPTranslationExtension) ISelection(org.eclipse.jface.viewers.ISelection) UIJob(org.eclipse.ui.progress.UIJob) List(java.util.List) PartInitException(org.eclipse.ui.PartInitException) Job(org.eclipse.core.runtime.jobs.Job) UIJob(org.eclipse.ui.progress.UIJob) IStorageEditorInput(org.eclipse.ui.IStorageEditorInput) Position(org.eclipse.jface.text.Position) IEditorPart(org.eclipse.ui.IEditorPart) JSPTranslationAdapter(org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter) IProblem(org.eclipse.jdt.core.compiler.IProblem) Annotation(org.eclipse.jface.text.source.Annotation) AnnotationTypeLookup(org.eclipse.ui.texteditor.AnnotationTypeLookup) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor)

Example 9 with JSPTranslationAdapter

use of org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter 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 10 with JSPTranslationAdapter

use of org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter 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

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