Search in sources :

Example 1 with ISourceGenerator

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

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

the class ElementNodeCleanupHandler method quoteAttrValue.

private IDOMNode quoteAttrValue(IDOMNode node) {
    IDOMNode newNode = node;
    // XMLElement element = (XMLElement) node;
    if (isCommentTag(node))
        // do nothing
        return node;
    boolean quoteAttrValues = getCleanupPreferences().getQuoteAttrValues();
    if (quoteAttrValues) {
        NamedNodeMap attributes = newNode.getAttributes();
        if (attributes != null) {
            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)) {
                        // $NON-NLS-1$ //$NON-NLS-2$
                        String newAttrValue = "\"" + eachAttr.getNameRegionText() + "\"";
                        IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
                        if (eachAttr.getEqualRegion() != null)
                            // equal region exists
                            structuredDocument.replaceText(structuredDocument, eachAttr.getEndOffset(), 0, newAttrValue);
                        else
                            // no equal region
                            // $NON-NLS-1$
                            structuredDocument.replaceText(structuredDocument, eachAttr.getNameRegionTextEndOffset(), 0, "=".concat(newAttrValue));
                        // save
                        newNode = (IDOMNode) structuredModel.getIndexedRegion(node.getStartOffset());
                    // new
                    // node
                    }
                } else {
                    // String oldAttrValue = oldAttrValueRegion.getText();
                    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();
                            structuredDocument.replaceText(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)

Example 3 with ISourceGenerator

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

the class TextImpl method getSource.

/**
 * Returns source for the data
 */
private String getSource(String data) {
    if (data == null)
        return null;
    IDOMModel model = getModel();
    if (model == null)
        // error
        return null;
    ISourceGenerator generator = model.getGenerator();
    if (generator == null)
        // error
        return null;
    return generator.generateTextData(this, data);
}
Also used : ISourceGenerator(org.eclipse.wst.xml.core.internal.provisional.document.ISourceGenerator) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)

Example 4 with ISourceGenerator

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

the class CommentElementHandlerForSSI method generateStartTagContent.

public String generateStartTagContent(IDOMElement element) {
    ISourceGenerator generator = element.getModel().getGenerator();
    StringBuffer buffer = new StringBuffer();
    buffer.append('#');
    buffer.append(element.getLocalName());
    NamedNodeMap attributes = element.getAttributes();
    int length = attributes.getLength();
    for (int i = 0; i < length; i++) {
        Attr attr = (Attr) attributes.item(i);
        if (attr == null) {
            continue;
        }
        buffer.append(' ');
        String attrName = generator.generateAttrName(attr);
        if (attrName != null) {
            buffer.append(attrName);
        }
        String attrValue = generator.generateAttrValue(attr);
        if (attrValue != null) {
            // attr name only for HTML boolean and JSP
            buffer.append('=');
            buffer.append(attrValue);
        }
    }
    return buffer.toString();
}
Also used : CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) NamedNodeMap(org.w3c.dom.NamedNodeMap) ISourceGenerator(org.eclipse.wst.xml.core.internal.provisional.document.ISourceGenerator) Attr(org.w3c.dom.Attr)

Example 5 with ISourceGenerator

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

the class ElementNodeFormatter method formatStartTag.

/**
 * This method formats the start tag name, and formats the attributes if
 * available.
 */
