Search in sources :

Example 16 with IDOMElement

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement 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 17 with IDOMElement

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement 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 18 with IDOMElement

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement 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)

Example 19 with IDOMElement

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

the class ElementNodeCleanupHandler method cleanupCSSAttrValue.

/**
 */
protected void cleanupCSSAttrValue(IDOMNode node) {
    if (node == null || node.getNodeType() != Node.ELEMENT_NODE)
        return;
    IDOMElement element = (IDOMElement) node;
    if (!element.isGlobalTag())
        return;
    // $NON-NLS-1$
    Attr attr = element.getAttributeNode("style");
    if (attr == null)
        return;
    String value = getCSSValue(attr);
    if (value == null)
        return;
    String oldValue = ((IDOMNode) attr).getValueSource();
    if (oldValue != null && value.equals(oldValue))
        return;
    attr.setValue(value);
}
Also used : IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) Attr(org.w3c.dom.Attr) IDOMAttr(org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr)

Example 20 with IDOMElement

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

the class ElementNodeCleanupHandler method applyTagNameCase.

protected IDOMNode applyTagNameCase(IDOMNode node) {
    IDOMElement element = (IDOMElement) node;
    if (element.isCommentTag())
        // do nothing
        return node;
    int tagNameCase = HTMLCorePreferenceNames.ASIS;
    if (!shouldPreserveCase(element)) {
        if (isXMLTag(element))
            tagNameCase = HTMLCorePreferenceNames.LOWER;
        else
            tagNameCase = getCleanupPreferences().getTagNameCase();
    }
    String oldTagName = node.getNodeName();
    String newTagName = oldTagName;
    IDOMNode newNode = node;
    /*
		 * 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 (tagNameCase == HTMLCorePreferenceNames.LOWER)
        newTagName = oldTagName.toLowerCase(Locale.US);
    else if (tagNameCase == HTMLCorePreferenceNames.UPPER)
        newTagName = oldTagName.toUpperCase(Locale.US);
    IDOMModel structuredModel = node.getModel();
    IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
    IStructuredDocumentRegion startTagStructuredDocumentRegion = node.getStartStructuredDocumentRegion();
    if (startTagStructuredDocumentRegion != null) {
        ITextRegionList regions = startTagStructuredDocumentRegion.getRegions();
        if (regions != null && regions.size() > 0) {
            ITextRegion startTagNameRegion = regions.get(1);
            int startTagNameStartOffset = startTagStructuredDocumentRegion.getStartOffset(startTagNameRegion);
            int startTagNameLength = startTagStructuredDocumentRegion.getTextEndOffset(startTagNameRegion) - startTagNameStartOffset;
            if (!newTagName.equals(oldTagName))
                replaceSource(structuredModel, structuredDocument, startTagNameStartOffset, startTagNameLength, newTagName);
            // save
            newNode = (IDOMNode) structuredModel.getIndexedRegion(startTagNameStartOffset);
        // new
        // node
        }
    }
    IStructuredDocumentRegion endTagStructuredDocumentRegion = node.getEndStructuredDocumentRegion();
    if (endTagStructuredDocumentRegion != null) {
        ITextRegionList regions = endTagStructuredDocumentRegion.getRegions();
        if (regions != null && regions.size() > 0) {
            ITextRegion endTagNameRegion = regions.get(1);
            int endTagNameStartOffset = endTagStructuredDocumentRegion.getStartOffset(endTagNameRegion);
            int endTagNameLength = endTagStructuredDocumentRegion.getTextEndOffset(endTagNameRegion) - endTagNameStartOffset;
            if (startTagStructuredDocumentRegion != endTagStructuredDocumentRegion && !newTagName.equals(oldTagName))
                replaceSource(structuredModel, structuredDocument, endTagNameStartOffset, endTagNameLength, newTagName);
        }
    }
    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) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Aggregations

IDOMElement (org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)59 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)20 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)19 Node (org.w3c.dom.Node)18 NodeList (org.w3c.dom.NodeList)13 Element (org.w3c.dom.Element)11 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)9 IDOMAttr (org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr)9 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)8 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)8 NamedNodeMap (org.w3c.dom.NamedNodeMap)8 ArrayList (java.util.ArrayList)7 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)7 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)7 IFile (org.eclipse.core.resources.IFile)6 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)6 List (java.util.List)5 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)5 Text (org.w3c.dom.Text)5 ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)4