Line data Source code
1 : /*
2 : * Package : xml2json
3 : * Author : S. Hamblett <steve.hamblett@linux.com>
4 : * Date : 12/09/2013
5 : * Copyright : S.Hamblett@OSCF
6 : *
7 : * General utility class
8 : *
9 : */
10 :
11 : part of xml2json;
12 :
13 : class _Xml2JsonUtils {
14 : /// Escape any control characters and quotes for JSONencoding
15 : static String escapeTextForJson(String text) {
16 1 : String text1 = text.replaceAll("\n", "\\\\n");
17 1 : text1 = text1.replaceAll("\'", "\\\\'");
18 1 : text1 = text1.replaceAll('"', '\\"');
19 1 : text1 = text1.replaceAll("\&", "\\\\&");
20 1 : text1 = text1.replaceAll("\r", "\\\\r");
21 1 : text1 = text1.replaceAll("\t", "\\\\t");
22 1 : text1 = text1.replaceAll("\b", "\\\\f");
23 :
24 : return text1;
25 : }
26 :
27 : /// Turn the processing node into a map of values.
28 : static Map mapProcessingNode(String text) {
29 1 : final Map nodeMap = new Map<String, String>();
30 1 : String text1 = text.trim();
31 1 : text1 = text1.replaceAll('"', '');
32 1 : final List properties = text1.split(' ');
33 1 : properties.forEach((var element) {
34 1 : final List elementList = element.split('=');
35 5 : if (elementList.length == 2) nodeMap[elementList[0]] = elementList[1];
36 : });
37 :
38 : return nodeMap;
39 : }
40 :
41 : /// Prepare the input XML string, close up tags, strip newlines between tags etc.
42 : static String prepareXmlString(String xmlString) {
43 1 : String xmlString1 = xmlString.trim();
44 1 : xmlString1 = xmlString1.replaceAll('>\n', '>');
45 1 : final RegExp regex = new RegExp(r'>\s*<');
46 1 : xmlString1 = xmlString1.replaceAll(regex, "><");
47 :
48 : return xmlString1;
49 : }
50 : }
|