public EnumerationEntity Parse(TypedEntity typedEntity, String name, IEnumerable<XElement> elements) { EnumerationEntity enumerationEntity = new EnumerationEntity (); XElement declarationElement = (from el in elements where el.Name == "div" && el.Attribute ("class") != null && el.Attribute ("class").Value == "declaration_indent" select el).FirstOrDefault (); XElement parameterElement = (from el in elements where el.Name == "div" && el.Attribute ("class") != null && el.Attribute ("class").Value == "param_indent" select el).FirstOrDefault (); //XElement discussionElement = (from el in elements // where el.Name == "h5" && el.Value.Trim() == "Discussion" // select el).FirstOrDefault(); XElement availabilityElement = (from el in elements let term = el.Descendants ("dt").FirstOrDefault () let definition = el.Descendants ("dd").FirstOrDefault () where el.Name == "dl" && term != null && term.Value.Trim () == "Availability" select definition).FirstOrDefault (); String declaration = declarationElement.TrimAll (); String type = "NOTYPE"; String values = String.Empty; bool result = this.SplitEnumeration (declaration, ref name, ref type, ref values); if (!result) { return null; } enumerationEntity.Name = name; enumerationEntity.BaseType = type; enumerationEntity.Namespace = "MISSING"; // Extract abstract IEnumerable<XElement> abstractElements = elements.SkipWhile (el => el.Name != "p").TakeWhile (el => el.Name == "p"); foreach (XElement element in abstractElements) { String line = element.TrimAll (); if (!String.IsNullOrEmpty (line)) { enumerationEntity.Summary.Add (line); } } //// Extract discussion //if (discussionElement != null) //{ // IEnumerable<XElement> discussionElements = discussionElement.ElementsAfterSelf().TakeWhile(el => el.Name == "p"); // foreach (XElement element in discussionElements) // { // String line = element.TrimAll(); // if (!String.IsNullOrEmpty(line)) // { // enumerationEntity.Summary.Add(line); // } // } //} // Parse the values string[] pairs = values.Split (new []{','}, StringSplitOptions.RemoveEmptyEntries); foreach (string pair in pairs) { if (String.Equals (pair.TrimAll (), String.Empty)) { continue; } String key; String value = String.Empty; if (pair.IndexOf ('=') != -1) { string[] parts = pair.Split (new []{'='}, StringSplitOptions.RemoveEmptyEntries); key = parts [0].Trim (); value = parts [1].Trim (); } else { key = pair.Trim (); } // Add a new value EnumerationValueEntity enumerationValueEntity = new EnumerationValueEntity (); enumerationValueEntity.Name = key; if (value.Length == 6 && value.StartsWith ("'") && value.EndsWith ("'")) { String v = value.Trim ('\''); enumerationValueEntity.Value = "0x" + FourCharToInt (v).ToString ("X8"); } else { enumerationValueEntity.Value = value; } enumerationEntity.Values.Add (enumerationValueEntity); } XElement termList = parameterElement != null ? parameterElement.Descendants ("dl").FirstOrDefault () : null; if (termList != null) { IEnumerable<XElement> dtList = from el in termList.Elements ("dt") select el; IEnumerable<XElement> ddList = from el in termList.Elements ("dd") select el; if (dtList.Count () == ddList.Count ()) { // Iterate over definitions for (int i = 0; i < dtList.Count(); i++) { String term = dtList.ElementAt (i).TrimAll (); //String summary = ddList.ElementAt(i).TrimAll(); IEnumerable<String> summaries = ddList.ElementAt (i).Elements ("p").Select (p => p.Value.TrimAll ()); // Find the parameter EnumerationValueEntity enumerationValueEntity = enumerationEntity.Values.Find (p => String.Equals (p.Name, term)); if (enumerationValueEntity != null) { foreach (string sum in summaries) { enumerationValueEntity.Summary.Add (sum); } } } } } // Get the availability if (availabilityElement != null) { enumerationEntity.MinAvailability = CommentHelper.ExtractAvailability (availabilityElement.TrimAll ()); } return enumerationEntity; }
private List<BaseEntity> ExtractEnumeration(XElement constantElement, String name, String summary, String declaration) { declaration = declaration.Trim (';'); // Default values String type = "NOTYPE"; String values = String.Empty; // Match the enumeration definition bool result = this.SplitEnumeration (declaration, ref name, ref type, ref values); if (!result) { return null; } if (type == "NOTYPE") { type = RefineEnumBaseType(values); } // Create the enumeration EnumerationEntity enumerationEntity = new EnumerationEntity (); enumerationEntity.Name = name; enumerationEntity.BaseType = type; enumerationEntity.Namespace = "MISSING"; enumerationEntity.Summary.Add (summary); // Parse the values var pairs = values.Split (new []{'\n'}, StringSplitOptions.RemoveEmptyEntries); foreach (string immutablePair in pairs) { String key; String value = String.Empty; String pair = immutablePair.Replace(",",""); // Handle value assignment if (pair.IndexOf ('=') != -1) { string[] parts = pair.Split (new []{'='}, StringSplitOptions.RemoveEmptyEntries); key = parts [0].Trim (); value = parts [1].Trim (); } else { key = pair.Trim (); } // Add a new value EnumerationValueEntity enumerationValueEntity = new EnumerationValueEntity (); enumerationValueEntity.Name = key; if (value.Length == 6 && value.StartsWith ("'") && value.EndsWith ("'")) { String v = value.Trim ('\''); enumerationValueEntity.Value = "0x" + FourCharToInt (v).ToString ("X8"); } else { enumerationValueEntity.Value = value; } // Convert number qualifiers from native to managed enumerationValueEntity.Value = ConvertNumericQualifier (enumerationValueEntity.Value); enumerationEntity.Values.Add (enumerationValueEntity); } // Get the definitions XElement termList = (from el in constantElement.ElementsAfterSelf ("dl") where (String)el.Attribute ("class") == "termdef" select el).FirstOrDefault (); if (termList != null) { IEnumerable<XElement> dtList = termList.Elements ("dt"); IEnumerable<XElement> ddList = termList.Elements ("dd"); if (dtList.Count () == ddList.Count ()) { // Iterate over definitions for (int i = 0; i < dtList.Count(); i++) { String term = dtList.ElementAt (i).Value.TrimAll (); IEnumerable<String> summaries = ddList.ElementAt (i).Elements ("p").Select (p => p.Value.TrimAll ()); // Find the enumeration value EnumerationValueEntity enumerationValueEntity = enumerationEntity.Values.Find (v => v.Name == term); if (enumerationValueEntity != null) { foreach (string sum in summaries) { if (CommentHelper.IsAvailability (sum)) { enumerationValueEntity.MinAvailability = CommentHelper.ExtractAvailability (sum); break; } enumerationValueEntity.Summary.Add (sum); } } else { this.Logger.WriteLine ("Term with no match '" + term + "'"); } } } else { this.Logger.WriteLine ("MISMATCH in terms"); } } // Make sure availability is ok enumerationEntity.AdjustAvailability (); return new List<BaseEntity> {enumerationEntity}; }
public EnumerationEntity Parse(TypedEntity typedEntity, XElement enumerationElement) { EnumerationEntity enumerationEntity = new EnumerationEntity(); String name = enumerationElement.Element("name").TrimAll(); enumerationEntity.Name = name.Trim('_'); enumerationEntity.BaseType = "MISSING"; enumerationEntity.Namespace = "MISSING"; // Elements for brief description IEnumerable<XElement> abstractElements = enumerationElement.Element("briefdescription").Elements("para"); // Extract for detailed description IEnumerable<XElement> detailsElements = (from el in enumerationElement.Element("detaileddescription").Elements("para") where !el.Elements("simplesect").Any() && el.Elements("parameterlist").Any() && el.Elements("xrefsect").Any() select el); // Elements for values IEnumerable<XElement> enumerationValueElements = enumerationElement.Elements("enumvalue"); // Add brief description foreach (XElement paragraph in abstractElements) { enumerationEntity.Summary.Add(paragraph.TrimAll()); } foreach (XElement paragraph in detailsElements) { enumerationEntity.Summary.Add(paragraph.TrimAll()); } // Add each value foreach (XElement enumerationValueElement in enumerationValueElements) { String key = enumerationValueElement.Element("name").TrimAll(); String value; if (enumerationValueElement.Element("initializer") != null) { value = enumerationValueElement.Element("initializer").Value; value = value.Replace("=", String.Empty); value = value.TrimAll(); } else { value = String.Empty; } // Add a new value EnumerationValueEntity enumerationValueEntity = new EnumerationValueEntity(); enumerationValueEntity.Name = key; enumerationValueEntity.Value = value; enumerationEntity.Values.Add(enumerationValueEntity); // Elements for brief description abstractElements = enumerationValueElement.Element("briefdescription").Elements("para"); // Add brief description foreach (XElement paragraph in abstractElements) { enumerationValueEntity.Summary.Add(paragraph.TrimAll()); } } /* // Get the availability if (availabilityElement != null) { enumerationEntity.MinAvailability = CommentHelper.ExtractAvailability(availabilityElement.TrimAll()); } */ return enumerationEntity; }
/// <summary> /// Parses the specified entity. /// </summary> /// <param name = "entity">The entity.</param> /// <param name = "reader">The reader.</param> public override void Parse(BaseEntity entity, TextReader reader) { EnumerationEntity enumerationEntity = (EnumerationEntity)entity; if (enumerationEntity == null) { throw new ArgumentException ("EnumerationEntity expected", "entity"); } IParser parser = ParserFactory.CreateParser (SupportedLanguage.CSharp, reader); parser.Parse (); // Extract the special nodes (comment, etc) List<ISpecial> specials = parser.Lexer.SpecialTracker.RetrieveSpecials (); this.CodeDomSpecialParser = new CodeDomSpecialParser (specials); // Parse the compilation unit CompilationUnit cu = parser.CompilationUnit; foreach (INode child1 in cu.Children) { NamespaceDeclaration namespaceDeclaration = child1 as NamespaceDeclaration; if (namespaceDeclaration == null) { continue; } foreach (INode child2 in child1.Children) { TypeDeclaration declaration = child2 as TypeDeclaration; if (declaration == null) { continue; } if (declaration.Type != ClassType.Enum) { continue; } // Extract basic informations enumerationEntity.BaseType = declaration.BaseTypes.Count > 0 ? declaration.BaseTypes [0].Type : "int"; // Extract attributes Attribute attribute = FindAttribute (declaration, "Flags"); enumerationEntity.Flags = (attribute != null); IEnumerable<Comment> comments = this.GetDocumentationCommentsBefore (declaration); AppendComment (entity, comments); // Append each values foreach (INode child3 in declaration.Children) { FieldDeclaration fieldDeclaration = child3 as FieldDeclaration; if (fieldDeclaration == null) { continue; } EnumerationValueEntity valueEntity = new EnumerationValueEntity (); valueEntity.Name = fieldDeclaration.Fields [0].Name; Expression expression = fieldDeclaration.Fields [0].Initializer; if (expression != null) { CodeDomExpressionPrinter printer = new CodeDomExpressionPrinter (); expression.AcceptVisitor (printer, null); valueEntity.Value = printer.ToString (); } comments = this.GetDocumentationCommentsBefore (fieldDeclaration); AppendComment (valueEntity, comments); enumerationEntity.Values.Add (valueEntity); } } } // Ensure that availability is set on entity. entity.AdjustAvailability (); }