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
|
# -*- coding: utf-8 -*-
require "rexml/document"
class OrderedAttributes < REXML::Formatters::Pretty
def write_element(elm, out)
att = elm.attributes
class <<att
alias _each_attribute each_attribute
def each_attribute(&b)
order = []
to_enum(:_each_attribute).each {|x| order.push(x.name)}
to_enum(:_each_attribute).sort_by {|x| order.find_index(x)}.each(&b)
end
end
super(elm, out)
end
end
class DataDictionary
def initialize( major, minor, sp )
filesp = ""
if( sp != 0 )
filesp = "SP" + sp.to_s
end
filename = "FIX#{major}#{minor}#{filesp}.xml"
@f = File.new( filename, File::CREAT|File::TRUNC|File::RDWR )
@major = major
@minor = minor
@sp = sp
directory = "FIX.#{major}.#{minor}"
if( sp != 0 )
directory = directory + "SP#{sp}"
end
directory += "/Base"
@fieldsDoc = REXML::Document.new( File.new( "#{directory}/Fields.xml" ) )
@enumsDoc = REXML::Document.new( File.new( "#{directory}/Enums.xml" ) )
@msgContentsDoc = REXML::Document.new( File.new("#{directory}/MsgContents.xml") )
@msgTypeDoc = REXML::Document.new( File.new("#{directory}/Messages.xml") )
@componentsDoc = REXML::Document.new( File.new("#{directory}/Components.xml") )
@specDoc = REXML::Document.new()
@tagToField = Hash.new
@tagToEnum = Hash.new
@msgIdToMsgType = Hash.new
@msgIdToMsgContents = Hash.new
@msgIdToTags = Hash.new
@componentNameToMsgId = Hash.new
puts filename
parseDictionary
printDictionary
end
def tagShouldBeSkipped( tag )
#return true if tag == 101
#return true if tag == 261
#return true if tag == 809
end
def tagShouldNotPrint( tag )
end
def enumsShouldBeSkipped( tag )
return true if tagShouldBeSkipped( tag )
return true if tag == 264
end
def addEnumsToTag( tag )
enumArray = Array.new
if( tag == 156 )
enumArray.push( ["M","MULTIPLY"] )
enumArray.push( ["D","DIVIDE"] )
else
return
end
@tagToEnum[ tag ] = enumArray
end
def toFieldName( fieldName, type )
fieldName = fieldName.split(" (")[0].split("(")[0].strip.gsub(' ', '')
fieldName.gsub!("UnitofMeasure", "UnitOfMeasure")
fieldName = "#{fieldName}#{type.capitalize}" if fieldName == "HaltReason"
return fieldName
end
def toMessageName( messageName )
messageName = messageName.split("(")[0].delete("& -/’_")
return "NewOrderSingle" if messageName == "OrderSingle"
return "NewOrderList" if messageName == "OrderList"
return "DontKnowTrade" if messageName == "DontKnowTradeDK"
return messageName
end
def toDescription( description )
return "" if description == nil
old = description.clone
description.upcase!
description = description.split("(")[0]
return "" if description == nil
description.strip!
description.gsub!('"', '')
description.gsub!('+ ', '+')
description.gsub!(',', '_')
description.gsub!(' ', '_')
description.gsub!('-', '_')
description.gsub!('–', '')
description.gsub!('/', '_')
description.gsub!('+', ' PLUS ')
description.gsub!("'", '')
description.gsub!("’", '')
description.gsub!("‘", '')
description.gsub!("&", '')
description.gsub!('<', '')
description.gsub!('>', '')
description.gsub!('[', '')
description.gsub!(']', '')
description.gsub!('=', '')
description.gsub!('"', '')
description.gsub!('.', '')
description.gsub!(':', '')
description.gsub!(';', '')
description.gsub!('%', '')
description.gsub!('*', '')
description.gsub!('_', ' ')
description.strip!
description.gsub!(' ', '_')
description = description.split("___")[0]
description.squeeze!("_") if description != nil
description.gsub!('NOT_ASSIGNED_', '') if description != nil
return "FULL_REFRESH" if description == "FULL_REFERSH"
return "INCREMENTAL_REFRESH" if description == "INCREMENTAL_REFRES"
return description
end
def toType( type )
type = type.gsub('-', '')
return "NUMINGROUP" if type == "NUMINGRP"
return "LOCALMKTDATE" if type == "LOCALMMKTDATE"
return "QTY" if type == "QUANTITY"
return "STRING" if type == "STIRNG"
return type
end
def parseFields
@fieldsDoc.elements["Fields"].elements.each("Field") { |fieldsElement|
tag = fieldsElement.elements["Tag"].text.to_i
type = fieldsElement.elements["Type"].text
next if type == nil
type.upcase!
next if tagShouldBeSkipped( tag )
addEnumsToTag( tag )
fieldHash = Hash.new
fieldHash[ "type" ] = toType( type )
fieldName = toFieldName(fieldsElement.elements["Name"].text, type)
fieldHash[ "fieldName" ] = fieldName
@tagToField[ tag ] = fieldHash
}
end
def parseEnums
enums = Hash.new
@enumsDoc.elements["Enums"].elements.each("Enum") { |enumsElement|
tag = enumsElement.elements["Tag"].text.to_i
next if enumsShouldBeSkipped( tag )
enum = enumsElement.elements["Value"].text
next if enum.upcase == "(NOT SPECIFIED)"
description = toDescription(enumsElement.elements["Description"].text)
description = toDescription(enum) if description == nil
next if description == nil
if( tag != 206 )
if( !enums.has_key?(tag) )
enums[tag] = Array.new
end
enumArray = enums[tag]
enumArray.push( [enum,description] )
@tagToEnum[ tag ] = enumArray
end
}
end
def parseMsgType
@msgTypeDoc.elements["Messages"].elements.each("Message") { |msgTypeElement|
msgId = msgTypeElement.elements["ComponentID"].text.to_i
msgType = msgTypeElement.elements["MsgType"].text
messageName = toMessageName(msgTypeElement.elements["Name"].text)
componentID = msgTypeElement.elements["ComponentID"].text
category = msgTypeElement.elements["CategoryID"].text
category = category == "Session" ? "admin" : "app"
msgTypeHash = Hash.new
msgTypeHash[ "msgtype" ] = msgType
msgTypeHash[ "name" ] = messageName
msgTypeHash[ "msgcat" ] = category
@msgIdToMsgType[ msgId ] = msgTypeHash
}
end
def parseMsgContents
@msgContentsDoc.elements["MsgContents"].elements.each("MsgContent") { |msgContentsElement|
tagsInMsg = []
msgId = msgContentsElement.elements["ComponentID"].text.to_i
tagText = msgContentsElement.elements["TagText"].text
next if tagText == "StandardHeader"
next if tagText == "StandardTrailer"
tag = tagText.to_i
indent = msgContentsElement.elements["Indent"].text.to_i
required = msgContentsElement.elements["Reqd"].text.to_i == 1 ? "Y" : "N"
position = msgContentsElement.elements["Position"].text.to_i
if( tag == 1017 && msgId != 1005 )
next
end
if( !@msgIdToMsgContents.has_key?(msgId) )
@msgIdToTags[ msgId ] = Hash.new
@msgIdToMsgContents[msgId] = Array.new
end
msgContentsArray = @msgIdToMsgContents[msgId]
if( tag == 0 )
msgContentsArray.push( [tagText, tagText, indent, required] )
@msgIdToMsgContents[ msgId ] = msgContentsArray
next
end
next if !@tagToField.has_key?(tag)
name = @tagToField[ tag ]["fieldName"]
if( !@msgIdToTags[ msgId ].has_key?(tag) )
@msgIdToTags[ msgId ][ tag ] = tag
msgContentsArray.push( [tag,name,indent,required] )
end
@msgIdToMsgContents[ msgId ] = msgContentsArray
}
end
def parseComponents
@componentsDoc.elements["Components"].elements.each("Component") { |componentsElement|
msgId = componentsElement.elements["ComponentID"].text.to_i
name = componentsElement.elements["Name"].text
@componentNameToMsgId[ name ] = msgId
}
end
def parseDictionary
parseFields
parseEnums
parseMsgType
parseMsgContents
parseComponents
end
def printVersion
return @specDoc.add_element( "fix", { "type" => "FIX", "major" => @major.to_s, "minor" => @minor.to_s, "servicepack" => @sp.to_s } )
end
def printFields
@fieldsElement = @fixElement.add_element( "fields" )
@tagToField.sort.each { |tag, fieldHash|
next if fieldHash == nil
next if tagShouldNotPrint( tag )
type = fieldHash[ "type" ]
fieldName = fieldHash[ "fieldName" ]
fieldElement = @fieldsElement.add_element( "field", { "number" => tag.to_s, "name" => fieldName, "type" => type } )
printEnums( fieldElement, tag )
}
end
def printEnums( fieldElement, tag )
return if !@tagToEnum.has_key?( tag )
enumArray = @tagToEnum[ tag ]
isBool = false
if( enumArray.size == 2 )
isBool = enumArray[0][0] == "Y" || enumArray[0][0] == "N"
isBool = enumArray[1][0] == "Y" || enumArray[1][0] == "N"
end
descriptions = Hash.new
enumArray.each_index { |index|
description = enumArray[index][1]
if( !descriptions.has_key?(description) )
descriptions[description] = 1
else
descriptions[description] = descriptions[description] + 1
end
}
enumArray.each_index { |index|
enum = enumArray[index][0]
description = enumArray[index][1]
if( descriptions[description] > 1 )
description = description + "_" + enum
end
next if description == nil
description = "YES" if isBool && enum == "Y"
description = "NO" if isBool && enum == "N"
fieldElement.add_element( "value", { "enum" => enum, "description" => description } )
}
end
def printComponents
componentsElement = @fixElement.add_element( "components" )
@componentNameToMsgId.each { |name, msgId|
next if name == "StandardHeader"
next if name == "StandardTrailer"
msgContentsArray = @msgIdToMsgContents[msgId]
componentElement = componentsElement.add_element( "component", { "name" => name } )
printMsgContents( msgContentsArray, componentElement )
}
end
def printMessages
messagesElement = @fixElement.add_element( "messages" )
@msgIdToMsgType.sort.each { |msgId, msgTypeHash|
next if msgTypeHash == nil
name = msgTypeHash[ "name" ]
msgtype = msgTypeHash[ "msgtype" ]
msgcat = msgTypeHash[ "msgcat" ]
if( @major >= 5 && msgcat == "admin" )
next
end
msgContentsArray = @msgIdToMsgContents[msgId]
messageElement = messagesElement.add_element( "message", { "name" => name, "msgtype" => msgtype, "msgcat" => msgcat } )
next if msgContentsArray == nil
printMsgContents( msgContentsArray, messageElement )
}
end
def printMsgContents( msgContentsArray, messageElement )
queue = Array.new
queue.push( messageElement )
nextIndent = 0
lastIndent = 0
messageName = messageElement.attributes["name"]
msgContentsArray.each_index { |index|
tag = msgContentsArray[index][0]
name = msgContentsArray[index][1]
indent = msgContentsArray[index][2]
required = msgContentsArray[index][3]
nextIndent = msgContentsArray[index+1][2] if msgContentsArray[index+1] != nil
if(@major == 5 && messageName == "InstrumentLeg" && name == "LegPrice")
next
end
if( indent < lastIndent )
queue.pop
end
if( nextIndent > indent )
groupElement = queue.last.add_element( "group", "name" => name, "required" => required )
queue.push( groupElement )
lastIndent = indent
next
end
if(tag.class == String)
componentElement = queue.last.add_element( "component", "name" => name, "required" => required )
else
fieldElement = queue.last.add_element( "field", "name" => name, "required" => required )
end
lastIndent = indent
}
end
def printHeader
headerElement = @fixElement.add_element( "header" )
return if @major >= 5
msgContentsArray = @msgIdToMsgContents[@componentNameToMsgId["StandardHeader"]]
printMsgContents( msgContentsArray, headerElement )
end
def printTrailer
trailerElement = @fixElement.add_element( "trailer" )
return if @major >= 5
msgContentsArray = @msgIdToMsgContents[@componentNameToMsgId["StandardTrailer"]]
printMsgContents( msgContentsArray, trailerElement )
end
def printDictionary
@fixElement = printVersion
printHeader
printMessages
printTrailer
printComponents
printFields
fmt = OrderedAttributes.new(indentation=1, true)
fmt.write(@specDoc, @f)
#@specDoc.write( @f, 1, false, true )
end
end
(0..4).each { |i| DataDictionary.new( 4, i, 0 ) }
(0..2).each { |i| DataDictionary.new( 5, 0, i ) }
|