Search in sources :

Example 71 with CMElementDeclaration

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

the class BugFixesTest method testXSIType.

// Add tests here
@SuppressWarnings("unchecked")
public void testXSIType() {
    String soapSchemaURI = locateFileUsingCatalog("http://schemas.xmlsoap.org/wsdl/soap/");
    CMDocumentFactoryXSD factory = new CMDocumentFactoryXSD();
    assertNotNull("Assert factory is not null", factory);
    CMDocument cmDocument = factory.createCMDocument(soapSchemaURI);
    assertNotNull("Assert CMDocument is not null", cmDocument);
    CMNamedNodeMap elements = cmDocument.getElements();
    boolean foundDesiredElement = false;
    for (Iterator<CMElementDeclaration> i = elements.iterator(); i.hasNext(); ) {
        CMElementDeclaration element = i.next();
        if ("binding".equals(element.getElementName())) {
            foundDesiredElement = true;
            CMNamedNodeMap attributes = element.getAttributes();
            // Three attributes: required, transport and style
            assertNotNull(attributes);
            // If the xsi:type was present, it would be 4 attributes
            assertTrue(attributes.getLength() == 3);
            CMNode attrNode = null;
            attrNode = attributes.getNamedItem("required");
            assertNotNull(attrNode);
            attrNode = attributes.getNamedItem("transport");
            assertNotNull(attrNode);
            attrNode = attributes.getNamedItem("style");
            assertNotNull(attrNode);
            // Should be null!
            attrNode = attributes.getNamedItem("type");
            assertNull(attrNode);
            break;
        }
    }
    // if we didn't even find the binding element, then something terrible went wrong
    assertTrue(foundDesiredElement);
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) CMDocumentFactoryXSD(org.eclipse.wst.xsd.contentmodel.internal.CMDocumentFactoryXSD)

Example 72 with CMElementDeclaration

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

the class DefaultXMLPartitionFormatter method updateFormattingConstraints.

/**
 * Given the provided information (parentConstraints & currentDOMRegion),
 * update the formatting constraints (for this & child)
 *
 * @param parentConstraints
 *            can be null
 * @param thisConstraints
 *            can be null
 * @param childConstraints
 *            can be null
 * @param currentDOMRegion
 *            cannot be null
 */
