added svcapi ui and camunda code
[it/otf.git] / otf-frontend / client / src / app / shared / services / json2html.service.ts
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\r
3 #   Licensed under the Apache License, Version 2.0 (the "License");            #\r
4 #   you may not use this file except in compliance with the License.           #\r
5 #   You may obtain a copy of the License at                                    #\r
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\r
9 #   Unless required by applicable law or agreed to in writing, software        #\r
10 #   distributed under the License is distributed on an "AS IS" BASIS,          #\r
11 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
12 #   See the License for the specific language governing permissions and        #\r
13 #   limitations under the License.                                             #\r
14 ##############################################################################*/\r
15 \r
16 \r
17 import { Injectable } from "@angular/core";\r
18 import { toInteger } from "@ng-bootstrap/ng-bootstrap/util/util";\r
19 \r
20 @Injectable({\r
21     providedIn: 'root'\r
22 })\r
23 \r
24 export class ToHtml {\r
25     constructor() {}\r
26 \r
27     convert(json:any = [{name: 'Adam', age: 23}, {name: 'Raj', age: 22}, {name: 'Justin', age: 5}], tabs = 0){\r
28         var html = '';\r
29         var tabHtml = '';\r
30         if(typeof json === 'string'){\r
31           json = JSON.parse(json);\r
32         }\r
33         for(let i = 0; i < tabs; i++){\r
34           tabHtml += '&nbsp;&nbsp;&nbsp;&nbsp;';\r
35         }\r
36         for(let key in json){\r
37           if(json.hasOwnProperty(key)){\r
38             if(typeof json[key] === "object"){\r
39               html += tabHtml + '<b><u>' + key + ':</u></b><br/>';\r
40               if(json.constructor === Array && toInteger(key) > 0){\r
41                 tabs--;\r
42               }\r
43               html += this.convert(json[key], ++tabs);\r
44             }else{\r
45               html += tabHtml + '<b><u>' + key + ':</u></b>' + '<br/>';\r
46               if(typeof json[key] === 'string'){\r
47                 json[key] = json[key].replace(/\\n/g, '<br/>' + tabHtml);\r
48               }\r
49               html += tabHtml + json[key] + '<br/>';\r
50               html += '<br/>';\r
51             }\r
52           }\r
53         }\r
54         return html;\r
55       }\r
56     \r
57     convertString(str){\r
58         return str.replace(/\\n/g, '<br/>');\r
59     }\r
60 }