//-----------------------------------------------------------------------------
//
// Convert MS XML documentation of doxygen compliant code into a
// XML documentation that works.
//
// Written by Harald Meyer auf'm Hofe
// (C) 2007 by Harald Meyer auf'm Hofe
// Licensed under the wxWidgets license, see LICENSE.txt for details.
//
// $Id: Program.cs,v 1.2 2007/12/25 19:24:36 harald_meyer Exp $
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
namespace NormalizeXmlDoc{
class Program
{
enum MissingTags
{
summary,
param,
result
}
static int Main(string[] args)
{
Console.WriteLine("NormalizeXmlDoc Xml.Documentation.Assembly.xml");
int retCode = 0;
foreach (string arg in args)
{
try
{
Console.WriteLine("Reading file {0}.", arg);
File.Move(arg, arg + ".bak");
using (TextWriter destString = new StreamWriter(arg))
{
int isMemberTag = 0;
XmlTextWriter dest = new XmlTextWriter(destString);
dest.Formatting = Formatting.Indented;
dest.WriteStartDocument();
XmlTextReader reader = new XmlTextReader(arg+".bak");
bool gotContentForMember = false;
// Parse the file and display each of the nodes.
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
string elemName = reader.Name;
if (isMemberTag == 0)
{
dest.WriteStartElement(reader.Name);
if (reader.HasAttributes)
{
while (reader.MoveToNextAttribute())
{
dest.WriteAttributeString(reader.Name, reader.Value);
}
}
}
if (isMemberTag > 0)
gotContentForMember = true;
if (elemName == "member")
{
++isMemberTag;
gotContentForMember = false;
}
break;
case XmlNodeType.Text:
if (isMemberTag > 0)
{
gotContentForMember = true;
string text = reader.Value;
text = text.Replace("\\c", "");
text = text.Replace("\\e", "");
text = text.Replace("\\b", "");
text = text.Replace("\\li", "-");
text = text.Replace("\\verbatim", "\n");
text = text.Replace("\\endverbatim", "\n");
text = text.Replace("\\code", "\n");
text = text.Replace("\\endcode", "\n");
string[] lines=text.Split('\n');
string summary="";
List<string[]> parameter = new List<string[]>();
string result = "";
MissingTags mode = MissingTags.summary;
int lineIndex = 0;
foreach (string originalLine in lines)
{
string line = originalLine.Trim();
if (line.Length == 0)
{
mode = MissingTags.summary;
}
else if (line.StartsWith("\\param"))
{
line=line.Substring(7);
int nextChar = line.IndexOf(' ');
mode = MissingTags.param;
}
else if (line.StartsWith("\\result"))
{
line = line.Substring(8);
mode = MissingTags.result;
}
switch (mode)
{
case MissingTags.summary:
if (lineIndex < 15)
summary += "\n\r" + line;
++lineIndex;
break;
case MissingTags.result:
result += "\n\r" + line;
break;
case MissingTags.param:
{
int nextBlank = line.IndexOf(' ');
string[] newEntry = new string[2];
if (nextBlank > 0)
{
newEntry[0] = line.Substring(0, nextBlank).Trim();
newEntry[1] = line.Substring(nextBlank).Trim();
}
else
{
newEntry[0] = line.Trim();
newEntry[1] = "";
}
break;
}
}
}
summary = summary.Trim(' ', '\n', '\r');
dest.WriteElementString("summary", summary);
result = result.Trim(' ', '\n', '\r');
if (result.Length>0)
dest.WriteElementString("result", result);
foreach (string[] paramEntry in parameter)
{
dest.WriteStartElement("param");
dest.WriteAttributeString("name", paramEntry[0]);
dest.WriteString(paramEntry[1]);
dest.WriteEndElement();
}
}
else
dest.WriteString(reader.Value);
break;
case XmlNodeType.CDATA:
dest.WriteCData(reader.Value);
break;
case XmlNodeType.ProcessingInstruction:
dest.WriteProcessingInstruction(reader.Name, reader.Value);
break;
case XmlNodeType.Comment:
dest.WriteComment(reader.Value);
break;
case XmlNodeType.XmlDeclaration:
break;
case XmlNodeType.Document:
break;
case XmlNodeType.DocumentType:
dest.WriteString(string.Format("<!DOCTYPE {0} [{1}]", reader.Name, reader.Value));
break;
case XmlNodeType.EntityReference:
dest.WriteEntityRef(reader.Name);
break;
case XmlNodeType.EndElement:
if (reader.Name == "member")
{
--isMemberTag;
if (!gotContentForMember)
dest.WriteElementString("summary", "");
}
if (isMemberTag == 0)
dest.WriteEndElement();
break;
}
}
dest.WriteEndDocument();
}
}
catch (Exception exc)
{
Console.WriteLine(exc);
retCode += 1;
}
}
return retCode;
}
}
}
|