protected void updateFormattingConstraints(XMLFormattingConstraints parentConstraints, XMLFormattingConstraints thisConstraints, XMLFormattingConstraints childConstraints, DOMRegion currentDOMRegion) {
    IStructuredDocumentRegion currentRegion = currentDOMRegion.documentRegion;
    IDOMNode currentNode = currentDOMRegion.domNode;
    // default to whatever parent's constraint said to do
    if (parentConstraints != null) {
        if (thisConstraints != null) {
            thisConstraints.copyConstraints(parentConstraints);
        }
        if (childConstraints != null) {
            childConstraints.copyConstraints(parentConstraints);
            // defaults are taken instead
            if (parentConstraints.isWhitespaceStrategyAHint())
                childConstraints.setWhitespaceStrategy(null);
        }
    }
    // set up constraints for direct children of document root
    Node parentNode = currentNode.getParentNode();
    if (parentNode != null && parentNode.getNodeType() == Node.DOCUMENT_NODE) {
        if (thisConstraints != null) {
            thisConstraints.setWhitespaceStrategy(XMLFormattingConstraints.IGNORE);
            thisConstraints.setIndentStrategy(XMLFormattingConstraints.NEW_LINE);
            thisConstraints.setIndentLevel(0);
        }
        if (childConstraints != null) {
            childConstraints.setWhitespaceStrategy(null);
            childConstraints.setIndentStrategy(null);
            childConstraints.setIndentLevel(0);
        }
    }
    // other conditions to check when setting up child constraints
    if (childConstraints != null) {
        XMLFormattingPreferences preferences = getFormattingPreferences();
        // on a new line and have an indent level of 0
        if (currentNode.getNodeType() == Node.DOCUMENT_NODE) {
            childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.IGNORE);
            childConstraints.setIndentStrategy(XMLFormattingConstraints.NEW_LINE);
            childConstraints.setIndentLevel(0);
        } else {
            // BUG108074 & BUG84688 - preserve whitespace in xsl:text &
            // xsl:attribute
            String nodeNamespaceURI = currentNode.getNamespaceURI();
            if (XSL_NAMESPACE.equals(nodeNamespaceURI)) {
                String nodeName = ((Element) currentNode).getLocalName();
                if (XSL_ATTRIBUTE.equals(nodeName) || XSL_TEXT.equals(nodeName)) {
                    childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.PRESERVE);
                }
            } else {
                // search within current tag for xml:space attribute
                ITextRegionList textRegions = currentRegion.getRegions();
                int i = 0;
                boolean xmlSpaceFound = false;
                boolean preserveFound = false;
                while (i < textRegions.size() && !xmlSpaceFound) {
                    ITextRegion textRegion = textRegions.get(i);
                    if (DOMRegionContext.XML_TAG_ATTRIBUTE_NAME.equals(textRegion.getType())) {
                        String regionText = currentRegion.getText(textRegion);
                        if (XML_SPACE.equals(regionText)) {
                            if ((i + 1) < textRegions.size()) {
                                ++i;
                                textRegion = textRegions.get(i);
                                if (DOMRegionContext.XML_TAG_ATTRIBUTE_EQUALS.equals(textRegion.getType()) && ((i + 1) < textRegions.size())) {
                                    ++i;
                                    textRegion = textRegions.get(i);
                                    regionText = currentRegion.getText(textRegion);
                                    if (PRESERVE.equals(regionText) || PRESERVE_QUOTED.equals(regionText)) {
                                        preserveFound = true;
                                    }
                                }
                            }
                            xmlSpaceFound = true;
                        }
                    }
                    ++i;
                }
                if (xmlSpaceFound) {
                    if (preserveFound) {
                        // preserve was found so set the strategy
                        childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.PRESERVE);
                    } else {
                        // xml:space was found but it was not collapse, so
                        // use default whitespace strategy
                        childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.DEFAULT);
                    }
                } else {
                    // how to hande nodes that have nonwhitespace text
                    // content
                    NodeList nodeList = currentNode.getChildNodes();
                    int length = nodeList.getLength();
                    int index = 0;
                    boolean textNodeFound = false;
                    // still reflect the parent constraints
                    while (index < length && !textNodeFound && parentConstraints != null && !XMLFormattingConstraints.PRESERVE.equals(parentConstraints.getWhitespaceStrategy())) {
                        Node childNode = nodeList.item(index);
                        if (childNode.getNodeType() == Node.TEXT_NODE) {
                            textNodeFound = !((IDOMText) childNode).isElementContentWhitespace();
                        }
                        ++index;
                    }
                    if (textNodeFound) {
                        if (length > 1) {
                            // more in here than just text, so consider
                            // this mixed content
                            childConstraints.setWhitespaceStrategy(preferences.getMixedWhitespaceStrategy());
                            childConstraints.setIndentStrategy(preferences.getMixedIndentStrategy());
                        } else {
                            // there's only text
                            childConstraints.setWhitespaceStrategy(preferences.getTextWhitespaceStrategy());
                            childConstraints.setIndentStrategy(preferences.getTextIndentStrategy());
                        }
                        childConstraints.setIsWhitespaceStrategyAHint(true);
                        childConstraints.setIsIndentStrategyAHint(true);
                    }
                    // try referring to content model for information on
                    // whitespace & indent strategy
                    ModelQueryAdapter adapter = (ModelQueryAdapter) ((IDOMDocument) currentNode.getOwnerDocument()).getAdapterFor(ModelQueryAdapter.class);
                    CMElementDeclaration elementDeclaration = (CMElementDeclaration) adapter.getModelQuery().getCMNode(currentNode);
                    if (elementDeclaration != null) {
                        // follow whitespace strategy preference for
                        // pcdata content
                        int contentType = elementDeclaration.getContentType();
                        String facetValue = null;
                        if (elementDeclaration.getDataType() != null)
                            facetValue = (String) elementDeclaration.getDataType().getProperty(PROPERTY_WHITESPACE_FACET);
                        if (facetValue != null) {
                            if (PRESERVE.equals(facetValue))
                                childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.PRESERVE);
                            else // For XSD types, "collapse" corresponds to the IGNOREANDTRIM strategy
                            if (COLLAPSE.equals(facetValue))
                                childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.IGNOREANDTRIM);
                            else if (REPLACE.equals(facetValue))
                                childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.REPLACE);
                        } else if (contentType == CMElementDeclaration.PCDATA && parentConstraints != null && !XMLFormattingConstraints.PRESERVE.equals(parentConstraints.getWhitespaceStrategy())) {
                            childConstraints.setWhitespaceStrategy(preferences.getPCDataWhitespaceStrategy());
                        } else if (contentType == CMElementDeclaration.ELEMENT && parentConstraints != null && !XMLFormattingConstraints.PRESERVE.equals(parentConstraints.getWhitespaceStrategy())) {
                            childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.IGNORE);
                            childConstraints.setIndentStrategy(XMLFormattingConstraints.INDENT);
                            childConstraints.setIsWhitespaceStrategyAHint(true);
                            childConstraints.setIsIndentStrategyAHint(true);
                        } else {
                            // look for xml:space in content model
                            CMNamedNodeMap cmAttributes = elementDeclaration.getAttributes();
                            // Not needed - we're looking for xml:space
                            // CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(cmAttributes);
                            // List nodes = ModelQueryUtil.getModelQuery(currentNode.getOwnerDocument()).getAvailableContent((Element) currentNode, elementDeclaration, 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);
                            // }
                            // }
                            // cmAttributes = allAttributes;
                            // Check implied values from the DTD way.
                            CMAttributeDeclaration attributeDeclaration = (CMAttributeDeclaration) cmAttributes.getNamedItem(XML_SPACE);
                            if (attributeDeclaration != null) {
                                // CMAttributeDeclaration found, check
                                // it
                                // out.
                                // BUG214516/196544 - Fixed NPE that was caused by an attr having
                                // a null attr type
                                String defaultValue = null;
                                CMDataType attrType = attributeDeclaration.getAttrType();
                                if (attrType != null) {
                                    if ((attrType.getImpliedValueKind() != CMDataType.IMPLIED_VALUE_NONE) && attrType.getImpliedValue() != null)
                                        defaultValue = attrType.getImpliedValue();
                                    else if ((attrType.getEnumeratedValues() != null) && (attrType.getEnumeratedValues().length > 0)) {
                                        defaultValue = attrType.getEnumeratedValues()[0];
                                    }
                                }
                                // default.
                                if (PRESERVE.equals(defaultValue))
                                    childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.PRESERVE);
                                else
                                    childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.DEFAULT);
                            } else // If the node has no attributes, inherit the parents whitespace strategy
                            {
                                if (parentConstraints != null)
                                    childConstraints.setWhitespaceStrategy(parentConstraints.getWhitespaceStrategy());
                                else
                                    childConstraints.setWhitespaceStrategy(null);
                            }
                        }
                    }
                }
            }
        }
        // set default values according to preferences
        if (childConstraints.getWhitespaceStrategy() == null) {
            childConstraints.setWhitespaceStrategy(preferences.getElementWhitespaceStrategy());
        }
        if (childConstraints.getIndentStrategy() == null) {
            childConstraints.setIndentStrategy(preferences.getElementIndentStrategy());
        }
    }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ModelQueryAdapter(org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter) CMDataType(org.eclipse.wst.xml.core.internal.contentmodel.CMDataType) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IDOMText(org.eclipse.wst.xml.core.internal.provisional.document.IDOMText) CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)

