1 |
|
2 |
|
3 | "use strict";
|
4 |
|
5 | var fs = require("fs");
|
6 | var sax = require("../lib/sax_parser.js");
|
7 | var expat = require("../lib/expat_parser.js");
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | module.exports.loadXmlFileContents = function(opt) {
|
15 | if (!opt.filePath) {
|
16 | throw new Error("You must provide the opt.filePath");
|
17 | }
|
18 |
|
19 | var loadStart = process.hrtime();
|
20 | var pomXmlContent = fs.readFileSync(opt.filePath, "utf8");
|
21 | var loadPomTime = process.hrtime(loadStart);
|
22 | return {
|
23 | xmlContent: pomXmlContent,
|
24 | timers: {
|
25 | load: (loadPomTime[1] / 1000000) + "ms"
|
26 | }
|
27 | };
|
28 | };
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | module.exports.parsePom = function(opt) {
|
36 | if (!opt) {
|
37 | throw new Error("You must provide options: opt.filePath and [opt.format=(*json|text)]");
|
38 | }
|
39 | if (!opt.xmlContent && !opt.filePath) {
|
40 | throw new Error("You must provide the opt.filePath or the opt.xmlContent");
|
41 | }
|
42 | if (!opt.format) {
|
43 | opt.format = "json";
|
44 |
|
45 | } else if (opt.format !== "text" && opt.format !== "json") {
|
46 | throw new Error("You must provide the opt.format = (json | text)");
|
47 | }
|
48 |
|
49 |
|
50 | var pom = { timers: { load: 0, parse: 0 } }
|
51 | if (!opt.xmlContent) {
|
52 | pom = this.loadXmlFileContents(opt);
|
53 | }
|
54 |
|
55 | var loadTimer = pom.timers;
|
56 |
|
57 |
|
58 | var parsePomStart = process.hrtime();
|
59 | pom = sax.parse(pom.xmlContent);
|
60 |
|
61 |
|
62 | pom.timers = {};
|
63 | var parsePomTime = process.hrtime(parsePomStart);
|
64 | pom.timers.parse = (parsePomTime[1] / 1000000) + "ms";
|
65 |
|
66 |
|
67 | pom.timers.load = loadTimer.load;
|
68 |
|
69 |
|
70 | return opt.format === "json" ? pom : (pom.groupId + ":" + pom.artifactId + ":" + pom.version);
|
71 | };
|