Search in sources :

Example 46 with INodeNotifier

use of org.eclipse.wst.sse.core.internal.provisional.INodeNotifier in project webtools.sourceediting by eclipse.

the class HTMLSourceValidator method validate.

/**
 * This validate call is for the ISourceValidator partial document validation approach
 *
 * @param dirtyRegion
 * @param helper
 * @param reporter
 * @see org.eclipse.wst.sse.ui.internal.reconcile.validator.ISourceValidator
 */
public void validate(IRegion dirtyRegion, IValidationContext helper, IReporter reporter) {
    if (helper == null || fDocument == null)
        return;
    if ((reporter != null) && (reporter.isCancelled() == true)) {
        throw new OperationCanceledException();
    }
    IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForRead(fDocument);
    if (model == null)
        // error
        return;
    try {
        IDOMDocument document = null;
        if (model instanceof IDOMModel) {
            document = ((IDOMModel) model).getDocument();
        }
        if (document == null || !hasHTMLFeature(document)) {
            // ignore
            return;
        }
        IPath filePath = null;
        IFile file = null;
        ITextFileBuffer fb = FileBufferModelManager.getInstance().getBuffer(fDocument);
        if (fb != null) {
            filePath = fb.getLocation();
            if (filePath.segmentCount() > 1) {
                file = ResourcesPlugin.getWorkspace().getRoot().getFile(filePath);
                if (!file.isAccessible()) {
                    file = null;
                }
            }
        } else {
            filePath = new Path(model.getId());
        }
        // this will be the wrong region if it's Text (instead of Element)
        // we don't know how to validate Text
        // model.getIndexedRegion(dirtyRegion.getOffset());
        IndexedRegion ir = getCoveringNode(dirtyRegion);
        if (ir instanceof Text) {
            while (ir != null && ir instanceof Text) {
                // it's assumed that this gets the IndexedRegion to
                // the right of the end offset
                ir = model.getIndexedRegion(ir.getEndOffset());
            }
        }
        if (ir instanceof INodeNotifier) {
            INodeAdapterFactory factory = HTMLValidationAdapterFactory.getInstance();
            ValidationAdapter adapter = (ValidationAdapter) factory.adapt((INodeNotifier) ir);
            if (adapter == null)
                // error
                return;
            if (reporter != null) {
                HTMLValidationReporter rep = null;
                rep = getReporter(reporter, file, (IDOMModel) model);
                rep.clear();
                adapter.setReporter(rep);
                Message mess = new LocalizedMessage(IMessage.LOW_SEVERITY, filePath.toString().substring(1));
                reporter.displaySubtask(this, mess);
            }
            adapter.validate(ir);
        }
    } finally {
        releaseModel(model);
    }
}
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) LocalizedMessage(org.eclipse.wst.html.core.internal.validation.LocalizedMessage) IMessage(org.eclipse.wst.validation.internal.provisional.core.IMessage) Message(org.eclipse.wst.validation.internal.core.Message) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) Text(org.w3c.dom.Text) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) INodeAdapterFactory(org.eclipse.wst.sse.core.internal.provisional.INodeAdapterFactory) HTMLValidationReporter(org.eclipse.wst.html.core.internal.validation.HTMLValidationReporter) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) ValidationAdapter(org.eclipse.wst.sse.core.internal.validate.ValidationAdapter) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) INodeNotifier(org.eclipse.wst.sse.core.internal.provisional.INodeNotifier) LocalizedMessage(org.eclipse.wst.html.core.internal.validation.LocalizedMessage)

Example 47 with INodeNotifier

use of org.eclipse.wst.sse.core.internal.provisional.INodeNotifier in project webtools.sourceediting by eclipse.

the class HTMLTagsCompletionProposalComputer method isXHTMLNode.

/**
 * Determine if this Document is an XHTML Document. Operates solely off of
 * the Document Type declaration
 */
private static boolean isXHTMLNode(Node node) {
    if (node == null) {
        return false;
    }
    Document doc = null;
    if (node.getNodeType() != Node.DOCUMENT_NODE)
        doc = node.getOwnerDocument();
    else
        doc = ((Document) node);
    if (doc instanceof IDOMDocument) {
        return ((IDOMDocument) doc).isXMLType();
    }
    if (doc instanceof INodeNotifier) {
        ModelQueryAdapter adapter = (ModelQueryAdapter) ((INodeNotifier) doc).getAdapterFor(ModelQueryAdapter.class);
        CMDocument cmdoc = null;
        if (adapter != null && adapter.getModelQuery() != null)
            cmdoc = adapter.getModelQuery().getCorrespondingCMDocument(doc);
        if (cmdoc != null) {
            // model
            if (cmdoc instanceof HTMLCMDocument)
                return false;
            if (cmdoc.supports(HTMLCMProperties.IS_XHTML))
                return Boolean.TRUE.equals(cmdoc.getProperty(HTMLCMProperties.IS_XHTML));
        }
    }
    // this should never be reached
    DocumentType docType = doc.getDoctype();
    // $NON-NLS-1$
    return docType != null && docType.getPublicId() != null && docType.getPublicId().indexOf("-//W3C//DTD XHTML ") == 0;
}
Also used : HTMLCMDocument(org.eclipse.wst.html.core.internal.contentmodel.HTMLCMDocument) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) ModelQueryAdapter(org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter) HTMLCMDocument(org.eclipse.wst.html.core.internal.contentmodel.HTMLCMDocument) DocumentType(org.w3c.dom.DocumentType) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) HTMLCMDocument(org.eclipse.wst.html.core.internal.contentmodel.HTMLCMDocument) Document(org.w3c.dom.Document) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) INodeNotifier(org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)

