Comparing version
127
lib/index.js
@@ -5,3 +5,4 @@ /** @module Node Pom Parser */ | ||
var fs = require("fs"); | ||
var Promise = require("bluebird"); | ||
var fs = Promise.promisifyAll(require("fs")); | ||
var xml2js = require("xml2js"); | ||
@@ -11,31 +12,12 @@ var util = require('util') | ||
/** | ||
* Load xml file contents using the sync mode. | ||
* @param {object} opt Is the options with filePath in it. | ||
* @return {object} The xml content with timer with load time. | ||
*/ | ||
function _loadXmlFileContents(opt) { | ||
if (!opt.filePath) { | ||
throw new Error("You must provide the opt.filePath"); | ||
} | ||
return fs.readFileSync(opt.filePath, "utf8"); | ||
// xmljs options https://github.com/Leonidas-from-XIV/node-xml2js#options | ||
var XML2JS_OPTS = { | ||
trim: true, | ||
normalizeTags: true, | ||
normalize: true, | ||
mergeAttrs: true | ||
}; | ||
/** | ||
* Removes all the arrays with single elements with a string value. | ||
* @param {object} o is the object to be traversed. | ||
*/ | ||
function removeSingleArrays(obj) { | ||
// Traverse all the elements of the object | ||
traverse(obj).forEach(function traversing(value) { | ||
// As the XML parser returns single fields as arrays. | ||
if (value instanceof Array && value.length === 1) { | ||
this.update(value[0]); | ||
} | ||
}); | ||
} | ||
/** | ||
* Extremely FAST pom header reader using node-expat c library. | ||
* Parses xml into javascript object by using a file path or an xml content. | ||
* @param {object} opt Is the option with the filePath or xmlContent and the optional format. | ||
@@ -53,38 +35,71 @@ * @return {object} The pom object along with the timers. | ||
// Use or load the content from the filePath | ||
var loadedXml = false; | ||
// If the xml content is was not provided by the api client. | ||
// https://github.com/petkaantonov/bluebird/blob/master/API.md#error-rejectedhandler----promise | ||
if (!opt.xmlContent) { | ||
opt.xmlContent = _loadXmlFileContents(opt); | ||
loadedXml = true; | ||
fs.readFileAsync(opt.filePath, "utf8").then(function(xmlContent) { | ||
return xmlContent; | ||
}).then(_parseWithXml2js).then(function(result) { | ||
callback(null, result); | ||
}).catch(function(e) { | ||
callback(e, null); | ||
}).error(function (e) { | ||
callback(e, null); | ||
}); | ||
} else { | ||
// parse the xml provided by the api client. | ||
_parseWithXml2js(opt.xmlContent).then(function(result) { | ||
delete result.xmlContent; | ||
callback(null, result); | ||
}).error(function (e) { | ||
callback(e); | ||
}); | ||
} | ||
// xmljs options https://github.com/Leonidas-from-XIV/node-xml2js#options | ||
//opt.attrkey = "@"; | ||
opt.trim = true; | ||
opt.normalizeTags = true; | ||
opt.normalize = true; | ||
opt.mergeAttrs = true; | ||
}; | ||
// parse the pom, erasing all | ||
xml2js.parseString(opt.xmlContent, opt, function (err, pomObject) { | ||
// Depending on the requested format, format it or not. | ||
if (err) { | ||
callback(err, null); | ||
} | ||
/** | ||
* Parses the given xml content. | ||
* @param xmlContent {string} Is the xml content in string using utf-8 format. | ||
* @param loadedXml {boolean} Whether the xml was loaded from the file-system. | ||
* @param callback {function} The callback function using Javascript PCS. | ||
*/ | ||
function _parseWithXml2js(xmlContent) { | ||
return new Promise(function(resolve, reject) { | ||
// parse the pom, erasing all | ||
xml2js.parseString(xmlContent, XML2JS_OPTS, function(err, pomObject) { | ||
if (err) { | ||
// Reject with the error | ||
reject(err); | ||
} | ||
// Replace the arrays with single elements with strings | ||
removeSingleArrays(pomObject); | ||
// Replace the arrays with single elements with strings | ||
removeSingleArrays(pomObject); | ||
var response = {}; | ||
// Only add the pomXml when loaded from the file-system. | ||
if (loadedXml) { | ||
response.pomXml = opt.xmlContent; | ||
// Response to the call | ||
resolve({ | ||
pomXml: xmlContent, // Only add the pomXml when loaded from the file-system. | ||
pomObject: pomObject // Always add the object | ||
}); | ||
}); | ||
}); | ||
} | ||
/** | ||
* Removes all the arrays with single elements with a string value. | ||
* @param {object} o is the object to be traversed. | ||
*/ | ||
function removeSingleArrays(obj) { | ||
// Traverse all the elements of the object | ||
traverse(obj).forEach(function traversing(value) { | ||
// As the XML parser returns single fields as arrays. | ||
if (value instanceof Array && value.length === 1) { | ||
this.update(value[0]); | ||
} | ||
// Always add the object | ||
response.pomObject = pomObject; | ||
// Callback with the value | ||
callback(null, response); | ||
}); | ||
}; | ||
} |
{ | ||
"name": "pom-parser", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "A parser for the Java/Maven pom.xml files.", | ||
@@ -10,2 +10,3 @@ "main": "lib/", | ||
"dependencies": { | ||
"bluebird": "^2.9.34", | ||
"traverse": "^0.6.6", | ||
@@ -12,0 +13,0 @@ "xml2js": "^0.4.9" |
191
README.md
Node.js pom.xml Parser | ||
======= | ||
[](https://travis-ci.org/marcellodesales/node-pom-parser) [] | ||
[](https://travis-ci.org/marcellodesales/node-pom-parser)  [](http://badge.fury.io/js/pom-parser) [](https://gemnasium.com/marcellodesales/node-pom-parser) | ||
Parsing Java's pom.xml and properly returning the json object, including attributes and values. | ||
[](https://nodei.co/npm/pom-parser/) | ||
Installation | ||
@@ -18,3 +20,6 @@ ====== | ||
* Reads any pom.xml. | ||
* Cleans up single-element arrays into string objects. | ||
* All xml elements are placed into properties. | ||
* All xml element attributes are merged into the parent element. | ||
* Both the xml string and the parsed object are returned. | ||
* You can provide parsing options. | ||
@@ -27,39 +32,159 @@ Use | ||
```js | ||
var pom = ext.parsePom({ filePath: __dirname + "/pom.xml"}); | ||
var pomParser = require("pom-parser"); | ||
// The required options, including the filePath. | ||
// Other parsing options from https://github.com/Leonidas-from-XIV/node-xml2js#options | ||
var opts = { | ||
filePath: __dirname + "/pom.xml", // The path to a pom file | ||
}; | ||
// Parse the pom based on a path | ||
pomParser.parse(opts, function(err, pomResponse) { | ||
if (err) { | ||
console.log("ERROR: " + err); | ||
process.exit(1); | ||
} | ||
console.log(pom); | ||
// The original pom xml that was loaded is provided. | ||
console.log("XML: " + pomResponse.pomXml); | ||
// The parsed pom pbject. | ||
console.log("OBJECT: " + JSON.stringify(pomResponse.pomObject)); | ||
}); | ||
``` | ||
It should print the follow object with the following properties: | ||
* '$' represents the groups of attributes within the tag (outer object). | ||
* '_' represents the text value of an element with attributes and text values. | ||
```js | ||
{ project: | ||
{ '$': | ||
{ xmlns: 'http://maven.apache.org/POM/4.0.0', | ||
'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance', | ||
'xsi:schemaLocation': 'http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd' }, | ||
parent: | ||
{ artifactId: 'tynamo-parent', | ||
groupId: 'org.tynamo', | ||
version: '0.0.9' }, | ||
modelVersion: { _: '4.0.0', '$': [Object] }, | ||
groupId: 'org.tynamo.examples', | ||
artifactId: 'tynamo-example-federatedaccounts', | ||
version: '0.0.1-SNAPSHOT', | ||
packaging: 'war', | ||
name: 'Tynamo Example - Federated Accounts', | ||
properties: | ||
{ 'tapestry-release-version': '5.3.1', | ||
'gae.version': '1.3.0', | ||
'gae.home': '${settings.localRepository}/com/google/appengine/appengine-api-1.0-sdk/${gae.version}/appengine-java-sdk-${gae.version}', | ||
'gae.application.version': '0' }, | ||
build: | ||
{ finalName: 'federatedaccounts', | ||
resources: [Object], | ||
plugins: [Object] }, | ||
reporting: { plugins: [Object] }, | ||
dependencies: { dependency: [Object] }, | ||
profiles: { profile: [Object] } } } | ||
{ | ||
"project": { | ||
"xmlns": "http://maven.apache.org/POM/4.0.0", | ||
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance", | ||
"xsi:schemaLocation": "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd", | ||
"parent": { | ||
"artifactid": "tynamo-parent", | ||
"groupid": "org.tynamo", | ||
"version": "0.0.9" | ||
}, | ||
"modelversion": { | ||
"_": "4.0.0", | ||
"parallel": "now" | ||
}, | ||
"groupid": "org.tynamo.examples", | ||
"artifactid": "tynamo-example-federatedaccounts", | ||
"version": "0.0.1-SNAPSHOT", | ||
"packaging": "war", | ||
"name": "Tynamo Example - Federated Accounts", | ||
"properties": { | ||
"tapestry-release-version": "5.3.1", | ||
"gae.version": "1.3.0", | ||
"gae.home": "${settings.localRepository}/com/google/appengine/appengine-api-1.0-sdk/${gae.version}/appengine-java-sdk-${gae.version}", | ||
"gae.application.version": "0" | ||
}, | ||
"build": { | ||
"finalname": "federatedaccounts", | ||
"resources": { | ||
"resource": [ | ||
{ | ||
"directory": "src/main/resources" | ||
}, | ||
{ | ||
"directory": "src/main/filtered-resources", | ||
"filtering": "true" | ||
} | ||
] | ||
}, | ||
"plugins": { | ||
"plugin": [ | ||
{ | ||
"groupid": "org.apache.maven.plugins", | ||
"artifactid": "maven-compiler-plugin", | ||
"configuration": { | ||
"source": "1.6", | ||
"target": "1.6", | ||
"optimize": "true" | ||
} | ||
}, | ||
{ | ||
"groupid": "net.kindleit", | ||
"artifactid": "maven-gae-plugin", | ||
"version": "0.8.0", | ||
"configuration": { | ||
"serverid": "tynamo-example-federatedaccounts" | ||
} | ||
}, | ||
{ | ||
"groupid": "org.apache.maven.plugins", | ||
"artifactid": "maven-war-plugin", | ||
"configuration": { | ||
"webresources": { | ||
"resource": { | ||
"directory": "src/main/webapp", | ||
"filtering": "true", | ||
"includes": { | ||
"include": "**/appengine-web.xml" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
"reporting": { | ||
"plugins": { | ||
"plugin": { | ||
"groupid": "org.apache.tapestry", | ||
"artifactid": "tapestry-component-report", | ||
"version": "${tapestry-release-version}", | ||
"configuration": { | ||
"rootpackage": "org.tynamo" | ||
} | ||
} | ||
} | ||
}, | ||
"dependencies": { | ||
"dependency": [ | ||
{ | ||
"groupid": "com.google.appengine", | ||
"artifactid": "appengine-api-1.0-sdk", | ||
"version": "${gae.version}" | ||
}, | ||
{ | ||
"groupid": "com.h2database", | ||
"artifactid": "h2" | ||
}, | ||
{ | ||
"groupid": "org.apache.tapestry", | ||
"artifactid": "tapestry-core", | ||
"version": "${tapestry-release-version}" | ||
}, | ||
{ | ||
"groupid": "javax.servlet", | ||
"artifactid": "servlet-api", | ||
"version": "2.5", | ||
"type": "jar", | ||
"scope": "provided" | ||
} | ||
] | ||
}, | ||
"profiles": { | ||
"profile": { | ||
"id": "repositories", | ||
"repositories": { | ||
"repository": { | ||
"id": "maven-gae-plugin-repo", | ||
"name": "maven-gae-plugin repository", | ||
"url": "http://maven-gae-plugin.googlecode.com/svn/repository" | ||
} | ||
}, | ||
"pluginrepositories": { | ||
"pluginrepository": { | ||
"id": "maven-gae-plugin-repo", | ||
"name": "maven-gae-plugin repository", | ||
"url": "http://maven-gae-plugin.googlecode.com/svn/repository" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
@@ -66,0 +191,0 @@ |
@@ -11,4 +11,7 @@ var pomParser = require("../lib"); | ||
var pom = null; | ||
var xml = null; | ||
before(function() { | ||
// Setup the tests using mocha's promise. | ||
// https://lostechies.com/derickbailey/2012/08/17/asynchronous-unit-tests-with-mocha-promises-and-winjs/ | ||
before(function(done) { | ||
pomParser.parse({filePath: POM_PATH}, function(err, response) { | ||
@@ -20,19 +23,32 @@ expect(err).to.be.null; | ||
pom = pomResponse.pomObject; | ||
xml = pomResponse.pomXml; | ||
done(); | ||
}); | ||
}); | ||
// Tear down the tests by printing the loaded xml and the parsed object. | ||
after(function(done) { | ||
console.log("\n\nThe XML loaded"); | ||
console.log(xml); | ||
console.log("\n\nThe parsed XML"); | ||
console.log(JSON.stringify(pom, null, 2)); | ||
done(); | ||
}); | ||
}); | ||
it('can load any pom.xml properly', function () { | ||
it('can load any pom.xml properly', function(done) { | ||
expect(pomResponse.pomXml).to.be.an("string"); | ||
expect(pomResponse.pomObject).to.be.an("object"); | ||
done(); | ||
}); | ||
it('parses xml attributes as properties', function () { | ||
it('parses xml attributes as properties', function(done) { | ||
expect(pom.project.xmlns).to.equal("http://maven.apache.org/POM/4.0.0"); | ||
expect(pom.project["xmlns:xsi"]).to.equal("http://www.w3.org/2001/XMLSchema-instance"); | ||
done(); | ||
}); | ||
it('parses xml elements as properties', function () { | ||
it('parses xml elements as properties', function(done) { | ||
expect(pom.project.parent).to.be.an("object"); | ||
expect(pom.project.parent.artifactid).to.equal("tynamo-parent"); | ||
done(); | ||
}); | ||
@@ -39,0 +55,0 @@ |
Sorry, the diff of this file is not supported yet
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
26370
37.7%10
11.11%185
79.61%199
168.92%3
50%+ Added
+ Added