Search in sources :

Example 11 with ModelQueryAdapter

use of org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter in project webtools.sourceediting by eclipse.

the class ElementImplTests method testCMAttrWithNullImpliedValue.

public void testCMAttrWithNullImpliedValue() {
    IDOMModel model = null;
    try {
        model = (IDOMModel) getModelForRead("testfiles/time.xml");
        if (model != null) {
            IDOMDocument document = model.getDocument();
            final String ATTR_NAME = "second";
            // Setup a ModelQueryAdapter whose sole purpose it to provide a attribute declaration with a null implied value
            document.addAdapter(new ModelQueryAdapter() {

                public boolean isAdapterForType(Object type) {
                    return type.equals(ModelQueryAdapter.class);
                }

                public void notifyChanged(INodeNotifier notifier, int eventType, Object changedFeature, Object oldValue, Object newValue, int pos) {
                }

                public CMDocumentCache getCMDocumentCache() {
                    return null;
                }

                public org.eclipse.wst.common.uriresolver.internal.provisional.URIResolver getIdResolver() {
                    return null;
                }

                public ModelQuery getModelQuery() {
                    return new ModelQueryImpl(null) {

                        /* (non-Javadoc)
							 * @see org.eclipse.wst.xml.core.internal.contentmodel.modelqueryimpl.ModelQueryImpl#getCMElementDeclaration(org.w3c.dom.Element)
							 */
                        public CMElementDeclaration getCMElementDeclaration(Element element) {
                            final CMElementDeclaration decl = new CMElementDeclarationImpl(null, null);
                            CMNamedNodeMapImpl map = (CMNamedNodeMapImpl) decl.getAttributes();
                            map.put(new CMAttributeDeclarationImpl(ATTR_NAME, CMAttributeDeclaration.OPTIONAL, new CMDataTypeImpl(ATTR_NAME, (String) null)));
                            return decl;
                        }
                    };
                }

                public void release() {
                }

                public void setIdResolver(org.eclipse.wst.common.uriresolver.internal.provisional.URIResolver newIdResolver) {
                }
            });
            Element element = document.getDocumentElement();
            assertNotNull(element);
            // Default value should be 0
            assertEquals("", element.getAttribute(ATTR_NAME));
        }
    } finally {
        if (model != null) {
            model.releaseFromRead();
        }
    }
}
Also used : CMNamedNodeMapImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl) CMAttributeDeclarationImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMAttributeDeclarationImpl) ModelQueryAdapter(org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) Element(org.w3c.dom.Element) URIResolver(org.eclipse.wst.sse.core.internal.util.URIResolver) CMDataTypeImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMDataTypeImpl) CMElementDeclarationImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMElementDeclarationImpl) INodeNotifier(org.eclipse.wst.sse.core.internal.provisional.INodeNotifier) ModelQueryImpl(org.eclipse.wst.xml.core.internal.contentmodel.modelqueryimpl.ModelQueryImpl) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) CMDocumentCache(org.eclipse.wst.xml.core.internal.contentmodel.util.CMDocumentCache) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)

Example 12 with ModelQueryAdapter

use of org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter 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 13 with ModelQueryAdapter

use of org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter in project webtools.sourceediting by eclipse.

the class ModelQueryUtil method getModelQueryAdapter.

public static ModelQueryAdapter getModelQueryAdapter(Document node) {
    ModelQueryAdapter result = null;
    if (node instanceof INodeNotifier) {
        INodeNotifier notifier = (INodeNotifier) node;
        result = (ModelQueryAdapter) notifier.getAdapterFor(ModelQueryAdapter.class);
    }
    return result;
}
Also used : ModelQueryAdapter(org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter) INodeNotifier(org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)

Example 14 with ModelQueryAdapter

use of org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter in project webtools.sourceediting by eclipse.

the class JFaceNodeAdapterFactory method initAdapter.

protected void initAdapter(INodeAdapter adapter, INodeNotifier node) {
    Assert.isTrue(cmDocumentManager == null);
    Assert.isTrue(fCMDocumentManagerListener == null);
    // register for CMDocumentManager events
    ModelQueryAdapter mqadapter = (ModelQueryAdapter) node.getAdapterFor(ModelQueryAdapter.class);
    if (mqadapter != null) {
        ModelQuery mquery = mqadapter.getModelQuery();
        if ((mquery != null) && (mquery.getCMDocumentManager() != null)) {
            cmDocumentManager = mquery.getCMDocumentManager();
            fCMDocumentManagerListener = new CMDocumentManagerListenerImpl();
            cmDocumentManager.addListener(fCMDocumentManagerListener);
        }
    }
}
Also used : ModelQueryAdapter(org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)

Example 15 with ModelQueryAdapter

use of org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter in project webtools.sourceediting by eclipse.

the class TestPageDirective method testAdapt.

public void testAdapt() {
    IDOMModel model = createUnmanagedHTMLModel();
    PageDirectiveAdapter pageDirectiveAdapter = (PageDirectiveAdapter) model.getDocument().getAdapterFor(PageDirectiveAdapter.class);
    Node ownerNode = model.getDocument();
    ModelQueryAdapter embeddedAdapter = (ModelQueryAdapter) pageDirectiveAdapter.adapt((INodeNotifier) ownerNode, ModelQueryAdapter.class);
    assertNotNull("could not adapt embedded adapter", embeddedAdapter);
}
Also used : ModelQueryAdapter(org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) Node(org.w3c.dom.Node) PageDirectiveAdapter(org.eclipse.jst.jsp.core.internal.document.PageDirectiveAdapter) INodeNotifier(org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)

Aggregations

ModelQueryAdapter (org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter)25 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)14 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)11 INodeNotifier (org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)8 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)7 ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)6 Document (org.w3c.dom.Document)6 IFile (org.eclipse.core.resources.IFile)5 Element (org.w3c.dom.Element)5 Node (org.w3c.dom.Node)5 NodeList (org.w3c.dom.NodeList)5 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)4 CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)4 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)4 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)4 IProject (org.eclipse.core.resources.IProject)3 PageDirectiveAdapter (org.eclipse.jst.jsp.core.internal.document.PageDirectiveAdapter)3 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)3 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)3 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)3