2121import com .google .common .collect .Iterators ;
2222import com .google .common .collect .PeekingIterator ;
2323
24+ import java .util .LinkedHashMap ;
2425import java .util .List ;
2526import java .util .Map ;
2627import java .util .logging .Logger ;
28+ import java .util .regex .Pattern ;
2729
2830public class StyledString implements CharSequence {
2931 private static final Logger LOGGER = Logger .getLogger (StyledString .class .getName ());
@@ -100,9 +102,10 @@ private void decodeIterate(PeekingIterator<Span> it) {
100102 mBuffer .append ('<' ).append (name );
101103 if (attributes != null ) {
102104 for (Map .Entry <String , String > entry : attributes .entrySet ()) {
103- mBuffer .append (' ' ).append (entry .getKey ()).append ("=\" " )
104- .append (ResXmlEncoders .escapeXmlChars (entry .getValue ()))
105- .append ('"' );
105+ mBuffer .append (' ' )
106+ .append (entry .getKey ()).append ("=\" " )
107+ .append (ResXmlEncoders .escapeXmlChars (entry .getValue ()))
108+ .append ('"' );
106109 }
107110 }
108111 // If an opening tag is followed by a matching closing tag, write as an empty-element tag.
@@ -132,8 +135,9 @@ private void decodeIterate(PeekingIterator<Span> it) {
132135 }
133136
134137 public static class Span {
135- private static final Splitter .MapSplitter ATTRIBUTES_SPLITTER =
136- Splitter .on (';' ).omitEmptyStrings ().withKeyValueSeparator (Splitter .on ('=' ).limit (2 ));
138+ private static final Splitter .MapSplitter ATTR_SPLITTER = Splitter .on (
139+ Pattern .compile (";(?=[\\ p{L}_][\\ p{L}\\ p{N}_.-]*=)" ))
140+ .withKeyValueSeparator (Splitter .on ('=' ).limit (2 ));
137141
138142 private final String mTag ;
139143 private final int mFirstChar ;
@@ -159,14 +163,12 @@ public int getLastChar() {
159163
160164 public String getName () {
161165 int separatorIdx = mTag .indexOf (';' );
162- return separatorIdx == -1 ? mTag : mTag .substring (0 , separatorIdx );
166+ return separatorIdx != -1 ? mTag .substring (0 , separatorIdx ) : mTag ;
163167 }
164168
165169 public Map <String , String > getAttributes () {
166170 int separatorIdx = mTag .indexOf (';' );
167- return separatorIdx != -1 ? ATTRIBUTES_SPLITTER .split (
168- mTag .substring (separatorIdx + 1 , mTag .endsWith (";" ) ? mTag .length () - 1 : mTag .length ())
169- ) : null ;
171+ return separatorIdx != -1 ? ATTR_SPLITTER .split (mTag .substring (separatorIdx + 1 )) : null ;
170172 }
171173 }
172174}
0 commit comments