Search in sources :

Example 1 with JSPTranslationUtil

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

the class FormattingStrategyJSPJava method format.

/*
	 * @see org.eclipse.jface.text.formatter.ContextBasedFormattingStrategy#format()
	 */
public void format() {
    super.format();
    final IDocument document = (IDocument) fDocuments.removeFirst();
    final TypedPosition partition = (TypedPosition) fPartitions.removeFirst();
    if (document != null && partition != null) {
        try {
            JSPTranslationUtil translationUtil = new JSPTranslationUtil(document);
            ICompilationUnit cu = translationUtil.getCompilationUnit();
            if (cu != null) {
                String cuSource = cu.getSource();
                /*
					 * Format the entire compilation unit, but only create
					 * edits for the requested JSP partition's range in the
					 * Java source
					 */
                TextEdit textEdit = formatString(CodeFormatter.K_COMPILATION_UNIT, cuSource, translationUtil.getTranslation().getJavaOffset(partition.getOffset()), partition.getLength(), TextUtilities.getDefaultLineDelimiter(document), getPreferences());
                TextEdit jspEdit = translationUtil.getTranslation().getJspEdit(textEdit);
                if (jspEdit != null && jspEdit.hasChildren())
                    jspEdit.apply(document);
            }
        } catch (MalformedTreeException exception) {
            Logger.logException(exception);
        } catch (BadLocationException exception) {
            // Can only happen on concurrent document modification - log
            // and bail out
            Logger.logException(exception);
        } catch (JavaModelException exception) {
            Logger.logException(exception);
        }
    }
}
Also used : JSPTranslationUtil(org.eclipse.jst.jsp.core.internal.java.JSPTranslationUtil) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaModelException(org.eclipse.jdt.core.JavaModelException) TypedPosition(org.eclipse.jface.text.TypedPosition) TextEdit(org.eclipse.text.edits.TextEdit) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 2 with JSPTranslationUtil

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

the class JSPJavaTranslatorCoreTest method test_109721.

public void test_109721() throws Exception {
    /*
		 * https://bugs.eclipse.org/109721 JSP editor does not find taglib
		 * directives in include-prelude or jsp:include, make sure the
		 * contents were processed
		 */
    String testName = "bug_109721";
    // Create new project
    final IProject project = BundleResourceUtil.createSimpleProject(testName, null, null);
    assertTrue(project.isAccessible());
    BundleResourceUtil.copyBundleEntriesIntoWorkspace("/testfiles/" + testName, "/" + testName);
    BundleResourceUtil.copyBundleEntryIntoWorkspace("/testfiles/struts.jar", "/" + testName + "/WebContent/WEB-INF/lib/struts.jar");
    waitForBuildAndValidation(project);
    String filename = "/WebContent/main.jsp";
    IStructuredModel sm = StructuredModelManager.getModelManager().getModelForRead(project.getFile(filename));
    assertNotNull("couldn't load JSP for test", sm);
    JSPTranslationUtil translationUtil = new JSPTranslationUtil(sm.getStructuredDocument());
    String translation = translationUtil.getTranslation().getJavaText();
    sm.releaseFromRead();
    assertTrue("Java content from Fragment included by web.xml is missing", translation.indexOf("int alpha = 5;") > 0);
    assertTrue("Tag that could only be known by processing Fragment included only by web.xml is not in the translation", translation.indexOf("org.apache.struts.taglib.bean.DefineTag") > 0);
}
Also used : JSPTranslationUtil(org.eclipse.jst.jsp.core.internal.java.JSPTranslationUtil) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IProject(org.eclipse.core.resources.IProject)

Example 3 with JSPTranslationUtil

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

the class JSPJavaTranslatorTest method testMultipleJSPSectionsInJavascript.

public void testMultipleJSPSectionsInJavascript() throws Exception {
    String filename = "testfiles/jspInJavascript2.jsp";
    IStructuredModel sm = getStructuredModelForRead(filename);
    assertNotNull("couldn't load JSP for test", sm);
    JSPTranslationUtil translationUtil = new JSPTranslationUtil(sm.getStructuredDocument());
    String javaText = StringUtils.replace(translationUtil.getTranslation().getJavaText(), "\r\n", "\n");
    javaText = StringUtils.replace(javaText, "\r", "\n");
    sm.releaseFromRead();
    String translatedText = loadContents("testfiles/jspInJavascript2.javasource");
    assertEquals("translated contents are not as expected", translatedText, javaText);
}
Also used : JSPTranslationUtil(org.eclipse.jst.jsp.core.internal.java.JSPTranslationUtil) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)

Aggregations

JSPTranslationUtil (org.eclipse.jst.jsp.core.internal.java.JSPTranslationUtil)3 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)2 IProject (org.eclipse.core.resources.IProject)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 IDocument (org.eclipse.jface.text.IDocument)1 TypedPosition (org.eclipse.jface.text.TypedPosition)1 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)1 TextEdit (org.eclipse.text.edits.TextEdit)1