Search in sources :

Example 56 with CMElementDeclaration

use of org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration 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 57 with CMElementDeclaration

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

the class CMPrinter method getProperties.

public List getProperties(CMNode cmnode) {
    List properties = new Vector();
    if (cmnode != null) {
        // $NON-NLS-1$
        properties.add("CMDocument");
        // $NON-NLS-1$
        properties.add("documentation");
        // $NON-NLS-1$
        properties.add("http://org.eclipse.wst/cm/properties/usesLocalElementDeclarations");
        // $NON-NLS-1$
        properties.add("http://org.eclipse.wst/cm/properties/isNameSpaceAware");
        // $NON-NLS-1$
        properties.add("http://org.eclipse.wst/cm/properties/nsPrefixQualification");
        // $NON-NLS-1$
        properties.add("http://org.eclipse.wst/cm/properties/nillable");
        // $NON-NLS-1$
        properties.add("http://org.eclipse.wst/cm/properties/mofNotifier");
        // $NON-NLS-1$
        properties.add("spec");
        if (cmnode instanceof CMElementDeclaration) {
            // $NON-NLS-1$
            properties.add("http://org.eclipse.wst/cm/properties/definitionInfo");
            // $NON-NLS-1$
            properties.add("http://org.eclipse.wst/cm/properties/definition");
            // $NON-NLS-1$
            properties.add("XSITypes");
            // $NON-NLS-1$
            properties.add("DerivedElementDeclaration");
            // properties.add("SubstitutionGroup"); Currrently broken for nexted xsd files
            // $NON-NLS-1$
            properties.add("Abstract");
        }
        if (cmnode instanceof CMDocument) {
            // $NON-NLS-1$
            properties.add("http://org.eclipse.wst/cm/properties/targetNamespaceURI");
            // $NON-NLS-1$
            properties.add("http://org.eclipse.wst/cm/properties/importedNamespaceInfo");
            // $NON-NLS-1$
            properties.add("http://org.eclipse.wst/cm/properties/namespaceInfo");
            // $NON-NLS-1$
            properties.add("http://org.eclipse.wst/cm/properties/elementFormDefault");
            // $NON-NLS-1$
            properties.add("annotationMap");
        }
    }
    return properties;
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) List(java.util.List) CMNodeList(org.eclipse.wst.xml.core.internal.contentmodel.CMNodeList) Vector(java.util.Vector)

Example 58 with CMElementDeclaration

use of org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration 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 59 with CMElementDeclaration

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

the class TestAttributesOrder method ensureDocumentHasGrammar.

/**
 * Reusable test to make sure the XML model for the given file has a grammar.
 *
 * @param file
 *          the file containing the XML document.
 */
private void ensureDocumentHasGrammar(IFile file) throws IOException, CoreException {
    IStructuredModel model = null;
    try {
        IModelManager modelManager = StructuredModelManager.getModelManager();
        model = modelManager.getModelForRead(file);
        // $NON-NLS-1$
        assertNotNull("failure loading model", model);
        IDOMModel domModel = (IDOMModel) model;
        IDOMDocument document = domModel.getDocument();
        // $NON-NLS-1$
        assertNotNull("failure getting document", document);
        ModelQuery modelQuery = ModelQueryUtil.getModelQuery(document);
        // $NON-NLS-1$
        assertNotNull("ModelQuery is missing", modelQuery);
        IDOMElement documentElement = (IDOMElement) document.getDocumentElement();
        // $NON-NLS-1$
        assertNotNull("missing document element", documentElement);
        CMElementDeclaration cmElementDeclaration = modelQuery.getCMElementDeclaration(documentElement);
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        assertNotNull("No element declaration for" + documentElement.getNodeName() + " (" + documentElement.getNamespaceURI() + ")", cmElementDeclaration);
        assertNotNull("No content assist available for" + documentElement.getNodeName() + " (" + documentElement.getNamespaceURI() + ")", modelQuery.getAvailableContent(documentElement, cmElementDeclaration, ModelQuery.INCLUDE_CHILD_NODES));
    } finally {
        if (model != null) {
            model.releaseFromRead();
        }
    }
}
Also used : CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 60 with CMElementDeclaration

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

the class TestCyclicGroupReferences method testCyclicGroupReferences.

/**
 * Test CMVisitor for cyclic group references.
 */
public void testCyclicGroupReferences() {
    // $NON-NLS-1$
    IFile file = getFile("Test.xml");
    CMVisitor cmVisitor = new CMVisitor();
    IStructuredModel model = null;
    try {
        IModelManager modelManager = StructuredModelManager.getModelManager();
        model = modelManager.getModelForRead(file);
        // $NON-NLS-1$
        assertNotNull("failure loading model", model);
        IDOMModel domModel = (IDOMModel) model;
        IDOMDocument document = domModel.getDocument();
        // $NON-NLS-1$
        assertNotNull("failure getting document", document);
        ModelQuery modelQuery = ModelQueryUtil.getModelQuery(model);
        // $NON-NLS-1$
        assertNotNull("ModelQuery is missing", modelQuery);
        IDOMElement documentElement = (IDOMElement) document.getDocumentElement();
        // $NON-NLS-1$
        assertNotNull("missing document element", documentElement);
        CMElementDeclaration cmElementDeclaration = modelQuery.getCMElementDeclaration(documentElement);
        assertNotNull("No element declaration for" + documentElement.getNodeName() + " (" + documentElement.getNamespaceURI() + ")", // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        cmElementDeclaration);
        cmVisitor.visitCMElementDeclaration(cmElementDeclaration);
    } catch (Throwable th) {
        fail("Test failed :" + th.getClass().getName());
    } finally {
        if (model != null) {
            model.releaseFromRead();
        }
    }
}
Also used : 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) IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) CMVisitor(org.eclipse.wst.xml.core.internal.contentmodel.util.CMVisitor) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

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