Search in sources :

Example 51 with IDOMDocument

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;
}
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 52 with IDOMDocument

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;
}
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 53 with IDOMDocument

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

Example 54 with IDOMDocument

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();
}
Also used : IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IMessage(org.eclipse.wst.validation.internal.provisional.core.IMessage) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) JSPTranslationAdapter(org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter) IProblem(org.eclipse.jdt.core.compiler.IProblem) CoreException(org.eclipse.core.runtime.CoreException) List(java.util.List) IMarker(org.eclipse.core.resources.IMarker)

Example 55 with IDOMDocument

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;
}
Also used : ModelParserAdapter(org.eclipse.wst.xml.core.internal.document.ModelParserAdapter) INodeAdapter(org.eclipse.wst.sse.core.internal.provisional.INodeAdapter) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)

Aggregations

IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)176 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)109 IFile (org.eclipse.core.resources.IFile)48 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)39 Element (org.w3c.dom.Element)39 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)33 NodeList (org.w3c.dom.NodeList)27 IStatus (org.eclipse.core.runtime.IStatus)23 Node (org.w3c.dom.Node)21 CoreException (org.eclipse.core.runtime.CoreException)15 ArrayList (java.util.ArrayList)14 IJsTranslation (org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation)14 JsTranslationAdapter (org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter)14 Document (org.w3c.dom.Document)14 IJSPTranslation (org.eclipse.jst.jsp.core.internal.java.IJSPTranslation)12 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)12 IOException (java.io.IOException)11 CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)11 JSPTranslationAdapter (org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter)10 IModelManager (org.eclipse.wst.sse.core.internal.provisional.IModelManager)9