import {SchemaConstants} from "./SchemaConstants"; import {Unit, UnitType} from "../drawing/Unit"; export abstract class JSONNode { abstract ele(name:string, node:JSONNode):JSONNode; abstract att(name: string, value: any); abstract toJSON(); } export class JSONObject extends JSONNode { private _json: { [key: string]: JSONNode }; private _attrs: { [key: string]: any }; constructor() { super(); this._json = {}; this._attrs = {}; } ele(name: string, node:JSONNode): JSONNode { this._json[name] = node; return node; } att(name: string, value: any) { this._attrs[name] = value; } toJSON() { const result = {}; Object.keys(this._json).forEach(key => { result[key] = this._json[key].toJSON(); }); Object.keys(this._attrs).forEach(key => { result[key] = this._attrs[key]; }); return result; } } export class JSONArray extends JSONNode { private _json: Array; constructor() { super(); this._json = []; } ele(name: string, node:JSONNode): JSONNode { this._json.push([name, node]); return node; } att(name: string, value: any) { throw new Error("JSON Array not support att method"); } toJSON() { return this._json.map(item => { return { [item[0]]: item[1].toJSON() }; }) } } export class JSONWriter { private _document:JSONObject; private _elementStack: Array; private _lastElement: JSONNode; constructor() { this._elementStack = []; this._lastElement = null; this._document = new JSONObject(); } private writeAttribute(name:string, value:any) { const parentNode = this._lastElement ? this._lastElement : this._document; parentNode.att(name, value); } startElement(name:string, array = false): JSONWriter { const parentNode = this._lastElement ? this._lastElement : this._document; this._lastElement = parentNode.ele(name, array? new JSONArray() : new JSONObject()); this._elementStack.push(this._lastElement); return this; } closeElement():JSONWriter { // remove last element this._elementStack.pop(); // reset last element this._lastElement = this._elementStack.length > 0 ? this._elementStack[this._elementStack.length - 1] : null; return this; } end():JSONWriter { this._elementStack.length = 0; return this; } addAttributeEnum(name:string, enumType:any, value:any, defaultValue?:any):JSONWriter { if (value !== null && value !== defaultValue) { this.writeAttribute(name, enumType[value]); } return this; } addAttribute(name:string, value?:any, defaultValue?:any):JSONWriter { if (value !== null && value !== defaultValue) { this.writeAttribute(name, value); } return this; } addAttributeUnit(name:string, value?:Unit, defaultValue?:Unit):JSONWriter { if (value !== null && value !== defaultValue) { let type = ''; switch (value.type) { case UnitType.Cm: type = 'cm'; break; case UnitType.Mm: type = 'mm'; break; case UnitType.Pixel: type = 'px'; break; case UnitType.Inch: type = 'in'; break; } this.writeAttribute(name, `${value.value}${type}`); } return this; } addAttributePositive(name:string, value:number):JSONWriter { if (value > 0) { this.writeAttribute(name, value.toFixed(0)); } return this; } toJSON():any { return { [SchemaConstants.ELEMENT_cspReport]: this._document.toJSON() } } }