| 1 |
/* |
| 2 |
* Copyright (C) 2010 Ryszard Wiśniewski <[email protected]> |
| 3 |
* Copyright (C) 2010 Connor Tumbleson <[email protected]> |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* https://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
package brut.androlib.res.decoder; |
| 18 |
|
| 19 |
import brut.androlib.res.xml.ResXmlEncoders; |
| 20 |
import com.google.common.base.Splitter; |
| 21 |
import com.google.common.base.Splitter.MapSplitter; |
| 22 |
import com.google.common.collect.Iterators; |
| 23 |
import com.google.common.collect.PeekingIterator; |
| 24 |
|
| 25 |
import java.util.List; |
| 26 |
import java.util.Map; |
| 27 |
import java.util.logging.Logger; |
| 28 |
|
| 29 |
public class StyledString { |
| 30 |
private final String mText; |
| 31 |
private final List<Span> mSpans; |
| 32 |
|
| 33 |
public StyledString(String text, List<Span> spans) { |
| 34 |
this.mText = text; |
| 35 |
this.mSpans = spans; |
| 36 |
} |
| 37 |
|
| 38 |
String getText() { |
| 39 |
return mText; |
| 40 |
} |
| 41 |
|
| 42 |
List<Span> getSpans() { |
| 43 |
return mSpans; |
| 44 |
} |
| 45 |
|
| 46 |
@Override |
| 47 |
public String toString() { |
| 48 |
return new Decoder().decode(this); |
| 49 |
} |
| 50 |
|
| 51 |
public static class Span implements Comparable<Span> { |
| 52 |
private static final MapSplitter ATTRIBUTES_SPLITTER = |
| 53 |
Splitter.on(';').omitEmptyStrings().withKeyValueSeparator(Splitter.on('=').limit(2)); |
| 54 |
|
| 55 |
private final String tag; |
| 56 |
private final int firstChar; |
| 57 |
private final int lastChar; |
| 58 |
|
| 59 |
public Span(String tag, int firstChar, int lastChar) { |
| 60 |
this.tag = tag; |
| 61 |
this.firstChar = firstChar; |
| 62 |
this.lastChar = lastChar; |
| 63 |
} |
| 64 |
|
| 65 |
public String getTag() { |
| 66 |
return tag; |
| 67 |
} |
| 68 |
|
| 69 |
public int getFirstChar() { |
| 70 |
return firstChar; |
| 71 |
} |
| 72 |
|
| 73 |
public int getLastChar() { |
| 74 |
return lastChar; |
| 75 |
} |
| 76 |
|
| 77 |
public String getName() { |
| 78 |
int separatorIdx = tag.indexOf(';'); |
| 79 |
return separatorIdx == -1 ? tag : tag.substring(0, separatorIdx); |
| 80 |
} |
| 81 |
|
| 82 |
public Map<String, String> getAttributes() { |
| 83 |
int separatorIdx = tag.indexOf(';'); |
| 84 |
return separatorIdx == -1 ? null : ATTRIBUTES_SPLITTER.split( |
| 85 |
tag.substring(separatorIdx + 1, tag.endsWith(";") ? tag.length() - 1 : tag.length()) |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public int compareTo(Span o) { |
| 91 |
int res = Integer.compare(firstChar, o.firstChar); |
| 92 |
if (res != 0) { |
| 93 |
return res; |
| 94 |
} |
| 95 |
res = Integer.compare(lastChar, o.lastChar); |
| 96 |
if (res != 0) { |
| 97 |
return -res; |
| 98 |
} |
| 99 |
return -tag.compareTo(o.tag); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
private static class Decoder { |
| 104 |
private String text; |
| 105 |
private StringBuilder xmlValue; |
| 106 |
private int lastOffset; |
| 107 |
|
| 108 |
String decode(StyledString styledString) { |
| 109 |
text = styledString.getText(); |
| 110 |
xmlValue = new StringBuilder(text.length() * 2); |
| 111 |
lastOffset = 0; |
| 112 |
|
| 113 |
// recurse top-level tags |
| 114 |
PeekingIterator<Span> it = Iterators.peekingIterator(styledString.getSpans().iterator()); |
| 115 |
while (it.hasNext()) { |
| 116 |
decodeIterate(it); |
| 117 |
} |
| 118 |
|
| 119 |
// write the remaining encoded raw text |
| 120 |
if (lastOffset < text.length()) { |
| 121 |
xmlValue.append(ResXmlEncoders.escapeXmlChars(text.substring(lastOffset))); |
| 122 |
} |
| 123 |
return xmlValue.toString(); |
| 124 |
} |
| 125 |
|
| 126 |
private void decodeIterate(PeekingIterator<Span> it) { |
| 127 |
Span span = it.next(); |
| 128 |
String name = span.getName(); |
| 129 |
Map<String, String> attributes = span.getAttributes(); |
| 130 |
int spanStart = span.getFirstChar(); |
| 131 |
int spanEnd = span.getLastChar() + 1; |
| 132 |
|
| 133 |
// write encoded raw text preceding the opening tag |
| 134 |
if (spanStart > lastOffset) { |
| 135 |
xmlValue.append(ResXmlEncoders.escapeXmlChars(text.substring(lastOffset, spanStart))); |
| 136 |
} |
| 137 |
lastOffset = spanStart; |
| 138 |
|
| 139 |
// write opening tag |
| 140 |
xmlValue.append('<').append(name); |
| 141 |
if (attributes != null) { |
| 142 |
for (Map.Entry<String, String> attrEntry : attributes.entrySet()) { |
| 143 |
xmlValue.append(' ').append(attrEntry.getKey()).append("=\"") |
| 144 |
.append(ResXmlEncoders.escapeXmlChars(attrEntry.getValue())).append('"'); |
| 145 |
} |
| 146 |
} |
| 147 |
// if an opening tag is followed by a matching closing tag, write as an empty-element tag |
| 148 |
if (spanStart == spanEnd) { |
| 149 |
xmlValue.append("/>"); |
| 150 |
return; |
| 151 |
} |
| 152 |
xmlValue.append('>'); |
| 153 |
|
| 154 |
// recurse nested tags |
| 155 |
while (it.hasNext() && it.peek().getFirstChar() < spanEnd) { |
| 156 |
decodeIterate(it); |
| 157 |
} |
| 158 |
|
| 159 |
// write encoded raw text preceding the closing tag |
| 160 |
if (spanEnd > lastOffset) { |
| 161 |
xmlValue.append(ResXmlEncoders.escapeXmlChars(text.substring(lastOffset, spanEnd))); |
| 162 |
} |
| 163 |
lastOffset = spanEnd; |
| 164 |
|
| 165 |
// write closing tag |
| 166 |
xmlValue.append("</").append(name).append('>'); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
private static final Logger LOGGER = Logger.getLogger(StyledString.class.getName()); |
| 171 |
} |