use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel in project webtools.sourceediting by eclipse.
the class JSDTContentAssistantProcessor method computeCompletionProposals.
/**
* Returns a list of completion proposals based on the specified location
* within the document that corresponds to the current cursor position
* within the text viewer.
*
* @param viewer
* the viewer whose document is used to compute the proposals
* @param documentPosition
* an offset within the document for which completions should be
* computed
* @return an array of completion proposals or <code>null</code> if no
* proposals are possible
*/
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int pos) {
initialize(pos);
JSDTProposalCollector collector = null;
IDOMModel xmlModel = null;
try {
fViewer = viewer;
xmlModel = (IDOMModel) StructuredModelManager.getModelManager().getExistingModelForRead(fViewer.getDocument());
IDOMDocument xmlDoc = xmlModel.getDocument();
JsTranslationAdapterFactory.setupAdapterFactory(xmlModel);
JsTranslationAdapter translationAdapter = (JsTranslationAdapter) xmlDoc.getAdapterFor(IJsTranslation.class);
if (translationAdapter != null) {
IJsTranslation translation = translationAdapter.getJsTranslation(true);
fJavaPosition = translation.getJavaScriptOffset(getDocumentPosition());
try {
IJavaScriptUnit cu = translation.getCompilationUnit();
// or without a valid position
if (cu == null || -1 == fJavaPosition) {
return new ICompletionProposal[0];
}
collector = getProposalCollector();
synchronized (cu) {
cu.codeComplete(fJavaPosition, collector, null);
}
} catch (CoreException coreEx) {
// a possible Java Model Exception due to not being a Web
// (Java) Project
coreEx.printStackTrace();
}
}
} catch (Exception exc) {
exc.printStackTrace();
// throw out exceptions on code assist.
} finally {
if (xmlModel != null) {
xmlModel.releaseFromRead();
}
}
ICompletionProposal[] results = new ICompletionProposal[0];
if (collector != null) {
results = collector.getJSPCompletionProposals();
if (results == null || results.length < 1) {
fErrorMessage = JsUIMessages.Java_Content_Assist_is_not_UI_;
}
}
return results;
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel 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.IDOMModel 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.IDOMModel in project webtools.sourceediting by eclipse.
the class GetOverrideStyleTest method testModel.
public void testModel() {
IDOMModel model = FileUtil.createHTMLModel();
try {
model.getStructuredDocument().setText(null, "<style>p { border-color: blue; margin: 0; } </style> <p style=\"border-color: red; margin: 1;\">");
Document document = model.getDocument();
DocumentCSS ddd = (DocumentCSS) document;
NodeList n = document.getElementsByTagName("p");
ddd.getOverrideStyle((Element) n.item(0), "");
} finally {
if (model != null) {
model.releaseFromEdit();
}
}
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel in project webtools.sourceediting by eclipse.
the class AttrTest3 method testModel.
public void testModel() {
IDOMModel model = createXMLModel();
try {
IStructuredDocument structuredDocument = model.getStructuredDocument();
Document document = model.getDocument();
structuredDocument.setText(this, "<a xmlns='default-uri' xmlns:b='b-uri'><c d='d-value' b:e='e-value'/></a>");
printSource(model);
printTree(model);
Element a = (Element) document.getFirstChild();
Element c = (Element) a.getFirstChild();
Attr xmlns = a.getAttributeNode("xmlns");
fOutputWriter.writeln("xmlns [" + xmlns.getNamespaceURI() + "]");
Attr xmlns_b = a.getAttributeNode("xmlns:b");
fOutputWriter.writeln("xmlns:b [" + xmlns_b.getNamespaceURI() + "]");
Attr d = c.getAttributeNode("d");
fOutputWriter.writeln("d [" + d.getNamespaceURI() + "]");
Attr b_e = c.getAttributeNode("b:e");
fOutputWriter.writeln("b:e [" + b_e.getNamespaceURI() + "]");
saveAndCompareTestResults();
} finally {
model.releaseFromEdit();
}
}
Aggregations