Search in sources :

Example 96 with IDOMModel

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

the class JSPContentSourceValidator 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 || !fEnableSourceValidation)
        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;
        ITextFileBuffer fb = FileBufferModelManager.getInstance().getBuffer(fDocument);
        if (fb == null)
            return;
        IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(fb.getLocation());
        if (file == null || !file.exists())
            return;
        // 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, file.getFullPath().toString().substring(1));
                reporter.displaySubtask(this, mess);
            }
            adapter.validate(ir);
        }
    } finally {
        if (model != null)
            model.releaseFromRead();
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) 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) 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)

Example 97 with IDOMModel

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

the class CleanupProcessorCSS method cleanupModel.

public void cleanupModel(IStructuredModel structuredModel, int start, int length) {
    CSSFormatUtil formatUtil = CSSFormatUtil.getInstance();
    if (structuredModel instanceof ICSSModel) {
        ICSSDocument doc = ((ICSSModel) structuredModel).getDocument();
        CSSSourceFormatter formatter = CSSSourceFormatterFactory.getInstance().getSourceFormatter((INodeNotifier) doc);
        StringBuffer buf = formatter.cleanup(doc);
        if (buf != null) {
            int startOffset = ((IndexedRegion) doc).getStartOffset();
            int endOffset = ((IndexedRegion) doc).getEndOffset();
            formatUtil.replaceSource(doc.getModel(), startOffset, endOffset - startOffset, buf.toString());
        }
    } else if (structuredModel instanceof IDOMModel) {
        List cssnodes = formatUtil.collectCSSNodes(structuredModel, start, length);
        if (cssnodes != null && !cssnodes.isEmpty()) {
            ICSSModel model = null;
            for (int i = 0; i < cssnodes.size(); i++) {
                ICSSNode node = (ICSSNode) cssnodes.get(i);
                CSSSourceFormatter formatter = CSSSourceFormatterFactory.getInstance().getSourceFormatter((INodeNotifier) node);
                StringBuffer buf = formatter.cleanup(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());
                }
            }
        }
    }
}
Also used : CSSSourceFormatter(org.eclipse.wst.css.core.internal.formatter.CSSSourceFormatter) CSSFormatUtil(org.eclipse.wst.css.core.internal.formatter.CSSFormatUtil) ICSSModel(org.eclipse.wst.css.core.internal.provisional.document.ICSSModel) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) List(java.util.List) ICSSDocument(org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument) 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 98 with IDOMModel

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

the class ElementNodeCleanupHandler method quoteAttrValue.

protected IDOMNode quoteAttrValue(IDOMNode node) {
    IDOMElement element = (IDOMElement) node;
    if (element.isCommentTag())
        // do nothing
        return node;
    boolean quoteAttrValues = getCleanupPreferences().getQuoteAttrValues();
    IDOMNode newNode = node;
    if (quoteAttrValues) {
        NamedNodeMap attributes = newNode.getAttributes();
        int attributesLength = attributes.getLength();
        ISourceGenerator generator = node.getModel().getGenerator();
        for (int i = 0; i < attributesLength; i++) {
            attributes = newNode.getAttributes();
            attributesLength = attributes.getLength();
            IDOMAttr eachAttr = (IDOMAttr) attributes.item(i);
            // ITextRegion oldAttrValueRegion = eachAttr.getValueRegion();
            String oldAttrValue = eachAttr.getValueRegionText();
            if (oldAttrValue == null) {
                IDOMModel structuredModel = node.getModel();
                if (isXMLType(structuredModel)) {
                    // TODO: Kit, please check. Is there any way to not
                    // rely on getting regions from attributes?
                    // $NON-NLS-1$ //$NON-NLS-2$
                    String newAttrValue = "=\"" + eachAttr.getNameRegionText() + "\"";
                    IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
                    replaceSource(structuredModel, structuredDocument, eachAttr.getNameRegionEndOffset(), 0, newAttrValue);
                    // save
                    newNode = (IDOMNode) structuredModel.getIndexedRegion(node.getStartOffset());
                // new
                // node
                }
            } else {
                char quote = StringUtils.isQuoted(oldAttrValue) ? oldAttrValue.charAt(0) : DOUBLE_QUOTE;
                String newAttrValue = generator.generateAttrValue(eachAttr, quote);
                // Workaround for now...
                if (oldAttrValue.length() == 1) {
                    char firstChar = oldAttrValue.charAt(0);
                    if (firstChar == SINGLE_QUOTE)
                        newAttrValue = SINGLE_QUOTES;
                    else if (firstChar == DOUBLE_QUOTE)
                        newAttrValue = DOUBLE_QUOTES;
                }
                if (newAttrValue != null) {
                    if (newAttrValue.compareTo(oldAttrValue) != 0) {
                        int attrValueStartOffset = eachAttr.getValueRegionStartOffset();
                        int attrValueLength = oldAttrValue.length();
                        int startTagStartOffset = node.getStartOffset();
                        IDOMModel structuredModel = node.getModel();
                        IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
                        replaceSource(structuredModel, structuredDocument, attrValueStartOffset, attrValueLength, newAttrValue);
                        // save
                        newNode = (IDOMNode) structuredModel.getIndexedRegion(startTagStartOffset);
                    // new
                    // node
                    }
                }
            }
        }
    }
    return newNode;
}
Also used : IDOMAttr(org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr) NamedNodeMap(org.w3c.dom.NamedNodeMap) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) ISourceGenerator(org.eclipse.wst.xml.core.internal.provisional.document.ISourceGenerator) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 99 with IDOMModel

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