Example 73 with CMElementDeclaration

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

the class CMValidatorValidationTest method verify.

private void verify(Element parentElement, String[] childElementsAvailable) {
    List list = new ArrayList();
    ModelQuery modelQuery = ModelQueryUtil.getModelQuery(parentElement.getOwnerDocument());
    CMElementDeclaration cmElementDeclaration = modelQuery.getCMElementDeclaration(parentElement);
    modelQuery.getInsertActions(parentElement, cmElementDeclaration, -1, ModelQuery.INCLUDE_CHILD_NODES, ModelQuery.VALIDITY_STRICT, list);
    assertEquals(childElementsAvailable.length, list.size());
    List availableNodeNameList = new ArrayList();
    Iterator iterator = list.iterator();
    while (iterator.hasNext()) {
        availableNodeNameList.add(((ModelQueryAction) iterator.next()).getCMNode().getNodeName());
    }
    for (int i = 0; i < childElementsAvailable.length; i++) {
        if (availableNodeNameList.indexOf(childElementsAvailable[i]) == -1) {
            fail();
        }
    }
}
Also used : CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) ModelQueryAction(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQueryAction) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)

Example 74 with CMElementDeclaration

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

the class XSITypeTest method testXSIType.

public void testXSIType() {
    IStructuredModel structuredModel = null;
    try {
        // $NON-NLS-1$
        IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(PROJECT_NAME + "/test.xml"));
        IModelManager modelManager = StructuredModelManager.getModelManager();
        structuredModel = modelManager.getModelForRead(file);
        Document document = ((IDOMModel) structuredModel).getDocument();
        ModelQuery modelQuery = ModelQueryUtil.getModelQuery(document);
        NodeList nodeList = null;
        CMElementDeclaration cmElementDeclaration = null;
        // Check for local XSI type
        // $NON-NLS-1$
        nodeList = document.getElementsByTagName("a");
        cmElementDeclaration = modelQuery.getCMElementDeclaration((Element) nodeList.item(0));
        // $NON-NLS-1$
        assertNotNull("Local XSI type failed to be resolved", cmElementDeclaration);
        // Check for external XSI type
        // $NON-NLS-1$
        nodeList = document.getElementsByTagName("b");
        cmElementDeclaration = modelQuery.getCMElementDeclaration((Element) nodeList.item(0));
        // $NON-NLS-1$
        assertNotNull("External XSI type failed to be resolved", cmElementDeclaration);
    } catch (Exception exception) {
        exception.printStackTrace();
    } finally {
        if (structuredModel != null) {
            structuredModel.releaseFromRead();
        }
    }
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) Document(org.w3c.dom.Document)

