Search in sources :

Example 36 with IDOMDocument

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument in project webtools.sourceediting by eclipse.

the class JsValidator method performValidation.

void performValidation(IFile f, IReporter reporter, IStructuredModel model, boolean inBatch) {
    if (model instanceof IDOMModel) {
        IDOMModel domModel = (IDOMModel) model;
        JsTranslationAdapterFactory.setupAdapterFactory(domModel);
        IDOMDocument xmlDoc = domModel.getDocument();
        JsTranslationAdapter translationAdapter = (JsTranslationAdapter) xmlDoc.getAdapterFor(IJsTranslation.class);
        // translationAdapter.resourceChanged();
        IJsTranslation translation = translationAdapter.getJsTranslation(false);
        if (!reporter.isCancelled()) {
            translation.setProblemCollectingActive(true);
            translation.reconcileCompilationUnit();
            List problems = translation.getProblems();
            // only update task markers if the model is the same as what's on disk
            boolean updateTasks = !domModel.isDirty() && f != null && f.isAccessible();
            if (updateTasks) {
                // remove old JavaScript task markers
                try {
                    IMarker[] foundMarkers = f.findMarkers(JAVASCRIPT_TASK_MARKER_TYPE, true, IResource.DEPTH_ONE);
                    for (int i = 0; i < foundMarkers.length; i++) {
                        foundMarkers[i].delete();
                    }
                } catch (CoreException e) {
                    Logger.logException(e);
                }
            }
            // add new messages
            for (int i = 0; i < problems.size() && !reporter.isCancelled(); i++) {
                IProblem problem = (IProblem) problems.get(i);
                IMessage m = createMessageFromProblem(problem, f, translation, domModel.getStructuredDocument());
                if (m != null) {
                    if (problem.getID() == IProblem.Task) {
                        if (updateTasks) {
                            // add new JavaScript task marker
                            try {
                                IMarker task = f.createMarker(JAVASCRIPT_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);
                    }
                }
            }
        }
    }
}
Also used : IJsTranslation(org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation) 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) JsTranslationAdapter(org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter) IProblem(org.eclipse.wst.jsdt.core.compiler.IProblem) CoreException(org.eclipse.core.runtime.CoreException) ArrayList(java.util.ArrayList) List(java.util.List) IMarker(org.eclipse.core.resources.IMarker)

Example 37 with IDOMDocument

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument in project webtools.sourceediting by eclipse.

the class CleanupActionHTMLDelegate method isXHTML.

private boolean isXHTML() {
    boolean isxhtml = false;
    if (fEditor instanceof ITextEditor) {
        ITextEditor textEditor = (ITextEditor) fEditor;
        IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
        IStructuredModel model = null;
        try {
            model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
            if (model instanceof IDOMModel) {
                IDOMDocument domDocument = ((IDOMModel) model).getDocument();
                if (domDocument != null)
                    isxhtml = domDocument.isXMLType();
            }
        } finally {
            if (model != null) {
                model.releaseFromRead();
            }
        }
    }
    return isxhtml;
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IDocument(org.eclipse.jface.text.IDocument)

Example 38 with IDOMDocument

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument in project webtools.sourceediting by eclipse.

the class XMLAssociationProvider method checkExternalSchema.

protected CMElementDeclaration checkExternalSchema(Element element) {
    final Document document = element.getOwnerDocument();
    if (document instanceof IDOMDocument) {
        final String baseLocation = ((IDOMDocument) document).getModel().getBaseLocation();
        if (baseLocation != null) {
            final IPath basePath = new Path(baseLocation);
            IFile file = null;
            if (basePath.segmentCount() > 1) {
                file = ResourcesPlugin.getWorkspace().getRoot().getFile(basePath);
            }
            final URI uri = (file == null || !file.isAccessible()) ? new File(baseLocation).toURI() : file.getLocationURI();
            if (uri != null) {
                IExternalSchemaLocationProvider[] providers = ExternalSchemaLocationProviderRegistry.getInstance().getProviders();
                for (int i = 0; i < providers.length; i++) {
                    long time = _trace ? System.currentTimeMillis() : 0;
                    final Map locations = providers[i].getExternalSchemaLocation(uri);
                    if (_trace) {
                        long diff = System.currentTimeMillis() - time;
                        if (diff > 250)
                            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                            Logger.log(Logger.INFO, "Schema location provider took [" + diff + "ms] for URI [" + uri + "]");
                    }
                    if (locations != null && !locations.isEmpty()) {
                        Object location = locations.get(IExternalSchemaLocationProvider.NO_NAMESPACE_SCHEMA_LOCATION);
                        if (location != null)
                            return getCMElementDeclaration(element, NamespaceTable.getElementLineage(element), uri.toString(), location.toString());
                    }
                }
            }
        }
    }
    return null;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) Document(org.w3c.dom.Document) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) IExternalSchemaLocationProvider(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.IExternalSchemaLocationProvider) URI(java.net.URI) IFile(org.eclipse.core.resources.IFile) File(java.io.File) Map(java.util.Map)

Example 39 with IDOMDocument

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument in project webtools.sourceediting by eclipse.

the class AutoImportProposal method getInsertPosition.

/**
 * @param doc
 * @param isXml
 * @return position after <jsp:root> if xml, otherwise right before the document element
 */
private int getInsertPosition(IDocument doc, boolean isXml) {
    int pos = 0;
    IStructuredModel sModel = StructuredModelManager.getModelManager().getExistingModelForRead(doc);
    try {
        if (sModel != null) {
            if (sModel instanceof IDOMModel) {
                IDOMDocument documentNode = ((IDOMModel) sModel).getDocument();
                /*
					 * document element must be sole Element child of Document
					 * to remain valid
					 */
                Node targetElement = null;
                if (isXml) {
                    targetElement = documentNode.getDocumentElement();
                }
                if (targetElement == null)
                    targetElement = getInsertNode(documentNode);
                if (targetElement != null) {
                    IStructuredDocumentRegion sdRegion = ((IDOMNode) targetElement).getFirstStructuredDocumentRegion();
                    if (isXml) {
                        /*
							 * document Element must be sole Element child of
							 * Document to remain valid, so insert after
							 */
                        pos = sdRegion.getEndOffset();
                        try {
                            while (pos < doc.getLength() && (doc.getChar(pos) == '\r' || doc.getChar(pos) == '\n')) {
                                pos++;
                            }
                        } catch (BadLocationException e) {
                        // not important, use pos as determined earlier
                        }
                    } else {
                        // insert before target element
                        pos = sdRegion.getStartOffset();
                    }
                } else {
                    pos = 0;
                }
            }
        }
    } finally {
        if (sModel != null)
            sModel.releaseFromRead();
    }
    return pos;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 40 with IDOMDocument

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument in project webtools.sourceediting by eclipse.

the class AutoImportProposal method isXmlFormat.

/**
 * @param doc
 * @return true if this document is xml-jsp syntax, otherwise false
 */
private boolean isXmlFormat(IDocument doc) {
    boolean isXml = false;
    IStructuredModel sModel = StructuredModelManager.getModelManager().getExistingModelForRead(doc);
    try {
        if (sModel != null) {
            if (!isXml) {
                if (sModel instanceof IDOMModel) {
                    IDOMDocument documentNode = ((IDOMModel) sModel).getDocument();
                    Element docElement = documentNode.getDocumentElement();
                    // $NON-NLS-1$  //$NON-NLS-2$
                    isXml = docElement != null && ((docElement.getNodeName().equals("jsp:root")) || docElement.getAttributeNode("xmlns:jsp") != null || ((((IDOMNode) docElement).getStartStructuredDocumentRegion() == null && ((IDOMNode) docElement).getEndStructuredDocumentRegion() == null)));
                }
            }
        }
    } finally {
        if (sModel != null)
            sModel.releaseFromRead();
    }
    return isXml;
}
Also used : IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) Element(org.w3c.dom.Element) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)

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