use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument in project webtools.sourceediting by eclipse.
the class JsFindOccurrencesProcessor method getJavaElementsForCurrentSelection.
/**
* uses JSPTranslation to get currently selected Java elements.
*
* @return currently selected IJavaElements
*/
private IJavaScriptElement[] getJavaElementsForCurrentSelection(IDocument document, ITextSelection selection) {
IJavaScriptElement[] elements = new IJavaScriptElement[0];
// get JSP translation object for this viewer's document
IStructuredModel model = null;
try {
model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
if (model != null && model instanceof IDOMModel) {
IDOMDocument xmlDoc = ((IDOMModel) model).getDocument();
JsTranslationAdapter adapter = (JsTranslationAdapter) xmlDoc.getAdapterFor(IJsTranslation.class);
if (adapter != null) {
IJsTranslation translation = adapter.getJsTranslation(false);
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=102211
elements = translation.getElementsFromJsRange(translation.getJavaScriptOffset(selection.getOffset()), translation.getJavaScriptOffset(selection.getOffset() + selection.getLength()));
}
}
} finally {
if (model != null) {
model.releaseFromRead();
}
}
return elements;
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument in project webtools.sourceediting by eclipse.
the class JsSearchDocument method getJSTranslation.
/**
* It's not recommended for clients to hold on to this JSPTranslation
* since it's kind of large. If possible, hold on to the
* JSPSearchDocument, which is more of a lightweight proxy.
*
* @return the JSPTranslation for the jsp file, or null if it's an
* unsupported file.
*/
public final IJsTranslation getJSTranslation() {
IJsTranslation translation = null;
IFile jspFile = getFile();
if (!JsSearchSupport.isJsp(jspFile)) {
return translation;
}
IStructuredModel model = null;
try {
// get existing model for read, then get document from it
IModelManager modelManager = getModelManager();
if (modelManager != null) {
model = modelManager.getModelForRead(jspFile);
}
// handle unsupported
if (model instanceof IDOMModel) {
IDOMModel xmlModel = (IDOMModel) model;
JsTranslationAdapterFactory.setupAdapterFactory(xmlModel);
IDOMDocument doc = xmlModel.getDocument();
JsTranslationAdapter adapter = (JsTranslationAdapter) doc.getAdapterFor(IJsTranslation.class);
translation = adapter.getJsTranslation(false);
}
} catch (IOException e) {
Logger.logException(e);
} catch (CoreException e) {
Logger.logException(e);
} catch (UnsupportedCharsetExceptionWithDetail e) {
// no need to log this. Just consider it an invalid file for our
// purposes.
// Logger.logException(e);
} finally {
if (model != null) {
model.releaseFromRead();
}
}
return translation;
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument in project webtools.sourceediting by eclipse.
the class AttrValueTest method testModel.
/* (non-Javadoc)
* @see org.eclipse.wst.html.core.tests.parser.ModelTest#testModel()
*/
public void testModel() {
IDOMModel model = createHTMLModel();
try {
assertNotNull(model);
IStructuredDocument document = model.getStructuredDocument();
assertNotNull(document);
document.setText(this, "<button value=\"" + VALUES[0] + "\"></button><button value=\"" + VALUES[1] + "\"></button><button value=\"" + VALUES[2] + "\"></button>");
IDOMDocument dom = model.getDocument();
NodeList nodes = dom.getElementsByTagName("button");
assertTrue("Must be 3 button elements in the document.", nodes.getLength() == 3);
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
Node attr = node.getAttributes().getNamedItem("value");
assertTrue("Attribute 'value' not present.", attr != null && attr.getNodeValue().length() > 0);
assertEquals("Attribute values are not equal", VALUES[i], attr.getNodeValue());
}
} finally {
if (model != null) {
model.releaseFromEdit();
}
}
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument in project webtools.sourceediting by eclipse.
the class JSPJavaValidator method performValidation.
void performValidation(IFile f, IReporter reporter, IStructuredModel model) {
for (int i = 0; i < DEPEND_ONs.length; i++) {
addDependsOn(f.getProject().getFile(DEPEND_ONs[i]));
}
if (model instanceof IDOMModel) {
IDOMModel domModel = (IDOMModel) model;
ModelHandlerForJSP.ensureTranslationAdapterFactory(domModel);
IDOMDocument xmlDoc = domModel.getDocument();
JSPTranslationAdapter translationAdapter = (JSPTranslationAdapter) xmlDoc.getAdapterFor(IJSPTranslation.class);
IJSPTranslation translation = translationAdapter.getJSPTranslation();
if (!reporter.isCancelled()) {
loadPreferences(f);
// only update task markers if the model is the same as what's on disk
boolean updateJavaTasks = UPDATE_JAVA_TASKS && !domModel.isDirty() && f != null && f.isAccessible();
if (updateJavaTasks) {
// remove old Java task markers
try {
IMarker[] foundMarkers = f.findMarkers(JAVA_TASK_MARKER_TYPE, true, IResource.DEPTH_ONE);
for (int i = 0; i < foundMarkers.length; i++) {
foundMarkers[i].delete();
}
} catch (CoreException e) {
Logger.logException(e);
}
}
translation.setProblemCollectingActive(true);
translation.reconcileCompilationUnit();
List problems = translation.getProblems();
// add new messages
for (int i = 0; i < problems.size() && !reporter.isCancelled(); i++) {
IProblem problem = (IProblem) problems.get(i);
/*
* Possible error in problem collection; EL translation is
* extensible, so we must be paranoid about this.
*/
if (problem == null)
continue;
IMessage m = createMessageFromProblem(problem, f, translation, domModel.getStructuredDocument());
if (m != null) {
if (problem.getID() == IProblem.Task) {
if (updateJavaTasks) {
// add new Java task marker
try {
IMarker task = f.createMarker(JAVA_TASK_MARKER_TYPE);
task.setAttribute(IMarker.LINE_NUMBER, new Integer(m.getLineNumber()));
task.setAttribute(IMarker.CHAR_START, new Integer(m.getOffset()));
task.setAttribute(IMarker.CHAR_END, new Integer(m.getOffset() + m.getLength()));
task.setAttribute(IMarker.MESSAGE, m.getText());
task.setAttribute(IMarker.USER_EDITABLE, Boolean.FALSE);
switch(m.getSeverity()) {
case IMessage.HIGH_SEVERITY:
{
task.setAttribute(IMarker.PRIORITY, new Integer(IMarker.PRIORITY_HIGH));
task.setAttribute(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
}
break;
case IMessage.LOW_SEVERITY:
{
task.setAttribute(IMarker.PRIORITY, new Integer(IMarker.PRIORITY_LOW));
task.setAttribute(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
}
break;
default:
{
task.setAttribute(IMarker.PRIORITY, new Integer(IMarker.PRIORITY_NORMAL));
task.setAttribute(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
}
}
} catch (CoreException e) {
Logger.logException(e);
}
}
} else {
reporter.addMessage(fMessageOriginator, m);
}
}
}
}
}
unloadPreferences();
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument in project webtools.sourceediting by eclipse.
the class HTMLModelParserAdapterFactory method adapt.
/**
* Method that returns the adapter associated with the given object. It
* may be a singleton or not ... depending on the needs of the
* INodeAdapter ... but in general it is recommended for an adapter to be
* stateless, so the efficiencies of a singleton can be gained.
*
* The implementation of this method should call addAdapter on the adapted
* object with the correct instance of the adapter.
*/
public INodeAdapter adapt(INodeNotifier notifier) {
INodeAdapter adapter = null;
if (notifier != null) {
if (notifier instanceof IDOMDocument) {
adapter = notifier.getExistingAdapter(ModelParserAdapter.class);
if (adapter == null) {
adapter = new HTMLModelParserAdapter();
notifier.addAdapter(adapter);
}
}
}
return adapter;
}
Aggregations