use of org.eclipse.jst.jsp.core.internal.java.JSPTranslator in project webtools.sourceediting by eclipse.
the class JSPTranslatorPersistenceTest method writeTranslator.
private static JSPTranslator writeTranslator(IStructuredModel jspModel, String externalizedFileName) throws IOException, CoreException {
JSPTranslator translator = new JSPTranslator();
translator.reset((IDOMNode) jspModel.getIndexedRegion(0), new NullProgressMonitor());
translator.translate();
writeObject(externalizedFileName, translator);
return translator;
}
use of org.eclipse.jst.jsp.core.internal.java.JSPTranslator 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();
}
}
}
use of org.eclipse.jst.jsp.core.internal.java.JSPTranslator in project webtools.sourceediting by eclipse.
the class JSPJavaTranslatorTest method testJSPInJavascript.
void testJSPInJavascript(String filename) throws UnsupportedEncodingException, IOException {
IStructuredModel sm = getStructuredModelForRead(filename);
if (sm != null) {
IDOMNode xmlNode = (IDOMNode) sm.getIndexedRegion(0);
if (xmlNode != null) {
JSPTranslator jspt = new JSPTranslator();
jspt.reset(xmlNode, null);
// int sourceTextPos = text.indexOf("<%= testJspString") + 17;
// jspt.setSourceCursor(sourceTextPos); // right after the
// text
jspt.translate();
String translation = jspt.getTranslation().toString();
// offsets are found using JSPTranslation now
// int translatedCursorPosition = jspt.getCursorPosition();
// assertEquals("incorrect cursor position >" +
// translatedCursorPosition, 519, translatedCursorPosition);
// assertEquals("translation was incorrect", "testJspString",
// translation.substring(519, 532));
int cursorStart = translation.indexOf("out.print( testJspString );") + 10;
assertEquals("incorrect cursor position >" + cursorStart, 914, cursorStart);
}
sm.releaseFromRead();
}
}
Aggregations