Search in sources :

Example 36 with CMDocument

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

the class AbstractXMLModelQueryCompletionProposalComputer method getAvailableRootChildren.

/**
 * returns a list of CMElementDeclarations
 *
 * @param document
 * @param childIndex
 * @return
 */
private 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) && child.getNodeName().equalsIgnoreCase(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));
                    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
                    String location = "" + (docType.getPublicId() != null ? docType.getPublicId() + "/" : "") + (docType.getSystemId() != null ? docType.getSystemId() : "");
                    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 {
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
            String location = "" + (docType.getPublicId() != null ? docType.getPublicId() + "/" : "") + (docType.getSystemId() != null ? docType.getSystemId() : "");
            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) List(java.util.List) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ArrayList(java.util.ArrayList) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)

Example 37 with CMDocument

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

the class BugFixesTest method testXSITypeVsTypeAttr.

public void testXSITypeVsTypeAttr() {
    // See bug 225447, 225819
    // Load the XSD file
    String XSD_FILE_NAME = "XSITypeTest.xsd";
    String fileURI = FILE_PROTOCOL + PLUGIN_ABSOLUTE_PATH + SAMPLES_DIR + XSD_FILE_NAME;
    CMDocumentFactoryXSD factory = new CMDocumentFactoryXSD();
    assertNotNull("Assert factory is not null", factory);
    CMDocument cmDocument = factory.createCMDocument(fileURI);
    assertNotNull("Assert CMDocument is not null", cmDocument);
    // Check and obtain the two global elements (elementA and elementB)
    CMNamedNodeMap elements = cmDocument.getElements();
    assertEquals(elements.getLength(), 2);
    CMElementDeclaration cmElementDeclaration = (CMElementDeclaration) elements.item(0);
    CMElementDeclaration cmElementDeclarationA = null;
    CMElementDeclaration cmElementDeclarationB = null;
    if ("elementA".equals(cmElementDeclaration.getElementName())) {
        cmElementDeclarationA = cmElementDeclaration;
        cmElementDeclarationB = (CMElementDeclaration) elements.item(1);
    } else {
        cmElementDeclarationB = cmElementDeclaration;
        cmElementDeclarationA = (CMElementDeclaration) elements.item(1);
    }
    // elementA has a "type" attribute with "X" enumerated value, make sure it appears in the model
    CMNamedNodeMap attributesA = cmElementDeclarationA.getAttributes();
    assertEquals(attributesA.getLength(), 1);
    CMAttributeDeclaration cmAttributeDeclarationA = (CMAttributeDeclaration) attributesA.item(0);
    assertEquals("type", cmAttributeDeclarationA.getAttrName());
    CMDataType attrTypeA = cmAttributeDeclarationA.getAttrType();
    String[] enumeratedValuesA = attrTypeA.getEnumeratedValues();
    assertEquals(1, enumeratedValuesA.length);
    assertEquals("X", enumeratedValuesA[0]);
    // elementB does not have a "type" attribute, make sure the xsi:type appears in the model
    CMNamedNodeMap attributesB = cmElementDeclarationB.getAttributes();
    assertEquals(attributesB.getLength(), 1);
    CMAttributeDeclaration cmAttributeDeclarationB = (CMAttributeDeclaration) attributesB.item(0);
    assertEquals("type", cmAttributeDeclarationB.getAttrName());
    CMDataType attrTypeB = cmAttributeDeclarationB.getAttrType();
    assertEquals("typeNames", attrTypeB.getDataTypeName());
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) 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) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) CMDocumentFactoryXSD(org.eclipse.wst.xsd.contentmodel.internal.CMDocumentFactoryXSD)

Example 38 with CMDocument

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

the class BugFixesTest method testForBug176420.

public void testForBug176420() {
    // Obtain the model from /testresources/samples/testSchemaForBug176420.xsd
    Bundle bundle = Platform.getBundle("org.eclipse.wst.xsd.core.tests");
    URL url = bundle.getEntry("/testresources/samples/testSchemaForBug176420.xsd");
    CMDocument document = XSDImpl.buildCMDocument(url.toExternalForm());
    assertNotNull("Content model loaded Null", document);
    // Obtain the enumerated values of the root element
    CMNode cmNode = document.getElements().item(0);
    String[] enumeratedValues = ((CMElementDeclaration) cmNode).getDataType().getEnumeratedValues();
    // Verify that all 12 enumerated values are included
    assertEquals(12, enumeratedValues.length);
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) Bundle(org.osgi.framework.Bundle) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) URL(java.net.URL)

Example 39 with CMDocument

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

the class BugFixesTest method testGlobalElementDocumentation.

public void testGlobalElementDocumentation() {
    // See bug 157254
    Bundle bundle = Platform.getBundle("org.eclipse.wst.xsd.core.tests");
    URL url = bundle.getEntry("/testresources/samples/documentation/globalreftest.xsd");
    CMDocument document = XSDImpl.buildCMDocument(url.toExternalForm());
    assertNotNull("Content model loaded Null", document);
    CMNamedNodeMap elements = document.getElements();
    CMElementDeclaration node = (CMElementDeclaration) elements.getNamedItem("rootTest");
    assertNotNull("Missing rootElement", node);
    CMElementDeclaration testElement = (CMElementDeclaration) node.getLocalElements().getNamedItem("test");
    assertNotNull("Missing test element", testElement);
    CMNodeList documentation = (CMNodeList) testElement.getProperty("documentation");
    if (documentation.getLength() == 0) {
        fail("test global element missing documentation.");
    }
    for (int cnt = 0; cnt < documentation.getLength(); cnt++) {
        DocumentationImpl doc = (DocumentationImpl) documentation.item(cnt);
        assertEquals("Test global element missing documentation.", "This some global documentation", doc.getValue());
    }
    testElement = (CMElementDeclaration) node.getLocalElements().getNamedItem("testElement");
    documentation = (CMNodeList) testElement.getProperty("documentation");
    if (documentation.getLength() == 0) {
        fail("testElement local element missing documentation.");
    }
    for (int cnt = 0; cnt < documentation.getLength(); cnt++) {
        DocumentationImpl doc = (DocumentationImpl) documentation.item(cnt);
        assertEquals("testElement documentation wrong.", "This is an override", doc.getValue());
    }
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) Bundle(org.osgi.framework.Bundle) DocumentationImpl(org.eclipse.wst.xsd.contentmodel.internal.XSDImpl.DocumentationImpl) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) URL(java.net.URL) CMNodeList(org.eclipse.wst.xml.core.internal.contentmodel.CMNodeList)

Example 40 with CMDocument

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

Aggregations

CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)83 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)33 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)26 CMNode (org.eclipse.wst.xml.core.internal.contentmodel.CMNode)20 List (java.util.List)15 ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)14 Document (org.w3c.dom.Document)12 Element (org.w3c.dom.Element)11 ArrayList (java.util.ArrayList)10 Path (org.eclipse.core.runtime.Path)8 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)8 JSPCMDocument (org.eclipse.wst.html.core.internal.contentmodel.JSPCMDocument)7 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)7 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)7 URL (java.net.URL)6 TLDCMDocumentManager (org.eclipse.jst.jsp.core.internal.contentmodel.tld.TLDCMDocumentManager)6 Node (org.w3c.dom.Node)6 NodeList (org.w3c.dom.NodeList)6 Iterator (java.util.Iterator)5 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)5