Skip to content

Commit

Permalink
Разэкранирование XML в тексте статусов.
Browse files Browse the repository at this point in the history
  • Loading branch information
solkin committed Mar 21, 2014
1 parent 7c54700 commit 1fdca56
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/com/tomclaw/mandarin/im/icq/IcqSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,8 @@ private void processEvent(String eventType, JSONObject eventData) {

String buddyStatus = eventData.getString(STATE);
String moodIcon = eventData.optString(MOOD_ICON);
String statusMessage = eventData.optString(STATUS_MSG);
String moodTitle = eventData.optString(MOOD_TITLE);
String statusMessage = StringUtil.unescapeXml(eventData.optString(STATUS_MSG));
String moodTitle = StringUtil.unescapeXml(eventData.optString(MOOD_TITLE));

int statusIndex = getStatusIndex(moodIcon, buddyStatus);
String statusTitle = getStatusTitle(moodTitle, statusIndex);
Expand Down
5 changes: 3 additions & 2 deletions src/com/tomclaw/mandarin/im/icq/MyInfo.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tomclaw.mandarin.im.icq;

import android.text.TextUtils;
import com.tomclaw.mandarin.util.StringUtil;
import com.tomclaw.mandarin.util.Unobfuscatable;

/**
Expand Down Expand Up @@ -49,14 +50,14 @@ public String optMoodTitle() {
if (TextUtils.isEmpty(moodTitle)) {
return "";
}
return moodTitle;
return StringUtil.unescapeXml(moodTitle);
}

public String optStatusMsg() {
if (TextUtils.isEmpty(statusMsg)) {
return "";
}
return statusMsg;
return StringUtil.unescapeXml(statusMsg);
}

public String getUserType() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.tomclaw.mandarin.main.adapters;

import android.app.Activity;
import android.app.LoaderManager;
import android.content.Context;
import android.content.CursorLoader;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.tomclaw.mandarin.main.adapters;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
Expand Down
189 changes: 189 additions & 0 deletions src/com/tomclaw/mandarin/util/Entities.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package com.tomclaw.mandarin.util;

import android.util.SparseArray;

import java.util.HashMap;
import java.util.Map;

/**
* This class supplies some methods
* to escape / unescape special chars according XML specifications
*/
class Entities {

private static final String[][] BASIC_ARRAY = {
{"quot", "34"}, // " - double-quote
{"amp", "38"}, // & - ampersand
{"lt", "60"}, // < - less-than
{"gt", "62"}, // > - greater-than
{"apos", "39"}, // XML apostrophe
};

/**
* <p>The set of entities supported by standard XML.</p>
*/
public static final Entities XML;

static {
XML = new Entities();
XML.addEntities(BASIC_ARRAY);
}


static interface EntityMap {
void add(String name, int value);

String name(int value);

int value(String name);
}

static class PrimitiveEntityMap implements EntityMap {
private Map<String, Integer> mapNameToValue = new HashMap<String, Integer>();
private SparseArray<String> mapValueToName = new SparseArray<String>();

public void add(String name, int value) {
mapNameToValue.put(name, value);
mapValueToName.put(value, name);
}

public String name(int value) {
return mapValueToName.get(value);
}

public int value(String name) {
Integer value = mapNameToValue.get(name);
if (value == null) {
return -1;
}
return value;
}
}

static class LookupEntityMap extends PrimitiveEntityMap {

private String[] lookupTable;
private int LOOKUP_TABLE_SIZE = 256;

public String name(int value) {
if (value < LOOKUP_TABLE_SIZE) {
return lookupTable()[value];
}
return super.name(value);
}

private String[] lookupTable() {
if (lookupTable == null) {
createLookupTable();
}
return lookupTable;
}

private void createLookupTable() {
lookupTable = new String[LOOKUP_TABLE_SIZE];
for (int i = 0, l = LOOKUP_TABLE_SIZE; i < l; ++i) {
lookupTable[i] = super.name(i);
}
}
}

EntityMap map = new Entities.LookupEntityMap();

public void addEntities(String[][] entityArray) {
for (String[] anEntityArray : entityArray) {
addEntity(anEntityArray[0], Integer.parseInt(anEntityArray[1]));
}
}

public void addEntity(String name, int value) {
map.add(name, value);
}

public String entityName(int value) {
return map.name(value);
}


public int entityValue(String name) {
return map.value(name);
}

/**
* <p>Escapes special characters in a <code>String</code>.</p>
*
* @param str The <code>String</code> to escape.
* @return A escaped <code>String</code>.
*/
public String escape(String str) {
char ch;
String entityName;
StringBuffer buf;
int intValue;
buf = new StringBuffer(str.length() * 2);
for (int i = 0, l = str.length(); i < l; ++i) {
ch = str.charAt(i);
entityName = this.entityName(ch);
if (entityName == null) {
if (ch > 0x7F) {
intValue = ch;
buf.append("&#");
buf.append(intValue);
buf.append(';');
} else {
buf.append(ch);
}
} else {
buf.append('&');
buf.append(entityName);
buf.append(';');
}
}
return buf.toString();
}

/**
* <p>Unescapes special characters in a <code>String</code>.</p>
*
* @param str The <code>String</code> to escape.
* @return A un-escaped <code>String</code>.
*/
public String unescape(String str) {
StringBuffer buf;
String entityName;
char ch, charAt1;
int entityValue;
buf = new StringBuffer(str.length());
for (int i = 0, l = str.length(); i < l; ++i) {
ch = str.charAt(i);
if (ch == '&') {
int semi = str.indexOf(';', i + 1);
if (semi == -1) {
buf.append(ch);
continue;
}
entityName = str.substring(i + 1, semi);
if (entityName.charAt(0) == '#') {
charAt1 = entityName.charAt(1);
if (charAt1 == 'x' || charAt1 == 'X') {
entityValue = Integer.valueOf(entityName.substring(2), 16);
} else {
entityValue = Integer.parseInt(entityName.substring(1));
}
} else {
entityValue = this.entityValue(entityName);
}
if (entityValue == -1) {
buf.append('&');
buf.append(entityName);
buf.append(';');
} else {
buf.append((char) (entityValue));
}
i = semi;
} else {
buf.append(ch);
}
}
return buf.toString();
}
}
4 changes: 4 additions & 0 deletions src/com/tomclaw/mandarin/util/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@ public static String getHmacSha256Base64(String key, String data)
byte[] digest = messageAuthenticationCode.doFinal();
return Base64.encodeToString(digest, Base64.NO_WRAP);
}

public static String unescapeXml(String string) {
return Entities.XML.unescape(string);
}
}

0 comments on commit 1fdca56

Please sign in to comment.