use of org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter in project webtools.sourceediting by eclipse.
the class TestHtmlTranslation method testMangleMultipleMixedServerSideInJS_and_CheckProblems.
public void testMangleMultipleMixedServerSideInJS_and_CheckProblems() {
// get model
String fileName = getName() + ".html";
IStructuredModel structuredModel = getSharedModel(fileName, "<script> var text = <? serverObject.getText() ?>; <%=\"a\"%> <%=\"b\"%> <? serverObject.getText() ?><%=\"c\"%> </script>");
assertNotNull("missing test model", structuredModel);
// do translation
JsTranslationAdapterFactory.setupAdapterFactory(structuredModel);
JsTranslationAdapter translationAdapter = (JsTranslationAdapter) ((IDOMModel) structuredModel).getDocument().getAdapterFor(IJsTranslation.class);
IJsTranslation translation = translationAdapter.getJsTranslation(false);
String translated = translation.getJsText();
assertEquals("translated contents not as expected", " var text = _$tag_______________________; _$tag___ _$tag___ _$tag________________________$tag___ ", translated);
assertTrue("translation empty", translated.length() > 5);
assertTrue("server-side script block included", translated.indexOf("<?") < 0);
assertTrue("server-side script block included", translated.indexOf("?>") < 0);
assertTrue("server-side script block included", translated.indexOf("<%") < 0);
assertTrue("server-side script block included", translated.indexOf("%>") < 0);
assertTrue("var dropped", translated.indexOf("var text = ") > -1);
assertTrue("problems found in translation ", translation.getProblems().isEmpty());
// release model
structuredModel.releaseFromRead();
}
use of org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter in project webtools.sourceediting by eclipse.
the class TestHtmlTranslation method testCDATAAroundJS1.
public void testCDATAAroundJS1() {
// get model
String fileName = getName() + ".html";
IStructuredModel structuredModel = getSharedModel(fileName, "<script> <![CDATA[ var text = serverObject.getText() ]]> </script>");
assertNotNull("missing test model", structuredModel);
// do translation
JsTranslationAdapterFactory.setupAdapterFactory(structuredModel);
JsTranslationAdapter translationAdapter = (JsTranslationAdapter) ((IDOMModel) structuredModel).getDocument().getAdapterFor(IJsTranslation.class);
IJsTranslation translation = translationAdapter.getJsTranslation(false);
String translated = translation.getJsText();
assertTrue("translation empty", translated.length() > 5);
assertTrue("CDATA start found", translated.indexOf("CDATA") < 0);
assertTrue("CDATA start found", translated.indexOf("[") < 0);
assertTrue("CDATA end found", translated.indexOf("]") < 0);
assertTrue("problems found in translation ", translation.getProblems().isEmpty());
// release model
structuredModel.releaseFromRead();
}
use of org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter in project webtools.sourceediting by eclipse.
the class TestHtmlTranslation method testCDATAAroundJS3.
public void testCDATAAroundJS3() {
// get model
String fileName = getName() + ".html";
IStructuredModel structuredModel = getSharedModel(fileName, "<script>//<![CDATA[\n var text = serverObject.getText(); ]]></script>");
assertNotNull("missing test model", structuredModel);
// do translation
JsTranslationAdapterFactory.setupAdapterFactory(structuredModel);
JsTranslationAdapter translationAdapter = (JsTranslationAdapter) ((IDOMModel) structuredModel).getDocument().getAdapterFor(IJsTranslation.class);
IJsTranslation translation = translationAdapter.getJsTranslation(false);
String translated = translation.getJsText();
assertTrue("translation empty", translated.length() > 5);
assertTrue("CDATA start found", translated.indexOf("CDATA") < 0);
assertTrue("CDATA start found", translated.indexOf("[") < 0);
assertTrue("CDATA end found", translated.indexOf("]") < 0);
assertTrue("problems found in translation ", translation.getProblems().isEmpty());
// release model
structuredModel.releaseFromRead();
}
use of org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter in project webtools.sourceediting by eclipse.
the class JSDTHoverProcessor 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 = null;
try {
xmlModel = (IDOMModel) StructuredModelManager.getModelManager().getExistingModelForRead(textViewer.getDocument());
if (xmlModel != null) {
IDOMDocument xmlDoc = xmlModel.getDocument();
JsTranslationAdapter adapter = (JsTranslationAdapter) xmlDoc.getAdapterFor(IJsTranslation.class);
if (adapter != null) {
try {
boolean proceed = false;
ITypedRegion[] partitions = xmlDoc.getStructuredDocument().computePartitioning(hoverRegion.getOffset(), hoverRegion.getLength());
for (int i = 0; i < partitions.length; i++) {
for (int j = 0; j < PARTITION_TYPES.length; j++) {
if (PARTITION_TYPES[j].equals(partitions[i].getType())) {
proceed = true;
break;
}
}
}
if (proceed) {
IJsTranslation translation = adapter.getJsTranslation(true);
IJavaScriptElement[] result = translation.getElementsFromJsRange(translation.getJavaScriptOffset(hoverRegion.getOffset()), translation.getJavaScriptOffset(hoverRegion.getOffset() + hoverRegion.getLength()));
return translation.fixupMangledName(getHoverInfo(result));
}
} catch (BadLocationException e) {
// do nothing
}
}
}
} finally {
if (xmlModel != null) {
xmlModel.releaseFromRead();
}
}
return null;
}
use of org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter in project webtools.sourceediting by eclipse.
the class FormattingStrategyJSDT method getTranslation.
public IJsTranslation getTranslation(IStructuredDocument document) {
IJsTranslation tran = null;
IDOMModel xmlModel = null;
try {
xmlModel = (IDOMModel) StructuredModelManager.getModelManager().getExistingModelForRead(document);
IDOMDocument xmlDoc = xmlModel.getDocument();
JsTranslationAdapterFactory.setupAdapterFactory(xmlModel);
JsTranslationAdapter translationAdapter = (JsTranslationAdapter) xmlDoc.getAdapterFor(IJsTranslation.class);
if (translationAdapter != null) {
tran = translationAdapter.getJsTranslation(true);
}
} finally {
if (xmlModel != null) {
xmlModel.releaseFromRead();
}
}
return tran;
}
Aggregations