Skip to content

Commit

Permalink
Added XML helper
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed May 14, 2017
1 parent 6ef6350 commit 12d90e7
Show file tree
Hide file tree
Showing 3 changed files with 315 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/helper/XML.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export interface XMLElement {
declaration?: any;
attributes?: any;
elements: XMLElement[];
name?: string;
find(name: string): XMLElement;
findMany(name: string): XMLElement[];
}
export declare abstract class XML {
static parse(xml: string): XMLElement;
static toXML(xml: XMLElement | any, includeDeclaration?: boolean): string;
static createElement(name: string, attributes?: any, text?: string): {
type: string;
name: string;
attributes: any;
elements: any[];
ele: (name: string, attributes?: any) => any;
add: (element: any) => any;
eleFn: (name: string, attributes?: any, text?: string) => any;
};
}
126 changes: 126 additions & 0 deletions lib/helper/XML.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var xmljs = require("xml-js");
function seekForNS(node, parentNS) {
if (!node.attributes)
return parentNS;
var ns = {};
for (var name_1 in parentNS)
ns[name_1] = parentNS[name_1];
for (var name_2 in node.attributes) {
if (name_2.indexOf('xmlns:') === 0 || name_2 === 'xmlns') {
var value = node.attributes[name_2];
if (name_2 === 'xmlns')
ns._default = value;
else
ns[name_2.substring('xmlns:'.length)] = value;
}
}
return ns;
}
function muteNodeNS(node, parentNS) {
if (parentNS === void 0) { parentNS = { _default: 'DAV' }; }
var nss = seekForNS(node, parentNS);
if (node.name) {
for (var ns in nss) {
if (ns === '_default')
continue;
if (node.name.indexOf(ns + ':') === 0)
node.name = nss[ns] + node.name.substring((ns + ':').length);
}
}
node.find = function (name) {
for (var index in node.elements)
if (node.elements[index].name && node.elements[index].name === name)
return node.elements[index];
throw new Error('Can\'t find the element.');
};
node.findMany = function (name) {
var elements = [];
for (var index in node.elements)
if (node.elements[index].name && node.elements[index].name === name)
elements.push(node.elements[index]);
return elements;
};
if (node.elements)
node.elements.forEach(function (n) { return muteNodeNS(n, nss); });
else
node.elements = [];
}
var XML = (function () {
function XML() {
}
XML.parse = function (xml) {
var x = xmljs.xml2js(xml, {
compact: false
});
muteNodeNS(x);
return x;
};
XML.toXML = function (xml, includeDeclaration) {
if (includeDeclaration === void 0) { includeDeclaration = true; }
var finalXml = xml;
if (includeDeclaration && !xml.declaration)
finalXml = {
declaration: {
attributes: {
version: '1.0',
encoding: 'utf-8'
}
},
elements: [
xml
]
};
return xmljs.js2xml(finalXml, {
compact: false
});
};
XML.createElement = function (name, attributes, text) {
if (!attributes)
attributes = {};
var lindex = name.lastIndexOf('/');
if (lindex !== -1) {
++lindex;
attributes['xmlns:x'] = name.substring(0, lindex);
name = 'x:' + name.substring(lindex);
}
var result = {
type: 'element',
name: name,
attributes: attributes,
elements: [],
ele: function (name, attributes) {
var el = result.eleFn(name, attributes);
result.elements.push(el);
return el;
},
add: function (element) {
if (element.constructor === String || element.constructor === Number)
element = {
type: 'text',
text: element.toString()
};
if (element.type === 'element') {
if (!element.attributes)
element.attributes = {};
var lindex_1 = element.name.lastIndexOf('/');
if (lindex_1 !== -1) {
++lindex_1;
element.attributes['xmlns:x'] = element.name.substring(0, lindex_1);
element.name = 'x:' + element.name.substring(lindex_1);
}
}
if (element.constructor === Array)
element.forEach(result.add);
else
result.elements.push(element);
return element;
},
eleFn: XML.createElement
};
return result;
};
return XML;
}());
exports.XML = XML;
168 changes: 168 additions & 0 deletions src/helper/XML.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import * as xmljs from 'xml-js'

function seekForNS(node : any, parentNS : any) : any
{
if(!node.attributes)
return parentNS;

const ns : any = {};
for(const name in parentNS)
ns[name] = parentNS[name];

for(const name in node.attributes)
{
if(name.indexOf('xmlns:') === 0 || name === 'xmlns')
{
const value = node.attributes[name];
if(name === 'xmlns')
ns._default = value;
else
ns[name.substring('xmlns:'.length)] = value;
}
}
return ns;
}

function muteNodeNS(node : any, parentNS = { _default: 'DAV' })
{
var nss = seekForNS(node, parentNS);

if(node.name)
{
for(var ns in nss)
{
if(ns === '_default')
continue;
if(node.name.indexOf(ns + ':') === 0)
node.name = nss[ns] + node.name.substring((ns + ':').length);
}
}

node.find = function(name : string) : XMLElement
{
for(const index in node.elements)
if(node.elements[index].name && node.elements[index].name === name)
return node.elements[index];
throw new Error('Can\'t find the element.');
}
node.findMany = function(name : string) : XMLElement[]
{
const elements : XMLElement[] = [];

for(const index in node.elements)
if(node.elements[index].name && node.elements[index].name === name)
elements.push(node.elements[index]);

return elements;
}

if(node.elements)
node.elements.forEach(n => muteNodeNS(n, nss))
else
node.elements = [];
}

export interface XMLElement
{
declaration ?: any
attributes ?: any
elements : XMLElement[]
name ?: string

find(name : string) : XMLElement
findMany(name : string) : XMLElement[]
}

export abstract class XML
{
static parse(xml : string) : XMLElement
{
const x = xmljs.xml2js(xml, {
compact: false
});

muteNodeNS(x);
return x as XMLElement;
}

static toXML(xml : XMLElement | any, includeDeclaration : boolean = true) : string
{
let finalXml : any = xml;

if(includeDeclaration && !xml.declaration)
finalXml = {
declaration: {
attributes: {
version: '1.0',
encoding: 'utf-8'
}
},
elements: [
xml
]
};

return xmljs.js2xml(finalXml, {
compact: false
});
}

static createElement(name : string, attributes ?: any, text ?: string)
{
if(!attributes)
attributes = {};

let lindex = name.lastIndexOf('/');
if(lindex !== -1)
{
++lindex;
attributes['xmlns:x'] = name.substring(0, lindex);
name = 'x:' + name.substring(lindex);
}

const result = {
type: 'element',
name: name,
attributes: attributes,
elements: [],
ele: function(name : string, attributes ?: any)
{
const el = result.eleFn(name, attributes);
result.elements.push(el);
return el;
},
add: function(element : any)
{
if(element.constructor === String || element.constructor === Number)
element = {
type: 'text',
text: element.toString()
};

if(element.type === 'element')
{
if(!element.attributes)
element.attributes = { };

let lindex = element.name.lastIndexOf('/');
if(lindex !== -1)
{
++lindex;
element.attributes['xmlns:x'] = element.name.substring(0, lindex);
element.name = 'x:' + element.name.substring(lindex);
}
}

if(element.constructor === Array)
(element as any).forEach(result.add);
else
result.elements.push(element);
return element;
},
eleFn: XML.createElement
}

return result;
}
}

0 comments on commit 12d90e7

Please sign in to comment.