|
10 | 10 | import com.reandroid.json.JSONObject; |
11 | 11 | import com.reandroid.utils.collection.CollectionUtil; |
12 | 12 | import com.reandroid.utils.collection.CombiningIterator; |
| 13 | +import com.reandroid.utils.collection.ComputeIterator; |
13 | 14 | import com.reandroid.utils.collection.IterableIterator; |
14 | 15 | import com.reandroid.utils.collection.SingleIterator; |
15 | 16 | import com.reandroid.utils.io.FileUtil; |
16 | 17 | import com.reandroid.xml.XMLDocument; |
17 | 18 | import com.reandroid.xml.XMLFactory; |
| 19 | +import com.reandroid.xml.XMLPath; |
18 | 20 | import com.reandroid.xml.XMLUtil; |
19 | 21 | import com.reandroid.xml.base.Document; |
20 | 22 | import org.xmlpull.v1.XmlPullParser; |
21 | 23 | import org.xmlpull.v1.XmlPullParserException; |
22 | 24 | import org.xmlpull.v1.XmlSerializer; |
23 | 25 |
|
24 | | -import java.io.*; |
| 26 | +import java.io.File; |
| 27 | +import java.io.IOException; |
| 28 | +import java.io.InputStream; |
| 29 | +import java.io.OutputStream; |
| 30 | +import java.io.StringWriter; |
25 | 31 | import java.util.Iterator; |
26 | 32 |
|
27 | 33 | public class ResXmlDocument extends ResXmlDocumentOrElement implements |
@@ -96,6 +102,78 @@ private ResXmlDocument getParentDocument() { |
96 | 102 | return getParentInstance(ResXmlDocument.class); |
97 | 103 | } |
98 | 104 |
|
| 105 | + public boolean removeAttributes(XMLPath xmlPath) { |
| 106 | + if (!xmlPath.isAttribute()) { |
| 107 | + throw new IllegalArgumentException("Path is not a type of attribute: " + xmlPath); |
| 108 | + } |
| 109 | + Iterator<ResXmlAttribute> iterator = xmlPath.findAll(getElements()); |
| 110 | + iterator = CollectionUtil.copyOf(iterator); |
| 111 | + boolean result = false; |
| 112 | + while (iterator.hasNext()) { |
| 113 | + ResXmlAttribute attribute = iterator.next(); |
| 114 | + attribute.removeSelf(); |
| 115 | + result = true; |
| 116 | + } |
| 117 | + return result; |
| 118 | + } |
| 119 | + public boolean removeElements(XMLPath xmlPath) { |
| 120 | + Iterator<ResXmlElement> iterator; |
| 121 | + if (xmlPath.isAttribute()) { |
| 122 | + Iterator<ResXmlAttribute> attributes = xmlPath.findAll(getElements()); |
| 123 | + iterator = ComputeIterator.of(attributes, ResXmlAttribute::getParentElement); |
| 124 | + } else { |
| 125 | + iterator = xmlPath.findAll(getElements()); |
| 126 | + } |
| 127 | + iterator = CollectionUtil.copyOf(iterator); |
| 128 | + boolean result = false; |
| 129 | + while (iterator.hasNext()) { |
| 130 | + ResXmlElement element = iterator.next(); |
| 131 | + element.removeSelf(); |
| 132 | + result = true; |
| 133 | + } |
| 134 | + return result; |
| 135 | + } |
| 136 | + public ResXmlElement newChildElement(XMLPath xmlPath) { |
| 137 | + if (xmlPath.containsAnyPathOrName()) { |
| 138 | + throw new IllegalArgumentException("invalid path: " + xmlPath); |
| 139 | + } |
| 140 | + if (!xmlPath.isElement()) { |
| 141 | + return newChildElement(xmlPath.getParent()); |
| 142 | + } |
| 143 | + String name = xmlPath.getName(); |
| 144 | + XMLPath parent = xmlPath.getParent(); |
| 145 | + if (parent != null) { |
| 146 | + return getOrCreateChildElement(parent) |
| 147 | + .newElement(name); |
| 148 | + } |
| 149 | + if (this instanceof ResXmlDocument) { |
| 150 | + return getOrCreateElement(name); |
| 151 | + } |
| 152 | + return newElement(name); |
| 153 | + } |
| 154 | + public ResXmlElement getOrCreateChildElement(XMLPath xmlPath) { |
| 155 | + if (xmlPath.containsAnyPathOrName()) { |
| 156 | + throw new IllegalArgumentException("invalid path: " + xmlPath); |
| 157 | + } |
| 158 | + if (!xmlPath.isElement()) { |
| 159 | + return newChildElement(xmlPath.getParent()); |
| 160 | + } |
| 161 | + ResXmlElement element = null; |
| 162 | + int depth = xmlPath.depth(); |
| 163 | + int i = depth; |
| 164 | + while (i >= 0) { |
| 165 | + XMLPath path = xmlPath.getPath(i); |
| 166 | + String name = path.getName(); |
| 167 | + if (i == depth) { |
| 168 | + element = this.getOrCreateElement(name); |
| 169 | + } else { |
| 170 | + element = element.getOrCreateElement(name); |
| 171 | + } |
| 172 | + i --; |
| 173 | + } |
| 174 | + return element; |
| 175 | + } |
| 176 | + |
99 | 177 | @Override |
100 | 178 | ResXmlDocumentChunk getChunk() { |
101 | 179 | return (ResXmlDocumentChunk) super.getChunk(); |
|
0 commit comments