File size: 45,488 Bytes
8c763fb | 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 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Reflection;
using System.Text;
using System.Xml;
namespace System.Management.Automation
{
/// <summary>
/// MamlNode is an xml node in MAML schema. Maml schema includes formatting oriented tags like para, list
/// etc, which needs to be taken care of during display. As a result, xml node in Maml schema can't be
/// converted into PSObject directly with XmlNodeAdapter.
///
/// MamlNode class provides logic in converting formatting tags into the format acceptable by monad format
/// and output engine.
///
/// Following three kinds of formating tags are supported per our agreement with Maml team,
/// 1. para,
/// <para>
/// para text here
/// </para>
/// 2. list,
/// <list class="ordered|unordered">
/// <listItem>
/// <para>
/// listItem Text here
/// </para>
/// </listItem>
/// </list>
/// 3. definition list,
/// <definitionList>
/// <definitionListItem>
/// <term>
/// definition term text here
/// </term>
/// <definition>
/// <para>
/// definition text here
/// </para>
/// </definition>
/// </definitionListItem>
/// </definitionList>
/// After processing, content of these three tags will be converted into textItem and its derivations,
/// 1. para => paraTextItem
/// <textItem class="paraTextItem">
/// <text>para text here</text>
/// </textItem>
/// 2. list => a list of listTextItem's (which can be ordered or unordered)
/// <textItem class="unorderedListTextItem">
/// <tag>*</tag>
/// <text>text for list item 1</text>
/// </textItem>
/// <textItem class="unorderedListTextItem">
/// <tag>*</tag>
/// <text>text for list item 2</text>
/// </textItem>
/// 3. definitionList => a list of definitionTextItem's
/// <definitionListItem>
/// <term>definition term here</term>
/// <definition>definition text here</definition>
/// </definitionListItem>
/// </summary>
internal class MamlNode
{
/// <summary>
/// Constructor for HelpInfo.
/// </summary>
internal MamlNode(XmlNode xmlNode)
{
_xmlNode = xmlNode;
}
private readonly XmlNode _xmlNode;
/// <summary>
/// Underline xmlNode for this MamlNode object.
/// </summary>
/// <value></value>
internal XmlNode XmlNode
{
get
{
return _xmlNode;
}
}
private PSObject _mshObject;
/// <summary>
/// MshObject which is converted from XmlNode.
/// </summary>
/// <value></value>
internal PSObject PSObject
{
get
{
if (_mshObject == null)
{
// There is no XSLT to convert docs to supported maml format
// We dont want comments etc to spoil our format.
// So remove all unsupported nodes before constructing help
// object.
RemoveUnsupportedNodes(_xmlNode);
_mshObject = GetPSObject(_xmlNode);
}
return _mshObject;
}
}
#region Conversion of xmlNode => PSObject
/// <summary>
/// Convert an xmlNode into an PSObject. There are four scenarios,
/// 1. Null xml, this will return an PSObject wrapping a null object.
/// 2. Atomic xml, which is an xmlNode with only one simple text child node
/// <atomicXml attribute="value">
/// atomic xml text
/// </atomicXml>
/// In this case, an PSObject that wraps string "atomic xml text" will be returned with following properties
/// attribute => name
/// 3. Composite xml, which is an xmlNode with structured child nodes, but not a special case for Maml formatting.
/// <compositeXml attribute="attribute">
/// <singleChildNode>
/// single child node text
/// </singleChildNode>
/// <dupChildNode>
/// dup child node text 1
/// </dupChildNode>
/// <dupChildNode>
/// dup child node text 2
/// </dupChildNode>
/// </compositeXml>
/// In this case, an PSObject will base generated based on an inside PSObject,
/// which in turn has following properties
/// a. property "singleChildNode", with its value an PSObject wrapping string "single child node text"
/// b. property "dupChildNode", with its value an PSObject array wrapping strings for two dupChildNode's
/// The outside PSObject will have property,
/// a. property "attribute", with its value an PSObject wrapping string "attribute"
/// 4. Maml formatting xml, this is a special case for Composite xml, for example
/// <description attribute="value">
/// <para>
/// para 1
/// </para>
/// <list>
/// <listItem>
/// <para>
/// list item 1
/// </para>
/// </listItem>
/// <listItem>
/// <para>
/// list item 2
/// </para>
/// </listItem>
/// </list>
/// <definitionList>
/// <definitionListItem>
/// <term>
/// term 1
/// </term>
/// <definition>
/// definition list item 1
/// </definition>
/// </definitionListItem>
/// <definitionListItem>
/// <term>
/// term 2
/// </term>
/// <definition>
/// definition list item 2
/// </definition>
/// </definitionListItem>
/// </definitionList>
/// </description>
/// In this case, an PSObject based on an PSObject array will be created. The inside PSObject array
/// will contain following items
/// . a MamlParaTextItem based on "para 1"
/// . a MamlUnorderedListItem based on "list item 1"
/// . a MamlUnorderedListItem based on "list item 2"
/// . a MamlDefinitionListItem based on "definition list item 1"
/// . a MamlDefinitionListItem based on "definition list item 2"
///
/// The outside PSObject will have a property
/// attribute => "value"
/// </summary>
/// <param name="xmlNode"></param>
/// <returns></returns>
private PSObject GetPSObject(XmlNode xmlNode)
{
if (xmlNode == null)
return new PSObject();
PSObject mshObject = null;
if (IsAtomic(xmlNode))
{
mshObject = new PSObject(xmlNode.InnerText.Trim());
}
else if (IncludeMamlFormatting(xmlNode))
{
mshObject = new PSObject(GetMamlFormattingPSObjects(xmlNode));
}
else
{
mshObject = new PSObject(GetInsidePSObject(xmlNode));
// Add typeNames to this MSHObject and create views so that
// the output is readable. This is done only for complex nodes.
mshObject.TypeNames.Clear();
if (xmlNode.Attributes["type"] != null)
{
if (string.Equals(xmlNode.Attributes["type"].Value, "field", StringComparison.OrdinalIgnoreCase))
mshObject.TypeNames.Add("MamlPSClassHelpInfo#field");
else if (string.Equals(xmlNode.Attributes["type"].Value, "method", StringComparison.OrdinalIgnoreCase))
mshObject.TypeNames.Add("MamlPSClassHelpInfo#method");
}
mshObject.TypeNames.Add("MamlCommandHelpInfo#" + xmlNode.LocalName);
}
if (xmlNode.Attributes != null)
{
foreach (XmlNode attribute in xmlNode.Attributes)
{
mshObject.Properties.Add(new PSNoteProperty(attribute.Name, attribute.Value));
}
}
return mshObject;
}
/// <summary>
/// Get inside PSObject created based on inside nodes of xmlNode.
///
/// The inside PSObject will be based on null. It will created one
/// property per inside node grouping by node names.
///
/// For example, for xmlNode like,
/// <command>
/// <name>get-item</name>
/// <note>note 1</note>
/// <note>note 2</note>
/// </command>
/// It will create an PSObject based on null, with following two properties
/// . property 1: name="name" value=an PSObject to wrap string "get-item"
/// . property 2: name="note" value=an PSObject array with following two PSObjects
/// 1. PSObject wrapping string "note 1"
/// 2. PSObject wrapping string "note 2"
/// </summary>
/// <param name="xmlNode"></param>
/// <returns></returns>
private PSObject GetInsidePSObject(XmlNode xmlNode)
{
Hashtable properties = GetInsideProperties(xmlNode);
PSObject mshObject = new PSObject();
IDictionaryEnumerator enumerator = properties.GetEnumerator();
while (enumerator.MoveNext())
{
mshObject.Properties.Add(new PSNoteProperty((string)enumerator.Key, enumerator.Value));
}
return mshObject;
}
/// <summary>
/// This is for getting inside properties of an XmlNode. Properties are
/// stored in a hashtable with key as property name and value as property value.
///
/// Inside node with same node names will be grouped into one property with
/// property value as an array.
///
/// For example, for xmlNode like,
/// <command>
/// <name>get-item</name>
/// <note>note 1</note>
/// <note>note 2</note>
/// </command>
/// It will create an PSObject based on null, with following two properties
/// . property 1: name="name" value=an PSObject to wrap string "get-item"
/// . property 2: name="note" value=an PSObject array with following two PSObjects
/// 1. PSObject wrapping string "note 1"
/// 2. PSObject wrapping string "note 2"
///
/// Since we don't know whether an node name will be used more than once,
/// We are making each property value is an array (PSObject[]) to start with.
/// At the end, SimplifyProperties will be called to reduce PSObject[] containing
/// only one element to PSObject itself.
/// </summary>
/// <param name="xmlNode"></param>
/// <returns></returns>
private Hashtable GetInsideProperties(XmlNode xmlNode)
{
Hashtable properties = new Hashtable(StringComparer.OrdinalIgnoreCase);
if (xmlNode == null)
return properties;
if (xmlNode.ChildNodes != null)
{
foreach (XmlNode childNode in xmlNode.ChildNodes)
{
AddProperty(properties, childNode.LocalName, GetPSObject(childNode));
}
}
return SimplifyProperties(properties);
}
/// <summary>
/// Removes unsupported child nodes recursively from the given
/// xml node so that they wont spoil the format.
/// </summary>
/// <param name="xmlNode">
/// Node whose children are verified for maml.
/// </param>
private static void RemoveUnsupportedNodes(XmlNode xmlNode)
{
// Start with the first child..
// We want to modify only children..
// The current node is taken care by the callee..
XmlNode childNode = xmlNode.FirstChild;
while (childNode != null)
{
// We dont want Comments..so remove..
if (childNode.NodeType == XmlNodeType.Comment)
{
XmlNode nodeToRemove = childNode;
childNode = childNode.NextSibling;
// Remove this node and its children if any..
xmlNode.RemoveChild(nodeToRemove);
}
else
{
// Search children...
RemoveUnsupportedNodes(childNode);
childNode = childNode.NextSibling;
}
}
}
/// <summary>
/// This is for adding a property into a property hashtable.
///
/// As mentioned in comment of GetInsideProperties, property values stored in
/// property hashtable is an array to begin with.
///
/// The property value to be added is an mshObject whose base object can be an
/// PSObject array itself. In that case, each PSObject in the array will be
/// added separately into the property value array. This case can only happen when
/// an node with maml formatting node inside is treated. The side effect of this
/// is that the properties for outside mshObject will be lost. An example of this
/// is that,
/// <command>
/// <description attrib1="value1">
/// <para></para>
/// <list></list>
/// <definitionList></definitionList>
/// </description>
/// </command>
/// After the processing, PSObject corresponding to command will have an property
/// with name "description" and a value of an PSObject array created based on
/// maml formatting node inside "description" node. The attribute of description node
/// "attrib1" will be lost. This seems to be OK with current practice of authoring
/// monad command help.
/// </summary>
/// <param name="properties">Property hashtable.</param>
/// <param name="name">Property name.</param>
/// <param name="mshObject">Property value.</param>
private static void AddProperty(Hashtable properties, string name, PSObject mshObject)
{
ArrayList propertyValues = (ArrayList)properties[name];
if (propertyValues == null)
{
propertyValues = new ArrayList();
properties[name] = propertyValues;
}
if (mshObject == null)
return;
if (mshObject.BaseObject is PSCustomObject || !mshObject.BaseObject.GetType().Equals(typeof(PSObject[])))
{
propertyValues.Add(mshObject);
return;
}
PSObject[] mshObjects = (PSObject[])mshObject.BaseObject;
for (int i = 0; i < mshObjects.Length; i++)
{
propertyValues.Add(mshObjects[i]);
}
return;
}
/// <summary>
/// This is for simplifying property value array of only one element.
///
/// As mentioned in comments for GetInsideProperties, this is needed
/// to reduce an array of only one PSObject into the PSObject itself.
///
/// A side effect of this function is to turn property values from
/// ArrayList into PSObject[].
/// </summary>
/// <param name="properties"></param>
/// <returns></returns>
private static Hashtable SimplifyProperties(Hashtable properties)
{
if (properties == null)
return null;
Hashtable result = new Hashtable(StringComparer.OrdinalIgnoreCase);
IDictionaryEnumerator enumerator = properties.GetEnumerator();
while (enumerator.MoveNext())
{
ArrayList propertyValues = (ArrayList)enumerator.Value;
if (propertyValues == null || propertyValues.Count == 0)
continue;
if (propertyValues.Count == 1)
{
if (!IsMamlFormattingPSObject((PSObject)propertyValues[0]))
{
PSObject mshObject = (PSObject)propertyValues[0];
// Even for strings or other basic types, they need to be contained in PSObject in case
// there is attributes for this object.
result[enumerator.Key] = mshObject;
continue;
}
}
result[enumerator.Key] = propertyValues.ToArray(typeof(PSObject));
}
return result;
}
/// <summary>
/// An xmlNode is atomic if it contains no structured inside nodes.
/// </summary>
/// <param name="xmlNode"></param>
/// <returns></returns>
private static bool IsAtomic(XmlNode xmlNode)
{
if (xmlNode == null)
return false;
if (xmlNode.ChildNodes == null)
return true;
if (xmlNode.ChildNodes.Count > 1)
return false;
if (xmlNode.ChildNodes.Count == 0)
return true;
if (xmlNode.ChildNodes[0].GetType().Equals(typeof(XmlText)))
return true;
return false;
}
#endregion
#region Maml formatting
/// <summary>
/// Check whether an xmlNode contains childnodes which is for
/// maml formatting.
/// </summary>
/// <param name="xmlNode"></param>
/// <returns></returns>
private static bool IncludeMamlFormatting(XmlNode xmlNode)
{
if (xmlNode == null)
return false;
if (xmlNode.ChildNodes == null || xmlNode.ChildNodes.Count == 0)
return false;
foreach (XmlNode childNode in xmlNode.ChildNodes)
{
if (IsMamlFormattingNode(childNode))
{
return true;
}
}
return false;
}
/// <summary>
/// Check whether a node is for maml formatting. This include following nodes,
/// a. para
/// b. list
/// c. definitionList.
/// </summary>
/// <param name="xmlNode"></param>
/// <returns></returns>
private static bool IsMamlFormattingNode(XmlNode xmlNode)
{
if (xmlNode.LocalName.Equals("para", StringComparison.OrdinalIgnoreCase))
return true;
if (xmlNode.LocalName.Equals("list", StringComparison.OrdinalIgnoreCase))
return true;
if (xmlNode.LocalName.Equals("definitionList", StringComparison.OrdinalIgnoreCase))
return true;
return false;
}
/// <summary>
/// Check whether an mshObject is created from a maml formatting node.
/// </summary>
/// <param name="mshObject"></param>
/// <returns></returns>
private static bool IsMamlFormattingPSObject(PSObject mshObject)
{
Collection<string> typeNames = mshObject.TypeNames;
if (typeNames == null || typeNames.Count == 0)
return false;
return typeNames[typeNames.Count - 1].Equals("MamlTextItem", StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Convert an xmlNode containing maml formatting nodes into an PSObject array.
///
/// For example, for node,
/// <description attribute="value">
/// <para>
/// para 1
/// </para>
/// <list>
/// <listItem>
/// <para>
/// list item 1
/// </para>
/// </listItem>
/// <listItem>
/// <para>
/// list item 2
/// </para>
/// </listItem>
/// </list>
/// <definitionList>
/// <definitionListItem>
/// <term>
/// term 1
/// </term>
/// <definition>
/// definition list item 1
/// </definition>
/// </definitionListItem>
/// <definitionListItem>
/// <term>
/// term 2
/// </term>
/// <definition>
/// definition list item 2
/// </definition>
/// </definitionListItem>
/// </definitionList>
/// </description>
/// In this case, an PSObject based on an PSObject array will be created. The inside PSObject array
/// will contain following items
/// . a MamlParaTextItem based on "para 1"
/// . a MamlUnorderedListItem based on "list item 1"
/// . a MamlUnorderedListItem based on "list item 2"
/// . a MamlDefinitionListItem based on "definition list item 1"
/// . a MamlDefinitionListItem based on "definition list item 2"
/// </summary>
/// <param name="xmlNode"></param>
/// <returns></returns>
private PSObject[] GetMamlFormattingPSObjects(XmlNode xmlNode)
{
ArrayList mshObjects = new ArrayList();
int paraNodes = GetParaMamlNodeCount(xmlNode.ChildNodes);
int count = 0;
// Don't trim the content if this is an "introduction" node.
bool trim = !string.Equals(xmlNode.Name, "maml:introduction", StringComparison.OrdinalIgnoreCase);
foreach (XmlNode childNode in xmlNode.ChildNodes)
{
if (childNode.LocalName.Equals("para", StringComparison.OrdinalIgnoreCase))
{
++count;
PSObject paraPSObject = GetParaPSObject(childNode, count != paraNodes, trim: trim);
if (paraPSObject != null)
mshObjects.Add(paraPSObject);
continue;
}
if (childNode.LocalName.Equals("list", StringComparison.OrdinalIgnoreCase))
{
ArrayList listPSObjects = GetListPSObjects(childNode);
for (int i = 0; i < listPSObjects.Count; i++)
{
mshObjects.Add(listPSObjects[i]);
}
continue;
}
if (childNode.LocalName.Equals("definitionList", StringComparison.OrdinalIgnoreCase))
{
ArrayList definitionListPSObjects = GetDefinitionListPSObjects(childNode);
for (int i = 0; i < definitionListPSObjects.Count; i++)
{
mshObjects.Add(definitionListPSObjects[i]);
}
continue;
}
// If we get here, there is some tags that is not supported by maml.
WriteMamlInvalidChildNodeError(xmlNode, childNode);
}
return (PSObject[])mshObjects.ToArray(typeof(PSObject));
}
/// <summary>
/// Gets the number of para nodes.
/// </summary>
/// <param name="nodes"></param>
/// <returns></returns>
private static int GetParaMamlNodeCount(XmlNodeList nodes)
{
int i = 0;
foreach (XmlNode childNode in nodes)
{
if (childNode.LocalName.Equals("para", StringComparison.OrdinalIgnoreCase))
{
if (childNode.InnerText.Trim().Equals(string.Empty))
{
continue;
}
++i;
}
}
return i;
}
/// <summary>
/// Write an error to helpsystem to indicate an invalid maml child node.
/// </summary>
/// <param name="node"></param>
/// <param name="childNode"></param>
private void WriteMamlInvalidChildNodeError(XmlNode node, XmlNode childNode)
{
ErrorRecord errorRecord = new ErrorRecord(new ParentContainsErrorRecordException("MamlInvalidChildNodeError"), "MamlInvalidChildNodeError", ErrorCategory.SyntaxError, null);
errorRecord.ErrorDetails = new ErrorDetails(typeof(MamlNode).Assembly, "HelpErrors", "MamlInvalidChildNodeError", node.LocalName, childNode.LocalName, GetNodePath(node));
this.Errors.Add(errorRecord);
}
/// <summary>
/// Write an error to help system to indicate an invalid child node count.
/// </summary>
/// <param name="node"></param>
/// <param name="childNodeName"></param>
/// <param name="count"></param>
private void WriteMamlInvalidChildNodeCountError(XmlNode node, string childNodeName, int count)
{
ErrorRecord errorRecord = new ErrorRecord(new ParentContainsErrorRecordException("MamlInvalidChildNodeCountError"), "MamlInvalidChildNodeCountError", ErrorCategory.SyntaxError, null);
errorRecord.ErrorDetails = new ErrorDetails(typeof(MamlNode).Assembly, "HelpErrors", "MamlInvalidChildNodeCountError", node.LocalName, childNodeName, count, GetNodePath(node));
this.Errors.Add(errorRecord);
}
private static string GetNodePath(XmlNode xmlNode)
{
if (xmlNode == null)
return string.Empty;
if (xmlNode.ParentNode == null)
return "\\" + xmlNode.LocalName;
return GetNodePath(xmlNode.ParentNode) + "\\" + xmlNode.LocalName + GetNodeIndex(xmlNode);
}
private static string GetNodeIndex(XmlNode xmlNode)
{
if (xmlNode == null || xmlNode.ParentNode == null)
return string.Empty;
int index = 0;
int total = 0;
foreach (XmlNode siblingNode in xmlNode.ParentNode.ChildNodes)
{
if (siblingNode == xmlNode)
{
index = total++;
continue;
}
if (siblingNode.LocalName.Equals(xmlNode.LocalName, StringComparison.OrdinalIgnoreCase))
{
total++;
}
}
if (total > 1)
{
return "[" + index.ToString("d", CultureInfo.CurrentCulture) + "]";
}
return string.Empty;
}
/// <summary>
/// Convert a para node into an mshObject.
///
/// For example,
/// <para>
/// para text
/// </para>
/// In this case, an PSObject of type "MamlParaTextItem" will be created with following property
/// a. text="para text"
/// </summary>
/// <param name="xmlNode"></param>
/// <param name="newLine"></param>
/// <param name="trim"></param>
/// <returns></returns>
private static PSObject GetParaPSObject(XmlNode xmlNode, bool newLine, bool trim = true)
{
if (xmlNode == null)
return null;
if (!xmlNode.LocalName.Equals("para", StringComparison.OrdinalIgnoreCase))
return null;
PSObject mshObject = new PSObject();
StringBuilder sb = new StringBuilder();
if (newLine && !xmlNode.InnerText.Trim().Equals(string.Empty))
{
sb.AppendLine(xmlNode.InnerText.Trim());
}
else
{
var innerText = xmlNode.InnerText;
if (trim)
{
innerText = innerText.Trim();
}
sb.Append(innerText);
}
mshObject.Properties.Add(new PSNoteProperty("Text", sb.ToString()));
mshObject.TypeNames.Clear();
mshObject.TypeNames.Add("MamlParaTextItem");
mshObject.TypeNames.Add("MamlTextItem");
return mshObject;
}
/// <summary>
/// Convert a list node into an PSObject array.
///
/// For example,
/// <list class="ordered">
/// <listItem>
/// <para>
/// text for list item 1
/// </para>
/// </listItem>
/// <listItem>
/// <para>
/// text for list item 2
/// </para>
/// </listItem>
/// </list>
/// In this case, an array of PSObject, each of type "MamlOrderedListText" will be created with following
/// two properties,
/// a. tag=" 1. " or " 2. "
/// b. text="text for list item 1" or "text for list item 2"
/// In the case of unordered list, similar PSObject will created with type to be "MamlUnorderedListText" and tag="*"
/// </summary>
/// <param name="xmlNode"></param>
/// <returns></returns>
private ArrayList GetListPSObjects(XmlNode xmlNode)
{
ArrayList mshObjects = new ArrayList();
if (xmlNode == null)
return mshObjects;
if (!xmlNode.LocalName.Equals("list", StringComparison.OrdinalIgnoreCase))
return mshObjects;
if (xmlNode.ChildNodes == null || xmlNode.ChildNodes.Count == 0)
return mshObjects;
bool ordered = IsOrderedList(xmlNode);
int index = 1;
foreach (XmlNode childNode in xmlNode.ChildNodes)
{
if (childNode.LocalName.Equals("listItem", StringComparison.OrdinalIgnoreCase))
{
PSObject listItemPSObject = GetListItemPSObject(childNode, ordered, ref index);
if (listItemPSObject != null)
mshObjects.Add(listItemPSObject);
continue;
}
// If we get here, there is some tags that is not supported by maml.
WriteMamlInvalidChildNodeError(xmlNode, childNode);
}
return mshObjects;
}
/// <summary>
/// Check whether a list is ordered or not.
/// </summary>
/// <param name="xmlNode"></param>
/// <returns></returns>
private static bool IsOrderedList(XmlNode xmlNode)
{
if (xmlNode == null)
return false;
if (xmlNode.Attributes == null || xmlNode.Attributes.Count == 0)
return false;
foreach (XmlNode attribute in xmlNode.Attributes)
{
if (attribute.Name.Equals("class", StringComparison.OrdinalIgnoreCase)
&& attribute.Value.Equals("ordered", StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
return false;
}
/// <summary>
/// Convert an listItem node into an PSObject with property "tag" and "text"
/// </summary>
/// <param name="xmlNode"></param>
/// <param name="ordered"></param>
/// <param name="index"></param>
/// <returns></returns>
private PSObject GetListItemPSObject(XmlNode xmlNode, bool ordered, ref int index)
{
if (xmlNode == null)
return null;
if (!xmlNode.LocalName.Equals("listItem", StringComparison.OrdinalIgnoreCase))
return null;
string text = string.Empty;
if (xmlNode.ChildNodes.Count > 1)
{
WriteMamlInvalidChildNodeCountError(xmlNode, "para", 1);
}
foreach (XmlNode childNode in xmlNode.ChildNodes)
{
if (childNode.LocalName.Equals("para", StringComparison.OrdinalIgnoreCase))
{
text = childNode.InnerText.Trim();
continue;
}
WriteMamlInvalidChildNodeError(xmlNode, childNode);
}
string tag = string.Empty;
if (ordered)
{
tag = index.ToString("d2", CultureInfo.CurrentCulture);
tag += ". ";
index++;
}
else
{
tag = "* ";
}
PSObject mshObject = new PSObject();
mshObject.Properties.Add(new PSNoteProperty("Text", text));
mshObject.Properties.Add(new PSNoteProperty("Tag", tag));
mshObject.TypeNames.Clear();
if (ordered)
{
mshObject.TypeNames.Add("MamlOrderedListTextItem");
}
else
{
mshObject.TypeNames.Add("MamlUnorderedListTextItem");
}
mshObject.TypeNames.Add("MamlTextItem");
return mshObject;
}
/// <summary>
/// Convert definitionList node into an array of PSObject, an for
/// each definitionListItem node inside this node.
/// </summary>
/// <param name="xmlNode"></param>
/// <returns></returns>
private ArrayList GetDefinitionListPSObjects(XmlNode xmlNode)
{
ArrayList mshObjects = new ArrayList();
if (xmlNode == null)
return mshObjects;
if (!xmlNode.LocalName.Equals("definitionList", StringComparison.OrdinalIgnoreCase))
return mshObjects;
if (xmlNode.ChildNodes == null || xmlNode.ChildNodes.Count == 0)
return mshObjects;
foreach (XmlNode childNode in xmlNode.ChildNodes)
{
if (childNode.LocalName.Equals("definitionListItem", StringComparison.OrdinalIgnoreCase))
{
PSObject definitionListItemPSObject = GetDefinitionListItemPSObject(childNode);
if (definitionListItemPSObject != null)
mshObjects.Add(definitionListItemPSObject);
continue;
}
// If we get here, we found some node that is not supported.
WriteMamlInvalidChildNodeError(xmlNode, childNode);
}
return mshObjects;
}
/// <summary>
/// Convert an definitionListItem node into an PSObject
///
/// For example
/// <definitionListItem>
/// <term>
/// term text
/// </term>
/// <definition>
/// <para>
/// definition text
/// </para>
/// </definition>
/// </definitionListItem>
/// In this case, an PSObject of type "definitionListText" will be created with following
/// properties
/// a. term="term text"
/// b. definition="definition text"
/// </summary>
/// <param name="xmlNode"></param>
/// <returns></returns>
private PSObject GetDefinitionListItemPSObject(XmlNode xmlNode)
{
if (xmlNode == null)
return null;
if (!xmlNode.LocalName.Equals("definitionListItem", StringComparison.OrdinalIgnoreCase))
return null;
string term = null;
string definition = null;
foreach (XmlNode childNode in xmlNode.ChildNodes)
{
if (childNode.LocalName.Equals("term", StringComparison.OrdinalIgnoreCase))
{
term = childNode.InnerText.Trim();
continue;
}
if (childNode.LocalName.Equals("definition", StringComparison.OrdinalIgnoreCase))
{
definition = GetDefinitionText(childNode);
continue;
}
// If we get here, we found some node that is not supported.
WriteMamlInvalidChildNodeError(xmlNode, childNode);
}
if (string.IsNullOrEmpty(term))
return null;
PSObject mshObject = new PSObject();
mshObject.Properties.Add(new PSNoteProperty("Term", term));
mshObject.Properties.Add(new PSNoteProperty("Definition", definition));
mshObject.TypeNames.Clear();
mshObject.TypeNames.Add("MamlDefinitionTextItem");
mshObject.TypeNames.Add("MamlTextItem");
return mshObject;
}
/// <summary>
/// Get the text for definition. The will treat some intermediate nodes like "definition" and "para"
/// </summary>
/// <param name="xmlNode"></param>
/// <returns></returns>
private string GetDefinitionText(XmlNode xmlNode)
{
if (xmlNode == null)
return null;
if (!xmlNode.LocalName.Equals("definition", StringComparison.OrdinalIgnoreCase))
return null;
if (xmlNode.ChildNodes == null || xmlNode.ChildNodes.Count == 0)
return string.Empty;
if (xmlNode.ChildNodes.Count > 1)
{
WriteMamlInvalidChildNodeCountError(xmlNode, "para", 1);
}
string text = string.Empty;
foreach (XmlNode childNode in xmlNode.ChildNodes)
{
if (childNode.LocalName.Equals("para", StringComparison.OrdinalIgnoreCase))
{
text = childNode.InnerText.Trim();
continue;
}
WriteMamlInvalidChildNodeError(xmlNode, childNode);
}
return text;
}
#endregion
#region Preformatted string processing
/// <summary>
/// This is for getting preformatted text from an xml document.
///
/// Normally in xml document, preformatted text will be indented by
/// a fix amount based on its position. The task of this function
/// is to remove that fixed amount from the text.
///
/// For example, in xml,
/// <preformatted>
/// void function()
/// {
/// // call some other function here;
/// }
/// </preformatted>
/// we can find that the preformatted text are indented unanimously
/// by 4 spaces because of its position in xml.
///
/// After massaging in this function, the result text will be,
///
/// void function
/// {
/// // call some other function here;
/// }
///
/// please notice that the indention is reduced.
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
private static string GetPreformattedText(string text)
{
// we are assuming tabsize=4 here.
// It is discouraged to use tab in preformatted text.
string noTabText = text.Replace("\t", " ");
string[] lines = noTabText.Split('\n');
string[] trimedLines = TrimLines(lines);
if (trimedLines == null || trimedLines.Length == 0)
return string.Empty;
int minIndentation = GetMinIndentation(trimedLines);
string[] shortedLines = new string[trimedLines.Length];
for (int i = 0; i < trimedLines.Length; i++)
{
if (IsEmptyLine(trimedLines[i]))
{
shortedLines[i] = trimedLines[i];
}
else
{
shortedLines[i] = trimedLines[i].Remove(0, minIndentation);
}
}
StringBuilder result = new StringBuilder();
for (int i = 0; i < shortedLines.Length; i++)
{
result.AppendLine(shortedLines[i]);
}
return result.ToString();
}
/// <summary>
/// Trim empty lines from the either end of an string array.
/// </summary>
/// <param name="lines">Lines to trim.</param>
/// <returns>An string array with empty lines trimed on either end.</returns>
private static string[] TrimLines(string[] lines)
{
if (lines == null || lines.Length == 0)
return null;
int i = 0;
for (i = 0; i < lines.Length; i++)
{
if (!IsEmptyLine(lines[i]))
break;
}
int start = i;
if (start == lines.Length)
return null;
for (i = lines.Length - 1; i >= start; i--)
{
if (!IsEmptyLine(lines[i]))
break;
}
int end = i;
string[] result = new string[end - start + 1];
for (i = start; i <= end; i++)
{
result[i - start] = lines[i];
}
return result;
}
/// <summary>
/// Get minimum indentation of a paragraph.
/// </summary>
/// <param name="lines"></param>
/// <returns></returns>
private static int GetMinIndentation(string[] lines)
{
int minIndentation = -1;
for (int i = 0; i < lines.Length; i++)
{
if (IsEmptyLine(lines[i]))
continue;
int indentation = GetIndentation(lines[i]);
if (minIndentation < 0 || indentation < minIndentation)
minIndentation = indentation;
}
return minIndentation;
}
/// <summary>
/// Get indentation of a line, i.e., number of spaces
/// at the beginning of the line.
/// </summary>
/// <param name="line"></param>
/// <returns></returns>
private static int GetIndentation(string line)
{
if (IsEmptyLine(line))
return 0;
string leftTrimedLine = line.TrimStart(' ');
return line.Length - leftTrimedLine.Length;
}
/// <summary>
/// Test whether a line is empty.
///
/// A line is empty if it contains only white spaces.
/// </summary>
/// <param name="line"></param>
/// <returns></returns>
private static bool IsEmptyLine(string line)
{
if (string.IsNullOrEmpty(line))
return true;
string trimedLine = line.Trim();
if (string.IsNullOrEmpty(trimedLine))
return true;
return false;
}
#endregion
#region Error handling
/// <summary>
/// This is for tracking the set of errors happened during the parsing of
/// maml text.
/// </summary>
/// <value></value>
internal Collection<ErrorRecord> Errors { get; } = new Collection<ErrorRecord>();
#endregion
}
}
|