-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.js
107 lines (98 loc) · 2.95 KB
/
parse.js
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
var parseString = require('xml2js').parseString,
fs = require('fs'),
util = require('util')
// pattern matches
timesRe = /^PT(\d+)H(\d+)M(\d+)S$/,
floatRe = /^(\d+)\.(\d+)$/,
period = /\./
module.exports = {
parseXml: parseXml,
_dump: _dump
}
function parseXml(fileName, callback) {
var db = {}
fs.readFile(fileName, 'utf8', function(err, data) {
if (err) {
return callback(err)
}
parseString(data, function (err, parsedData) {
if (err) throw err
var project = parsedData.Project
project.Tasks = project.Tasks[0].Task
project.Resources = project.Resources[0].Resource
project.Assignments = project.Assignments[0].Assignment
project.Calendars = {}
cleanUpXmlObject(project)
//console.log(project)
// _dump(db)
project.Assignments.forEach(function(assignment) {
// _dump(assignment)
var task = db.Tasks[assignment.TaskUID]
var resource = db.Resources[assignment.ResourceUID]
if (task && resource) {
// _dump(resource)
task._owner = resource.Name
console.log('task', task.UID, task.Name, '→', resource.UID, task._owner)
}
})
callback(null, db)
})
})
function addModel(name, value) {
if (!value.Name) {
return
}
var collection = db[name]
if (!collection) {
collection = db[name] = {}
}
collection[value.UID] = value
}
/* Don't judge me, I am hacking */
function cleanUpXmlObject(valueWithProps) {
for (var key in valueWithProps) {
if (!valueWithProps.hasOwnProperty(key)) continue
var value = valueWithProps[key]
if (Array.isArray(value)) {
if (value.length == 1) {
value = value[0]
} else {
value.forEach(function (child) {
cleanUpXmlObject(child)
if (child.UID) {
addModel(key, child)
}
})
continue
}
}
if (key === 'PredecessorLink') {
cleanUpXmlObject(value)
value = [value]
} if (key === 'OutlineNumber' || key === 'WBS') {
value = value.split(period)
} else {
var intValue = parseInt(value)
if (!isNaN(intValue) && '' + intValue === value) {
// console.log(valueWithProps.UID, ':', key, '=', value)
value = intValue
} else {
var floatParts = floatRe.exec(value)
if (floatParts) {
value = parseFloat(parseFloat(floatParts[1]) + '.' + parseFloat(floatParts[2]))
// console.log('FLOAT', key, ':', value)
} else {
var times = timesRe.exec(value)
if (times) {
value = parseInt(times[1]) * 3600 + parseInt(times[2]) * 60 + parseInt(times[3])
}
}
}
}
valueWithProps[key] = value
}
}
}
function _dump(value) {
console.log(util.inspect(value, { showHidden: true, depth: null, colors: true }))
}