the class ElementNodeCleanupHandler method compressEmptyElementTag.

/**
 * <p>Compress empty element tags if the prefence is set to do so</p>
 *
 * @copyof org.eclipse.wst.xml.core.internal.cleanup.ElementNodeCleanupHandler#compressEmptyElementTag
 *
 * @param node the {@link IDOMNode} to possible compress
 * @return the compressed node if the given node should be compressed, else the node as it was given
 */
private IDOMNode compressEmptyElementTag(IDOMNode node) {
    boolean compressEmptyElementTags = getCleanupPreferences().getCompressEmptyElementTags();
    IDOMNode newNode = node;
    IStructuredDocumentRegion startTagStructuredDocumentRegion = newNode.getFirstStructuredDocumentRegion();
    IStructuredDocumentRegion endTagStructuredDocumentRegion = newNode.getLastStructuredDocumentRegion();
    // only compress tags if they are empty
    if ((compressEmptyElementTags && startTagStructuredDocumentRegion != endTagStructuredDocumentRegion && startTagStructuredDocumentRegion != null)) {
        // only compress end tags if its XHTML or not a container
        if (isXMLTag((IDOMElement) newNode) || !newNode.isContainer()) {
            ITextRegionList regions = startTagStructuredDocumentRegion.getRegions();
            ITextRegion lastRegion = regions.get(regions.size() - 1);
            // format children and end tag if not empty element tag
            if (lastRegion.getType() != DOMRegionContext.XML_EMPTY_TAG_CLOSE) {
                NodeList childNodes = newNode.getChildNodes();
                if (childNodes == null || childNodes.getLength() == 0 || (childNodes.getLength() == 1 && (childNodes.item(0)).getNodeType() == Node.TEXT_NODE && ((childNodes.item(0)).getNodeValue().trim().length() == 0))) {
                    IDOMModel structuredModel = newNode.getModel();
                    IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
                    int startTagStartOffset = newNode.getStartOffset();
                    int offset = endTagStructuredDocumentRegion.getStart();
                    int length = endTagStructuredDocumentRegion.getLength();
                    // $NON-NLS-1$
                    structuredDocument.replaceText(structuredDocument, offset, length, "");
                    // save
                    newNode = (IDOMNode) structuredModel.getIndexedRegion(startTagStartOffset);
                    offset = startTagStructuredDocumentRegion.getStart() + lastRegion.getStart();
                    // $NON-NLS-1$
                    structuredDocument.replaceText(structuredDocument, offset, 0, "/");
                    // save
                    newNode = (IDOMNode) structuredModel.getIndexedRegion(startTagStartOffset);
                }
            }
        }
    }
    return newNode;
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) NodeList(org.w3c.dom.NodeList) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 100 with IDOMModel

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

the class ElementNodeCleanupHandler method applyAttrNameCase.

protected void applyAttrNameCase(IDOMNode node) {
    IDOMElement element = (IDOMElement) node;
    if (element.isCommentTag())
        // do nothing
        return;
    int attrNameCase = HTMLCorePreferenceNames.ASIS;
    if (!shouldPreserveCase(element)) {
        if (isXMLTag(element))
            attrNameCase = HTMLCorePreferenceNames.LOWER;
        else
            attrNameCase = getCleanupPreferences().getAttrNameCase();
    }
    NamedNodeMap attributes = node.getAttributes();
    int attributesLength = attributes.getLength();
    for (int i = 0; i < attributesLength; i++) {
        IDOMNode eachAttr = (IDOMNode) attributes.item(i);
        if (hasNestedRegion(eachAttr.getNameRegion()))
            continue;
        String oldAttrName = eachAttr.getNodeName();
        String newAttrName = oldAttrName;
        /*
			 * 254961 - all HTML tag names and attribute names should be in
			 * English even for HTML files in other languages like Japanese or
			 * Turkish. English locale should be used to convert between
			 * uppercase and lowercase (otherwise "link" would be converted to
			 * Turkish "I Overdot Capital").
			 */
        if (attrNameCase == HTMLCorePreferenceNames.LOWER)
            newAttrName = oldAttrName.toLowerCase(Locale.US);
        else if (attrNameCase == HTMLCorePreferenceNames.UPPER)
            newAttrName = oldAttrName.toUpperCase(Locale.US);
        if (newAttrName.compareTo(oldAttrName) != 0) {
            int attrNameStartOffset = eachAttr.getStartOffset();
            int attrNameLength = oldAttrName.length();
            IDOMModel structuredModel = node.getModel();
            IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
            replaceSource(structuredModel, structuredDocument, attrNameStartOffset, attrNameLength, newAttrName);
        }
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

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