This repository was archived by the owner on Jun 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollection.js
79 lines (59 loc) · 1.83 KB
/
Collection.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
var fs = require('fs')
var os = require('os')
var path = require('path')
var et = require('elementtree')
var pd = require('pretty-data').pd
var findCollectionFile = require('./find-collection-file')
module.exports = Collection
function Collection () {
if (!(this instanceof Collection)) return new Collection()
this._tree = null
Object.defineProperty(this, 'entries', {
get: this._getEntries
})
}
Collection.prototype.toXML = function () {
return pd.xml(this._tree.write())
}
Collection.prototype.load = function (pathToCollectionFile, callback) {
if (!pathToCollectionFile) {
return findCollectionFile((err, filepath) => {
if (err) return this.onError(err)
this._load(filepath, callback)
})
}
this._load(pathToCollectionFile, callback)
}
Collection.prototype._load = function (filepath, callback) {
fs.readFile(filepath, 'utf-8', (err, xml) => {
if (err) return callback(err)
this._xml = xml
this._tree = et.parse(this._xml)
callback()
})
}
Collection.prototype._getEntries = function () {
var collectionEl = this._tree.findall('COLLECTION')[0]
var entryElements = collectionEl.findall('ENTRY')
var entries = entryElements.map(Entry)
return entries
}
function Entry (node) {
if (!(this instanceof Entry)) return new Entry(node)
this._node = node
this._location = null
Object.defineProperty(this, 'location', {
get: () => {
return this._location || this._getLocation()
}
})
}
Entry.prototype._getLocation = function () {
var location = this._node.getchildren().find(node => node.tag === 'LOCATION')
var volume = location.get('VOLUME')
var dir = location.get('DIR').replace(/\/:/g, '/')
var filename = location.get('FILE')
if (os.platform() !== 'win32') { volume = '' }
this._location = path.join(volume, dir, filename)
return this._location
}