Example 48 with INodeNotifier

use of org.eclipse.wst.sse.core.internal.provisional.INodeNotifier in project webtools.sourceediting by eclipse.

the class XMLModelLoader method preLoadAdapt.

protected void preLoadAdapt(IStructuredModel structuredModel) {
    super.preLoadAdapt(structuredModel);
    IDOMModel domModel = (IDOMModel) structuredModel;
    // if there is a model in the adapter, this will adapt it to
    // first node. After that the PropagatingAdater spreads over the
    // children being
    // created. Each time that happends, a side effect is to
    // also "spread" sprecific registered adapters,
    // they two can propigate is needed.
    ((INodeNotifier) domModel.getDocument()).getAdapterFor(PropagatingAdapter.class);
    if (Debug.debugNotificationAndEvents) {
        PropagatingAdapter propagatingAdapter = (PropagatingAdapter) ((INodeNotifier) domModel.getDocument()).getAdapterFor(PropagatingAdapter.class);
        propagatingAdapter.addAdaptOnCreateFactory(new DebugAdapterFactory());
    }
}
Also used : IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) DebugAdapterFactory(org.eclipse.wst.xml.core.internal.DebugAdapterFactory) PropagatingAdapter(org.eclipse.wst.sse.core.internal.PropagatingAdapter) INodeNotifier(org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)

Example 49 with INodeNotifier

use of org.eclipse.wst.sse.core.internal.provisional.INodeNotifier in project webtools.sourceediting by eclipse.

the class AbstractCSSSourceFormatter method formatChildren.

/**
 */
protected final void formatChildren(ICSSNode node, StringBuffer source) {
    ICSSNode child = node.getFirstChild();
    ICSSNode last = null;
    boolean first = true;
    while (child != null) {
        // append child
        CSSSourceFormatter formatter = (CSSSourceFormatter) ((INodeNotifier) child).getAdapterFor(CSSSourceFormatter.class);
        if (formatter == null) {
            formatter = CSSSourceFormatterFactory.getInstance().getSourceFormatter((INodeNotifier) child);
        }
        StringBuffer childSource = ((AbstractCSSSourceFormatter) formatter).formatProc(child);
        if (!first) {
            formatBefore(node, child, new String(childSource), source, null);
        }
        source.append(childSource);
        last = child;
        // append between children
        child = child.getNextSibling();
        first = false;
    }
    // This handles the case where the last child doesn't align with the end of the parent node, likely malformed content
    if (node instanceof IndexedRegion && last instanceof IndexedRegion && (node.getOwnerDocument().getNodeType() == ICSSNode.STYLEDECLARATION_NODE)) {
        IndexedRegion parent = (IndexedRegion) node;
        IndexedRegion lastChild = (IndexedRegion) last;
        if (lastChild.getEndOffset() < parent.getEndOffset()) {
            // Find the region at the end offset of the last child
            IStructuredDocumentRegion region = node.getOwnerDocument().getModel().getStructuredDocument().getRegionAtCharacterOffset(lastChild.getEndOffset());
            ITextRegionList regions = region != null ? region.getRegions() : null;
            if (regions != null) {
                Iterator it = regions.iterator();
                while (it.hasNext()) {
                    ITextRegion token = (ITextRegion) it.next();
                    if (token.getType() == CSSRegionContexts.CSS_UNKNOWN) {
                        // Found something that won't be consumed. Append the regions that remain in the node to the source
                        do {
                            source.append(region.getFullText());
                        } while ((region = region.getNext()) != null && region.getEndOffset() <= parent.getEndOffset());
                        break;
                    }
                }
            }
        }
    }
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) RegionIterator(org.eclipse.wst.css.core.internal.util.RegionIterator) Iterator(java.util.Iterator) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) INodeNotifier(org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)

Example 50 with INodeNotifier

use of org.eclipse.wst.sse.core.internal.provisional.INodeNotifier in project webtools.sourceediting by eclipse.

the class FormatProcessorCSS method formatModel.

public void formatModel(IStructuredModel structuredModel, int start, int length) {
    CSSFormatUtil formatUtil = CSSFormatUtil.getInstance();
    if (structuredModel instanceof ICSSModel) {
        // BUG102822 take advantage of IDocumentExtension4
        IDocumentExtension4 docExt4 = null;
        if (structuredModel.getStructuredDocument() instanceof IDocumentExtension4) {
            docExt4 = (IDocumentExtension4) structuredModel.getStructuredDocument();
        }
        DocumentRewriteSession rewriteSession = null;
        try {
            DocumentRewriteSessionType rewriteType = (length > MAX_SMALL_FORMAT_SIZE) ? DocumentRewriteSessionType.UNRESTRICTED : DocumentRewriteSessionType.UNRESTRICTED_SMALL;
            rewriteSession = (docExt4 == null || docExt4.getActiveRewriteSession() != null) ? null : docExt4.startRewriteSession(rewriteType);
            ICSSDocument doc = ((ICSSModel) structuredModel).getDocument();
            IStructuredDocumentRegion startRegion = structuredModel.getStructuredDocument().getRegionAtCharacterOffset(start);
            IStructuredDocumentRegion endRegion = structuredModel.getStructuredDocument().getRegionAtCharacterOffset(start + length);
            if (startRegion != null && endRegion != null) {
                start = startRegion.getStart();
                CSSSourceFormatter formatter = CSSSourceFormatterFactory.getInstance().getSourceFormatter((INodeNotifier) doc);
                StringBuffer buf = formatter.format(doc, new Region(start, (endRegion.getEnd() - start)));
                if (buf != null) {
                    formatUtil.replaceSource(doc.getModel(), start, endRegion.getEnd() - start, buf.toString());
                }
            }
        } finally {
            // BUG102822 take advantage of IDocumentExtension4
            if (docExt4 != null && rewriteSession != null)
                docExt4.stopRewriteSession(rewriteSession);
        }
    } else if (structuredModel instanceof IDOMModel) {
        List cssnodes = formatUtil.collectCSSNodes(structuredModel, start, length);
        if (cssnodes != null && !cssnodes.isEmpty()) {
            ICSSModel model = null;
            // BUG102822 take advantage of IDocumentExtension4
            IDocumentExtension4 docExt4 = null;
            if (structuredModel.getStructuredDocument() instanceof IDocumentExtension4) {
                docExt4 = (IDocumentExtension4) structuredModel.getStructuredDocument();
            }
            DocumentRewriteSession rewriteSession = null;
            try {
                DocumentRewriteSessionType rewriteType = (length > MAX_SMALL_FORMAT_SIZE) ? DocumentRewriteSessionType.UNRESTRICTED : DocumentRewriteSessionType.UNRESTRICTED_SMALL;
                rewriteSession = (docExt4 == null || docExt4.getActiveRewriteSession() != null) ? null : docExt4.startRewriteSession(rewriteType);
                for (int i = 0; i < cssnodes.size(); i++) {
                    ICSSNode node = (ICSSNode) cssnodes.get(i);
                    CSSSourceFormatter formatter = CSSSourceFormatterFactory.getInstance().getSourceFormatter((INodeNotifier) node);
                    StringBuffer buf = formatter.format(node);
                    if (buf != null) {
                        int startOffset = ((IndexedRegion) node).getStartOffset();
                        int endOffset = ((IndexedRegion) node).getEndOffset();
                        if (model == null) {
                            model = node.getOwnerDocument().getModel();
                        }
                        formatUtil.replaceSource(model, startOffset, endOffset - startOffset, buf.toString());
                    }
                }
            } finally {
                // BUG102822 take advantage of IDocumentExtension4
                if (docExt4 != null && rewriteSession != null)
                    docExt4.stopRewriteSession(rewriteSession);
            }
        }
    }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) CSSSourceFormatter(org.eclipse.wst.css.core.internal.formatter.CSSSourceFormatter) ICSSModel(org.eclipse.wst.css.core.internal.provisional.document.ICSSModel) IDocumentExtension4(org.eclipse.jface.text.IDocumentExtension4) DocumentRewriteSessionType(org.eclipse.jface.text.DocumentRewriteSessionType) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) ICSSDocument(org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) CSSFormatUtil(org.eclipse.wst.css.core.internal.formatter.CSSFormatUtil) DocumentRewriteSession(org.eclipse.jface.text.DocumentRewriteSession) Region(org.eclipse.jface.text.Region) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) List(java.util.List) INodeNotifier(org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)

Aggregations

INodeNotifier (org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)71 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)35 INodeAdapter (org.eclipse.wst.sse.core.internal.provisional.INodeAdapter)31 Node (org.w3c.dom.Node)29 Document (org.w3c.dom.Document)26 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)24 NullInputStream (org.eclipse.wst.sse.core.internal.encoding.util.NullInputStream)18 RegionChangedEvent (org.eclipse.wst.sse.core.internal.provisional.events.RegionChangedEvent)18 StructuredDocumentEvent (org.eclipse.wst.sse.core.internal.provisional.events.StructuredDocumentEvent)18 ICSSNode (org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)14 Iterator (java.util.Iterator)9 List (java.util.List)8 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)8 ModelQueryAdapter (org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter)8 ArrayList (java.util.ArrayList)7 Collection (java.util.Collection)7 ICSSModel (org.eclipse.wst.css.core.internal.provisional.document.ICSSModel)7 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)7 Element (org.w3c.dom.Element)7 CSSSourceFormatter (org.eclipse.wst.css.core.internal.formatter.CSSSourceFormatter)6