OCTOBER 8TH, 2008
By RYAN
Fixed the nesting problem. Fixed item parsing. Item stats for nested ones units show up now. As with the Ruby parser, throw different combinations at it and see what happens.
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
| using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Xml.XPath;
namespace abparser
{
class Program
{
static void Main(string[] args)
{
RosterParserTest.XmlParser parser = new RosterParserTest.XmlParser();
parser.ParseRoster(@"C:\Temp\de7th.rst", @"C:\Temp\output.xml");
Console.ReadLine();
}
}
}
namespace RosterParserTest
{
class XmlParser
{
static XmlDocument Roster = new XmlDocument();
static XmlElement rootElement = Roster.CreateElement("", "Army", "");
public static string RemoveWhitespace(string str)
{
try
{
//Ryan's Regex
return new Regex(@"(\s+|\{.*?\}|\(.*?\)|\/+|\.+)").Replace(str, String.Empty);
}
catch (Exception)
{
return str;
}
}
public void ParseNestedXML(XmlElement thisElement, XmlElement rosterElement)
{
bool linkUnitStatsDone = false; //This is a dirty hack.
string replaceMe = thisElement.GetAttribute("name").ToString();
replaceMe = RemoveWhitespace(replaceMe);
XmlElement baseElement;
XmlNodeList linkUnitStatNodeList = thisElement.SelectNodes("./link | ./unitstat");
/*Grab the last of the PascalCase names. HarGanethExecutioners becomes Executioners
* SupremeSorceress becomes Sorceress, etc. Replace the rest of the name with a backreference */
string regexMatcher = Regex.Replace(replaceMe, @".*?([A-Z][a-z]+)$", "${1}");
//This way, it'll actually parse the NodeList for stats in nested things.
if (Regex.IsMatch(rosterElement.Name.ToString(), regexMatcher)) //So that I don't get duplicate empty nodes.
{
baseElement = rosterElement; //Adding to the previous node in the tree.
foreach (XmlElement parseElement in thisElement)
{
if (parseElement.HasChildNodes && parseElement.InnerXml.Contains("entity"))
{
ParseNestedXML(parseElement, baseElement); //Parsing out nested.
} //if (parseElement.HasChildNodes && parseElement.InnerXml.Contains("entity"))
else if (!linkUnitStatsDone)
{
ParseLinkUnitStats(linkUnitStatNodeList, baseElement);
linkUnitStatsDone = true; //Hack implemented.
} //else if (!linkUnitStatsDone)
} //foreach (XmlElement parseElement in thisElement)
}
else
{
baseElement = Roster.CreateElement(replaceMe);
foreach (XmlElement parseElement in thisElement)
{
if (parseElement.HasChildNodes && parseElement.InnerXml.Contains("entity"))
{
ParseNestedXML(parseElement, baseElement); //Whee recursion.
} //if (parseElement.HasChildNodes && parseElement.InnerXml.Contains("entity"))
else
{
ParseLinkUnitStats(parseElement, baseElement); //This has always worked.
} //else
rosterElement.AppendChild(baseElement); //Add to the local node.
rootElement.AppendChild(rosterElement); //Add to the Army node.
} //foreach (XmlElement parseElement in thisElement)
} //else
} //public void ParseNestedXML(XmlElement thisElement, XmlElement rosterElement)
public void ParseRoster(string path, string output)
{
XmlDocument parsingRoster = new XmlDocument();
parsingRoster.Load(path);
XmlNodeList parsingElements = parsingRoster.SelectNodes("/document/squad");
foreach (XmlElement thisElement in parsingElements)
{
XmlElement rosterElement = Roster.CreateElement("Unit");
ParseNestedXML(thisElement, rosterElement);
} //foreach (XmlElement thisElement in parsingElements)
Roster.AppendChild(rootElement);
Roster.Save(output);
} //public void ParseRoster(string path, string output)
public void ParseLinkUnitStats(XmlElement parseElement, XmlElement baseElement)
{
foreach (XmlElement correctElement in parseElement)
{
if (correctElement.HasAttribute("name"))
{
string subReplaceMe = correctElement.GetAttribute("name").ToString();
subReplaceMe = RemoveWhitespace(subReplaceMe);
XmlElement addElement = Roster.CreateElement(subReplaceMe);
if (!Regex.Match(subReplaceMe, @"(Left|Worker|Helper|Pts|Coun|Group)").Success)
{
if (parseElement.HasChildNodes && parseElement.InnerXml.Contains("entity"))
{
//Console.WriteLine("Found an item (XmlElement)");
ParseNestedXML(addElement, correctElement);
}
else if (correctElement.HasAttribute("description"))
{
addElement.InnerText = correctElement.GetAttribute("description").ToString();
} //if correctElement.HasAttribute("description"))
else if (correctElement.HasAttribute("value") && (Regex.IsMatch(correctElement.GetAttribute("value"), @"[^0|-]")))
{
addElement.InnerText = RemoveWhitespace(correctElement.GetAttribute("value").ToString());
baseElement.AppendChild(addElement);
} //else if (correctElement.HasAttribute("value"))
} //else
if (parseElement.HasAttribute("basename"))
{
/*It's a non-dwarf item. Whee! They don't show up in the XmlNodeList one.
Get rid of newlines and periods at the end, then set it as the InnerText
This doesn't catch cases where the item has other properties inside it, but
I haven't seen those */
baseElement.InnerText = Regex.Replace(parseElement.GetAttribute("itemsummary"), @"(\\n|\.)", String.Empty);
}
} //if (correctElement.HasAttribute("name")
} //foreach (XmlElement correctElement in parseElement)
} //public void ParseLinkUnitStats(XmlElement parseElement, XmlElement baseElement)
public void ParseLinkUnitStats(XmlNodeList parseNodeList, XmlElement baseElement)
{
foreach (XmlElement correctElement in parseNodeList)
{
if (correctElement.HasAttribute("name"))
{
string subReplaceMe = correctElement.GetAttribute("name").ToString();
subReplaceMe = RemoveWhitespace(subReplaceMe);
if (!Regex.Match(subReplaceMe, @"(Left|Worker|Helper|Pts|Coun|Group)").Success)
{
XmlElement addElement = Roster.CreateElement(subReplaceMe);
if (correctElement.HasChildNodes && correctElement.InnerXml.Contains("entity"))
{
//Console.WriteLine("Found an item (XmlNodeList)");
ParseNestedXML(addElement, correctElement);
}
if (correctElement.HasAttribute("description"))
{
addElement.InnerText = correctElement.GetAttribute("description").ToString();
baseElement.AppendChild(addElement);
} //if (correctElement.HasAttribute("description"))
else if (correctElement.HasAttribute("value") && (Regex.IsMatch(correctElement.GetAttribute("value"), @"[^0|-]")))
{
addElement.InnerText = RemoveWhitespace(correctElement.GetAttribute("value").ToString());
baseElement.AppendChild(addElement);
} //else if (correctElement.HasAttribute("value"))
} //else
} //if (correctElement.HasAttribute("name"))
} //foreach (XmlElement correctElement in parseNodeList)
} //public void ParseLinkUnitStats(XmlNodeList parseNodeList, XmlElement baseElement)
} //class XmlParser
} //namespace RosterParserTest |
OCTOBER 3RD, 2008
By RYAN
I’m already not that fond of working with XML in .NET. Here are a couple of fixes:
public static string RemoveWhitespace(string str)
{
try
{
return new Regex(@"(\s+|\{.*?\}|\(.*?\)|\/+|\.+)").Replace(str, String.Empty);
}
catch (Exception)
{
return str;
}
}
Which actually gets rid of the crap in the braces, parentheses, etc (as well as getting rid of periods).
Secondly, I loathe empty nodes (stats, etc).
replaceMe = RemoveWhitespace(replaceMe);
Console.WriteLine(replaceMe);
if (replaceMe != String.Empty)
{
XmlElement baseElement = Roster.CreateElement(replaceMe);
foreach (XmlElement parseElement in thisElement)
{
if (parseElement.HasChildNodes && parseElement.InnerXml.Contains("entity"))
{
ParseNestedXML(parseElement, baseElement);
}
else
{
foreach (XmlElement correctElement in parseElement)
{
if (correctElement.HasAttribute("name"))
{
string subReplaceMe = correctElement.GetAttribute("name").ToString();
subReplaceMe = RemoveWhitespace(subReplaceMe);
XmlElement addElement = Roster.CreateElement(subReplaceMe);
if (correctElement.HasAttribute("description"))
{
addElement.InnerText = correctElement.GetAttribute("description").ToString();
baseElement.AppendChild(addElement);
}
//Bye, stats with a value of zero or a hyphen!
else if (correctElement.HasAttribute("value") && (Regex.Match(correctElement.GetAttribute("value").ToString(), @"[^0|-]").Success))
{
addElement.InnerText = correctElement.GetAttribute("value").ToString();
baseElement.AppendChild(addElement);
}
}
}
}
rosterElement.AppendChild(baseElement);
rootElement.AppendChild(rosterElement);
}
}
I find it kind of ironic that recursion is used after bitching about recursion. I’ll probably take a look at the nesting problems, and whatnot this weekend, assuming I have any time.
I wonder if it’s possible to get a job doing nothing but writing regular expressions…
OCTOBER 1ST, 2008
By RYAN
Ok, boring. I didn’t spend as much time working on it tonight as I intended to, but it parses the Dwarf roster, at least, fine. It does not parse the Dark Elf roster properly (namely, it doesn’t pull the description out of items or Gifts of Khaine, and it doubles up the <item> tag for reasons I’m not sure of), but that’ll get fixed when I’m at work tomorrow.
Ruby code:
#!/usr/bin/ruby
require "rexml/document"
require "pp"
require "rexml/formatters/default"
include REXML
inputxml = File.read('dwarfroster.rst')
@roster = Document.new inputxml
@army = Document.new.add_element("army")
def parsenested(process, addto)
#Try to guess if it's a champion, character in the unit, or item
process.elements.each('entity') do |p|
#puts p
if p.elements["link"].has_elements?
#Recursively run through these to figure out what the hell it is
if p.elements["link/entity"].attributes["itemsummary"].any?
adder = addto.add_element("item")
puts "Found nested\n"
parsenested(p.elements["link"], adder)
else
#This is really just stubbed out, since I haven't seen it
end
elsif p.attributes["statset"] =~ /Normal/
#It's a character, crew, or mount. Figure out which
if p.attributes["totalcost"] !~ /^0/
#It's a champion or character
adder = addto.add_element("champion")
puts "Found champ\n"
parse(p, adder)
else
#It's crew or the like
adder = addto.add_element("crew")
puts "Found crew\n"
parse(p, adder)
end
else
#It's an item
puts "Found item\n"
if addto.elements["item"].nil?
@adder = addto.add_element("item")
end
added = @adder.add_element(p.attributes["name"].gsub(/\s+/, ''))
p.elements.each('link') do |ele|
unless ele.attributes["name"] =~ /(Worker|Helper|Cost|Left)/
added.add_element(ele.attributes["name"].gsub(/\s+/, '')).add_text(ele.attributes["description"])
end
end
end
end
end
def parse(s, addto)
#In some cases, the basename differs (i.e. Supreme Sorc vs. High Sorc)
#Also, it'll pick up whether there's a champion in the unit by the diff
#of base and count
%w[basename count base].each do |b|
if s.attributes[b].any?
addto.add_element(b).add_text(s.attributes[b])
end
end
stats = addto.add_element("stats")
s.elements.each('unitstat') do |a|
#unit.fetch(:stats) { |el| unit[el] = {}}
#I don't want blank stats
if a.attributes["value"].any? && (a.attributes["value"] !~ /(0|-)/)
stats.add_element(a.attributes["name"].gsub(/\s+/, '')).add_text(a.attributes["value"])
end
end
s.elements.each('link') do |link|
if link.has_elements?
#Figure out what the hell it is
parsenested(link, addto)
else
#unitatt = unit.add_element("attributes")
#Rip out the name if it doesn't have "Helper, Worker, Points Left, or Cost"
unless link.attributes["name"] =~ /(Worker|Helper|Cost|Left)/
#Get rid of the stuff in braces AB puts in
if addto.elements["attributes"].nil?
@unitatt = addto.add_element("attributes")
end
@unitatt.add_element(link.attributes["name"].gsub(/\{.*?\}/, '').gsub(/\s+/, '')).add_text('true')
end
end
end
end
@roster.elements.each('document/roster') do |ele|
info = @army.add_element("info")
#Pick out the race, army name, total points, used points, canonical race name
%w[race size activesize racename].each do |attr|
info.add_element(attr).add_text(ele.attributes[attr])
end
#@army.push(info)
end
@roster.elements.each('document/squad') do |ele|
@unit = @army.add_element("unit")
#Pick out the name of the model and its cost, plus how many models
%w[name modelcount totalcost].each do |attr|
@unit.add_element(attr).add_text(ele.attributes[attr])
end
ele.elements.each('entity') do |s|
#Parse it out
parse(s, @unit)
end
end
#pp @army
prettyprint = REXML::Formatters::Pretty.new
output = String.new
puts prettyprint.write(@army, output)
And the XML output:
<?xml version="1.0" encoding="ISO-8859-1"?>
<army>
<info>
<race>Dwarf</race>
<size>1500</size>
<activesize>1499.</activesize>
<racename>Dwarfs</racename>
</info>
<unit>
<name>Thane</name>
<modelcount>1</modelcount>
<totalcost>134</totalcost>
<basename>Thane</basename>
<count>1</count>
<base>1</base>
<stats>
<Ld>9</Ld>
<Mv>3</Mv>
<Save>3+</Save>
<St>4/8</St>
<To>5</To>
<UnitSt.>1</UnitSt.>
<WS>6</WS>
<Wo>2</Wo>
<At>3</At>
<BS>4</BS>
<In>3</In>
<ItemPts>75</ItemPts>
</stats>
<attributes>
<General>true</General>
<HandWeapon>true</HandWeapon>
<GreatWeapon>true</GreatWeapon>
<GromrilArmor>true</GromrilArmor>
</attributes>
<item>
<RunicWeapon>
<MasterRuneofKraggtheGrim>Allows other runes to be placed on a Great Weapon.</MasterRuneofKraggtheGrim>
<RuneofCleaving>+1 Strength</RuneofCleaving>
</RunicWeapon>
<RunicArmor>
<RuneofStone>+1 Armor Save</RuneofStone>
</RunicArmor>
</item>
</unit>
<unit>
<name>Thane</name>
<modelcount>1</modelcount>
<totalcost>132</totalcost>
<basename>Thane</basename>
<count>1</count>
<base>1</base>
<stats>
<In>3</In>
<ItemPts>75</ItemPts>
<Ld>9</Ld>
<Mv>3</Mv>
<Save>2+/1+</Save>
<St>4/7</St>
<To>5</To>
<UnitSt.>1</UnitSt.>
<WS>6</WS>
<Wo>2</Wo>
<At>3</At>
<BS>4</BS>
</stats>
<attributes>
<HandWeapon>true</HandWeapon>
<GromrilArmor>true</GromrilArmor>
<Shield>true</Shield>
</attributes>
<item>
<RunicWeapon>
<RuneofCleaving>+1 Strength</RuneofCleaving>
</RunicWeapon>
<RunicArmor>
<RuneofStone>+1 Armor Save</RuneofStone>
</RunicArmor>
</item>
</unit>
<unit>
<name>Thane</name>
<modelcount>1</modelcount>
<totalcost>95</totalcost>
<basename>Thane</basename>
<count>1</count>
<base>1</base>
<stats>
<In>3</In>
<ItemPts>75</ItemPts>
<Ld>9</Ld>
<Mv>3</Mv>
<Save>3+</Save>
<St>4</St>
<To>5</To>
<UnitSt.>1</UnitSt.>
<WS>6</WS>
<Wo>2</Wo>
<At>3</At>
<BS>4</BS>
</stats>
<attributes>
<HandWeapon>true</HandWeapon>
<GromrilArmor>true</GromrilArmor>
<BattleStandardBearer>true</BattleStandardBearer>
</attributes>
<item>
<RunicArmor>
<RuneofStone>+1 Armor Save</RuneofStone>
</RunicArmor>
</item>
</unit>
<unit>
<name>Dwarf Warriors</name>
<modelcount>20</modelcount>
<totalcost>205</totalcost>
<basename>Dwarf Warriors</basename>
<count>19</count>
<base>20</base>
<stats>
<In>2</In>
<Ld>9</Ld>
<Mv>3</Mv>
<Save>4+/3+</Save>
<St>3</St>
<To>4</To>
<UnitSt.>1</UnitSt.>
<WS>4</WS>
<Wo>1</Wo>
<At>1</At>
<BS>3</BS>
</stats>
<champion>
<basename>Veteran</basename>
<count>1</count>
<base>1</base>
<stats>
<In>2</In>
<Ld>9</Ld>
<Mv>3</Mv>
<Save>4+/3+</Save>
<St>3</St>
<To>4</To>
<UnitSt.>1</UnitSt.>
<WS>4</WS>
<Wo>1</Wo>
<At>2</At>
<BS>3</BS>
</stats>
<attributes>
<HandWeapon>true</HandWeapon>
<HeavyArmor>true</HeavyArmor>
<Shield>true</Shield>
</attributes>
</champion>
<attributes>
<Musician>true</Musician>
<StandardBearer>true</StandardBearer>
<HandWeapon>true</HandWeapon>
<HeavyArmor>true</HeavyArmor>
<Shield>true</Shield>
</attributes>
</unit>
<unit>
<name>Quarellers</name>
<modelcount>10</modelcount>
<totalcost>110</totalcost>
<basename>Quarrellers</basename>
<count>10</count>
<base>10</base>
<stats>
<In>2</In>
<Ld>9</Ld>
<Mv>3</Mv>
<Save>6+</Save>
<St>3</St>
<To>4</To>
<UnitSt.>1</UnitSt.>
<WS>4</WS>
<Wo>1</Wo>
<At>1</At>
<BS>3</BS>
</stats>
<attributes>
<HandWeapon>true</HandWeapon>
<Crossbow>true</Crossbow>
<LightArmor>true</LightArmor>
</attributes>
</unit>
<unit>
<name>Quarellers</name>
<modelcount>10</modelcount>
<totalcost>110</totalcost>
<basename>Quarrellers</basename>
<count>10</count>
<base>10</base>
<stats>
<In>2</In>
<Ld>9</Ld>
<Mv>3</Mv>