Search in sources :

Example 76 with IDOMDocument

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

the class AbstractContentAssistProcessor method addTagInsertionProposals.

protected void addTagInsertionProposals(ContentAssistRequest contentAssistRequest, int childPosition) {
    List cmnodes = null;
    Node parent = contentAssistRequest.getParent();
    String error = null;
    // only valid if it's XML (check added 2/17/2004)
    if ((parent != null) && (parent.getNodeType() == Node.DOCUMENT_NODE) && ((IDOMDocument) parent).isXMLType() && !isCursorAfterXMLPI(contentAssistRequest)) {
        return;
    }
    // only want proposals if cursor is after doctype...
    if (!isCursorAfterDoctype(contentAssistRequest)) {
        return;
    }
    // have a content model (so can't propose any children..)
    if ((parent != null) && (parent instanceof IDOMNode) && isCommentNode((IDOMNode) parent)) {
        // loop and find non comment node?
        while ((parent != null) && isCommentNode((IDOMNode) parent)) {
            parent = parent.getParentNode();
        }
    }
    if (parent.getNodeType() == Node.ELEMENT_NODE) {
        CMElementDeclaration parentDecl = getCMElementDeclaration(parent);
        if (parentDecl != null) {
            // XSD-specific ability - no filtering
            CMDataType childType = parentDecl.getDataType();
            if (childType != null) {
                String[] childStrings = childType.getEnumeratedValues();
                String defaultValue = childType.getImpliedValue();
                if (childStrings != null || defaultValue != null) {
                    // the content string is the sole valid child...so
                    // replace the rest
                    int begin = contentAssistRequest.getReplacementBeginPosition();
                    int length = contentAssistRequest.getReplacementLength();
                    if (parent instanceof IDOMNode) {
                        if (((IDOMNode) parent).getLastStructuredDocumentRegion() != ((IDOMNode) parent).getFirstStructuredDocumentRegion()) {
                            begin = ((IDOMNode) parent).getFirstStructuredDocumentRegion().getEndOffset();
                            length = ((IDOMNode) parent).getLastStructuredDocumentRegion().getStartOffset() - begin;
                        }
                    }
                    String proposedInfo = getAdditionalInfo(parentDecl, childType);
                    for (int i = 0; i < childStrings.length; i++) {
                        if (!childStrings[i].equals(defaultValue)) {
                            CustomCompletionProposal textProposal = new CustomCompletionProposal(childStrings[i], begin, length, childStrings[i].length(), XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ENUM), childStrings[i], null, proposedInfo, XMLRelevanceConstants.R_TAG_INSERTION);
                            contentAssistRequest.addProposal(textProposal);
                        }
                    }
                    if (defaultValue != null) {
                        CustomCompletionProposal textProposal = new CustomCompletionProposal(defaultValue, begin, length, defaultValue.length(), XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_DEFAULT), defaultValue, null, proposedInfo, XMLRelevanceConstants.R_TAG_INSERTION);
                        contentAssistRequest.addProposal(textProposal);
                    }
                }
            }
        }
        if ((parentDecl != null) && (parentDecl.getContentType() == CMElementDeclaration.PCDATA)) {
            addPCDATAProposal(parentDecl.getNodeName(), contentAssistRequest);
        } else {
            // retrieve the list of all possible children within this
            // parent context
            cmnodes = getAvailableChildElementDeclarations((Element) parent, childPosition, ModelQueryAction.INSERT);
            // retrieve the list of the possible children within this
            // parent context and at this index
            List strictCMNodeSuggestions = null;
            if (XMLUIPreferenceNames.SUGGESTION_STRATEGY_VALUE_STRICT.equals(XMLUIPlugin.getInstance().getPreferenceStore().getString(XMLUIPreferenceNames.SUGGESTION_STRATEGY))) {
                strictCMNodeSuggestions = getValidChildElementDeclarations((Element) parent, childPosition, ModelQueryAction.INSERT);
            }
            Iterator nodeIterator = cmnodes.iterator();
            if (!nodeIterator.hasNext()) {
                if (getCMElementDeclaration(parent) != null) {
                    error = NLS.bind(XMLUIMessages._Has_no_available_child, (new Object[] { parent.getNodeName() }));
                } else {
                    error = NLS.bind(XMLUIMessages.Element__is_unknown, (new Object[] { parent.getNodeName() }));
                }
            }
            String matchString = contentAssistRequest.getMatchString();
            // matchstring
            while ((matchString.length() > 0) && (Character.isWhitespace(matchString.charAt(0)) || beginsWith(matchString, "<"))) {
                // $NON-NLS-1$
                matchString = matchString.substring(1);
            }
            while (nodeIterator.hasNext()) {
                Object o = nodeIterator.next();
                if (o instanceof CMElementDeclaration) {
                    CMElementDeclaration elementDecl = (CMElementDeclaration) o;
                    // only add proposals for the child element's that
                    // begin with the matchstring
                    String tagname = getRequiredName(parent, elementDecl);
                    boolean isStrictCMNodeSuggestion = strictCMNodeSuggestions != null ? strictCMNodeSuggestions.contains(elementDecl) : false;
                    Image image = CMImageUtil.getImage(elementDecl);
                    if (image == null) {
                        if (strictCMNodeSuggestions != null) {
                            image = isStrictCMNodeSuggestion ? XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_TAG_GENERIC_EMPHASIZED) : XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_TAG_GENERIC_DEEMPHASIZED);
                        } else {
                            image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_TAG_GENERIC);
                        }
                    }
                    // elementDecl);
                    if (beginsWith(tagname, matchString)) {
                        String proposedText = getRequiredText(parent, elementDecl);
                        // https://bugs.eclipse.org/bugs/show_bug.cgi?id=89811
                        // place cursor in first empty quotes
                        int markupAdjustment = getCursorPositionForProposedText(proposedText);
                        String proposedInfo = getAdditionalInfo(parentDecl, elementDecl);
                        int relevance = isStrictCMNodeSuggestion ? XMLRelevanceConstants.R_STRICTLY_VALID_TAG_INSERTION : XMLRelevanceConstants.R_TAG_INSERTION;
                        CustomCompletionProposal proposal = new CustomCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), markupAdjustment, image, tagname, null, proposedInfo, relevance);
                        contentAssistRequest.addProposal(proposal);
                    }
                }
            }
            if (contentAssistRequest.getProposals().size() == 0) {
                if (error != null) {
                    setErrorMessage(error);
                } else if ((contentAssistRequest.getMatchString() != null) && (contentAssistRequest.getMatchString().length() > 0)) {
                    setErrorMessage(NLS.bind(XMLUIMessages.No_known_child_tag, (new Object[] { parent.getNodeName(), contentAssistRequest.getMatchString() })));
                // $NON-NLS-1$ = "No known child tag names of <{0}> begin with \"{1}\"."
                } else {
                    setErrorMessage(NLS.bind(XMLUIMessages.__Has_no_known_child, (new Object[] { parent.getNodeName() })));
                }
            }
        }
    } else if (parent.getNodeType() == Node.DOCUMENT_NODE) {
        // Can only prompt with elements if the cursor position is past
        // the XML processing
        // instruction and DOCTYPE declaration
        boolean xmlpiFound = false;
        boolean doctypeFound = false;
        int minimumOffset = -1;
        for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
            // $NON-NLS-1$
            boolean xmlpi = ((child.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) && child.getNodeName().equals("xml"));
            boolean doctype = child.getNodeType() == Node.DOCUMENT_TYPE_NODE;
            if (xmlpi || (doctype && (minimumOffset < 0))) {
                minimumOffset = ((IDOMNode) child).getFirstStructuredDocumentRegion().getStartOffset() + ((IDOMNode) child).getFirstStructuredDocumentRegion().getTextLength();
            }
            xmlpiFound = xmlpiFound || xmlpi;
            doctypeFound = doctypeFound || doctype;
        }
        if (contentAssistRequest.getReplacementBeginPosition() >= minimumOffset) {
            List childDecls = getAvailableRootChildren((Document) parent, childPosition);
            for (int i = 0; i < childDecls.size(); i++) {
                CMElementDeclaration ed = (CMElementDeclaration) childDecls.get(i);
                if (ed != null) {
                    Image image = CMImageUtil.getImage(ed);
                    if (image == null) {
                        image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_TAG_GENERIC);
                    }
                    String proposedText = getRequiredText(parent, ed);
                    String tagname = getRequiredName(parent, ed);
                    // account for the &lt; and &gt;
                    int markupAdjustment = getContentGenerator().getMinimalStartTagLength(parent, ed);
                    String proposedInfo = getAdditionalInfo(null, ed);
                    CustomCompletionProposal proposal = new CustomCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), markupAdjustment, image, tagname, null, proposedInfo, XMLRelevanceConstants.R_TAG_INSERTION);
                    contentAssistRequest.addProposal(proposal);
                }
            }
        }
    }
}
Also used : CMDataType(org.eclipse.wst.xml.core.internal.contentmodel.CMDataType) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) Image(org.eclipse.swt.graphics.Image) 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) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Iterator(java.util.Iterator) CMNodeList(org.eclipse.wst.xml.core.internal.contentmodel.CMNodeList) List(java.util.List) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList)

