Skip to content

Instantly share code, notes, and snippets.

@swiatczak
Created August 18, 2014 05:07
Show Gist options
  • Save swiatczak/5dcd44b2179712c82341 to your computer and use it in GitHub Desktop.
Save swiatczak/5dcd44b2179712c82341 to your computer and use it in GitHub Desktop.
CoffeeScript - parse PeopleSoft project XML export file
path = require 'path'
fs = require 'fs'
expat = require 'node-expat'
#
# peoplesoft XML project file parser - similar to:
# python version: https://github.com/swiatczak/psprojectparser
#
# just playing with class construction and chaining of the methods
#
# References: http://coffeescriptcookbook.com/chapters/classes_and_objects/chaining
# http://arcturo.github.io/library/coffeescript/03_classes.html
#
# After reading - http://stackoverflow.com/questions/18691826/new-to-node-js-and-im-not-sure-if-im-doing-this-correct
# - I started thinking about perhaps trying to think in terms of callbacks and logic chunks - rather than
# convenience and making it all look like Python or Java
#
class ProjectParser
startRE = /<instance class="(.*)">/ig
endRE = /<\/instance>/i
# creates a chained accessor methods for each property defined
@chained: (obj, attr) ->
(newValues...) ->
if newValues.length == 0
obj.properties[attr]
else
obj.properties[attr] = newValues[0]
obj
#
constructor: ->
@properties=
prjfile: ''
wrkfldr: ''
outfldr: ''
this[a] = ProjectParser.chained(this, a) for a of @properties
# parse file
parseSection: (section) =>
console.log section.type
addSection: (section) =>
@sections = @sections || []
@sections.push(section)
getSections: (err, data) =>
throw err if err
lines = data.split('\n')
section = ""
inSection = false
sectType = ""
if lines
for line in lines
grp = startRE.exec(line)
if grp
inSection = true
sectType = grp[1]
section = line
else
if inSection
if line.match(endRE)
section = section + "\n" + line
@addSection({section: section, type: sectType })
@parseSection({section: section, type: sectType })
section = ""
inSection = false
else
section = section + "\n" + line
# read project file
readFile: =>
path = path.join(@wrkfldr(), @prjfile())
fs.readFile(path, 'utf8', @getSections)
pp = new ProjectParser().prjfile('UA_H_WA_JI_012_29833.XML').wrkfldr('data/UA_H_WA_JI_012_29833').outfldr('data/UA_H_WA_JI_012_29833/out')
pp.readFile()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment