Search in sources :

Example 31 with IDOMElement

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

the class XSLModelObjectFactory method configure.

private void configure(IDOMNode node, XSLElement element) {
    setPositionInfo(node, element);
    IDOMElement domElem = (IDOMElement) node;
    element.setName(domElem.getLocalName());
    NamedNodeMap map = node.getAttributes();
    for (int i = 0; i < map.getLength(); i++) {
        IDOMAttr attr = (IDOMAttr) map.item(i);
        XSLAttribute xslatt = new XSLAttribute(element, attr.getName(), attr.getValue());
        setPositionInfo(attr, xslatt);
        element.setAttribute(xslatt);
    }
    setParentElement(node, element);
}
Also used : IDOMAttr(org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr) NamedNodeMap(org.w3c.dom.NamedNodeMap) XSLAttribute(org.eclipse.wst.xsl.core.model.XSLAttribute) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 32 with IDOMElement

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

the class EditElementAction method run.

public void run() {
    Shell shell = XMLUIPlugin.getInstance().getWorkbench().getActiveWorkbenchWindow().getShell();
    if (validateEdit(manager.getModel(), shell)) {
        manager.beginNodeAction(this);
        EditElementDialog dialog = new EditElementDialog(shell, element);
        dialog.create();
        dialog.getShell().setText(title);
        dialog.setBlockOnOpen(true);
        dialog.open();
        if (dialog.getReturnCode() == Window.OK) {
            Document document = parent.getNodeType() == Node.DOCUMENT_NODE ? (Document) parent : parent.getOwnerDocument();
            if (element != null) {
                // to do :-(
                if (element instanceof IDOMElement) {
                    IDOMElement elementImpl = (IDOMElement) element;
                    IDOMModel model = elementImpl.getModel();
                    String oldName = elementImpl.getNodeName();
                    String newName = dialog.getElementName();
                    setStructuredDocumentRegionElementName(model, elementImpl.getStartStructuredDocumentRegion(), oldName, newName);
                    setStructuredDocumentRegionElementName(model, elementImpl.getEndStructuredDocumentRegion(), oldName, newName);
                }
            } else {
                Element newElement = document.createElement(dialog.getElementName());
                NodeList nodeList = parent.getChildNodes();
                int nodeListLength = nodeList.getLength();
                Node refChild = (insertionIndex < nodeListLength) && (insertionIndex >= 0) ? nodeList.item(insertionIndex) : null;
                parent.insertBefore(newElement, refChild);
                manager.reformat(newElement, false);
                manager.setViewerSelection(newElement);
            }
        }
        manager.endNodeAction(this);
    }
}
Also used : Shell(org.eclipse.swt.widgets.Shell) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) Element(org.w3c.dom.Element) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) EditElementDialog(org.eclipse.wst.xml.ui.internal.dialogs.EditElementDialog) Document(org.w3c.dom.Document) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 33 with IDOMElement

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

the class XMLContentAssistUtilities method computeJSPEndTagProposal.

/**
 * A convenience method for getting the closing proposal given the
 * contents (IndexedRegion) of a tag that is started, but possibly not
 * ended
 *
 * @param viewer
 *            the text viewer
 * @param documentPosition
 *            the cursor position in the viewer
 * @param indexedNode
 *            the contents of the tag that is started but possibly not
 *            ended
 * @param parentTagName
 *            the tag on which you are checkin for an ending tag
 * @param imagePath
 *            content assist image relative path
 * @return ICompletionProposal
 */
