@sap/xsodata
Version:
Expose data from a HANA database as OData V2 service with help of .xsodata files.
103 lines (77 loc) • 2.93 kB
JavaScript
'use strict';
//SAPINFO atom response format not supported yet
var utils = require('./../utils/utils');
var XMLWriter = require('xml-writer');
var XMLSerializer = function XMLSerializer(context, bufferSize) {
var self = this;
this.context = context;
self.response = context.response;
self.bufferSize = bufferSize;
self.buffer = '';
var writer = function writer(string) {
self.buffer += string;
if (self.buffer.length > bufferSize) {
self.response.write(self.buffer);
self.buffer = '';
}
};
this.baseUrl = context.uriTree.baseUrl;
this.xw = new XMLWriter(false, writer); //change to false for unformatted output
};
XMLSerializer.prototype.flush = function () {
this.context.response.write(this.buffer);
this.buffer = '';
};
XMLSerializer.prototype.writeXMLHeader = function () {
this.xw.startDocument('1.0', 'utf-8', true);
};
XMLSerializer.prototype.writeFeedHeader = function () {
this.xw.startElement('feed');
this.xw.writeAttribute('xml:base', utils.ensureSlashEnding(this.baseUrl));
this.xw.writeAttribute('xmlns:d', 'http://schemas.microsoft.com/ado/2007/08/dataservices');
this.xw.writeAttribute('xmlns:m', 'http://schemas.microsoft.com/ado/2007/08/dataservices/metadata');
this.xw.writeAttribute('xmlns', 'http://www.w3.org/2005/Atom');
};
XMLSerializer.prototype.writeTitleIdAuthor = function (title, id) {
this.xw.startElement('title');
this.xw.writeAttribute('type', 'text');
this.xw.text(title);
this.xw.endElement();
this.xw.startElement('id');
this.xw.text(this.baseUrl + id);
this.xw.endElement();
this.xw.startElement('author');
this.xw.startElement('name');
this.xw.endElement();
this.xw.endElement();
};
XMLSerializer.prototype.writeFeedFooter = function () {
this.xw.endElement();
};
XMLSerializer.prototype.writeEntryHeader = function () {
this.xw.startElement('entry');
this.xw.writeAttribute('xml:base', this.baseUrl);
this.xw.writeAttribute('xmlns:atom', 'http://www.w3.org/2005/Atom');
this.xw.writeAttribute('xmlns:app', 'http://www.w3.org/2007/app');
this.xw.writeAttribute('xmlns', 'http://www.w3.org/2007/app');
};
XMLSerializer.prototype.writeEntryFooter = function () {
this.xw.endElement();
};
XMLSerializer.prototype.writeLink = function (ref, type, title, href) {
this.xw.startElement('link');
this.xw.writeAttribute('rel', ref);
if (type) {
this.xw.writeAttribute('type', type);
}
this.xw.writeAttribute('title', title);
this.xw.writeAttribute('href', href);
this.xw.endElement();
};
XMLSerializer.prototype.writeCategory = function (term, schema) {
this.xw.startElement('category ');
this.xw.writeAttribute('term', term);
this.xw.writeAttribute('schema', schema);
this.xw.endElement();
};
exports.XMLSerializer = XMLSerializer;