Example 77 with IDOMDocument

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

the class AbstractContentAssistProcessor method getXML.

/**
 * Similar to the call in HTMLContentAssistProcessor. Pass in a node, it
 * tells you if the document is XML type.
 *
 * @param node
 */
protected boolean getXML(Node node) {
    if (node == null) {
        return false;
    }
    Document doc = null;
    doc = (node.getNodeType() != Node.DOCUMENT_NODE) ? node.getOwnerDocument() : ((Document) node);
    return (doc instanceof IDOMDocument) && ((IDOMDocument) doc).isXMLType();
}
Also used : IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) 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)

Example 78 with IDOMDocument

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

the class DelegatingSourceValidator method validate.

/**
 * Calls a delegate validator getting and updates it's list of
 * ValidationMessages with a good squiggle offset and length.
 *
 * @param helper
 *            loads an object.
 * @param reporter
 *            Is an instance of an IReporter interface, which is used for
 *            interaction with the user.
 */
public void validate(IValidationContext helper, IReporter reporter) throws ValidationException {
    String[] delta = helper.getURIs();
    if (delta.length > 0) {
        // get the file, model and document:
        IFile file = getFile(delta[0]);
        IDOMModel xmlModel = null;
        if (file != null)
            xmlModel = getModelForResource(file);
        // some problem occurred, abort
        if (xmlModel == null)
            return;
        try {
            IDOMDocument document = xmlModel.getDocument();
            // store the text in a byte array; make a full copy to ease
            // any threading problems
            byte[] byteArray;
            try {
                byteArray = xmlModel.getStructuredDocument().get().getBytes("UTF-8");
            } catch (UnsupportedEncodingException e) {
                // Not likely to happen
                byteArray = xmlModel.getStructuredDocument().get().getBytes();
            }
            if (isDelegateValidatorEnabled(file)) {
                IValidator validator = getDelegateValidator();
                if (validator != null) {
                    // Validate the file:
                    IValidationContext vHelper = new MyHelper(new ByteArrayInputStream(byteArray), file);
                    MyReporter vReporter = new MyReporter();
                    if (validator instanceof IValidatorJob) {
                        ((IValidatorJob) validator).validateInJob(vHelper, vReporter);
                    } else {
                        validator.validate(vHelper, vReporter);
                    }
                    List messages = vReporter.list;
                    // set the offset and length
                    updateValidationMessages(messages, document, reporter);
                }
            }
        } finally {
            if (xmlModel != null) {
                xmlModel.releaseFromRead();
            }
        }
    }
}
Also used : IValidatorJob(org.eclipse.wst.validation.internal.provisional.core.IValidatorJob) IFile(org.eclipse.core.resources.IFile) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IValidator(org.eclipse.wst.validation.internal.provisional.core.IValidator) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) IValidationContext(org.eclipse.wst.validation.internal.provisional.core.IValidationContext)