public static ICompletionProposal computeJSPEndTagProposal(ITextViewer viewer, int documentPosition, IndexedRegion indexedNode, String parentTagName, String imagePath) {
    ICompletionProposal p = null;
    // check if tag is closed
    boolean hasEndTag = true;
    boolean isJSPTag = false;
    IDOMNode xnode = null;
    // $NON-NLS-1$
    String tagName = "";
    if (indexedNode instanceof IDOMNode) {
        xnode = ((IDOMNode) indexedNode);
        // it's ended already...
        if (xnode.getEndStructuredDocumentRegion() != null) {
            return null;
        }
        IDOMNode openNode = null;
        if (!xnode.getNodeName().equalsIgnoreCase(parentTagName)) {
            openNode = (IDOMNode) xnode.getParentNode();
        }
        if (openNode != null) {
            if (openNode instanceof IDOMElement) {
                isJSPTag = ((IDOMElement) openNode).isJSPTag();
            }
            tagName = openNode.getNodeName();
            hasEndTag = (openNode.getEndStructuredDocumentRegion() != null);
        }
    }
    // it's closed, don't add close tag proposal
    if (!hasEndTag && !isJSPTag) {
        // create appropriate close tag text
        // $NON-NLS-1$
        String proposedText = "</" + tagName;
        String viewerText = viewer.getTextWidget().getText();
        if ((viewerText.length() >= documentPosition) && (viewerText.length() >= 2) && (documentPosition >= 2)) {
            String last2chars = viewerText.substring(documentPosition - 2, documentPosition);
            if (last2chars.endsWith("</")) {
                proposedText = tagName;
            } else if (last2chars.endsWith("<")) {
                // $NON-NLS-1$
                proposedText = "/" + tagName;
            }
        }
        // create proposal
        p = new // $NON-NLS-1$
        CustomCompletionProposal(// $NON-NLS-1$
        proposedText + ">", documentPosition, 0, proposedText.length() + 1, XMLEditorPluginImageHelper.getInstance().getImage(imagePath), NLS.bind(XMLUIMessages.End_with_, (new Object[] { proposedText })), null, null, XMLRelevanceConstants.R_END_TAG);
    } else if (!hasEndTag && isJSPTag) {
        // create appropriate close tag text
        // $NON-NLS-1$
        String proposedText = "%";
        String viewerText = viewer.getTextWidget().getText();
        // already there...
        if ((viewerText.length() >= documentPosition) && (viewerText.length() >= 2)) {
            String last2chars = viewerText.substring(documentPosition - 2, documentPosition);
            String lastchar = viewerText.substring(documentPosition - 1, documentPosition);
            if (// $NON-NLS-1$
            lastchar.equals("%")) {
                if (last2chars.endsWith("<%")) {
                    // $NON-NLS-1$
                    proposedText = "%";
                } else {
                    // $NON-NLS-1$
                    proposedText = "";
                }
            }
        }
        // create proposal
        p = new // $NON-NLS-1$
        CustomCompletionProposal(// $NON-NLS-1$
        proposedText + ">", documentPosition, 0, proposedText.length() + 1, XMLEditorPluginImageHelper.getInstance().getImage(imagePath), NLS.bind(XMLUIMessages.End_with_, (new Object[] { proposedText })), null, null, XMLRelevanceConstants.R_END_TAG);
    }
    return p;
}
Also used : IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 34 with IDOMElement

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

the class DelegatingSourceValidator method computeStartAndEndLocation.

/**
 * Calculates the "better" offsets.
 *
 * @param startOffset -
 *            the offset given by Xerces
 * @param errorMessage -
 *            the Xerces error Message
 * @param selectionStrategy -
 *            the selectionStrategy
 * @param document -
 *            the document
 * @return int[] - position 0 has the start offset of the squiggle range,
 *         position 1 has the endOffset
 */
/*
	 * The way the offsets is calculated is: - find the indexed region
	 * (element) closest to the given offset - if we are between two elements,
	 * choosing left or right element will depend on parameter 'errorSide' -
	 * based on the selectionStrategy choose the underlining strategy (eg
	 * START_TAG means underline the start tag of that element) - use
	 * information from nameOrValue and the DOM to get better offsets
	 * 
	 */
