|
| 1 | +/* |
| 2 | + * This file is a part of MDClasses. |
| 3 | + * |
| 4 | + * Copyright © 2019 - 2020 |
| 5 | + * Tymko Oleg <olegtymko@yandex.ru>, Maximov Valery <maximovvalery@gmail.com> and contributors |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: LGPL-3.0-or-later |
| 8 | + * |
| 9 | + * MDClasses is free software; you can redistribute it and/or |
| 10 | + * modify it under the terms of the GNU Lesser General Public |
| 11 | + * License as published by the Free Software Foundation; either |
| 12 | + * version 3.0 of the License, or (at your option) any later version. |
| 13 | + * |
| 14 | + * MDClasses is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | + * Lesser General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Lesser General Public |
| 20 | + * License along with MDClasses. |
| 21 | + */ |
| 22 | +package com.github._1c_syntax.mdclasses.deserialize; |
| 23 | + |
| 24 | +import com.fasterxml.jackson.core.JsonParser; |
| 25 | +import com.fasterxml.jackson.core.JsonToken; |
| 26 | +import com.fasterxml.jackson.core.TreeNode; |
| 27 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 28 | +import com.fasterxml.jackson.databind.JsonDeserializer; |
| 29 | +import com.fasterxml.jackson.databind.node.NullNode; |
| 30 | +import com.github._1c_syntax.mdclasses.utils.ObjectMapperFactory; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | +import java.util.ArrayList; |
| 34 | +import java.util.Collection; |
| 35 | +import java.util.HashMap; |
| 36 | +import java.util.List; |
| 37 | +import java.util.Map; |
| 38 | + |
| 39 | +/** |
| 40 | + * Класс реализация кастомной десериализации XML файлов конфигурации. |
| 41 | + * В дочерних классах необходимо реализовать только метод чтение свойства readToken |
| 42 | + */ |
| 43 | +abstract class AbstractDeserializer extends JsonDeserializer<Object> { |
| 44 | + |
| 45 | + protected final String rootKey; |
| 46 | + |
| 47 | + protected AbstractDeserializer(String key) { |
| 48 | + rootKey = key; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public Map<String, Object> deserialize( |
| 53 | + JsonParser parser, |
| 54 | + DeserializationContext context |
| 55 | + ) throws IOException { |
| 56 | + Map<String, Object> childObjects = new HashMap<>(); |
| 57 | + |
| 58 | + if (parser.getCurrentToken() != JsonToken.START_OBJECT) { |
| 59 | + return childObjects; |
| 60 | + } |
| 61 | + |
| 62 | + int level = 1; |
| 63 | + while (parser.nextToken() != null) { |
| 64 | + var token = parser.getCurrentToken(); |
| 65 | + var name = parser.getCurrentName(); |
| 66 | + |
| 67 | + // скрректируем уровень, для того, чтобы вовремя выйти |
| 68 | + level = correctLevel(name, token, level); |
| 69 | + if (level == 0) { |
| 70 | + break; |
| 71 | + } |
| 72 | + |
| 73 | + // обрабатываем только свойства |
| 74 | + if (token == JsonToken.FIELD_NAME) { |
| 75 | + readToken(parser, childObjects, name, context); |
| 76 | + } |
| 77 | + } |
| 78 | + return childObjects; |
| 79 | + } |
| 80 | + |
| 81 | + protected abstract void readToken(JsonParser parser, |
| 82 | + Map<String, Object> childObjects, |
| 83 | + String name, |
| 84 | + DeserializationContext context) throws IOException; |
| 85 | + |
| 86 | + private int correctLevel(String name, JsonToken token, int level) { |
| 87 | + var isRoot = rootKey.equals(name); |
| 88 | + if (isRoot && token == JsonToken.END_OBJECT) { |
| 89 | + level--; |
| 90 | + } else if (isRoot && token == JsonToken.START_OBJECT) { |
| 91 | + level++; |
| 92 | + } |
| 93 | + |
| 94 | + return level; |
| 95 | + } |
| 96 | + |
| 97 | + protected boolean isEndToken(String name, JsonParser parser) throws IOException { |
| 98 | + return parser.getCurrentToken() == JsonToken.END_OBJECT && name.equals(parser.getCurrentName()); |
| 99 | + } |
| 100 | + |
| 101 | + protected String getValueFromNode(TreeNode node) { |
| 102 | + if (node instanceof NullNode) { |
| 103 | + return null; |
| 104 | + } |
| 105 | + return getValueFromNode(node, String.class); |
| 106 | + } |
| 107 | + |
| 108 | + protected <T> T getValueFromNode(TreeNode node, Class<T> classValue) { |
| 109 | + return ObjectMapperFactory.getXmlMapper().convertValue(node, classValue); |
| 110 | + } |
| 111 | + |
| 112 | + protected <T> T readValueByType(JsonParser parser, Class<T> classValue) throws IOException { |
| 113 | + return ObjectMapperFactory.getXmlMapper().readValue(parser, classValue); |
| 114 | + } |
| 115 | + |
| 116 | + protected void addProperty(Map<String, Object> childObjects, String name, Object newValue) { |
| 117 | + var value = childObjects.get(name); |
| 118 | + if (value == null) { |
| 119 | + childObjects.put(name, newValue); |
| 120 | + } else if (value instanceof List) { |
| 121 | + List<Object> values = new ArrayList<>((Collection<?>) value); |
| 122 | + values.add(newValue); |
| 123 | + childObjects.put(name, values); |
| 124 | + } else { |
| 125 | + List<Object> values = new ArrayList<>(); |
| 126 | + values.add(value); |
| 127 | + values.add(newValue); |
| 128 | + childObjects.put(name, values); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + protected void readAndAddProperty(Map<String, Object> childObjects, String name, JsonParser parser) throws IOException { |
| 133 | + var newValue = getValueFromNode(parser.readValueAsTree()); |
| 134 | + addProperty(childObjects, name, newValue); |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments