use of org.eclipse.jst.jsp.core.internal.java.IJSPProblem 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.IJSPProblem in project webtools.sourceediting by eclipse.
the class TaglibHelper method createValidationMessageProblem.
/**
* @param customTag
* @param validationMessage
* @return
*/
private Object createValidationMessageProblem(final IStructuredDocument document, final ITextRegionCollection customTag, final String validationMessage) {
final int start;
if (customTag.getNumberOfRegions() > 3) {
start = customTag.getStartOffset(customTag.getRegions().get(2));
} else if (customTag.getNumberOfRegions() > 1) {
start = customTag.getStartOffset(customTag.getRegions().get(1));
} else {
start = customTag.getStartOffset();
}
final int end;
if (customTag.getNumberOfRegions() > 3) {
end = customTag.getTextEndOffset(customTag.getRegions().get(customTag.getNumberOfRegions() - 2)) - 1;
} else if (customTag.getNumberOfRegions() > 1) {
end = customTag.getTextEndOffset(customTag.getRegions().get(1)) - 1;
} else {
end = customTag.getTextEndOffset();
}
final int line = document.getLineOfOffset(start);
final char[] name;
IPath location = TaglibController.getLocation(document);
if (location == null) {
name = new char[0];
} else {
name = location.toString().toCharArray();
}
return new IJSPProblem() {
public void setSourceStart(int sourceStart) {
}
public void setSourceLineNumber(int lineNumber) {
}
public void setSourceEnd(int sourceEnd) {
}
public boolean isInfo() {
return false;
}
public boolean isWarning() {
return true;
}
public boolean isError() {
return false;
}
public int getSourceStart() {
return start;
}
public int getSourceLineNumber() {
return line;
}
public int getSourceEnd() {
return end;
}
public char[] getOriginatingFileName() {
return name;
}
public String getMessage() {
return validationMessage;
}
public int getID() {
return getEID();
}
public String[] getArguments() {
return new String[0];
}
public int getEID() {
return IJSPProblem.TEIValidationMessage;
}
};
}
use of org.eclipse.jst.jsp.core.internal.java.IJSPProblem in project webtools.sourceediting by eclipse.
the class TaglibHelper method createJSPProblem.
/**
* @param customTag
* @param teiClass
* @return
*/
private Object createJSPProblem(final IStructuredDocument document, final ITextRegionCollection customTag, final int problemID, final String messageKey, final String argument, boolean preferVars) {
final String tagname = customTag.getText(customTag.getRegions().get(1));
final int start;
if (customTag.getNumberOfRegions() > 1) {
start = customTag.getStartOffset(customTag.getRegions().get(1));
} else {
start = customTag.getStartOffset();
}
final int end;
if (customTag.getNumberOfRegions() > 1) {
end = customTag.getTextEndOffset(customTag.getRegions().get(1)) - 1;
} else {
end = customTag.getTextEndOffset() - 1;
}
final int line = document.getLineOfOffset(start);
final char[] name;
IPath location = TaglibController.getLocation(document);
if (location == null) {
name = new char[0];
} else {
name = location.toString().toCharArray();
}
/*
* Note: these problems would result in translation errors on the
* server, so the severity is not meant to be controllable
*/
return new IJSPProblem() {
public void setSourceStart(int sourceStart) {
}
public void setSourceLineNumber(int lineNumber) {
}
public void setSourceEnd(int sourceEnd) {
}
public boolean isWarning() {
return false;
}
public boolean isInfo() {
return false;
}
public boolean isError() {
return true;
}
public int getSourceStart() {
return start;
}
public int getSourceLineNumber() {
return line;
}
public int getSourceEnd() {
return end;
}
public char[] getOriginatingFileName() {
return name;
}
public String getMessage() {
return MessageFormat.format(messageKey, new String[] { tagname, argument });
}
public int getID() {
return problemID;
}
public String[] getArguments() {
return new String[0];
}
public int getEID() {
return problemID;
}
};
}
use of org.eclipse.jst.jsp.core.internal.java.IJSPProblem in project webtools.sourceediting by eclipse.
the class JSPJavaValidator method createMessageFromProblem.
/**
* Creates an IMessage from asn IProblem
*
* @param problem
* @param f
* @param translation
* @param structuredDoc
* @return message representation of the problem, or null if it could not
* create one
*/
private IMessage createMessageFromProblem(IProblem problem, IFile f, IJSPTranslation translation, IStructuredDocument structuredDoc) {
int sev = -1;
int sourceStart = -1;
int sourceEnd = -1;
if (problem instanceof IJSPProblem) {
sourceStart = problem.getSourceStart();
sourceEnd = problem.getSourceEnd();
switch(((IJSPProblem) problem).getEID()) {
case IJSPProblem.TEIClassNotFound:
sev = getMessageSeverity(JSPCorePreferenceNames.VALIDATION_TRANSLATION_TEI_CLASS_NOT_FOUND);
break;
case IJSPProblem.TEIValidationMessage:
sev = getMessageSeverity(JSPCorePreferenceNames.VALIDATION_TRANSLATION_TEI_VALIDATION_MESSAGE);
break;
case IJSPProblem.TEIClassNotInstantiated:
sev = getMessageSeverity(JSPCorePreferenceNames.VALIDATION_TRANSLATION_TEI_CLASS_NOT_INSTANTIATED);
break;
case IJSPProblem.TEIClassMisc:
sev = getMessageSeverity(JSPCorePreferenceNames.VALIDATION_TRANSLATION_TEI_CLASS_RUNTIME_EXCEPTION);
break;
case IJSPProblem.TagClassNotFound:
sev = getMessageSeverity(JSPCorePreferenceNames.VALIDATION_TRANSLATION_TAG_HANDLER_CLASS_NOT_FOUND);
break;
case IJSPProblem.UseBeanInvalidID:
sev = getMessageSeverity(JSPCorePreferenceNames.VALIDATION_TRANSLATION_USEBEAN_INVALID_ID);
break;
case IJSPProblem.UseBeanMissingTypeInfo:
sev = getMessageSeverity(JSPCorePreferenceNames.VALIDATION_TRANSLATION_USBEAN_MISSING_TYPE_INFO);
break;
case IJSPProblem.UseBeanAmbiguousType:
sev = getMessageSeverity(JSPCorePreferenceNames.VALIDATION_TRANSLATION_USEBEAN_AMBIGUOUS_TYPE_INFO);
break;
default:
sev = problem.isError() ? IMessage.HIGH_SEVERITY : (problem.isWarning() ? IMessage.NORMAL_SEVERITY : ValidationMessage.IGNORE);
}
} else {
sourceStart = translation.getJspOffset(problem.getSourceStart());
sourceEnd = translation.getJspOffset(problem.getSourceEnd());
switch(problem.getID()) {
case IProblem.LocalVariableIsNeverUsed:
{
sev = getSourceSeverity(JSPCorePreferenceNames.VALIDATION_JAVA_LOCAL_VARIABLE_NEVER_USED, sourceStart, sourceEnd);
}
break;
case IProblem.NullLocalVariableReference:
{
sev = getSourceSeverity(JSPCorePreferenceNames.VALIDATION_JAVA_NULL_LOCAL_VARIABLE_REFERENCE, sourceStart, sourceEnd);
}
break;
case IProblem.ArgumentIsNeverUsed:
{
sev = getSourceSeverity(JSPCorePreferenceNames.VALIDATION_JAVA_ARGUMENT_IS_NEVER_USED, sourceStart, sourceEnd);
}
break;
case IProblem.PotentialNullLocalVariableReference:
{
sev = getSourceSeverity(JSPCorePreferenceNames.VALIDATION_JAVA_POTENTIAL_NULL_LOCAL_VARIABLE_REFERENCE, sourceStart, sourceEnd);
}
break;
case IProblem.UnusedImport:
{
sev = getSourceSeverity(JSPCorePreferenceNames.VALIDATION_JAVA_UNUSED_IMPORT, sourceStart, sourceEnd);
}
break;
case IProblem.UnusedPrivateField:
case IProblem.MissingSerialVersion:
{
// JSP files don't get serialized...right?
sev = ValidationMessage.IGNORE;
}
break;
case IProblem.UnresolvedVariable:
{
try {
// If the unresolved variable is in a fragment, post as a warning. The fragment may be included in a page that has declared the variable
IContentType contentType = f.getContentDescription().getContentType();
IContentType fragmentType = Platform.getContentTypeManager().getContentType(ContentTypeIdForJSP.ContentTypeID_JSPFRAGMENT);
if (contentType != null && fragmentType != null && contentType.isKindOf(fragmentType)) {
sev = IMessage.NORMAL_SEVERITY;
break;
}
}// Doesn't matter, we'll just fall back to the default
catch (CoreException e) {
}
}
default:
{
if (problem.isError()) {
sev = IMessage.HIGH_SEVERITY;
} else if (problem.isWarning()) {
sev = IMessage.NORMAL_SEVERITY;
} else {
sev = IMessage.LOW_SEVERITY;
}
}
if (sev == ValidationMessage.IGNORE) {
return null;
}
/* problems without JSP positions are in generated code */
if (sourceStart == -1) {
int problemID = problem.getID();
/*
* Quoting IProblem doc: "When a problem is tagged as
* Internal, it means that no change other than a
* local source code change can fix the corresponding
* problem." Assuming that our generated code is
* correct, that should reduce the reported problems
* to those the user can correct.
*/
if (((problemID & IProblem.Internal) != 0) && ((problemID & IProblem.Syntax) != 0) && translation instanceof JSPTranslation) {
// Attach to the last code scripting section
JSPTranslation jspTranslation = ((JSPTranslation) translation);
Position[] jspPositions = (Position[]) jspTranslation.getJsp2JavaMap().keySet().toArray(new Position[jspTranslation.getJsp2JavaMap().size()]);
for (int i = 0; i < jspPositions.length; i++) {
sourceStart = Math.max(sourceStart, jspPositions[i].getOffset());
}
IMessage m = new LocalizedMessage(sev, problem.getMessage(), f);
m.setOffset(sourceStart);
m.setLength(1);
return m;
} else {
return null;
}
}
}
}
if (sev == ValidationMessage.IGNORE) {
return null;
}
final boolean isIndirect = translation.isIndirect(problem.getSourceStart());
if (isIndirect && !FragmentValidationTools.shouldValidateFragment(f)) {
return null;
}
// line number for marker starts @ 1
// line number from document starts @ 0
int lineNo = structuredDoc.getLineOfOffset(sourceStart) + 1;
IMessage m = new LocalizedMessage(sev, problem.getMessage(), f);
m.setLineNo(lineNo);
m.setOffset(sourceStart);
m.setLength((sourceEnd >= sourceStart) ? (sourceEnd - sourceStart + 1) : 0);
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=119633
if (isIndirect) {
adjustIndirectPosition(m, translation);
}
return m;
}
use of org.eclipse.jst.jsp.core.internal.java.IJSPProblem in project webtools.sourceediting by eclipse.
the class JSPJavaTranslatorCoreTest method test_389174.
public void test_389174() throws CoreException, IOException {
IProject j = BundleResourceUtil.createJavaWebProject(getName());
assertTrue(j.exists());
String typeName = "List<List<Boolean>>";
InputStream source = new ByteArrayInputStream(("<jsp:useBean id=\"x\" type=\"" + typeName + "\" />").getBytes());
IFile file = j.getFile(getName() + "_test.jsp");
file.create(source, IResource.FORCE, null);
JSPTranslator translator = new JSPTranslator();
IDOMModel structuredModel = null;
try {
structuredModel = (IDOMModel) StructuredModelManager.getModelManager().getModelForRead(file);
translator.reset(structuredModel.getDocument(), new NullProgressMonitor());
translator.translate();
assertTrue("specified type did not survive translation", translator.getTranslation().indexOf(typeName) >= 0);
IJSPProblem[] translationProblems = (IJSPProblem[]) translator.getTranslationProblems().toArray(new IJSPProblem[0]);
for (int i = 0; i < translationProblems.length; i++) {
assertEquals("expected IJSPProblem type: " + translationProblems[i].getMessage(), Integer.toHexString(IProblem.UndefinedType), Integer.toHexString(translationProblems[i].getID()));
}
} finally {
if (structuredModel != null)
structuredModel.releaseFromRead();
}
}
Aggregations