1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
|
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.ServiceModel.Dispatcher
{
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Xml;
using System.Xml.Schema;
class HelpExampleGenerator
{
const int MaxDepthLevel = 256;
public const string XmlSchemaNamespace = "http://www.w3.org/2001/XMLSchema";
public const string XmlNamespacePrefix = "xmlns";
public const string XmlSchemaInstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance";
public const string XmlSchemaInstanceNil = "nil";
public const string XmlSchemaInstanceType = "type";
static Dictionary<Type, Action<XmlSchemaObject, HelpExampleGeneratorContext>> XmlObjectHandler = new Dictionary<Type, Action<XmlSchemaObject, HelpExampleGeneratorContext>>
{
{ typeof(XmlSchemaComplexContent), ContentHandler },
{ typeof(XmlSchemaSimpleContent), ContentHandler },
{ typeof(XmlSchemaSimpleTypeRestriction), SimpleTypeRestrictionHandler },
{ typeof(XmlSchemaChoice), ChoiceHandler },
// Nothing to do, inheritance is resolved by Schema compilation process
{ typeof(XmlSchemaComplexContentExtension), EmptyHandler },
{ typeof(XmlSchemaSimpleContentExtension), EmptyHandler },
// No need to generate XML for these objects
{ typeof(XmlSchemaAny), EmptyHandler },
{ typeof(XmlSchemaAnyAttribute), EmptyHandler },
{ typeof(XmlSchemaAnnotated), EmptyHandler },
{ typeof(XmlSchema), EmptyHandler },
// The following schema objects are not handled
{ typeof(XmlSchemaAttributeGroup), ErrorHandler },
{ typeof(XmlSchemaAttributeGroupRef), ErrorHandler },
{ typeof(XmlSchemaComplexContentRestriction), ErrorHandler },
{ typeof(XmlSchemaSimpleContentRestriction), ErrorHandler },
// Enumerations are supported by the GenerateContentForSimpleType
{ typeof(XmlSchemaEnumerationFacet), EmptyHandler },
{ typeof(XmlSchemaMaxExclusiveFacet), ErrorHandler },
{ typeof(XmlSchemaMaxInclusiveFacet), ErrorHandler },
{ typeof(XmlSchemaMinExclusiveFacet), ErrorHandler },
{ typeof(XmlSchemaMinInclusiveFacet), ErrorHandler },
{ typeof(XmlSchemaNumericFacet), ErrorHandler },
{ typeof(XmlSchemaFractionDigitsFacet), ErrorHandler },
{ typeof(XmlSchemaLengthFacet), ErrorHandler },
{ typeof(XmlSchemaMaxLengthFacet), ErrorHandler },
{ typeof(XmlSchemaMinLengthFacet), ErrorHandler },
{ typeof(XmlSchemaTotalDigitsFacet), ErrorHandler },
{ typeof(XmlSchemaPatternFacet), ErrorHandler },
{ typeof(XmlSchemaWhiteSpaceFacet), ErrorHandler },
{ typeof(XmlSchemaGroup), ErrorHandler },
{ typeof(XmlSchemaIdentityConstraint), ErrorHandler },
{ typeof(XmlSchemaKey), ErrorHandler },
{ typeof(XmlSchemaKeyref), ErrorHandler },
{ typeof(XmlSchemaUnique), ErrorHandler },
{ typeof(XmlSchemaNotation), ErrorHandler },
{ typeof(XmlSchemaAll), ErrorHandler },
{ typeof(XmlSchemaGroupRef), ErrorHandler },
{ typeof(XmlSchemaSimpleTypeUnion), ErrorHandler },
{ typeof(XmlSchemaSimpleTypeList), XmlSimpleTypeListHandler },
{ typeof(XmlSchemaXPath), ErrorHandler },
{ typeof(XmlSchemaAttribute), XmlAttributeHandler },
{ typeof(XmlSchemaElement), XmlElementHandler },
{ typeof(XmlSchemaComplexType), XmlComplexTypeHandler },
{ typeof(XmlSchemaSequence), XmlSequenceHandler },
{ typeof(XmlSchemaSimpleType), XmlSimpleTypeHandler },
};
static Dictionary<Type, Action<XmlSchemaObject, HelpExampleGeneratorContext>> JsonObjectHandler = new Dictionary<Type, Action<XmlSchemaObject, HelpExampleGeneratorContext>>
{
{ typeof(XmlSchemaComplexContent), ContentHandler },
{ typeof(XmlSchemaSimpleContent), ContentHandler },
{ typeof(XmlSchemaSimpleTypeRestriction), SimpleTypeRestrictionHandler },
{ typeof(XmlSchemaChoice), ChoiceHandler },
// Nothing to do, inheritance is resolved by Schema compilation process
{ typeof(XmlSchemaComplexContentExtension), EmptyHandler },
{ typeof(XmlSchemaSimpleContentExtension), EmptyHandler },
// No need to generate XML for these objects
{ typeof(XmlSchemaAny), EmptyHandler },
{ typeof(XmlSchemaAnyAttribute), EmptyHandler },
{ typeof(XmlSchemaAnnotated), EmptyHandler },
{ typeof(XmlSchema), EmptyHandler },
// The following schema objects are not handled
{ typeof(XmlSchemaAttributeGroup), ErrorHandler },
{ typeof(XmlSchemaAttributeGroupRef), ErrorHandler },
{ typeof(XmlSchemaComplexContentRestriction), ErrorHandler },
{ typeof(XmlSchemaSimpleContentRestriction), ErrorHandler },
// Enumerations are supported by the GenerateContentForSimpleType
{ typeof(XmlSchemaEnumerationFacet), EmptyHandler },
{ typeof(XmlSchemaMaxExclusiveFacet), ErrorHandler },
{ typeof(XmlSchemaMaxInclusiveFacet), ErrorHandler },
{ typeof(XmlSchemaMinExclusiveFacet), ErrorHandler },
{ typeof(XmlSchemaMinInclusiveFacet), ErrorHandler },
{ typeof(XmlSchemaNumericFacet), ErrorHandler },
{ typeof(XmlSchemaFractionDigitsFacet), ErrorHandler },
{ typeof(XmlSchemaLengthFacet), ErrorHandler },
{ typeof(XmlSchemaMaxLengthFacet), ErrorHandler },
{ typeof(XmlSchemaMinLengthFacet), ErrorHandler },
{ typeof(XmlSchemaTotalDigitsFacet), ErrorHandler },
{ typeof(XmlSchemaPatternFacet), ErrorHandler },
{ typeof(XmlSchemaWhiteSpaceFacet), ErrorHandler },
{ typeof(XmlSchemaGroup), ErrorHandler },
{ typeof(XmlSchemaIdentityConstraint), ErrorHandler },
{ typeof(XmlSchemaKey), ErrorHandler },
{ typeof(XmlSchemaKeyref), ErrorHandler },
{ typeof(XmlSchemaUnique), ErrorHandler },
{ typeof(XmlSchemaNotation), ErrorHandler },
{ typeof(XmlSchemaAll), ErrorHandler },
{ typeof(XmlSchemaGroupRef), ErrorHandler },
{ typeof(XmlSchemaSimpleTypeUnion), ErrorHandler },
{ typeof(XmlSchemaSimpleTypeList), JsonSimpleTypeListHandler },
{ typeof(XmlSchemaXPath), ErrorHandler },
{ typeof(XmlSchemaElement), JsonElementHandler },
{ typeof(XmlSchemaComplexType), JsonComplexTypeHandler },
{ typeof(XmlSchemaSequence), JsonSequenceHandler },
{ typeof(XmlSchemaSimpleType), JsonSimpleTypeHandler },
};
public static void GenerateJsonSample(XmlSchemaSet schemaSet, XmlQualifiedName name, XmlWriter writer, IDictionary<XmlQualifiedName, Type> knownTypes)
{
HelpExampleGeneratorContext context = new HelpExampleGeneratorContext
{
currentDepthLevel = 0,
elementDepth = new Dictionary<XmlSchemaElement, int>(),
knownTypes = knownTypes,
objectHandler = JsonObjectHandler,
schemaSet = schemaSet,
overrideElementName = JsonGlobals.rootString,
writer = writer,
};
if (!schemaSet.IsCompiled)
{
schemaSet.Compile();
}
InvokeHandler(schemaSet.GlobalElements[name], context);
}
public static void GenerateXmlSample(XmlSchemaSet schemaSet, XmlQualifiedName name, XmlWriter writer)
{
HelpExampleGeneratorContext context = new HelpExampleGeneratorContext
{
currentDepthLevel = 0,
elementDepth = new Dictionary<XmlSchemaElement, int>(),
knownTypes = null,
objectHandler = XmlObjectHandler,
schemaSet = schemaSet,
overrideElementName = null,
writer = writer,
};
if (!schemaSet.IsCompiled)
{
schemaSet.Compile();
}
InvokeHandler(schemaSet.GlobalElements[name], context);
}
[System.Diagnostics.DebuggerStepThrough]
static void InvokeHandler(XmlSchemaObject schemaObject, HelpExampleGeneratorContext context)
{
if (++context.currentDepthLevel < MaxDepthLevel)
{
Action<XmlSchemaObject, HelpExampleGeneratorContext> action;
Type objectType = schemaObject.GetType();
if (context.objectHandler.TryGetValue(objectType, out action))
{
action(schemaObject, context);
}
else if (objectType.Name != "EmptyParticle")
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR2.GetString(SR2.HelpExampleGeneratorHandlerNotFound, schemaObject.GetType().Name)));
}
--context.currentDepthLevel;
}
else
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR2.GetString(SR2.HelpExampleGeneratorMaxDepthLevelReached, schemaObject.GetType().Name)));
}
}
static void XmlAttributeHandler(XmlSchemaObject schemaObject, HelpExampleGeneratorContext context)
{
XmlSchemaAttribute attribute = (XmlSchemaAttribute)schemaObject;
string content = GenerateContentForXmlSimpleType(attribute.AttributeSchemaType);
if (String.IsNullOrEmpty(content))
{
context.writer.WriteAttributeString("i", XmlSchemaInstanceNil, XmlSchemaInstanceNamespace, "true");
}
else
{
context.writer.WriteAttributeString(attribute.QualifiedName.Name, attribute.QualifiedName.Namespace, content);
}
}
static void ChoiceHandler(XmlSchemaObject schemaObject, HelpExampleGeneratorContext context)
{
XmlSchemaChoice choice = (XmlSchemaChoice)schemaObject;
InvokeHandler(choice.Items[0], context);
}
static void ContentHandler(XmlSchemaObject schemaObject, HelpExampleGeneratorContext context)
{
XmlSchemaContentModel model = (XmlSchemaContentModel)schemaObject;
InvokeHandler(model.Content, context);
}
static void SimpleTypeRestrictionHandler(XmlSchemaObject schemaObject, HelpExampleGeneratorContext context)
{
XmlSchemaSimpleTypeRestriction restriction = (XmlSchemaSimpleTypeRestriction)schemaObject;
foreach (XmlSchemaObject facet in restriction.Facets)
{
InvokeHandler(facet, context);
}
}
static void ErrorHandler(XmlSchemaObject schemaObject, HelpExampleGeneratorContext context)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR2.GetString(SR2.HelpExampleGeneratorSchemaObjectNotSupported, schemaObject.GetType().Name)));
}
static void EmptyHandler(XmlSchemaObject schemaObject, HelpExampleGeneratorContext context)
{
}
static void XmlElementHandler(XmlSchemaObject schemaObject, HelpExampleGeneratorContext context)
{
XmlSchemaElement element = (XmlSchemaElement)schemaObject;
XmlSchemaElement contentElement = GenerateValidElementsComment(element, context);
context.writer.WriteStartElement(element.QualifiedName.Name, element.QualifiedName.Namespace);
if (contentElement != element)
{
string value = contentElement.QualifiedName.Name;
if (contentElement.QualifiedName.Namespace != element.QualifiedName.Namespace && !String.IsNullOrEmpty(contentElement.QualifiedName.Namespace))
{
string prefix = context.writer.LookupPrefix(contentElement.QualifiedName.Namespace);
if (prefix == null)
{
prefix = string.Concat("d", context.currentDepthLevel.ToString(System.Globalization.NumberFormatInfo.InvariantInfo));
context.writer.WriteAttributeString(XmlNamespacePrefix, prefix, null, contentElement.QualifiedName.Namespace);
}
value = String.Format(CultureInfo.InvariantCulture, "{0}:{1}", prefix, contentElement.QualifiedName.Name);
}
context.writer.WriteAttributeString("i", XmlSchemaInstanceType, XmlSchemaInstanceNamespace, value);
}
foreach (XmlSchemaObject constraint in contentElement.Constraints)
{
InvokeHandler(constraint, context);
}
InvokeHandler(contentElement.ElementSchemaType, context);
context.writer.WriteEndElement();
}
static void XmlComplexTypeHandler(XmlSchemaObject schemaObject, HelpExampleGeneratorContext context)
{
XmlSchemaComplexType complexType = (XmlSchemaComplexType)schemaObject;
foreach (XmlSchemaObject attribute in complexType.AttributeUses.Values)
{
InvokeHandler(attribute, context);
}
if (complexType.ContentModel != null)
{
InvokeHandler(complexType.ContentModel, context);
}
InvokeHandler(complexType.ContentTypeParticle, context);
if (complexType.IsMixed)
{
context.writer.WriteString(SR2.GetString(SR2.HelpExampleGeneratorThisElementContainsText));
}
}
static void XmlSequenceHandler(XmlSchemaObject schemaObject, HelpExampleGeneratorContext context)
{
XmlSchemaSequence sequence = (XmlSchemaSequence)schemaObject;
foreach (XmlSchemaObject innerObject in sequence.Items)
{
XmlSchemaElement element = innerObject as XmlSchemaElement;
for (int count = 0; element != null && count < 2 && element.MaxOccurs > count; ++count)
{
if (element != null && IsObject(element))
{
int instances = 0;
context.elementDepth.TryGetValue(element, out instances);
context.elementDepth[element] = ++instances;
if (instances < 3)
{
InvokeHandler(innerObject, context);
}
else
{
context.writer.WriteStartElement(element.QualifiedName.Name, element.QualifiedName.Namespace);
context.writer.WriteAttributeString("i", XmlSchemaInstanceNil, XmlSchemaInstanceNamespace, "true");
context.writer.WriteEndElement();
}
--context.elementDepth[element];
}
else
{
InvokeHandler(innerObject, context);
}
}
}
}
static void XmlSimpleTypeListHandler(XmlSchemaObject schemaObject, HelpExampleGeneratorContext context)
{
XmlSchemaSimpleTypeList simpleTypeList = (XmlSchemaSimpleTypeList)schemaObject;
InvokeHandler(simpleTypeList.ItemType, context);
}
static void XmlSimpleTypeHandler(XmlSchemaObject schemaObject, HelpExampleGeneratorContext context)
{
XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType)schemaObject;
if (simpleType.QualifiedName.Namespace != System.Runtime.Serialization.Globals.SerializationNamespace
&& simpleType.QualifiedName.Namespace != XmlSchemaNamespace
&& simpleType.QualifiedName.Name != "guid")
{
InvokeHandler(simpleType.Content, context);
}
string content = GenerateContentForXmlSimpleType(simpleType);
if (String.IsNullOrEmpty(content))
{
if (!(simpleType.Content is XmlSchemaSimpleTypeList))
{
context.writer.WriteAttributeString("i", XmlSchemaInstanceNil, XmlSchemaInstanceNamespace, "true");
}
}
else
{
context.writer.WriteString(content);
}
}
static string GenerateContentForXmlSimpleType(XmlSchemaSimpleType simpleType)
{
if (simpleType.Content != null && simpleType.Content is XmlSchemaSimpleTypeRestriction)
{
XmlSchemaSimpleTypeRestriction restriction = (XmlSchemaSimpleTypeRestriction)simpleType.Content;
foreach (XmlSchemaObject facet in restriction.Facets)
{
if (facet is XmlSchemaEnumerationFacet)
{
XmlSchemaEnumerationFacet enumeration = (XmlSchemaEnumerationFacet)facet;
return enumeration.Value;
}
}
}
if (simpleType.QualifiedName.Name == "dateTime")
{
DateTime dateTime = DateTime.Parse("1999-05-31T11:20:00", CultureInfo.InvariantCulture);
return dateTime.ToString("s", CultureInfo.InvariantCulture);
}
else if (simpleType.QualifiedName.Name == "char")
{
return "97";
}
return GetConstantValue(simpleType.QualifiedName.Name);
}
static void JsonElementHandler(XmlSchemaObject schemaObject, HelpExampleGeneratorContext context)
{
XmlSchemaElement element = (XmlSchemaElement)schemaObject;
XmlSchemaElement contentElement = GetDerivedTypes(element, context).FirstOrDefault();
if (contentElement == null)
{
contentElement = element;
}
if (context.overrideElementName != null)
{
context.writer.WriteStartElement(null, context.overrideElementName, null);
context.overrideElementName = null;
}
else
{
context.writer.WriteStartElement(null, element.Name, null);
}
if (IsArrayElementType(element))
{
context.writer.WriteAttributeString(JsonGlobals.typeString, JsonGlobals.arrayString);
context.overrideElementName = JsonGlobals.itemString;
}
else if (IsObject(element))
{
if (contentElement != element)
{
Type derivedType = null;
context.knownTypes.TryGetValue(contentElement.QualifiedName, out derivedType);
if (derivedType != null)
{
context.writer.WriteStartAttribute(null, JsonGlobals.serverTypeString, null);
context.writer.WriteString(String.Format(CultureInfo.InvariantCulture, "{0}:#{1}", derivedType.Name, derivedType.Namespace));
context.writer.WriteEndAttribute();
}
}
context.writer.WriteAttributeString(JsonGlobals.typeString, JsonGlobals.objectString);
}
InvokeHandler(contentElement.ElementSchemaType, context);
context.overrideElementName = null;
context.writer.WriteEndElement();
}
static void JsonComplexTypeHandler(XmlSchemaObject schemaObject, HelpExampleGeneratorContext context)
{
XmlSchemaComplexType complexType = (XmlSchemaComplexType)schemaObject;
if (complexType.ContentModel != null)
{
InvokeHandler(complexType.ContentModel, context);
}
InvokeHandler(complexType.ContentTypeParticle, context);
}
static void JsonSequenceHandler(XmlSchemaObject schemaObject, HelpExampleGeneratorContext context)
{
XmlSchemaSequence sequence = (XmlSchemaSequence)schemaObject;
foreach (XmlSchemaObject innerObject in sequence.Items)
{
XmlSchemaElement element = innerObject as XmlSchemaElement;
if (element != null && IsObject(element))
{
int instances = 0;
context.elementDepth.TryGetValue(element, out instances);
context.elementDepth[element] = ++instances;
if (instances < 3)
{
InvokeHandler(innerObject, context);
}
else
{
if (context.overrideElementName != null)
{
context.writer.WriteStartElement(context.overrideElementName);
context.overrideElementName = null;
}
else
{
context.writer.WriteStartElement(element.QualifiedName.Name);
}
context.writer.WriteAttributeString(JsonGlobals.typeString, JsonGlobals.nullString);
context.writer.WriteEndElement();
}
--context.elementDepth[element];
}
else
{
InvokeHandler(innerObject, context);
}
}
}
static void JsonSimpleTypeListHandler(XmlSchemaObject schemaObject, HelpExampleGeneratorContext context)
{
XmlSchemaSimpleTypeList simpleTypeList = (XmlSchemaSimpleTypeList)schemaObject;
InvokeHandler(simpleTypeList.ItemType, context);
}
static void JsonSimpleTypeHandler(XmlSchemaObject schemaObject, HelpExampleGeneratorContext context)
{
XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType)schemaObject;
// Enumerations return 0
if (simpleType.Content != null)
{
if (simpleType.Content is XmlSchemaSimpleTypeRestriction)
{
XmlSchemaSimpleTypeRestriction restriction = (XmlSchemaSimpleTypeRestriction)simpleType.Content;
foreach (XmlSchemaObject facet in restriction.Facets)
{
if (facet is XmlSchemaEnumerationFacet)
{
context.writer.WriteAttributeString(string.Empty, JsonGlobals.typeString, string.Empty, JsonGlobals.numberString);
context.writer.WriteString("0");
return;
}
}
}
else if (simpleType.Content is XmlSchemaSimpleTypeList)
{
InvokeHandler(simpleType.Content, context);
}
}
string value = GetConstantValue(simpleType.QualifiedName.Name);
if (simpleType.QualifiedName.Name == "base64Binary")
{
char[] base64stream = value.ToCharArray();
context.writer.WriteAttributeString(string.Empty, JsonGlobals.typeString, string.Empty, JsonGlobals.arrayString);
for (int i = 0; i < base64stream.Length; i++)
{
context.writer.WriteStartElement(JsonGlobals.itemString, string.Empty);
context.writer.WriteAttributeString(string.Empty, JsonGlobals.typeString, string.Empty, JsonGlobals.numberString);
context.writer.WriteValue((int)base64stream[i]);
context.writer.WriteEndElement();
}
}
else if (simpleType.QualifiedName.Name == "dateTime")
{
DateTime dateTime = DateTime.Parse("1999-05-31T11:20:00", CultureInfo.InvariantCulture);
context.writer.WriteString(JsonGlobals.DateTimeStartGuardReader);
context.writer.WriteValue((dateTime.ToUniversalTime().Ticks - JsonGlobals.unixEpochTicks) / 10000);
switch (dateTime.Kind)
{
case DateTimeKind.Unspecified:
case DateTimeKind.Local:
TimeSpan ts = TimeZone.CurrentTimeZone.GetUtcOffset(dateTime.ToLocalTime());
if (ts.Ticks < 0)
{
context.writer.WriteString("-");
}
else
{
context.writer.WriteString("+");
}
int hours = Math.Abs(ts.Hours);
context.writer.WriteString((hours < 10) ? "0" + hours : hours.ToString(CultureInfo.InvariantCulture));
int minutes = Math.Abs(ts.Minutes);
context.writer.WriteString((minutes < 10) ? "0" + minutes : minutes.ToString(CultureInfo.InvariantCulture));
break;
case DateTimeKind.Utc:
break;
}
context.writer.WriteString(JsonGlobals.DateTimeEndGuardReader);
}
else if (simpleType.QualifiedName.Name == "char")
{
context.writer.WriteString(XmlConvert.ToString('a'));
}
else if (!String.IsNullOrEmpty(value))
{
if (simpleType.QualifiedName.Name == "integer" ||
simpleType.QualifiedName.Name == "int" ||
simpleType.QualifiedName.Name == "long" ||
simpleType.QualifiedName.Name == "unsignedLong" ||
simpleType.QualifiedName.Name == "unsignedInt" ||
simpleType.QualifiedName.Name == "short" ||
simpleType.QualifiedName.Name == "unsignedShort" ||
simpleType.QualifiedName.Name == "byte" ||
simpleType.QualifiedName.Name == "unsignedByte" ||
simpleType.QualifiedName.Name == "decimal" ||
simpleType.QualifiedName.Name == "float" ||
simpleType.QualifiedName.Name == "double" ||
simpleType.QualifiedName.Name == "negativeInteger" ||
simpleType.QualifiedName.Name == "nonPositiveInteger" ||
simpleType.QualifiedName.Name == "positiveInteger" ||
simpleType.QualifiedName.Name == "nonNegativeInteger")
{
context.writer.WriteAttributeString(JsonGlobals.typeString, JsonGlobals.numberString);
}
else if (simpleType.QualifiedName.Name == "boolean")
{
context.writer.WriteAttributeString(JsonGlobals.typeString, JsonGlobals.booleanString);
}
context.writer.WriteString(value);
}
else
{
if (!(simpleType.Content is XmlSchemaSimpleTypeList))
{
context.writer.WriteAttributeString(JsonGlobals.typeString, JsonGlobals.nullString);
}
}
}
static string GetConstantValue(string typeName)
{
if (typeName == "base64Binary")
{
return "QmFzZSA2NCBTdHJlYW0=";
}
else if (typeName == "string" ||
typeName == "normalizedString" ||
typeName == "token" ||
typeName == "NMTOKEN" ||
typeName == "NMTOKENS")
{
return SR2.GetString(SR2.HelpExampleGeneratorStringContent);
}
else if (typeName == "hexBinary")
{
return "GpM7";
}
else if (typeName == "integer" || typeName == "int")
{
return "2147483647";
}
else if (typeName == "positiveInteger" || typeName == "nonNegativeInteger")
{
return "+2147483647";
}
else if (typeName == "long")
{
return "9223372036854775807";
}
else if (typeName == "unsignedLong")
{
return "18446744073709551615";
}
else if (typeName == "unsignedInt")
{
return "4294967295";
}
else if (typeName == "short")
{
return "32767";
}
else if (typeName == "unsignedShort")
{
return "65535";
}
else if (typeName == "byte")
{
return "127";
}
else if (typeName == "unsignedByte")
{
return "255";
}
else if (typeName == "decimal")
{
return "12678967.543233";
}
else if (typeName == "float")
{
return "1.26743237E+15";
}
else if (typeName == "double")
{
return "1.26743233E+15";
}
else if (typeName == "negativeInteger" || typeName == "nonPositiveInteger")
{
return "-12678967543233";
}
else if (typeName == "boolean")
{
return "true";
}
else if (typeName == "duration")
{
return "P428DT10H30M12.3S";
}
else if (typeName == "date")
{
return "1999-05-31";
}
else if (typeName == "time")
{
return "13:20:00.000, 13:20:00.000-05:00";
}
else if (typeName == "gYear")
{
return "1999";
}
else if (typeName == "gYearMonth")
{
return "1999-02";
}
else if (typeName == "gMonth")
{
return "--05";
}
else if (typeName == "gMonthDay")
{
return "--05-31";
}
else if (typeName == "gDay")
{
return "---31";
}
else if (typeName == "Name")
{
return "Name";
}
else if (typeName == "QName" || typeName == "NOTATION")
{
return "namespace:Name";
}
else if (typeName == "NCName" ||
typeName == "ID" ||
typeName == "IDREF" ||
typeName == "IDREFS" ||
typeName == "ENTITY" ||
typeName == "ENTITY" ||
typeName == "ID")
{
return "NCNameString";
}
else if (typeName == "anyURI")
{
return "http://www.example.com/";
}
else if (typeName == "language")
{
return SR2.GetString(SR2.HelpExampleGeneratorLanguage);
}
else if (typeName == "guid")
{
return "1627aea5-8e0a-4371-9022-9b504344e724";
}
return null;
}
static XmlSchemaElement GenerateValidElementsComment(XmlSchemaElement element, HelpExampleGeneratorContext context)
{
XmlSchemaElement firstNonAbstractElement = element;
StringBuilder validTypes = new StringBuilder();
foreach (XmlSchemaElement derivedElement in GetDerivedTypes(element, context))
{
if (firstNonAbstractElement == element)
{
firstNonAbstractElement = derivedElement;
}
if (validTypes.Length > 0)
{
validTypes.AppendFormat(", {0}", derivedElement.Name);
}
else
{
validTypes.AppendFormat(SR2.GetString(SR2.HelpPageValidElementOfType, derivedElement.Name));
}
}
if (validTypes.Length > 0)
{
context.writer.WriteComment(validTypes.ToString());
}
return firstNonAbstractElement;
}
static IEnumerable<XmlSchemaElement> GetDerivedTypes(XmlSchemaElement element, HelpExampleGeneratorContext context)
{
if (element.ElementSchemaType is XmlSchemaComplexType)
{
foreach (XmlSchemaElement derivedElement in context.schemaSet.GlobalElements.Values.OfType<XmlSchemaElement>().Where(e =>
e.IsAbstract == false &&
e.ElementSchemaType != element.ElementSchemaType &&
e.ElementSchemaType is XmlSchemaComplexType &&
DerivesFrom((XmlSchemaComplexType)element.ElementSchemaType, (XmlSchemaComplexType)e.ElementSchemaType)).OrderBy(e => e.Name))
{
yield return derivedElement;
}
}
}
static bool DerivesFrom(XmlSchemaComplexType parent, XmlSchemaComplexType child)
{
if (parent == child)
{
return true;
}
else if (child.BaseXmlSchemaType is XmlSchemaComplexType)
{
return DerivesFrom(parent, (XmlSchemaComplexType)child.BaseXmlSchemaType);
}
else
{
return false;
}
}
static bool IsArrayElementType(XmlSchemaElement element)
{
if (element.ElementSchemaType is XmlSchemaComplexType)
{
XmlSchemaComplexType complexType = element.ElementSchemaType as XmlSchemaComplexType;
if (complexType.ContentTypeParticle != null && complexType.ContentTypeParticle is XmlSchemaSequence)
{
XmlSchemaSequence sequence = complexType.ContentTypeParticle as XmlSchemaSequence;
if (sequence.Items.Count > 0)
{
XmlSchemaElement firstElement = sequence.Items[0] as XmlSchemaElement;
if (firstElement != null && firstElement.MaxOccurs > 1)
{
return true;
}
}
}
}
return false;
}
static bool IsObject(XmlSchemaElement element)
{
return element.ElementSchemaType is XmlSchemaComplexType;
}
class HelpExampleGeneratorContext
{
public string overrideElementName;
public int currentDepthLevel;
public IDictionary<XmlQualifiedName, Type> knownTypes;
public XmlSchemaSet schemaSet;
public IDictionary<XmlSchemaElement, int> elementDepth;
public XmlWriter writer;
public Dictionary<Type, Action<XmlSchemaObject, HelpExampleGeneratorContext>> objectHandler;
}
}
}
|