Search in sources :

Example 61 with CMNode

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

the class XPathElementContentAssist method createXPathXMLProposals.

private void createXPathXMLProposals(Node ancestorNode, Iterator<CMNode> cmNodeIt) {
    while (cmNodeIt.hasNext()) {
        CMNode cmNode = cmNodeIt.next();
        String proposedText = getRequiredName(ancestorNode, cmNode);
        if (!(proposedText.contains("xsl:") || proposedText.contains("xslt:"))) {
            // $NON-NLS-1$ //$NON-NLS-2$
            int offset = getReplacementBeginPosition();
            Image image = getCMNodeImage(cmNode);
            int startLength = getCursorPosition() - offset;
            String additionalInfo = getInfoProvider().getInfo(cmNode);
            if (matchString.length() > 0) {
                if (proposedText.startsWith(matchString)) {
                    CustomCompletionProposal proposal = createProposal(proposedText, additionalInfo, offset, image, startLength);
                    addProposal(proposal);
                }
            } else {
                CustomCompletionProposal proposal = createProposal(proposedText, additionalInfo, offset, image, startLength);
                addProposal(proposal);
            }
        }
    }
}
Also used : CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) Image(org.eclipse.swt.graphics.Image)

Example 62 with CMNode

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

the class CMValidatorTest method main.

public static void main(String[] arg) {
    if (arg.length > 1) {
        try {
            // CMDocumentFactoryRegistry.getInstance().registerCMDocumentBuilderWithClassName("org.eclipse.wst.xml.core.internal.contentmodel.mofimpl.CMDocumentBuilderImpl");
            String grammarFileName = arg[0];
            String elementName = arg[1];
            CMDocument cmDocument = ContentModelManager.getInstance().createCMDocument(grammarFileName, null);
            CMNamedNodeMap elementMap = cmDocument.getElements();
            CMElementDeclaration element = (CMElementDeclaration) elementMap.getNamedItem(elementName);
            if (element != null) {
                /*
          println("found element [" + elementName + "]  contentType = " + element.getContentType());
          GraphNode graphNode = createGraph(element);
          printGraph(graphNode);
          */
                // $NON-NLS-1$
                println("-------------- begin validate ---------------");
                StringElementContentComparator comparator = new StringElementContentComparator();
                CMValidator validator = new CMValidator();
                ElementPathRecordingResult result = new ElementPathRecordingResult();
                validator.getOriginArray(element, CMValidator.createStringList(arg, 2), comparator, result);
                if (result.isValid) {
                    CMNode[] nodeMapping = result.getOriginArray();
                    // $NON-NLS-1$
                    println("Validation Success!");
                    // $NON-NLS-1$
                    print("  ");
                    for (int i = 0; i < nodeMapping.length; i++) {
                        // $NON-NLS-1$
                        String name = nodeMapping[i] != null ? nodeMapping[i].getNodeName() : "null";
                        // $NON-NLS-1$ //$NON-NLS-2$
                        print("[" + name + "]");
                    }
                    // $NON-NLS-1$
                    println("");
                } else {
                    // $NON-NLS-1$
                    println("Validation Failed! ");
                    if (result.errorMessage != null) {
                        // $NON-NLS-1$
                        println("  " + result.errorMessage);
                    }
                }
                // $NON-NLS-1$
                println("-------------- end validate ---------------");
            } else {
                // $NON-NLS-1$ //$NON-NLS-2$
                println("element [" + elementName + "] can not be found");
            }
        } catch (Exception e) {
            // $NON-NLS-1$
            println("CMValidator error");
            e.printStackTrace();
        }
    } else {
        // $NON-NLS-1$ //$NON-NLS-2$
        println("2 args required... only " + arg.length + " provided");
        // $NON-NLS-1$
        println("usage java org.eclipse.wst.newxml.util.XMLUtil grammarFileName rootElementName pattern");
    }
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) StringElementContentComparator(org.eclipse.wst.xml.core.internal.contentmodel.internal.util.CMValidator.StringElementContentComparator) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) CMValidator(org.eclipse.wst.xml.core.internal.contentmodel.internal.util.CMValidator) ElementPathRecordingResult(org.eclipse.wst.xml.core.internal.contentmodel.internal.util.CMValidator.ElementPathRecordingResult) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)

Example 63 with CMNode

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

the class AttributeShowingLabelProvider method getText.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
	 */
