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;
}
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());
}
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);
}
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());
}
}
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);
}
Aggregations