protected void formatStartTag(IDOMNode node, IStructuredFormatContraints formatContraints) {
    StructuredFormatPreferencesXML preferences = (StructuredFormatPreferencesXML) getFormatPreferences();
    String singleIndent = preferences.getIndent();
    String lineIndent = formatContraints.getCurrentIndent();
    String attrIndent = lineIndent + singleIndent;
    boolean splitMultiAttrs = preferences.getSplitMultiAttrs();
    boolean alignEndBracket = preferences.isAlignEndBracket();
    boolean sawXmlSpace = false;
    IStructuredDocumentRegion flatNode = node.getFirstStructuredDocumentRegion();
    NamedNodeMap attributes = node.getAttributes();
    // for null just in case.
    if (attributes != null) {
        // compute current available line width
        int currentAvailableLineWidth = 0;
        try {
            // 1 is for "<"
            int nodeNameOffset = node.getStartOffset() + 1 + node.getNodeName().length();
            int lineOffset = node.getStructuredDocument().getLineInformationOfOffset(nodeNameOffset).getOffset();
            String text = node.getStructuredDocument().get(lineOffset, nodeNameOffset - lineOffset);
            int usedWidth = getIndentationLength(text);
            currentAvailableLineWidth = preferences.getLineWidth() - usedWidth;
        } catch (BadLocationException e) {
            // log for now, unless we find reason not to
            Logger.log(Logger.INFO, e.getMessage());
        }
        StringBuffer stringBuffer = new StringBuffer();
        String lineDelimiter = node.getModel().getStructuredDocument().getLineDelimiter();
        int attrLength = attributes.getLength();
        int lastUndefinedRegionOffset = 0;
        boolean startTagSpansOver1Line = false;
        for (int i = 0; i < attrLength; i++) {
            AttrImpl attr = (AttrImpl) attributes.item(i);
            ITextRegion nameRegion = attr.getNameRegion();
            ITextRegion equalRegion = attr.getEqualRegion();
            ITextRegion valueRegion = attr.getValueRegion();
            // append undefined regions
            String undefinedRegion = getUndefinedRegions(node, lastUndefinedRegionOffset, attr.getStartOffset() - lastUndefinedRegionOffset);
            stringBuffer.append(undefinedRegion);
            lastUndefinedRegionOffset = attr.getStartOffset();
            // check for xml:space attribute
            if (flatNode.getText(nameRegion).compareTo(XML_SPACE) == 0) {
                if (valueRegion == null) {
                    // [111674] If nothing has been written yet, treat as
                    // preserve, but only as hint
                    formatContraints.setInPreserveSpaceElement(true);
                // Note we don't set 'sawXmlSpace', so that default or
                // fixed DTD/XSD values may override.
                } else {
                    ISourceGenerator generator = node.getModel().getGenerator();
                    String newAttrValue = generator.generateAttrValue(attr);
                    // Workaround for now.
                    if (flatNode.getText(valueRegion).length() == 1) {
                        char firstChar = flatNode.getText(valueRegion).charAt(0);
                        if ((firstChar == DOUBLE_QUOTE) || (firstChar == SINGLE_QUOTE))
                            newAttrValue = DOUBLE_QUOTES;
                    }
                    if (newAttrValue.compareTo(PRESERVE_QUOTED) == 0)
                        formatContraints.setInPreserveSpaceElement(true);
                    else
                        formatContraints.setInPreserveSpaceElement(false);
                    sawXmlSpace = true;
                }
            }
            if (splitMultiAttrs && attrLength > 1) {
                stringBuffer.append(lineDelimiter + attrIndent);
                stringBuffer.append(flatNode.getText(nameRegion));
                startTagSpansOver1Line = true;
                if (valueRegion != null) {
                    // append undefined regions
                    undefinedRegion = getUndefinedRegions(node, lastUndefinedRegionOffset, flatNode.getStartOffset(equalRegion) - lastUndefinedRegionOffset);
                    stringBuffer.append(undefinedRegion);
                    lastUndefinedRegionOffset = flatNode.getStartOffset(equalRegion);
                    stringBuffer.append(EQUAL_CHAR);
                    // append undefined regions
                    undefinedRegion = getUndefinedRegions(node, lastUndefinedRegionOffset, flatNode.getStartOffset(valueRegion) - lastUndefinedRegionOffset);
                    stringBuffer.append(undefinedRegion);
                    lastUndefinedRegionOffset = flatNode.getStartOffset(valueRegion);
                    // Note: trim() should not be needed for
                    // valueRegion.getText(). Just a workaround for a
                    // problem found in valueRegion for now.
                    stringBuffer.append(flatNode.getText(valueRegion).trim());
                }
            } else {
                if (valueRegion != null) {
                    int textLength = 1 + flatNode.getText(nameRegion).length() + 1 + flatNode.getText(valueRegion).length();
                    if (i == attrLength - 1) {
                        if (flatNode != null) {
                            ITextRegionList regions = flatNode.getRegions();
                            ITextRegion lastRegion = regions.get(regions.size() - 1);
                            if (lastRegion.getType() != DOMRegionContext.XML_EMPTY_TAG_CLOSE)
                                // 3 is for " />"
                                textLength += 3;
                            else
                                // 1 is for ">"
                                textLength++;
                        }
                    }
                    if (currentAvailableLineWidth >= textLength) {
                        stringBuffer.append(SPACE_CHAR);
                        currentAvailableLineWidth--;
                    } else {
                        stringBuffer.append(lineDelimiter + attrIndent);
                        startTagSpansOver1Line = true;
                        currentAvailableLineWidth = preferences.getLineWidth() - attrIndent.length();
                    }
                    stringBuffer.append(flatNode.getText(nameRegion));
                    // append undefined regions
                    undefinedRegion = getUndefinedRegions(node, lastUndefinedRegionOffset, flatNode.getStartOffset(equalRegion) - lastUndefinedRegionOffset);
                    stringBuffer.append(undefinedRegion);
                    lastUndefinedRegionOffset = flatNode.getStartOffset(equalRegion);
                    stringBuffer.append(EQUAL_CHAR);
                    // append undefined regions
                    undefinedRegion = getUndefinedRegions(node, lastUndefinedRegionOffset, flatNode.getStartOffset(valueRegion) - lastUndefinedRegionOffset);
                    stringBuffer.append(undefinedRegion);
                    lastUndefinedRegionOffset = flatNode.getStartOffset(valueRegion);
                    // Note: trim() should not be needed for
                    // valueRegion.getText(). Just a workaround for a
                    // problem found in valueRegion for now.
                    stringBuffer.append(flatNode.getText(valueRegion).trim());
                    currentAvailableLineWidth -= flatNode.getText(nameRegion).length();
                    currentAvailableLineWidth--;
                    currentAvailableLineWidth -= flatNode.getText(valueRegion).trim().length();
                } else {
                    if (currentAvailableLineWidth >= 1 + flatNode.getText(nameRegion).length()) {
                        stringBuffer.append(SPACE_CHAR);
                        currentAvailableLineWidth--;
                    } else {
                        stringBuffer.append(lineDelimiter + attrIndent);
                        startTagSpansOver1Line = true;
                        currentAvailableLineWidth = preferences.getLineWidth() - attrIndent.length();
                    }
                    stringBuffer.append(flatNode.getText(nameRegion));
                    currentAvailableLineWidth -= flatNode.getText(nameRegion).length();
                }
            }
        }
        // append undefined regions
        String undefinedRegion = getUndefinedRegions(node, lastUndefinedRegionOffset, node.getEndOffset() - lastUndefinedRegionOffset);
        stringBuffer.append(undefinedRegion);
        IDOMModel structuredModel = node.getModel();
        IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
        // 1 is for "<"
        int offset = node.getStartOffset() + 1 + node.getNodeName().length();
        // 1 is for "<"
        int length = node.getFirstStructuredDocumentRegion().getTextLength() - 1 - node.getNodeName().length();
        if (flatNode != null) {
            ITextRegionList regions = flatNode.getRegions();
            ITextRegion firstRegion = regions.get(0);
            ITextRegion lastRegion = regions.get(regions.size() - 1);
            if (firstRegion.getType() == DOMRegionContext.XML_END_TAG_OPEN)
                // skip formatting for end tags in this format: </tagName>
                return;
            else {
                if (lastRegion.getType() == DOMRegionContext.XML_TAG_CLOSE || lastRegion.getType() == DOMRegionContext.XML_EMPTY_TAG_CLOSE)
                    length = length - lastRegion.getLength();
                if (lastRegion.getType() == DOMRegionContext.XML_EMPTY_TAG_CLOSE) {
                    // unless already going to move end bracket
                    if (!startTagSpansOver1Line || !alignEndBracket)
                        stringBuffer.append(SPACE_CHAR);
                }
            }
        }
        if (startTagSpansOver1Line && alignEndBracket) {
            stringBuffer.append(lineDelimiter).append(lineIndent);
        }
        replace(structuredDocument, offset, length, stringBuffer.toString());
        // BUG108074 & BUG84688 - preserve whitespace in xsl:text &
        // xsl:attribute
        String nodeNamespaceURI = node.getNamespaceURI();
        if (XSL_NAMESPACE.equals(nodeNamespaceURI)) {
            String nodeName = ((Element) node).getLocalName();
            if (XSL_ATTRIBUTE.equals(nodeName) || XSL_TEXT.equals(nodeName)) {
                sawXmlSpace = true;
                formatContraints.setInPreserveSpaceElement(true);
            }
        }
        // leave that to the validator.
        if (!sawXmlSpace) {
            ModelQueryAdapter adapter = (ModelQueryAdapter) ((IDOMDocument) node.getOwnerDocument()).getAdapterFor(ModelQueryAdapter.class);
            CMElementDeclaration elementDeclaration = (CMElementDeclaration) adapter.getModelQuery().getCMNode(node);
            if (elementDeclaration != null) {
                int contentType = elementDeclaration.getContentType();
                if (preferences.isPreservePCDATAContent() && contentType == CMElementDeclaration.PCDATA) {
                    formatContraints.setInPreserveSpaceElement(true);
                } else {
                    CMNamedNodeMap cmAttributes = elementDeclaration.getAttributes();
                    // Check implied values from the DTD way.
                    CMAttributeDeclaration attributeDeclaration = (CMAttributeDeclaration) cmAttributes.getNamedItem(XML_SPACE);
                    if (attributeDeclaration != null) {
                        // CMAttributeDeclaration found, check it out.
                        String defaultValue = attributeDeclaration.getAttrType().getImpliedValue();
                        // everything else means back to default.
                        if (PRESERVE.compareTo(defaultValue) == 0)
                            formatContraints.setInPreserveSpaceElement(true);
                        else
                            formatContraints.setInPreserveSpaceElement(false);
                    }
                }
            }
        }
    }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) NamedNodeMap(org.w3c.dom.NamedNodeMap) ModelQueryAdapter(org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) Element(org.w3c.dom.Element) AttrImpl(org.eclipse.wst.xml.core.internal.document.AttrImpl) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ISourceGenerator(org.eclipse.wst.xml.core.internal.provisional.document.ISourceGenerator) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

ISourceGenerator (org.eclipse.wst.xml.core.internal.provisional.document.ISourceGenerator)7 NamedNodeMap (org.w3c.dom.NamedNodeMap)5 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)4 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)4 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)3 IDOMAttr (org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr)3 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)2 Attr (org.w3c.dom.Attr)2 BadLocationException (org.eclipse.jface.text.BadLocationException)1 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)1 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)1 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)1 CMAttributeDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration)1 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)1 AttrImpl (org.eclipse.wst.xml.core.internal.document.AttrImpl)1 IDOMElement (org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)1 ModelQueryAdapter (org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter)1 Element (org.w3c.dom.Element)1