protected int[] computeStartAndEndLocation(int startOffset, String selectionStrategy, String errorSide, String nameOrValue, IDOMDocument document) {
    try {
        int[] startEndPositions = new int[2];
        IndexedRegion region = document.getModel().getIndexedRegion(startOffset);
        IndexedRegion prevRegion = document.getModel().getIndexedRegion(startOffset - 1);
        if (prevRegion != region) {
            // exactly where we need to be.
            if (ERROR_SIDE_LEFT.equals(errorSide)) {
                region = prevRegion;
            }
        }
        // the start of the region where the error was
        if (region != null) {
            startEndPositions[0] = region.getStartOffset();
            startEndPositions[1] = startEndPositions[0];
        } else {
            // this will message will not get added to the IReporter
            // since the length is 0
            startEndPositions[0] = 0;
            startEndPositions[1] = 0;
        }
        if (region instanceof Node) {
            Node node = (Node) region;
            if (START_TAG.equals(selectionStrategy)) {
                // underline the opening tag
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    IDOMElement element = (IDOMElement) node;
                    startEndPositions[0] = element.getStartOffset() + 1;
                    startEndPositions[1] = startEndPositions[0] + element.getTagName().length();
                }
            } else if (END_TAG.equals(selectionStrategy)) {
                // underline the end tag
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    IDOMElement element = (IDOMElement) node;
                    startEndPositions[0] = element.getEndStartOffset();
                    startEndPositions[1] = element.getEndOffset();
                }
            } else if (ATTRIBUTE_NAME.equals(selectionStrategy)) {
                // underline the attribute's name
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    IDOMElement element = (IDOMElement) node;
                    IDOMNode attributeNode = (IDOMNode) (element.getAttributeNode(nameOrValue));
                    if (attributeNode != null) {
                        startEndPositions[0] = attributeNode.getStartOffset();
                        startEndPositions[1] = attributeNode.getStartOffset() + nameOrValue.length();
                    }
                }
            } else if (ATTRIBUTE_VALUE.equals(selectionStrategy)) {
                // underline the attribute's value
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    IDOMElement element = (IDOMElement) node;
                    IDOMAttr attributeNode = (IDOMAttr) (element.getAttributeNode(nameOrValue));
                    if (attributeNode != null) {
                        startEndPositions[0] = attributeNode.getValueRegionStartOffset();
                        String valueRegionText = attributeNode.getValueRegionText();
                        int valueRegionLength = valueRegionText == null ? 0 : valueRegionText.length();
                        startEndPositions[1] = startEndPositions[0] + valueRegionLength;
                    }
                }
            } else if (ALL_ATTRIBUTES.equals(selectionStrategy)) {
                // underline all attributes
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    IDOMElement element = (IDOMElement) node;
                    NamedNodeMap attributes = element.getAttributes();
                    if (attributes != null) {
                        IDOMNode first = (IDOMNode) attributes.item(0);
                        IDOMNode last = (IDOMNode) attributes.item(attributes.getLength() - 1);
                        if ((first != null) && (last != null)) {
                            startEndPositions[0] = first.getStartOffset();
                            startEndPositions[1] = last.getEndOffset();
                        }
                    }
                }
            } else if (TEXT.equals(selectionStrategy)) {
                // underline the text between the tags
                if (node.getNodeType() == Node.TEXT_NODE) {
                    IDOMText textNode = (IDOMText) node;
                    int start = textNode.getStartOffset();
                    String value = textNode.getNodeValue();
                    int index = 0;
                    char curChar = value.charAt(index);
                    // whitespace:
                    while ((curChar == '\n') || (curChar == '\t') || (curChar == '\r') || (curChar == ' ')) {
                        curChar = value.charAt(index);
                        index++;
                    }
                    if (index > 0) {
                        index--;
                    }
                    start = start + index;
                    startEndPositions[0] = start;
                    startEndPositions[1] = start + value.trim().length();
                } else if (node.getNodeType() == Node.ELEMENT_NODE) {
                    IDOMElement element = (IDOMElement) node;
                    Node child = element.getFirstChild();
                    if (child instanceof IDOMNode) {
                        IDOMNode xmlChild = ((IDOMNode) child);
                        startEndPositions[0] = xmlChild.getStartOffset();
                        startEndPositions[1] = xmlChild.getEndOffset();
                    }
                }
            } else if (FIRST_NON_WHITESPACE_TEXT.equals(selectionStrategy)) {
                // text node
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    NodeList nodes = node.getChildNodes();
                    for (int i = 0; i < nodes.getLength(); i++) {
                        Node currentNode = nodes.item(i);
                        if (currentNode.getNodeType() == Node.TEXT_NODE) {
                            // TODO (Trung) I don't think we should call
                            // getNodeValue(), trim(), length()
                            // repeatedly.
                            // This is inefficient, to improve use local
                            // variables to store values.
                            IDOMText textNode = (IDOMText) currentNode;
                            if (textNode.getNodeValue().trim().length() > 0) {
                                String value = textNode.getNodeValue();
                                int index = 0;
                                int start = textNode.getStartOffset();
                                char curChar = value.charAt(index);
                                // skipping over whitespace:
                                while ((curChar == '\n') || (curChar == '\t') || (curChar == '\r') || (curChar == ' ')) {
                                    curChar = value.charAt(index);
                                    index++;
                                }
                                if (index > 0) {
                                    index--;
                                }
                                start = start + index;
                                startEndPositions[0] = start;
                                startEndPositions[1] = start + value.trim().length();
                                break;
                            }
                        }
                    }
                }
            } else if (TEXT_ENTITY_REFERENCE.equals(selectionStrategy)) {
                if (node.getNodeType() == Node.ENTITY_REFERENCE_NODE) {
                    startEndPositions[0] = region.getStartOffset();
                    startEndPositions[1] = region.getEndOffset();
                } else if (node.getNodeType() == Node.ELEMENT_NODE) {
                    /*
						 * In this case the undeclared entity might be in one
						 * of the attribute values. Search through the
						 * attributes to find the range of the undeclared
						 * entity.
						 */
                    // $NON-NLS-1$ //$NON-NLS-2$
                    String entity = "&" + nameOrValue + ";";
                    NamedNodeMap attributes = node.getAttributes();
                    for (int i = 0; i < attributes.getLength(); i++) {
                        IDOMAttr attr = (IDOMAttr) attributes.item(i);
                        String nodeValue = attr.getNodeValue();
                        int index = nodeValue.indexOf(entity);
                        if (index != -1) {
                            startEndPositions[0] = attr.getValueRegionStartOffset() + index + 1;
                            startEndPositions[1] = startEndPositions[0] + entity.length();
                        }
                    }
                }
            } else if (VALUE_OF_ATTRIBUTE_WITH_GIVEN_VALUE.equals(selectionStrategy)) {
                // ATTRIBUTE_VALUE ?
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    // here we will search through all attributes for the
                    // one with the
                    // with the value we want:
                    // TODO (Trung) I see a potential problem here.
                    // What happens when there is another attribute having
                    // the same value
                    // with this attribute's buggy value ?
                    // Need to solve when time permits.
                    NamedNodeMap attributes = node.getAttributes();
                    for (int i = 0; i < attributes.getLength(); i++) {
                        IDOMAttr attr = (IDOMAttr) attributes.item(i);
                        String nodeValue = attr.getNodeValue().trim();
                        if (nodeValue.equals(nameOrValue)) {
                            startEndPositions[0] = attr.getValueRegionStartOffset() + 1;
                            startEndPositions[1] = startEndPositions[0] + nodeValue.length();
                            break;
                        }
                    }
                }
            } else if (ATTRIBUTE_NAME_LAST.equals(selectionStrategy)) {
                // underline the last attribute's name
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    NamedNodeMap attributeMap = node.getAttributes();
                    final int length = attributeMap.getLength();
                    Node tempNode = null;
                    Node attrNode = null;
                    for (int i = 0; i < length; i++) {
                        tempNode = attributeMap.item(i);
                        if (tempNode != null && tempNode.getNodeName().equals(nameOrValue)) {
                            attrNode = tempNode;
                        }
                    }
                    IDOMNode attributeNode = (IDOMNode) (attrNode);
                    if (attributeNode != null) {
                        startEndPositions[0] = attributeNode.getStartOffset();
                        startEndPositions[1] = attributeNode.getStartOffset() + nameOrValue.length();
                    }
                }
            }
        }
        return startEndPositions;
    } finally // catch (Exception e) { // e.printStackTrace();
    // }
    {
    }