Example 75 with CMElementDeclaration

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

the class AbstractContentAssistProcessor method getAvailableRootChildren.

// returns a list of CMElementDeclarations
protected List getAvailableRootChildren(Document document, int childIndex) {
    List list = null;
    // extract the valid 'root' node name from the DocumentType Node
    DocumentType docType = document.getDoctype();
    String rootName = null;
    if (docType != null) {
        rootName = docType.getNodeName();
    }
    if (rootName == null) {
        return new ArrayList(0);
    }
    for (Node child = document.getFirstChild(); child != null; child = child.getNextSibling()) {
        // is it required to be an Element?
        if ((child.getNodeType() == Node.ELEMENT_NODE) && stringsEqual(child.getNodeName(), rootName)) {
            // count it as present
            if ((child instanceof IDOMNode) && ((((IDOMNode) child).getStartStructuredDocumentRegion() == null) || (((IDOMNode) child).getEndStructuredDocumentRegion() == null))) {
                continue;
            }
            if (Debug.displayInfo) {
                // $NON-NLS-1$
                System.out.println(rootName + " already present!");
            }
            setErrorMessage(NLS.bind(XMLUIMessages.The_document_element__, (new Object[] { rootName })));
            return new ArrayList(0);
        }
    }
    list = new ArrayList(1);
    ModelQuery modelQuery = ModelQueryUtil.getModelQuery(document);
    if (modelQuery != null) {
        CMDocument cmdoc = modelQuery.getCorrespondingCMDocument(document);
        if (cmdoc != null) {
            if (rootName != null) {
                CMElementDeclaration rootDecl = (CMElementDeclaration) cmdoc.getElements().getNamedItem(rootName);
                if (rootDecl != null) {
                    list.add(rootDecl);
                } else {
                    // supply the given document name anyway, even if it
                    // is an error
                    list.add(new SimpleCMElementDeclaration(rootName));
                    if (Debug.displayInfo || Debug.displayWarnings) {
                        // $NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$
                        System.out.println("No definition found for " + rootName + " in " + docType.getPublicId() + "/" + docType.getSystemId());
                    }
                    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
                    String location = "" + (docType.getPublicId() != null ? docType.getPublicId() + "/" : "") + (docType.getSystemId() != null ? docType.getSystemId() : "");
                    // $NON-NLS-4$//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$
                    if (location.length() > 0) {
                        setErrorMessage(NLS.bind(XMLUIMessages.No_definition_for_in, (new Object[] { rootName, location })));
                    } else {
                        setErrorMessage(NLS.bind(XMLUIMessages.No_definition_for, (new Object[] { rootName })));
                    }
                }
            }
        } else {
            if (Debug.displayInfo || Debug.displayWarnings) {
                // $NON-NLS-1$
                System.out.println("No content model found.");
            }
            // $NON-NLS-1$
            // $NON-NLS-1$
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
            String location = "" + (docType.getPublicId() != null ? docType.getPublicId() + "/" : "") + (docType.getSystemId() != null ? docType.getSystemId() : "");
            // $NON-NLS-4$//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$
            if (location.length() > 0) {
                setErrorMessage(NLS.bind(XMLUIMessages.No_content_model_for, (new Object[] { location })));
            } else {
                setErrorMessage(XMLUIMessages.No_content_model_found_UI_);
            }
        }
    }
    return list;
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) DocumentType(org.w3c.dom.DocumentType) 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) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)

Aggregations

CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)147 CMNode (org.eclipse.wst.xml.core.internal.contentmodel.CMNode)53 List (java.util.List)46 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)43 Element (org.w3c.dom.Element)41 ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)38 CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)37 ArrayList (java.util.ArrayList)35 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)33 Node (org.w3c.dom.Node)32 CMAttributeDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration)30 NodeList (org.w3c.dom.NodeList)28 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)24 Iterator (java.util.Iterator)19 CMNodeList (org.eclipse.wst.xml.core.internal.contentmodel.CMNodeList)19 CustomCompletionProposal (org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal)17 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)17 NamedNodeMap (org.w3c.dom.NamedNodeMap)17 Image (org.eclipse.swt.graphics.Image)15 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)15