Search in sources :

Example 61 with IDOMModel

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;
}
Also used : IJsTranslation(org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation) CoreException(org.eclipse.core.runtime.CoreException) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) JsTranslationAdapter(org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter) IJavaScriptUnit(org.eclipse.wst.jsdt.core.IJavaScriptUnit) CoreException(org.eclipse.core.runtime.CoreException)

Example 62 with IDOMModel

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;
}
Also used : IJsTranslation(org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IJavaScriptElement(org.eclipse.wst.jsdt.core.IJavaScriptElement) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) JsTranslationAdapter(org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter)

Example 63 with IDOMModel

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;
}
Also used : IJsTranslation(org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation) IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) UnsupportedCharsetExceptionWithDetail(org.eclipse.wst.sse.core.internal.exceptions.UnsupportedCharsetExceptionWithDetail) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) JsTranslationAdapter(org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter) IOException(java.io.IOException)

Example 64 with IDOMModel

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();
        }
    }
}
Also used : DocumentCSS(org.w3c.dom.css.DocumentCSS) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document)

Example 65 with IDOMModel

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();
    }
}
Also used : IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) Element(org.w3c.dom.Element) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) Document(org.w3c.dom.Document) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) Attr(org.w3c.dom.Attr)

Aggregations

IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)433 Document (org.w3c.dom.Document)123 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)120 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)110 Element (org.w3c.dom.Element)109 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)103 Node (org.w3c.dom.Node)57 IFile (org.eclipse.core.resources.IFile)56 NodeList (org.w3c.dom.NodeList)47 IModelManager (org.eclipse.wst.sse.core.internal.provisional.IModelManager)45 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)44 IJsTranslation (org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation)41 JsTranslationAdapter (org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter)41 Text (org.w3c.dom.Text)39 INodeNotifier (org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)35 IJSPTranslation (org.eclipse.jst.jsp.core.internal.java.IJSPTranslation)28 IOException (java.io.IOException)26 CoreException (org.eclipse.core.runtime.CoreException)26 JSPTranslationAdapter (org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter)26 INodeAdapter (org.eclipse.wst.sse.core.internal.provisional.INodeAdapter)25