@Override
public String getText(Object o) {
    StringBuffer text = null;
    if (o instanceof Node) {
        Node node = (Node) o;
        if ((node.getNodeType() == Node.ELEMENT_NODE) && fShowAttributes) {
            text = new StringBuffer(super.getText(o));
            // https://bugs.eclipse.org/bugs/show_bug.cgi?id=88444
            if (node.hasAttributes()) {
                Element element = (Element) node;
                NamedNodeMap attributes = element.getAttributes();
                Node idTypedAttribute = null;
                Node requiredAttribute = null;
                boolean hasId = false;
                boolean hasName = false;
                Node shownAttribute = null;
                // try to get content model element
                // declaration
                CMElementDeclaration elementDecl = null;
                ModelQuery mq = ModelQueryUtil.getModelQuery(element.getOwnerDocument());
                if (mq != null) {
                    elementDecl = mq.getCMElementDeclaration(element);
                }
                // ID
                if (elementDecl != null) {
                    int i = 0;
                    while ((i < attributes.getLength()) && (idTypedAttribute == null)) {
                        Node attr = attributes.item(i);
                        String attrName = attr.getNodeName();
                        CMNamedNodeMap attributeDeclarationMap = elementDecl.getAttributes();
                        CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(attributeDeclarationMap);
                        List nodes = ModelQueryUtil.getModelQuery(node.getOwnerDocument()).getAvailableContent(element, elementDecl, ModelQuery.INCLUDE_ATTRIBUTES);
                        for (int k = 0; k < nodes.size(); k++) {
                            CMNode cmnode = (CMNode) nodes.get(k);
                            if (cmnode.getNodeType() == CMNode.ATTRIBUTE_DECLARATION) {
                                allAttributes.put(cmnode);
                            }
                        }
                        attributeDeclarationMap = allAttributes;
                        CMAttributeDeclaration attrDecl = (CMAttributeDeclaration) attributeDeclarationMap.getNamedItem(attrName);
                        if (attrDecl != null) {
                            if ((attrDecl.getAttrType() != null) && (CMDataType.ID.equals(attrDecl.getAttrType().getDataTypeName()))) {
                                idTypedAttribute = attr;
                            } else if ((attrDecl.getUsage() == CMAttributeDeclaration.REQUIRED) && (requiredAttribute == null)) {
                                // as a backup, keep tabs on
                                // any required
                                // attributes
                                requiredAttribute = attr;
                            } else {
                                hasId = hasId || attrName.equals(ATTR_ID);
                                hasName = hasName || attrName.equals(ATTR_NAME);
                            }
                        }
                        ++i;
                    }
                }
                /*
					 * If no suitable attribute was found, try using a
					 * required attribute, if none, then prefer "id" or
					 * "name", otherwise just use first attribute
					 */
                if (idTypedAttribute != null) {
                    shownAttribute = idTypedAttribute;
                } else if (requiredAttribute != null) {
                    shownAttribute = requiredAttribute;
                } else if (hasId) {
                    shownAttribute = attributes.getNamedItem(ATTR_ID);
                } else if (hasName) {
                    shownAttribute = attributes.getNamedItem(ATTR_NAME);
                }
                if (shownAttribute == null) {
                    shownAttribute = attributes.item(0);
                }
                // display the attribute and value (without quotes)
                String attributeName = shownAttribute.getNodeName();
                if ((attributeName != null) && (attributeName.length() > 0)) {
                    // $NON-NLS-1$
                    text.append(" ");
                    text.append(attributeName);
                    String attributeValue = shownAttribute.getNodeValue();
                    if ((attributeValue != null) && (attributeValue.length() > 0)) {
                        // $NON-NLS-1$
                        text.append("=");
                        text.append(StringUtils.strip(attributeValue));
                    }
                }
            // if (XSLCore.XSL_NAMESPACE_URI.equals(node.getNamespaceURI())) {
            // Element el = (Element) node;
            // Attr attr = el.getAttributeNode("mode"); //$NON-NLS-1$
            // if (attr != null) {
            // text.append(" "); //$NON-NLS-1$
            // text.append(attr.getName());
            // text.append("="); //$NON-NLS-1$
            // text.append(StringUtils.strip(attr.getNodeValue()));
            // }
            // }
            }
        } else {
            text = new StringBuffer(super.getText(o));
        }
    } else {
        return super.toString();
    }
    return text.toString();
}
Also used : CMNamedNodeMapImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) NamedNodeMap(org.w3c.dom.NamedNodeMap) Node(org.w3c.dom.Node) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) Element(org.w3c.dom.Element) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) List(java.util.List) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)

Example 64 with CMNode

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

the class AbstractXMLModelQueryCompletionProposalComputer method getAvailableChildElementDeclarations.

/**
 * returns a list of CMNodes that are available within this parent context
 * Given the grammar shown below and a snippet of XML code (where the '|'
 * indicated the cursor position)
 * the list would return all of the element declarations that are
 * potential child elements of Foo.
 *
 * grammar : Foo -> (A, B, C)
 * snippet : <Foo><A>|
 * result : {A, B, C}
 *
 * TODO cs... do we need to pass in the 'kindOfAction'? Seems to me we
 * could assume it's always an insert.
 *
 * @param parent
 * @param childPosition
 * @param kindOfAction
 * @return
 */
private List getAvailableChildElementDeclarations(Element parent, int childPosition, int kindOfAction) {
    List modelQueryActions = getAvailableModelQueryActionsAtIndex(parent, childPosition, ModelQuery.VALIDITY_NONE);
    Iterator iterator = modelQueryActions.iterator();
    List cmnodes = new Vector();
    while (iterator.hasNext()) {
        ModelQueryAction action = (ModelQueryAction) iterator.next();
        if ((childPosition < 0) || (((action.getStartIndex() <= childPosition) && (childPosition <= action.getEndIndex())) && (action.getKind() == kindOfAction))) {
            CMNode actionCMNode = action.getCMNode();
            if ((actionCMNode != null) && !cmnodes.contains(actionCMNode)) {
                cmnodes.add(actionCMNode);
            }
        }
    }
    return cmnodes;
}
Also used : ModelQueryAction(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQueryAction) Iterator(java.util.Iterator) List(java.util.List) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ArrayList(java.util.ArrayList) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) Vector(java.util.Vector)

Example 65 with CMNode

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

the class AbstractXMLModelQueryCompletionProposalComputer method getValidChildElementDeclarations.

/**
 * returns a list of CMNodes that can be validly inserted at this
 * childPosition
 * Given the grammar shown below and a snippet of XML code (where the '|'
 * indicated the cursor position)
 * the list would return only the element declarations can be inserted
 * while maintaing validity of the content.
 *
 * grammar : Foo -> (A, B, C)
 * snippet : <Foo><A>|
 * result : {B}
 *
 * @param parent
 * @param childPosition
 * @param kindOfAction
 * @return
 */
private List getValidChildElementDeclarations(Element parent, int childPosition, int kindOfAction) {
    List modelQueryActions = getAvailableModelQueryActionsAtIndex(parent, childPosition, ModelQuery.VALIDITY_STRICT);
    Iterator iterator = modelQueryActions.iterator();
    List cmnodes = new Vector();
    while (iterator.hasNext()) {
        ModelQueryAction action = (ModelQueryAction) iterator.next();
        if ((childPosition < 0) || (((action.getStartIndex() <= childPosition) && (childPosition <= action.getEndIndex())) && (action.getKind() == kindOfAction))) {
            CMNode actionCMNode = action.getCMNode();
            if ((actionCMNode != null) && !cmnodes.contains(actionCMNode)) {
                cmnodes.add(actionCMNode);
            }
        }
    }
    return cmnodes;
}
Also used : ModelQueryAction(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQueryAction) Iterator(java.util.Iterator) List(java.util.List) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ArrayList(java.util.ArrayList) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) Vector(java.util.Vector)

Aggregations

CMNode (org.eclipse.wst.xml.core.internal.contentmodel.CMNode)133 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)37 List (java.util.List)36 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)35 ArrayList (java.util.ArrayList)28 Iterator (java.util.Iterator)23 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)20 CMNodeList (org.eclipse.wst.xml.core.internal.contentmodel.CMNodeList)19 CMAttributeDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration)17 CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)17 Element (org.w3c.dom.Element)16 NodeList (org.w3c.dom.NodeList)15 CMNamedNodeMapImpl (org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl)14 ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)13 Node (org.w3c.dom.Node)13 NamedNodeMap (org.w3c.dom.NamedNodeMap)10 ModelQueryAction (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQueryAction)9 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)9 Image (org.eclipse.swt.graphics.Image)8 CustomCompletionProposal (org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal)8