• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import ConvertXML from '@ohos.convertxml';
17import xml from '@ohos.xml';
18import { logger } from '@ohos/common/src/main/ets/util/Logger';
19
20const TAG: string = 'ConvertXmlUtil';
21
22export function serializerNode(): string {
23  let arrayBuffer = new ArrayBuffer(1024);
24  let serializer = new xml.XmlSerializer(arrayBuffer);
25  serializer.setDeclaration();
26  serializer.setNamespace('h', 'https://gitee.com/openharmony');
27  serializer.startElement('note');
28  serializer.setAttributes('importance', 'high');
29  serializer.addEmptyElement('b');
30  serializer.setComment('contact information');
31  serializer.setText('ZhangSan 18712345678');
32  serializer.setCDATA('CData');
33  serializer.setDocType('DocType');
34  serializer.endElement();
35  let array = new Uint8Array(arrayBuffer);
36  let serializerStr = '';
37  for (let i = 0; i < array.length; ++i) {
38    serializerStr = serializerStr + String.fromCodePoint(array[i]);
39  }
40  return serializerStr;
41}
42
43export function parserNode(input: string): string {
44  let arrayBuffer = new ArrayBuffer(input.length * 2);
45  let bufView = new Uint8Array(arrayBuffer);
46  let strLen = input.length;
47  for (let k = 0; k < strLen; ++k) {
48    bufView[k] = input.charCodeAt(k);
49  }
50  let parser = new xml.XmlPullParser(arrayBuffer);
51  let arr: Record<number, string> = {};
52  let i = 0;
53
54  let func = (key: xml.EventType, info: xml.ParseInfo) => {
55    arr[i] = `key:${key}, value:${info.getDepth()} ${info.getColumnNumber()} ` +
56    `${info.getLineNumber()} ${info.getAttributeCount()} ${info.getName()} ` +
57    `${info.getText()} ${info.isEmptyElementTag()} ${info.isWhitespace()}\n`;
58    i++;
59    return true;
60  }
61
62  let options: xml.ParseOptions = { supportDoctype: true, ignoreNameSpace: true, tokenValueCallbackFunction: func };
63  parser.parse(options);
64  let str = '';
65  for (let j = 0; j < i; ++j) {
66    str = str + arr[j];
67  }
68  return str;
69}
70
71export function convertNode(input: string): string {
72  logger.info(TAG, 'convertNode start');
73  let options: ConvertXML.ConvertOptions = {
74    trim: false,
75    declarationKey: "_declaration",
76    instructionKey: "_instruction",
77    attributesKey: "_attributes",
78    textKey: "_text",
79    cdataKey: "_cdata",
80    doctypeKey: "_doctype",
81    commentKey: "_comment",
82    parentKey: "_parent",
83    typeKey: "_type",
84    nameKey: "_name",
85    elementsKey: "_elements"
86  };
87  let conv = new ConvertXML.ConvertXML();
88  logger.info(TAG, 'convertNode new ConvertXML');
89  let result = JSON.stringify(conv.convertToJSObject(input, options));
90  logger.info(TAG, `convertNode conv.convert result = ${result}`);
91  return JSON.stringify(result);
92}
93