Search in sources :

Example 6 with CMDataType

use of org.eclipse.wst.xml.core.internal.contentmodel.CMDataType in project webtools.sourceediting by eclipse.

the class CMDataTypeValueHelper method getValue.

public String getValue(CMAttributeDeclaration ad, NamespaceTable namespaceTable) {
    String value = null;
    if (isXSIType(ad)) {
        List list = getQualifiedXSITypes(ad, namespaceTable);
        value = list.size() > 0 ? (String) list.get(0) : null;
    }
    // 
    if (value == null) {
        if (// $NON-NLS-1$
        ad.getAttrName().equals("xml:lang")) {
            // $NON-NLS-1$
            value = "EN";
        }
    }
    if (value == null) {
        CMDataType dataType = ad.getAttrType();
        if (dataType != null) {
            value = getValue(dataType);
        }
    }
    return value;
}
Also used : CMDataType(org.eclipse.wst.xml.core.internal.contentmodel.CMDataType) List(java.util.List)

Example 7 with CMDataType

use of org.eclipse.wst.xml.core.internal.contentmodel.CMDataType in project webtools.sourceediting by eclipse.

the class CMValidator method getOriginArray.

public void getOriginArray(CMElementDeclaration ed, List elementContent, ElementContentComparator comparator, ElementPathRecordingResult result) {
    CMNode[] cmNodeArray = null;
    validate(ed, elementContent, comparator, result);
    if (result.isValid) {
        CMDataType dataType = ed.getDataType();
        int size = elementContent.size();
        cmNodeArray = new CMNode[size];
        Vector originList = result.getElementOriginList();
        int originListSize = originList.size();
        int originListIndex = 0;
        for (int i = 0; i < size; i++) {
            Object o = elementContent.get(i);
            if (comparator.isElement(o)) {
                if (originListIndex < originListSize) {
                    cmNodeArray[i] = (CMNode) originList.get(originListIndex);
                    originListIndex++;
                }
            } else if (comparator.isPCData(o)) {
                cmNodeArray[i] = dataType;
            }
        // else the CMNode at this index is null
        }
        result.setOriginArray(cmNodeArray);
    }
}
Also used : CMDataType(org.eclipse.wst.xml.core.internal.contentmodel.CMDataType) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) Vector(java.util.Vector)

Example 8 with CMDataType

use of org.eclipse.wst.xml.core.internal.contentmodel.CMDataType in project webtools.sourceediting by eclipse.

the class MarkupTagInfoProvider method printDefaultInfo.

/**
 * Adds the default info (element name, content model, data type) of
 * CMNode to the string buffer, sb
 */
protected void printDefaultInfo(CMNode node, StringBuffer sb) {
    {
        if (node.getNodeType() == CMNode.ELEMENT_DECLARATION) {
            CMElementDeclaration ed = (CMElementDeclaration) node;
            sb.append(PARAGRAPH_START + BOLD_START + XMLUIMessages.Element____1 + SPACE + BOLD_END);
            sb.append(node.getNodeName());
            sb.append(PARAGRAPH_END);
            printDocumentation(sb, node);
            if (ed.getContentType() == CMElementDeclaration.PCDATA) {
                CMDataType dataType = ed.getDataType();
                if (dataType != null) {
                    printDataTypeInfo(sb, dataType);
                }
            } else {
                CMDescriptionBuilder builder = new CMDescriptionBuilder();
                String description = builder.buildDescription(node);
                if ((description != null) && (description.length() > 0)) {
                    sb.append(PARAGRAPH_START + BOLD_START + XMLUIMessages.Content_Model____2 + SPACE + BOLD_END);
                    sb.append(description + PARAGRAPH_END);
                }
            }
        } else if (node.getNodeType() == CMNode.ATTRIBUTE_DECLARATION) {
            CMAttributeDeclaration ad = (CMAttributeDeclaration) node;
            sb.append(PARAGRAPH_START + BOLD_START + XMLUIMessages.Attribute____3 + SPACE + BOLD_END);
            sb.append(node.getNodeName());
            sb.append(PARAGRAPH_END);
            printDocumentation(sb, node);
            CMDataType dataType = ad.getAttrType();
            if (dataType != null) {
                printDataTypeInfo(sb, dataType);
            }
        } else if (node.getNodeType() == CMNode.DATA_TYPE) {
            sb.append(PARAGRAPH_START + BOLD_START + XMLUIMessages.Data_Type____4 + SPACE + BOLD_END);
            sb.append(node.getNodeName());
            sb.append(PARAGRAPH_END);
            printDocumentation(sb, node);
        }
    }
}
Also used : CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) CMDataType(org.eclipse.wst.xml.core.internal.contentmodel.CMDataType) CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration) CMDescriptionBuilder(org.eclipse.wst.xml.core.internal.contentmodel.util.CMDescriptionBuilder)

