|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright 2015-2023 Valentyn Kolesnikov |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | +package com.github.underscore; |
| 25 | + |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.LinkedHashMap; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +public class XmlBuilder { |
| 32 | + private static final String SELF_CLOSING = "-self-closing"; |
| 33 | + private static final String TRUE = "true"; |
| 34 | + private final Map<String, Object> data; |
| 35 | + private String path; |
| 36 | + private String savedPath; |
| 37 | + |
| 38 | + XmlBuilder(String rootName) { |
| 39 | + data = new LinkedHashMap<>(); |
| 40 | + Map<String, Object> value = new LinkedHashMap<>(); |
| 41 | + value.put(SELF_CLOSING, TRUE); |
| 42 | + data.put(rootName, value); |
| 43 | + path = rootName; |
| 44 | + } |
| 45 | + |
| 46 | + public static XmlBuilder create(String rootName) { |
| 47 | + return new XmlBuilder(rootName); |
| 48 | + } |
| 49 | + |
| 50 | + public static XmlBuilder parse(String xml) { |
| 51 | + Map<String, Object> xmlData = U.fromXmlMap(xml); |
| 52 | + XmlBuilder xmlBuilder = new XmlBuilder(Xml.XmlValue.getMapKey(xmlData)); |
| 53 | + xmlBuilder.setData(xmlData); |
| 54 | + return xmlBuilder; |
| 55 | + } |
| 56 | + |
| 57 | + @SuppressWarnings("unchecked") |
| 58 | + public XmlBuilder e(String elementName) { |
| 59 | + U.remove(data, path + "." + SELF_CLOSING); |
| 60 | + Map<String, Object> value = new LinkedHashMap<>(); |
| 61 | + value.put(SELF_CLOSING, TRUE); |
| 62 | + Object object = U.get(data, path + "." + elementName); |
| 63 | + if (object instanceof Map) { |
| 64 | + List<Object> list = new ArrayList<>(); |
| 65 | + list.add(object); |
| 66 | + list.add(value); |
| 67 | + U.set(data, path + "." + elementName, list); |
| 68 | + path += "." + elementName + ".1"; |
| 69 | + savedPath = path; |
| 70 | + } else if (object instanceof List) { |
| 71 | + path += "." + elementName + "." + ((List<Object>) object).size(); |
| 72 | + savedPath = path; |
| 73 | + ((List<Object>) object).add(value); |
| 74 | + } else { |
| 75 | + U.set(data, path + "." + elementName, value); |
| 76 | + path += "." + elementName; |
| 77 | + } |
| 78 | + return this; |
| 79 | + } |
| 80 | + |
| 81 | + public XmlBuilder a(String attributeName, String value) { |
| 82 | + U.remove(data, path + "." + SELF_CLOSING); |
| 83 | + U.set(data, path + ".-" + attributeName, value); |
| 84 | + return this; |
| 85 | + } |
| 86 | + |
| 87 | + public XmlBuilder c(String comment) { |
| 88 | + U.remove(data, path + "." + SELF_CLOSING); |
| 89 | + U.update(data, path + ".#comment", comment); |
| 90 | + return this; |
| 91 | + } |
| 92 | + |
| 93 | + public XmlBuilder i(String target, String value) { |
| 94 | + U.remove(data, path + "." + SELF_CLOSING); |
| 95 | + U.set(data, "?" + target, value); |
| 96 | + return this; |
| 97 | + } |
| 98 | + |
| 99 | + public XmlBuilder d(String cdata) { |
| 100 | + U.remove(data, path + "." + SELF_CLOSING); |
| 101 | + U.update(data, path + ".#cdata-section", cdata); |
| 102 | + return this; |
| 103 | + } |
| 104 | + |
| 105 | + public XmlBuilder t(String text) { |
| 106 | + U.remove(data, path + "." + SELF_CLOSING); |
| 107 | + U.update(data, path + ".#text", text); |
| 108 | + return this; |
| 109 | + } |
| 110 | + |
| 111 | + public XmlBuilder importXmlBuilder(XmlBuilder xmlBuilder) { |
| 112 | + data.putAll(xmlBuilder.data); |
| 113 | + return this; |
| 114 | + } |
| 115 | + |
| 116 | + public XmlBuilder up() { |
| 117 | + if (path.equals(savedPath)) { |
| 118 | + path = path.substring(0, path.lastIndexOf(".")); |
| 119 | + } |
| 120 | + path = path.substring(0, path.lastIndexOf(".")); |
| 121 | + return this; |
| 122 | + } |
| 123 | + |
| 124 | + public XmlBuilder root() { |
| 125 | + int index = path.indexOf("."); |
| 126 | + XmlBuilder xmlBuilder = new XmlBuilder(index == -1 ? path : path.substring(0, index)); |
| 127 | + xmlBuilder.setData(data); |
| 128 | + return xmlBuilder; |
| 129 | + } |
| 130 | + |
| 131 | + public org.w3c.dom.Document getDocument() { |
| 132 | + try { |
| 133 | + return Xml.Document.createDocument(asString()); |
| 134 | + } catch (Exception ex) { |
| 135 | + throw new IllegalArgumentException(ex); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + public XmlBuilder set(final String path, final Object value) { |
| 140 | + U.set(data, path, value); |
| 141 | + return this; |
| 142 | + } |
| 143 | + |
| 144 | + public XmlBuilder remove(final String key) { |
| 145 | + U.remove(data, key); |
| 146 | + return this; |
| 147 | + } |
| 148 | + |
| 149 | + public Map<String, Object> build() { |
| 150 | + return U.deepCopyMap(data); |
| 151 | + } |
| 152 | + |
| 153 | + public XmlBuilder clear() { |
| 154 | + data.clear(); |
| 155 | + return this; |
| 156 | + } |
| 157 | + |
| 158 | + public boolean isEmpty() { |
| 159 | + return data.isEmpty(); |
| 160 | + } |
| 161 | + |
| 162 | + public int size() { |
| 163 | + return data.size(); |
| 164 | + } |
| 165 | + |
| 166 | + public String asString() { |
| 167 | + return U.toXml(data); |
| 168 | + } |
| 169 | + |
| 170 | + public String toXml(Xml.XmlStringBuilder.Step identStep) { |
| 171 | + return Xml.toXml(data, identStep); |
| 172 | + } |
| 173 | + |
| 174 | + public String toXml() { |
| 175 | + return U.toXml(data); |
| 176 | + } |
| 177 | + |
| 178 | + public String toJson(Json.JsonStringBuilder.Step identStep) { |
| 179 | + return Json.toJson(data, identStep); |
| 180 | + } |
| 181 | + |
| 182 | + public String toJson() { |
| 183 | + return U.toJson(data); |
| 184 | + } |
| 185 | + |
| 186 | + private void setData(Map<String, Object> newData) { |
| 187 | + data.clear(); |
| 188 | + data.putAll(newData); |
| 189 | + } |
| 190 | +} |
0 commit comments