Example 79 with IDOMDocument

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

the class DOMExtensionItemMenuListener method menuAboutToShow.

public void menuAboutToShow(IMenuManager manager) {
    manager.removeAll();
    ISelection selection = treeViewer.getSelection();
    if (selection instanceof IStructuredSelection) {
        IStructuredSelection structuredSelection = (IStructuredSelection) selection;
        if (structuredSelection.getFirstElement() instanceof ElementImpl) {
            ElementImpl elementImpl = (ElementImpl) structuredSelection.getFirstElement();
            IDOMDocument domDocument = (IDOMDocument) elementImpl.getOwnerDocument();
            InternalNodeActionManager actionManager = new InternalNodeActionManager(domDocument.getModel(), treeViewer);
            actionManager.fillContextMenu(manager, structuredSelection);
        }
    }
}
Also used : ElementImpl(org.eclipse.wst.xml.core.internal.document.ElementImpl) ISelection(org.eclipse.jface.viewers.ISelection) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection)

Example 80 with IDOMDocument

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

the class XMLGeneratorImpl method generateStartTag.

/**
 * generateStartTag method
 *
 * @return java.lang.String
 * @param element
 *            Element
 */
public String generateStartTag(Element element) {
    if (element == null)
        return null;
    ElementImpl impl = (ElementImpl) element;
    if (impl.isJSPTag()) {
        // check if JSP content type and JSP Document
        IDOMDocument document = (IDOMDocument) element.getOwnerDocument();
        if (document != null && document.isJSPType()) {
            if (document.isJSPDocument() && !impl.hasChildNodes()) {
                impl.setJSPTag(false);
            }
        } else {
            impl.setJSPTag(false);
        }
    }
    if (impl.isCommentTag() && impl.getExistingAdapter(TagAdapter.class) == null) {
        CommentElementRegistry registry = CommentElementRegistry.getInstance();
        registry.setupCommentElement(impl);
    }
    // first check if tag adapter exists
    TagAdapter adapter = (TagAdapter) impl.getExistingAdapter(TagAdapter.class);
    if (adapter != null) {
        String startTag = adapter.getStartTag(impl);
        if (startTag != null)
            return startTag;
    }
    StringBuffer buffer = new StringBuffer();
    if (impl.isCommentTag()) {
        if (impl.isJSPTag())
            buffer.append(JSPTag.COMMENT_OPEN);
        else
            buffer.append(COMMENT_OPEN);
        String tagName = generateTagName(element);
        if (tagName != null)
            buffer.append(tagName);
    } else if (impl.isJSPTag()) {
        buffer.append(JSPTag.TAG_OPEN);
        String tagName = generateTagName(element);
        if (tagName != null)
            buffer.append(tagName);
        if (impl.isContainer())
            // JSP container
            return buffer.toString();
    } else {
        buffer.append('<');
        String tagName = generateTagName(element);
        if (tagName != null)
            buffer.append(tagName);
    }
    NamedNodeMap attributes = element.getAttributes();
    int length = attributes.getLength();
    for (int i = 0; i < length; i++) {
        AttrImpl attr = (AttrImpl) attributes.item(i);
        if (attr == null)
            continue;
        buffer.append(' ');
        String attrName = generateAttrName(attr);
        if (attrName != null)
            buffer.append(attrName);
        String attrValue = generateAttrValue(attr);
        if (attrValue != null) {
            // attr name only for HTML boolean and JSP
            buffer.append('=');
            buffer.append(attrValue);
        }
    }
    String closeTag = generateCloseTag(element);
    if (closeTag != null)
        buffer.append(closeTag);
    return buffer.toString();
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) CommentElementRegistry(org.eclipse.wst.xml.core.internal.commentelement.impl.CommentElementRegistry)

Aggregations

IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)176 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)109 IFile (org.eclipse.core.resources.IFile)48 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)39 Element (org.w3c.dom.Element)39 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)33 NodeList (org.w3c.dom.NodeList)27 IStatus (org.eclipse.core.runtime.IStatus)23 Node (org.w3c.dom.Node)21 CoreException (org.eclipse.core.runtime.CoreException)15 ArrayList (java.util.ArrayList)14 IJsTranslation (org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation)14 JsTranslationAdapter (org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter)14 Document (org.w3c.dom.Document)14 IJSPTranslation (org.eclipse.jst.jsp.core.internal.java.IJSPTranslation)12 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)12 IOException (java.io.IOException)11 CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)11 JSPTranslationAdapter (org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter)10 IModelManager (org.eclipse.wst.sse.core.internal.provisional.IModelManager)9