// return null;
}
Also used : IDOMAttr(org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr) NamedNodeMap(org.w3c.dom.NamedNodeMap) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) IDOMText(org.eclipse.wst.xml.core.internal.provisional.document.IDOMText) NodeList(org.w3c.dom.NodeList) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 35 with IDOMElement

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

the class JSPActionValidator method checkJSPAttributeAction.

/**
 * Checks for jsp:attribute actions of <code>element</code> to see if they
 * satisfy the required attribute <code>attr</code>
 *
 * @param element The element with a required attribute
 * @param attr The required attribute
 * @return <code>true</code> if a jsp:attribute action has the name of
 * the required attribute <code>attr</code>; <code>false</code> otherwise.
 */
private boolean checkJSPAttributeAction(IDOMElement element, CMAttributeDeclaration attr) {
    if (element != null && attr != null) {
        // $NON-NLS-1$
        NodeList elements = element.getElementsByTagName("jsp:attribute");
        String neededAttrName = attr.getNodeName();
        for (int i = 0; i < elements.getLength(); i++) {
            Element childElement = (Element) elements.item(i);
            /*
				 * Get the name attribute of jsp:attribute and compare its
				 * value to the required attribute name
				 */
            if (childElement.hasAttribute("name") && neededAttrName.equals(childElement.getAttribute("name"))) {
                // $NON-NLS-1$ //$NON-NLS-2$
                return true;
            }
        }
    }
    return false;
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) 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