Example 9 with CMDataType

use of org.eclipse.wst.xml.core.internal.contentmodel.CMDataType in project webtools.sourceediting by eclipse.

the class DOMContentBuilderImpl method visitCMElementDeclaration.

public void visitCMElementDeclaration(CMElementDeclaration ed) {
    int forcedMin = (buildOptionalElements(buildPolicy) || alwaysVisit) ? 1 : 0;
    int min = Math.max(ed.getMinOccur(), forcedMin);
    // a group.
    if (!cmGroupStack.isEmpty()) {
        CMGroup group = (CMGroup) cmGroupStack.peek();
        int gmin = group.getMinOccur();
        if (gmin == 0)
            if (buildOptionalElements(buildPolicy)) {
            /* do nothing: min = min */
            } else {
                // min = 0
                min = min * gmin;
            }
        else {
            min = min * gmin;
        }
    }
    int max = Math.min(ed.getMaxOccur(), getNumOfRepeatableElements());
    if (max < min)
        max = min;
    alwaysVisit = false;
    // involved.
    if (// leave
    buildFirstSubstitution(buildPolicy) || isAbstract(ed)) // this
    // for
    // backward
    // compatibility
    // for now
    {
        // Note - To change so that if ed is optional, we do not
        // generate anything here.
        ed = getSubstitution(ed);
    // Note - the returned ed may be an abstract element in
    // which case the xml will be invalid.
    }
    if (min > 0 && !visitedCMElementDeclarationList.contains(ed)) {
        visitedCMElementDeclarationList.add(ed);
        for (int i = 1; i <= max; i++) {
            // create an Element for each
            Element element = null;
            if (rootElement != null) {
                element = rootElement;
                rootElement = null;
            } else {
                element = createElement(ed, computeName(ed, currentParent), currentParent);
            }
            // visit the children of the GrammarElement
            Node oldParent = currentParent;
            currentParent = element;
            handlePushParent(element, ed);
            namespaceTable.addElement(element);
            boolean oldAttachNodesToParent = attachNodesToParent;
            attachNodesToParent = true;
            // instead of calling super.visitCMElementDeclaration()
            // we duplicate the code with some minor modifications
            CMNamedNodeMap nodeMap = ed.getAttributes();
            int size = nodeMap.getLength();
            for (int j = 0; j < size; j++) {
                visitCMNode(nodeMap.item(j));
            }
            CMContent content = ed.getContent();
            if (content != null) {
                visitCMNode(content);
            }
            if (ed.getContentType() == CMElementDeclaration.PCDATA) {
                CMDataType dataType = ed.getDataType();
                if (dataType != null) {
                    visitCMDataType(dataType);
                }
            }
            // end duplication
            attachNodesToParent = oldAttachNodesToParent;
            handlePopParent(element, ed);
            currentParent = oldParent;
            linkNode(element);
        }
        int size = visitedCMElementDeclarationList.size();
        visitedCMElementDeclarationList.remove(size - 1);
    }
}
Also used : CMGroup(org.eclipse.wst.xml.core.internal.contentmodel.CMGroup) CMDataType(org.eclipse.wst.xml.core.internal.contentmodel.CMDataType) CMAnyElement(org.eclipse.wst.xml.core.internal.contentmodel.CMAnyElement) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) CMContent(org.eclipse.wst.xml.core.internal.contentmodel.CMContent)

Example 10 with CMDataType

use of org.eclipse.wst.xml.core.internal.contentmodel.CMDataType 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)

Aggregations

CMDataType (org.eclipse.wst.xml.core.internal.contentmodel.CMDataType)26 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)12 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)9 CMAttributeDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration)7 CMNode (org.eclipse.wst.xml.core.internal.contentmodel.CMNode)7 List (java.util.List)6 Element (org.w3c.dom.Element)6 ArrayList (java.util.ArrayList)5 CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)5 Node (org.w3c.dom.Node)5 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)4 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)4 Iterator (java.util.Iterator)3 Vector (java.util.Vector)3 CMNodeList (org.eclipse.wst.xml.core.internal.contentmodel.CMNodeList)3 IDOMElement (org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)3 NodeList (org.w3c.dom.NodeList)3 Image (org.eclipse.swt.graphics.Image)2 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)2 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)2