diff --git a/common/EtsXmlTextConvert/README_zh.md b/common/EtsXmlTextConvert/README_zh.md new file mode 100644 index 0000000000000000000000000000000000000000..eb0f0a04daa0f271397ed2c44e672c5ee23665be --- /dev/null +++ b/common/EtsXmlTextConvert/README_zh.md @@ -0,0 +1,17 @@ +# xml文本转换 + +### 简介 + +本示例通过XmlSerializer序列化构建xml文本,通过XmlPullParser解析xml文本,通过ConvertXML转化xml文本为JavaScript对象。 + +### 使用说明 + +1.点击**Xml 序列化**按钮,会构建xml文本,并在按钮上方的文本框显示xml文本内容。 + +2.点击**Xml Pull解析**按钮,会解析已经预置的xml文本,并在文本框显示解析信息。 + +3.点击**Xml 文本转换**按钮,会将预置的xml文本转换为JavaScript对象,并在文本框显示。 + +### 约束与限制 + +本示例仅支持在标准系统上运行。 \ No newline at end of file diff --git a/common/EtsXmlTextConvert/build.gradle b/common/EtsXmlTextConvert/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..b8a6b940895fde983fdc92594a093a0955bb8c4e --- /dev/null +++ b/common/EtsXmlTextConvert/build.gradle @@ -0,0 +1,33 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. +apply plugin: 'com.huawei.ohos.app' + +//For instructions on signature configuration, see https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ide_debug_device-0000001053822404#section1112183053510 +ohos { + compileSdkVersion 8 + supportSystem "standard" +} + +buildscript { + repositories { + maven { + url 'https://repo.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + } + dependencies { + classpath 'com.huawei.ohos:hap:3.0.5.2' + } +} + +allprojects { + repositories { + maven { + url 'https://repo.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + } +} diff --git a/common/EtsXmlTextConvert/entry/build.gradle b/common/EtsXmlTextConvert/entry/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..e7ab61a295250d4e5c32d2096a53451cf045bbf3 --- /dev/null +++ b/common/EtsXmlTextConvert/entry/build.gradle @@ -0,0 +1,16 @@ +apply plugin: 'com.huawei.ohos.hap' +//For instructions on signature configuration, see https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ide_debug_device-0000001053822404#section1112183053510 +ohos { + compileSdkVersion 8 + defaultConfig { + compatibleSdkVersion 7 + } + buildTypes { + release { + proguardOpt { + proguardEnabled false + rulesFiles 'proguard-rules.pro' + } + } + } +} diff --git a/common/EtsXmlTextConvert/entry/src/main/config.json b/common/EtsXmlTextConvert/entry/src/main/config.json new file mode 100644 index 0000000000000000000000000000000000000000..bd5daa5bf5b38d0e83a014cf652ad396bae42f59 --- /dev/null +++ b/common/EtsXmlTextConvert/entry/src/main/config.json @@ -0,0 +1,67 @@ +{ + "app": { + "bundleName": "ohos.samples.etsxmltextconvert", + "vendor": "samples", + "version": { + "code": 1000000, + "name": "1.0.0" + } + }, + "deviceConfig": {}, + "module": { + "package": "ohos.samples.etsxmltextconvert", + "name": ".MyApplication", + "mainAbility": ".MainAbility", + "srcPath": "", + "deviceType": [ + "phone" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "entry", + "moduleType": "entry", + "installationFree": false + }, + "abilities": [ + { + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ], + "orientation": "unspecified", + "visible": true, + "srcPath": "MainAbility", + "name": ".MainAbility", + "srcLanguage": "ets", + "icon": "$media:icon", + "description": "$string:description_mainability", + "formsEnabled": false, + "label": "$string:entry_MainAbility", + "type": "page", + "launchType": "standard" + } + ], + "js": [ + { + "mode": { + "syntax": "ets", + "type": "pageAbility" + }, + "pages": [ + "pages/index" + ], + "name": ".MainAbility", + "window": { + "designWidth": 720, + "autoDesignWidth": false + } + } + ] + } +} \ No newline at end of file diff --git a/common/EtsXmlTextConvert/entry/src/main/ets/MainAbility/app.ets b/common/EtsXmlTextConvert/entry/src/main/ets/MainAbility/app.ets new file mode 100644 index 0000000000000000000000000000000000000000..bf28e58b36cf11db491d3f00768ef2c9e41d05cf --- /dev/null +++ b/common/EtsXmlTextConvert/entry/src/main/ets/MainAbility/app.ets @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export default { + onCreate() { + console.info('Application onCreate') + }, + onDestroy() { + console.info('Application onDestroy') + }, +} \ No newline at end of file diff --git a/common/EtsXmlTextConvert/entry/src/main/ets/MainAbility/common/selectOption.ets b/common/EtsXmlTextConvert/entry/src/main/ets/MainAbility/common/selectOption.ets new file mode 100644 index 0000000000000000000000000000000000000000..b712cf801861deb91ca001e9a8066a49d5cc7760 --- /dev/null +++ b/common/EtsXmlTextConvert/entry/src/main/ets/MainAbility/common/selectOption.ets @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import xml from '@ohos.xml' +import convertXml from '@ohos.convertxml' + +@Component +export struct SelectOption { + private options: Resource[] = [$r('app.string.serializer'), $r('app.string.parser'), $r('app.string.convert')] + @Link outputs: string + @Link inputs: string + + serializerNode() { + var arrayBuffer = new ArrayBuffer(1024) + var serializer = new xml.XmlSerializer(arrayBuffer) + serializer.setDeclaration() + serializer.setNamespace("h", "https://developer.harmonyos.com/") + serializer.startElement("note") + serializer.setAttributes("importance", "high") + serializer.addEmptyElement("b") + serializer.setCommnet("contact information") + serializer.setText('ZhangSan 18712345678') + serializer.setCData('CData') + serializer.setDocType('DocType') + serializer.endElement() + var array = new Uint8Array(arrayBuffer) + var serializerStr = "" + for (var i = 0; i < array.length; ++i) { + serializerStr = serializerStr + String.fromCodePoint(array[i]) + } + this.outputs = serializerStr + } + + parserNode() { + var arrayBuffer = new ArrayBuffer(this.inputs.length * 2) + var bufView = new Uint8Array(arrayBuffer); + var strLen = this.inputs.length; + for (var k = 0; k < strLen; ++k) { + bufView[k] = this.inputs.charCodeAt(k); + } + var parser = new xml.XmlPullParser(arrayBuffer) + var arr = {} + var i = 0 + + function func(key, info) { + arr[i] = 'key:' + JSON.stringify(key) + ',value:' + info.getDepth() + ' ' + info.getColumnNumber() + '' + + info.getLineNumber() + ' ' + info.getAttributeCount() + ' ' + info.getName() + ' ' + info.getNamespace() + ' ' + + info.getPrefix() + ' ' + info.getText() + ' ' + info.isEmptyElementTag() + ' ' + info.isWhitespace() + '\n' + i++ + return true + } + + var options = { supportDoctype: true, ignoreNameSpace: true, tokenValueCallbackFunction: func } + parser.parse(options) + var str = '' + for (var j = 0; j < i; ++j) { + str = str + arr[j] + } + this.outputs = str + } + + convertNode() { + var convert = new convertXml.ConvertXML() + var result = convert.convert(this.inputs, { compact: false, spaces: 0 }) + this.outputs = result + } + + build() { + Column() { + Flex({ + direction: FlexDirection.Row, + alignItems: ItemAlign.Center, + justifyContent: FlexAlign.Center, + wrap: FlexWrap.Wrap + }) { + ForEach(this.options, item => { + Button() { + Text(item) + .fontSize(20) + .textAlign(TextAlign.Center) + .fontColor(Color.White) + .width('340') + } + .type(ButtonType.Capsule) + .backgroundColor('#0D9FFB') + .padding(5) + .margin({ top: 10 }) + .onClick(() => { + var index = this.options.indexOf(item) + switch (index) { + case 0: + this.serializerNode() + break + case 1: + this.parserNode() + break + case 2: + this.convertNode() + break + default: + break + } + }) + }, item => JSON.stringify(item)) + }.width('100%') + }.width('100%') + } +} \ No newline at end of file diff --git a/common/EtsXmlTextConvert/entry/src/main/ets/MainAbility/pages/index.ets b/common/EtsXmlTextConvert/entry/src/main/ets/MainAbility/pages/index.ets new file mode 100644 index 0000000000000000000000000000000000000000..68011a91843db184c93da5da74be7b26a6818736 --- /dev/null +++ b/common/EtsXmlTextConvert/entry/src/main/ets/MainAbility/pages/index.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import {SelectOption} from '../common/selectOption.ets' + +@Entry +@Component +struct Index { + @State output: string = 'eTSXmlTextConvert' + @State input: string = + '' + + '' + + ' Happy' + + ' Work' + + ' Play' + + '' + + build() { + Column() { + Text($r("app.string.entry_MainAbility")) + .width('100%') + .height(50) + .backgroundColor('#0D9FFB') + .fontColor(Color.White) + .fontSize(20) + .padding({ left: 20 }) + Scroll() { + Column() { + Text(this.output) + .width('90%') + .height('45%') + .fontSize(15) + .padding(10) + .margin({ left: 10, right: 10, top: 20, bottom: 10 }) + .border({ width: 2, radius: 5, color: Color.Gray }) + SelectOption({ outputs: $output, inputs: $input }) + } + } + } + .width('100%') + .height('100%') + } +} + diff --git a/common/EtsXmlTextConvert/entry/src/main/resources/base/element/string.json b/common/EtsXmlTextConvert/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..2b9578cfa1d55efc1a8cb7d2d8675a60c7f9d243 --- /dev/null +++ b/common/EtsXmlTextConvert/entry/src/main/resources/base/element/string.json @@ -0,0 +1,24 @@ +{ + "string": [ + { + "name": "entry_MainAbility", + "value": "eTSXmlTextConvert" + }, + { + "name": "description_mainability", + "value": "ETS_Empty Ability" + }, + { + "name": "serializer", + "value": "Xml Serializer" + }, + { + "name": "parser", + "value": "Xml Pull Parser" + }, + { + "name": "convert", + "value": "Xml Convert" + } + ] +} \ No newline at end of file diff --git a/common/EtsXmlTextConvert/entry/src/main/resources/base/media/icon.png b/common/EtsXmlTextConvert/entry/src/main/resources/base/media/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..ce307a8827bd75456441ceb57d530e4c8d45d36c Binary files /dev/null and b/common/EtsXmlTextConvert/entry/src/main/resources/base/media/icon.png differ diff --git a/common/EtsXmlTextConvert/entry/src/main/resources/en/element/string.json b/common/EtsXmlTextConvert/entry/src/main/resources/en/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..2b9578cfa1d55efc1a8cb7d2d8675a60c7f9d243 --- /dev/null +++ b/common/EtsXmlTextConvert/entry/src/main/resources/en/element/string.json @@ -0,0 +1,24 @@ +{ + "string": [ + { + "name": "entry_MainAbility", + "value": "eTSXmlTextConvert" + }, + { + "name": "description_mainability", + "value": "ETS_Empty Ability" + }, + { + "name": "serializer", + "value": "Xml Serializer" + }, + { + "name": "parser", + "value": "Xml Pull Parser" + }, + { + "name": "convert", + "value": "Xml Convert" + } + ] +} \ No newline at end of file diff --git a/common/EtsXmlTextConvert/entry/src/main/resources/zh/element/string.json b/common/EtsXmlTextConvert/entry/src/main/resources/zh/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..962718edcab6054af7628d66e0ae408bf96dfcff --- /dev/null +++ b/common/EtsXmlTextConvert/entry/src/main/resources/zh/element/string.json @@ -0,0 +1,24 @@ +{ + "string": [ + { + "name": "entry_MainAbility", + "value": "eTSXmlTextConvert" + }, + { + "name": "description_mainability", + "value": "ETS_Empty Ability" + }, + { + "name": "serializer", + "value": "Xml 序列化" + }, + { + "name": "parser", + "value": "Xml Pull解析" + }, + { + "name": "convert", + "value": "Xml 文本转换" + } + ] +} \ No newline at end of file diff --git a/common/EtsXmlTextConvert/screenshots/devices/main.png b/common/EtsXmlTextConvert/screenshots/devices/main.png new file mode 100644 index 0000000000000000000000000000000000000000..a6ad70e58e55e8120a2767e555ad4eba289e6fb6 Binary files /dev/null and b/common/EtsXmlTextConvert/screenshots/devices/main.png differ diff --git a/common/EtsXmlTextConvert/settings.gradle b/common/EtsXmlTextConvert/settings.gradle new file mode 100644 index 0000000000000000000000000000000000000000..4773db73233a570c2d0c01a22e75321acfbf7a07 --- /dev/null +++ b/common/EtsXmlTextConvert/settings.gradle @@ -0,0 +1 @@ +include ':entry'