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