private string[] ExpandToMultipleWords(string shortForm, MultiWordPattern pattern, XElement methodXml, string methodComment, string classComment)
{
//construct the regular expression for the pattern
string patternRegex;
if (pattern == MultiWordPattern.Acronym)
{
StringBuilder sb = new StringBuilder(@"\b");
for (int i = 0; i < shortForm.Length - 1; i++)
{
sb.AppendFormat(@"{0}\w+\s+", shortForm[i]);
}
sb.AppendFormat(@"{0}\w+\b", shortForm[shortForm.Length - 1]);
patternRegex = sb.ToString();
}
else if (pattern == MultiWordPattern.CombinationWord)
{
StringBuilder sb = new StringBuilder(@"\b");
for (int i = 0; i < shortForm.Length - 1; i++)
{
sb.AppendFormat(@"{0}\w*?\s*?", shortForm[i]);
}
sb.AppendFormat(@"{0}\w*?\b", shortForm[shortForm.Length - 1]);
patternRegex = sb.ToString();
}
else
{
throw new ArgumentException("Pattern must be a valid member of MultiWordPattern.", "pattern");
}
//search for potential long forms
if (pattern == MultiWordPattern.Acronym || shortForm.Length > 3)
{
List<string> matches = new List<string>();
bool foundMatch = false;
//1. Search JavaDoc comments. We don't have these, so skip this step.
//2. Search Type Names and variable names for "pattern sf"
var decls = from decl in methodXml.Descendants(SRC.Declaration)
select decl;
foreach (var decl in decls)
{
//Console.WriteLine("Decl found: {0}", decl.Value);
Match m = Regex.Match(decl.Value, string.Format(@"({0})\s+{1}", patternRegex, shortForm), RegexOptions.IgnoreCase);
if (m.Success)
{
matches.Add(m.Groups[1].Value);
foundMatch = true;
}
}
//3. Search Method Name for pattern
if (!foundMatch)
{
Match m = Regex.Match(methodXml.Element(SRC.Name).Value, string.Format("({0})", patternRegex), RegexOptions.IgnoreCase);
if (m.Success)
{
matches.Add(m.Groups[1].Value);
foundMatch = true;
}
}
//4. Search all identifiers in the method for "pattern" (including type names)
if (!foundMatch)
{
var nameQuery = from name in methodXml.Descendants(SRC.Name)
select name;
foreach (XElement name in nameQuery)
{
Match m = Regex.Match(name.Value, string.Format("({0})", patternRegex), RegexOptions.IgnoreCase);
if (m.Success)
{
matches.Add(m.Groups[1].Value);
foundMatch = true;
}
}
}
//5. Search string literals for "pattern"
if (!foundMatch)
{
var stringQuery = from lit in methodXml.Descendants(LIT.Literal)
where lit.Attribute("type").Value == "string"
select lit;
foreach (XElement str in stringQuery)
{
Match m = Regex.Match(str.Value, string.Format("({0})", patternRegex), RegexOptions.IgnoreCase);
if (m.Success)
{
matches.Add(m.Groups[1].Value);
foundMatch = true;
}
}
}
//6. Search method comment words for "pattern"
if (!foundMatch)
{
Match m = Regex.Match(methodComment, string.Format("({0})", patternRegex), RegexOptions.IgnoreCase);
if (m.Success)
{
matches.Add(m.Groups[1].Value);
foundMatch = true;
}
}
//7. Search class comment words for "pattern"
if (!foundMatch && pattern == MultiWordPattern.Acronym)
{
Match m = Regex.Match(classComment, string.Format("({0})", patternRegex), RegexOptions.IgnoreCase);
if (m.Success)
{
matches.Add(m.Groups[1].Value);
foundMatch = true;
}
}
return matches.ToArray();
}
else
{
return new string[0];
}
}