public static string GetTestResult(XmlSchemaSet schemas) { StringBuilder b = new StringBuilder(); List <XmlSchemaElement> versionables = new List <XmlSchemaElement>(); List <XmlSchemaElement> maintainables = new List <XmlSchemaElement>(); Dictionary <XmlQualifiedName, List <XmlSchemaElement> > substitutes = schemas.GetSubstitutionGroups(); foreach (XmlSchemaElement element in schemas.GlobalElements.Values) { if (element.IsAbstract) { continue; } XmlSchemaComplexType ct = element.ElementSchemaType as XmlSchemaComplexType; if (ct == null) { continue; } if (ct.AttributeUses.Values.Cast <XmlSchemaAttribute>().Where(a => a.Name == "isVersionable").Count() > 0) { versionables.Add(element); } else if (ct.AttributeUses.Values.Cast <XmlSchemaAttribute>().Where(a => a.Name == "isMaintainable").Count() > 0) { maintainables.Add(element); } else { continue; } } XmlSchemaElement fragment = schemas.GlobalElements.Values.Cast <XmlSchemaElement>().Where(x => x.Name == "Fragment").FirstOrDefault(); XmlSchemaComplexType fragmentType = fragment.ElementSchemaType as XmlSchemaComplexType; XmlSchemaParticle particle = fragmentType.Particle; XmlSchemaSequence sequence = particle as XmlSchemaSequence; XmlSchemaChoice choice = sequence.Items[0] as XmlSchemaChoice; List <XmlSchemaElement> references = new List <XmlSchemaElement>(); foreach (XmlSchemaElement childElement in choice.Items) { XmlSchemaComplexType ct = childElement.ElementSchemaType as XmlSchemaComplexType; if (ct != null) { if (ct.IsAbstract) { if (substitutes.ContainsKey(childElement.QualifiedName)) { references.AddRange(substitutes[childElement.QualifiedName]); } } } references.Add(childElement); } int refCount = references.Count; int actual = versionables.Count + maintainables.Count; List <XmlSchemaElement> missing = new List <XmlSchemaElement>(versionables); missing.AddRange(maintainables); missing = missing.OrderBy(x => x.QualifiedName.ToString()).ToList(); references = references.OrderBy(x => x.QualifiedName.ToString()).ToList(); for (int i = 0; i < missing.Count; ++i) { XmlSchemaElement element = missing[i]; foreach (XmlSchemaElement reference in references) { if (reference.QualifiedName.ToString() == element.QualifiedName.ToString()) { missing.RemoveAt(i); i--; break; } } } string table = @"<table class=""table""> <thead> <tr> <th>#</th> <th>Item</th> <th>Location</th> </tr> </thead> <tbody>"; string tableRow = @"<tr> <td>{0}</td> <td>{1}</td> <td>{2} {3}:{4}</td> </tr>"; if (missing.Count > 0) { b.Append(@"<div class=""alert alert-danger"">Versionables and Maintainables found without FragmentInstance inclusion.</div>"); b.AppendLine(table); int j = 1; foreach (var error in missing) { b.AppendLine(string.Format(tableRow, j++, error.QualifiedName.ToString(), error.SourceUri.Split('/').Last(), error.LineNumber, error.LinePosition)); } b.AppendLine("</table>"); } else { b.Append("<div class=\"alert alert-success\">All Versionables and Maintainables found with FragmentInstance inclusion.</div>"); } return(b.ToString()); }
public static string GetTestResult(XmlSchemaSet schema) { XmlSchemaElement instance = schema.GlobalElements.Values .OfType <XmlSchemaElement>() .Where(x => x.QualifiedName.Name == "DDIInstance") .FirstOrDefault(); Dictionary <XmlQualifiedName, List <XmlSchemaElement> > substitutes = schema.GetSubstitutionGroups(); HashSet <Tuple <XmlQualifiedName, XmlSchemaElement> > elementsFound = new HashSet <Tuple <XmlQualifiedName, XmlSchemaElement> >(); instance.WalkXsdReferences(elementsFound, instance.QualifiedName); List <Tuple <XmlQualifiedName, XmlSchemaElement> > noChoice = new List <Tuple <XmlQualifiedName, XmlSchemaElement> >(); List <Tuple <XmlQualifiedName, XmlSchemaElement> > wrongFormatChoice = new List <Tuple <XmlQualifiedName, XmlSchemaElement> >(); List <Tuple <XmlQualifiedName, XmlSchemaElement> > wrongNumberChoice = new List <Tuple <XmlQualifiedName, XmlSchemaElement> >(); foreach (var tuple in elementsFound) { XmlSchemaElement element = tuple.Item2; XmlSchemaComplexType ct = element.ElementSchemaType as XmlSchemaComplexType; if (ct == null) { continue; } if (ct.AttributeUses.Values.Cast <XmlSchemaAttribute>().Any(a => a.Name == "isVersionable" || a.Name == "isMaintainable")) { if (element.Parent != null) { if (element == instance) { continue; } XmlSchemaChoice choice = element.Parent as XmlSchemaChoice; if (choice == null) { noChoice.Add(tuple); } else { List <XmlSchemaElement> siblings = choice.Items.OfType <XmlSchemaElement>().ToList(); if (siblings.Count != 2) { wrongNumberChoice.Add(tuple); } else { bool hasSiblingRef = siblings.Any(x => x.QualifiedName.Name == element.QualifiedName.Name + "Reference"); if (!hasSiblingRef) { if (substitutes.ContainsKey(element.QualifiedName)) { var anyRefs = substitutes[element.QualifiedName].Select(x => x.QualifiedName.Name + "Reference") .Intersect(siblings.Select(x => x.QualifiedName.Name)).ToList(); if (anyRefs.Count != 0) { continue; } } wrongFormatChoice.Add(tuple); } } } } } } StringBuilder b = new StringBuilder(); string tableRow = @"<tr> <td>{0}</td> <td>{1}</td> <td>{2}</td> </tr>"; if (noChoice.Count > 0) { b.Append(@"<div class=""alert alert-danger"">Versionables and Maintainables not in a referenceable xs:Choice format.</div>"); b.AppendLine(@"<table class=""table""> <thead> <tr> <th>#</th> <th>Item</th> <th>No xs:choice in Parent</th> </tr> </thead> <tbody>"); int i = 1; foreach (var n in noChoice.OrderBy(x => x.Item1.ToString())) { b.Append(string.Format(tableRow, i++, n.Item2.QualifiedName.ToString(), n.Item1.ToString())); } b.AppendLine("</table>"); } else { b.Append("<div class=\"alert alert-success\">All Versionables and Maintainables are in a referenceable xs:Choice format.</div>"); } if (wrongNumberChoice.Count > 0) { b.Append(@"<div class=""alert alert-danger"">Wrong number of elements in Versionables and Maintainables referenceable xs:Choice format.</div>"); b.AppendLine(@"<table class=""table""> <thead> <tr> <th>#</th> <th>Item</th> <th>Incorrect number of elements in xs:choice</th> </tr> </thead> <tbody>"); int i = 1; foreach (var n in wrongNumberChoice.OrderBy(x => x.Item1.ToString())) { b.Append(string.Format(tableRow, i++, n.Item2.QualifiedName.ToString(), n.Item1.ToString())); } b.AppendLine("</table>"); } else { b.Append("<div class=\"alert alert-success\">Correct number of elements in all Versionables and Maintainables referenceable xs:Choice format.</div>"); } if (wrongFormatChoice.Count > 0) { b.Append(@"<div class=""alert alert-danger"">Missing xxxReference to Versionables and Maintainables in referenceable xs:Choice format.</div>"); b.AppendLine(@"<table class=""table""> <thead> <tr> <th>#</th> <th>Item</th> <th>No xxxReference element in xs:choice</th> </tr> </thead> <tbody>"); int i = 1; foreach (var n in wrongFormatChoice.OrderBy(x => x.Item1.ToString())) { b.Append(string.Format(tableRow, i++, n.Item2.QualifiedName.ToString(), n.Item1.ToString())); } b.AppendLine("</table>"); } else { b.Append("<div class=\"alert alert-success\">Correct number of elements in all Versionables and Maintainables referenceable xs:Choice format.</div>"); } return(b.ToString()); }