aboutsummaryrefslogtreecommitdiffstats
path: root/main/src
diff options
context:
space:
mode:
authorBananeweizen <bananeweizen@gmx.de>2011-11-19 08:39:17 +0100
committerBananeweizen <bananeweizen@gmx.de>2011-11-19 08:39:17 +0100
commitefe762ae85a8db89b36f0b3fe7bff37b3ae7a8e9 (patch)
tree906de400a1bc35fbd5f87f8f9eec85755e1c795f /main/src
parentd51848cfdc0c647de75bbdce7c006b685a7a49d1 (diff)
downloadcgeo-efe762ae85a8db89b36f0b3fe7bff37b3ae7a8e9.zip
cgeo-efe762ae85a8db89b36f0b3fe7bff37b3ae7a8e9.tar.gz
cgeo-efe762ae85a8db89b36f0b3fe7bff37b3ae7a8e9.tar.bz2
don't use regular expressions for String replacement
* prefer StringUtils.replace over String.replaceAll * have new test to verify this change * remove cgBase instance from LogTemplates
Diffstat (limited to 'main/src')
-rw-r--r--main/src/cgeo/geocaching/GCConstants.java3
-rw-r--r--main/src/cgeo/geocaching/LogTemplateProvider.java32
-rw-r--r--main/src/cgeo/geocaching/VisitCacheActivity.java12
-rw-r--r--main/src/cgeo/geocaching/cgBase.java14
-rw-r--r--main/src/cgeo/geocaching/cgCache.java4
-rw-r--r--main/src/cgeo/geocaching/cgCacheListAdapter.java6
-rw-r--r--main/src/cgeo/geocaching/cgeocaches.java4
-rw-r--r--main/src/cgeo/geocaching/cgeodetail.java14
-rw-r--r--main/src/cgeo/geocaching/cgeoinit.java2
-rw-r--r--main/src/cgeo/geocaching/cgeopopup.java2
-rw-r--r--main/src/cgeo/geocaching/cgeotouch.java17
-rw-r--r--main/src/cgeo/geocaching/cgeotrackable.java4
12 files changed, 55 insertions, 59 deletions
diff --git a/main/src/cgeo/geocaching/GCConstants.java b/main/src/cgeo/geocaching/GCConstants.java
index 8eef8b9..cf5e011 100644
--- a/main/src/cgeo/geocaching/GCConstants.java
+++ b/main/src/cgeo/geocaching/GCConstants.java
@@ -127,7 +127,4 @@ public final class GCConstants {
public final static Pattern PATTERN_VIEWSTATEFIELDCOUNT = Pattern.compile("id=\"__VIEWSTATEFIELDCOUNT\"[^(value)]+value=\"(\\d+)\"[^>]+>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
public final static Pattern PATTERN_VIEWSTATES = Pattern.compile("id=\"__VIEWSTATE(\\d*)\"[^(value)]+value=\"([^\"]+)\"[^>]+>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
public final static Pattern PATTERN_USERTOKEN2 = Pattern.compile("userToken\\s*=\\s*'([^']+)'");
-
- public final static String PATTERN_PARAGRAPH = Pattern.quote("</p>");
-
}
diff --git a/main/src/cgeo/geocaching/LogTemplateProvider.java b/main/src/cgeo/geocaching/LogTemplateProvider.java
index a83a812..7d330a3 100644
--- a/main/src/cgeo/geocaching/LogTemplateProvider.java
+++ b/main/src/cgeo/geocaching/LogTemplateProvider.java
@@ -13,15 +13,15 @@ import java.util.regex.Pattern;
*/
public class LogTemplateProvider {
public static abstract class LogTemplate {
- private String template;
- private int resourceId;
+ private final String template;
+ private final int resourceId;
protected LogTemplate(String template, int resourceId) {
this.template = template;
this.resourceId = resourceId;
}
- abstract String getValue(cgBase base, boolean offline);
+ abstract String getValue(boolean offline);
public int getResourceId() {
return resourceId;
@@ -35,9 +35,9 @@ public class LogTemplateProvider {
return template;
}
- protected String apply(String input, cgBase base, boolean offline) {
+ protected String apply(String input, boolean offline) {
if (input.contains("[" + template + "]")) {
- return input.replaceAll("\\[" + template + "\\]", getValue(base, offline));
+ return StringUtils.replace(input, "[" + template + "]", getValue(offline));
}
return input;
}
@@ -51,36 +51,36 @@ public class LogTemplateProvider {
new LogTemplate("DATE", R.string.init_signature_template_date) {
@Override
- String getValue(final cgBase base, final boolean offline) {
- return base.formatFullDate(System.currentTimeMillis());
+ String getValue(final boolean offline) {
+ return cgBase.formatFullDate(System.currentTimeMillis());
}
},
new LogTemplate("TIME", R.string.init_signature_template_time) {
@Override
- String getValue(final cgBase base, final boolean offline) {
- return base.formatTime(System.currentTimeMillis());
+ String getValue(final boolean offline) {
+ return cgBase.formatTime(System.currentTimeMillis());
}
},
new LogTemplate("DATETIME", R.string.init_signature_template_datetime) {
@Override
- String getValue(final cgBase base, final boolean offline) {
+ String getValue(final boolean offline) {
final long currentTime = System.currentTimeMillis();
- return base.formatFullDate(currentTime) + " " + base.formatTime(currentTime);
+ return cgBase.formatFullDate(currentTime) + " " + cgBase.formatTime(currentTime);
}
},
new LogTemplate("USER", R.string.init_signature_template_user) {
@Override
- String getValue(final cgBase base, final boolean offline) {
+ String getValue(final boolean offline) {
return Settings.getUsername();
}
},
new LogTemplate("NUMBER", R.string.init_signature_template_number) {
@Override
- String getValue(final cgBase base, final boolean offline) {
+ String getValue(final boolean offline) {
if (offline) {
return "";
}
@@ -108,13 +108,13 @@ public class LogTemplateProvider {
return null;
}
- public static String applyTemplates(String signature, cgBase base, boolean offline) {
+ public static String applyTemplates(String signature, boolean offline) {
if (signature == null) {
return "";
}
String result = signature;
for (LogTemplate template : getTemplates()) {
- result = template.apply(result, base, offline);
+ result = template.apply(result, offline);
}
return result;
}
@@ -137,7 +137,7 @@ public class LogTemplateProvider {
if (count.length() == 0) {
findCount = 0;
} else {
- findCount = Integer.parseInt(count.replaceAll(",", ""));
+ findCount = Integer.parseInt(count.replaceAll("[.,]", ""));
}
}
}
diff --git a/main/src/cgeo/geocaching/VisitCacheActivity.java b/main/src/cgeo/geocaching/VisitCacheActivity.java
index fa329a3..0da4db9 100644
--- a/main/src/cgeo/geocaching/VisitCacheActivity.java
+++ b/main/src/cgeo/geocaching/VisitCacheActivity.java
@@ -324,7 +324,7 @@ public class VisitCacheActivity extends cgLogForm {
final int id = item.getItemId();
if (id == MENU_SIGNATURE) {
- insertIntoLog(LogTemplateProvider.applyTemplates(Settings.getSignature(), base, false), true);
+ insertIntoLog(LogTemplateProvider.applyTemplates(Settings.getSignature(), false), true);
return true;
} else if (id >= 10 && id <= 19) {
rating = (id - 9) / 2.0;
@@ -336,7 +336,7 @@ public class VisitCacheActivity extends cgLogForm {
}
final LogTemplate template = LogTemplateProvider.getTemplate(id);
if (template != null) {
- final String newText = template.getValue(base, false);
+ final String newText = template.getValue(false);
insertIntoLog(newText, true);
return true;
}
@@ -474,7 +474,7 @@ public class VisitCacheActivity extends cgLogForm {
} else if (StringUtils.isNotBlank(Settings.getSignature())
&& Settings.isAutoInsertSignature()
&& StringUtils.isBlank(((EditText) findViewById(R.id.log)).getText())) {
- insertIntoLog(LogTemplateProvider.applyTemplates(Settings.getSignature(), base, false), false);
+ insertIntoLog(LogTemplateProvider.applyTemplates(Settings.getSignature(), false), false);
}
if (!types.contains(typeSelected)) {
@@ -497,7 +497,7 @@ public class VisitCacheActivity extends cgLogForm {
});
final Button dateButton = (Button) findViewById(R.id.date);
- dateButton.setText(base.formatShortDate(date.getTime().getTime()));
+ dateButton.setText(cgBase.formatShortDate(date.getTime().getTime()));
dateButton.setOnClickListener(new DateListener());
final EditText logView = (EditText) findViewById(R.id.log);
@@ -530,7 +530,7 @@ public class VisitCacheActivity extends cgLogForm {
date = dateIn;
final Button dateButton = (Button) findViewById(R.id.date);
- dateButton.setText(base.formatShortDate(date.getTime().getTime()));
+ dateButton.setText(cgBase.formatShortDate(date.getTime().getTime()));
}
public void setType(int type) {
@@ -599,7 +599,7 @@ public class VisitCacheActivity extends cgLogForm {
setType(typeSelected);
final Button dateButton = (Button) findViewById(R.id.date);
- dateButton.setText(base.formatShortDate(date.getTime().getTime()));
+ dateButton.setText(cgBase.formatShortDate(date.getTime().getTime()));
dateButton.setOnClickListener(new DateListener());
final EditText logView = (EditText) findViewById(R.id.log);
diff --git a/main/src/cgeo/geocaching/cgBase.java b/main/src/cgeo/geocaching/cgBase.java
index 4ede03f..1acaf87 100644
--- a/main/src/cgeo/geocaching/cgBase.java
+++ b/main/src/cgeo/geocaching/cgBase.java
@@ -149,7 +149,7 @@ public class cgBase {
private static String idBrowser = "Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.86 Safari/533.4";
final private cgeoapplication app;
- final Context context;
+ private static Context context;
final private Resources res;
final private static Map<String, Integer> gcIcons = new HashMap<String, Integer>();
@@ -1073,7 +1073,7 @@ public class cgBase {
// replace linebreak and paragraph tags
String hint = GCConstants.PATTERN_LINEBREAK.matcher(result).replaceAll("\n");
if (hint != null) {
- cache.setHint(hint.replaceAll(GCConstants.PATTERN_PARAGRAPH, "").trim());
+ cache.setHint(StringUtils.replace(hint, "</p>", "").trim());
}
}
@@ -2486,7 +2486,7 @@ public class cgBase {
}
public static String urlencode_rfc3986(String text) {
- final String encoded = URLEncoder.encode(text).replace("+", "%20").replaceAll("%7E", "~");
+ final String encoded = StringUtils.replace(URLEncoder.encode(text).replace("+", "%20"), "%7E", "~");
return encoded;
}
@@ -3235,7 +3235,7 @@ public class cgBase {
* milliseconds since the epoch
* @return the formatted string
*/
- public String formatTime(long date) {
+ public static String formatTime(long date) {
return DateUtils.formatDateTime(context, date, DateUtils.FORMAT_SHOW_TIME);
}
@@ -3247,7 +3247,7 @@ public class cgBase {
* milliseconds since the epoch
* @return the formatted string
*/
- public String formatDate(long date) {
+ public static String formatDate(long date) {
return DateUtils.formatDateTime(context, date, DateUtils.FORMAT_SHOW_DATE);
}
@@ -3260,7 +3260,7 @@ public class cgBase {
* milliseconds since the epoch
* @return the formatted string
*/
- public String formatFullDate(long date) {
+ public static String formatFullDate(long date) {
return DateUtils.formatDateTime(context, date, DateUtils.FORMAT_SHOW_DATE
| DateUtils.FORMAT_SHOW_YEAR);
}
@@ -3273,7 +3273,7 @@ public class cgBase {
* milliseconds since the epoch
* @return the formatted string
*/
- public String formatShortDate(long date) {
+ public static String formatShortDate(long date) {
return DateUtils.formatDateTime(context, date, DateUtils.FORMAT_SHOW_DATE
| DateUtils.FORMAT_NUMERIC_DATE);
}
diff --git a/main/src/cgeo/geocaching/cgCache.java b/main/src/cgeo/geocaching/cgCache.java
index d41e725..146dfcb 100644
--- a/main/src/cgeo/geocaching/cgCache.java
+++ b/main/src/cgeo/geocaching/cgCache.java
@@ -294,11 +294,11 @@ public class cgCache implements ICache {
return true;
}
- public boolean logOffline(final IAbstractActivity fromActivity, final int logType, final cgBase base) {
+ public boolean logOffline(final IAbstractActivity fromActivity, final int logType) {
String log = "";
if (StringUtils.isNotBlank(Settings.getSignature())
&& Settings.isAutoInsertSignature()) {
- log = LogTemplateProvider.applyTemplates(Settings.getSignature(), base, true);
+ log = LogTemplateProvider.applyTemplates(Settings.getSignature(), true);
}
logOffline(fromActivity, log, Calendar.getInstance(), logType);
return true;
diff --git a/main/src/cgeo/geocaching/cgCacheListAdapter.java b/main/src/cgeo/geocaching/cgCacheListAdapter.java
index 1d2f1b8..0e7e368 100644
--- a/main/src/cgeo/geocaching/cgCacheListAdapter.java
+++ b/main/src/cgeo/geocaching/cgCacheListAdapter.java
@@ -549,9 +549,9 @@ public class cgCacheListAdapter extends ArrayAdapter<cgCache> {
StringBuilder cacheInfo = new StringBuilder(50);
if (historic && cache.getVisitedDate() != null) {
- cacheInfo.append(base.formatTime(cache.getVisitedDate()));
+ cacheInfo.append(cgBase.formatTime(cache.getVisitedDate()));
cacheInfo.append("; ");
- cacheInfo.append(base.formatDate(cache.getVisitedDate()));
+ cacheInfo.append(cgBase.formatDate(cache.getVisitedDate()));
} else {
if (StringUtils.isNotBlank(cache.getGeocode())) {
cacheInfo.append(cache.getGeocode());
@@ -580,7 +580,7 @@ public class cgCacheListAdapter extends ArrayAdapter<cgCache> {
if (cacheInfo.length() > 0) {
cacheInfo.append(SEPARATOR);
}
- cacheInfo.append(base.formatShortDate(cache.getHidden().getTime()));
+ cacheInfo.append(cgBase.formatShortDate(cache.getHidden().getTime()));
}
if (cache.isMembers()) {
if (cacheInfo.length() > 0) {
diff --git a/main/src/cgeo/geocaching/cgeocaches.java b/main/src/cgeo/geocaching/cgeocaches.java
index cbc65d6..30b1758 100644
--- a/main/src/cgeo/geocaching/cgeocaches.java
+++ b/main/src/cgeo/geocaching/cgeocaches.java
@@ -1304,7 +1304,7 @@ public class cgeocaches extends AbstractListActivity {
}
int logType = id - MENU_LOG_VISIT_OFFLINE;
- cache.logOffline(this, logType, base);
+ cache.logOffline(this, logType);
}
return true;
}
@@ -2288,7 +2288,7 @@ public class cgeocaches extends AbstractListActivity {
.append(',')
.append(logTypes.get(log.type))
.append(",\"")
- .append(log.log.replaceAll("\"", "'"))
+ .append(StringUtils.replaceChars(log.log, '"', '\''))
.append("\"\n");
}
}
diff --git a/main/src/cgeo/geocaching/cgeodetail.java b/main/src/cgeo/geocaching/cgeodetail.java
index 135b836..a9663fa 100644
--- a/main/src/cgeo/geocaching/cgeodetail.java
+++ b/main/src/cgeo/geocaching/cgeodetail.java
@@ -653,7 +653,7 @@ public class cgeodetail extends AbstractActivity {
}
int logType = menuItem - MENU_LOG_VISIT_OFFLINE;
- cache.logOffline(this, logType, base);
+ cache.logOffline(this, logType);
return true;
}
@@ -823,7 +823,7 @@ public class cgeodetail extends AbstractActivity {
// cache hidden
if (cache.getHidden() != null && cache.getHidden().getTime() > 0) {
- addCacheDetail(cache.isEventCache() ? R.string.cache_event : R.string.cache_hidden, base.formatFullDate(cache.getHidden().getTime()));
+ addCacheDetail(cache.isEventCache() ? R.string.cache_event : R.string.cache_hidden, cgBase.formatFullDate(cache.getHidden().getTime()));
}
// cache location
@@ -1223,7 +1223,7 @@ public class cgeodetail extends AbstractActivity {
rowView = (RelativeLayout) inflater.inflate(R.layout.log_item, null);
if (log.date > 0) {
- ((TextView) rowView.findViewById(R.id.added)).setText(base.formatShortDate(log.date));
+ ((TextView) rowView.findViewById(R.id.added)).setText(cgBase.formatShortDate(log.date));
} else {
((TextView) rowView.findViewById(R.id.added)).setVisibility(View.GONE);
}
@@ -1969,6 +1969,7 @@ public class cgeodetail extends AbstractActivity {
noAttributeIconsFound = true;
+ final String packageName = cgeoapplication.getInstance().getBaseContext().getPackageName();
for (String attributeName : cache.getAttributes()) {
boolean strikethru = attributeName.endsWith("_no");
// cut off _yes / _no
@@ -1988,8 +1989,7 @@ public class cgeodetail extends AbstractActivity {
// dynamically search icon of the attribute
Drawable d = null;
- int id = res.getIdentifier("attribute_" + attributeName, "drawable",
- base.context.getPackageName());
+ int id = res.getIdentifier("attribute_" + attributeName, "drawable", packageName);
if (id > 0) {
noAttributeIconsFound = false;
d = res.getDrawable(id);
@@ -2029,12 +2029,12 @@ public class cgeodetail extends AbstractActivity {
StringBuilder buffer = new StringBuilder();
String attribute;
+ final String packageName = cgeoapplication.getInstance().getBaseContext().getPackageName();
for (int i = 0; i < cache.getAttributes().size(); i++) {
attribute = cache.getAttributes().get(i);
// dynamically search for a translation of the attribute
- int id = res.getIdentifier("attribute_" + attribute, "string",
- base.context.getPackageName());
+ int id = res.getIdentifier("attribute_" + attribute, "string", packageName);
if (id > 0) {
String translated = res.getString(id);
if (StringUtils.isNotBlank(translated)) {
diff --git a/main/src/cgeo/geocaching/cgeoinit.java b/main/src/cgeo/geocaching/cgeoinit.java
index 2c5b74f..f5da14e 100644
--- a/main/src/cgeo/geocaching/cgeoinit.java
+++ b/main/src/cgeo/geocaching/cgeoinit.java
@@ -593,7 +593,7 @@ public class cgeoinit extends AbstractActivity {
TextView lastBackup = (TextView) findViewById(R.id.backup_last);
File lastBackupFile = cgeoapplication.isRestoreFile();
if (lastBackupFile != null) {
- lastBackup.setText(res.getString(R.string.init_backup_last) + " " + base.formatTime(lastBackupFile.lastModified()) + ", " + base.formatDate(lastBackupFile.lastModified()));
+ lastBackup.setText(res.getString(R.string.init_backup_last) + " " + cgBase.formatTime(lastBackupFile.lastModified()) + ", " + cgBase.formatDate(lastBackupFile.lastModified()));
} else {
lastBackup.setText(res.getString(R.string.init_backup_last_no));
}
diff --git a/main/src/cgeo/geocaching/cgeopopup.java b/main/src/cgeo/geocaching/cgeopopup.java
index a712202..a7d009a 100644
--- a/main/src/cgeo/geocaching/cgeopopup.java
+++ b/main/src/cgeo/geocaching/cgeopopup.java
@@ -190,7 +190,7 @@ public class cgeopopup extends AbstractActivity {
}
int logType = menuItem - MENU_LOG_VISIT_OFFLINE;
- cache.logOffline(this, logType, base);
+ cache.logOffline(this, logType);
return true;
}
diff --git a/main/src/cgeo/geocaching/cgeotouch.java b/main/src/cgeo/geocaching/cgeotouch.java
index 4f642b0..e633730 100644
--- a/main/src/cgeo/geocaching/cgeotouch.java
+++ b/main/src/cgeo/geocaching/cgeotouch.java
@@ -185,8 +185,8 @@ public class cgeotouch extends cgLogForm {
textContent = text.getText().toString();
final long now = System.currentTimeMillis();
- dateString = base.formatDate(now);
- timeString = base.formatTime(now);
+ dateString = cgBase.formatDate(now);
+ timeString = cgBase.formatTime(now);
if ((id & 0x4) == 0x4) {
addText += dateString;
@@ -201,11 +201,10 @@ public class cgeotouch extends cgLogForm {
if (addText.length() > 0) {
addText += "\n";
}
- addText += Settings.getSignature()
- .replaceAll("\\[DATE\\]", dateString)
- .replaceAll("\\[TIME\\]", timeString)
- .replaceAll("\\[USER\\]", Settings.getUsername())
- .replaceAll("\\[NUMBER\\]", "");
+ // number replaced by empty string (there are no numbers for trackables)
+ addText += StringUtils.replaceEach(Settings.getSignature(),
+ new String[] { "[DATE]", "[TIME]", "[USER]", "[NUMBER]" },
+ new String[] { dateString, timeString, Settings.getUsername(), "" });
}
if (textContent.length() > 0 && addText.length() > 0) {
addText = "\n" + addText;
@@ -270,7 +269,7 @@ public class cgeotouch extends cgLogForm {
});
Button dateButton = (Button) findViewById(R.id.date);
- dateButton.setText(base.formatShortDate(date.getTime().getTime()));
+ dateButton.setText(cgBase.formatShortDate(date.getTime().getTime()));
dateButton.setOnClickListener(new cgeotouchDateListener());
if (tweetBox == null) {
@@ -301,7 +300,7 @@ public class cgeotouch extends cgLogForm {
date = dateIn;
final Button dateButton = (Button) findViewById(R.id.date);
- dateButton.setText(base.formatShortDate(date.getTime().getTime()));
+ dateButton.setText(cgBase.formatShortDate(date.getTime().getTime()));
}
public void setType(int type) {
diff --git a/main/src/cgeo/geocaching/cgeotrackable.java b/main/src/cgeo/geocaching/cgeotrackable.java
index 453be7b..e639e77 100644
--- a/main/src/cgeo/geocaching/cgeotrackable.java
+++ b/main/src/cgeo/geocaching/cgeotrackable.java
@@ -207,7 +207,7 @@ public class cgeotrackable extends AbstractActivity {
itemValue = (TextView) itemLayout.findViewById(R.id.value);
itemName.setText(res.getString(R.string.trackable_released));
- itemValue.setText(base.formatDate(trackable.getReleased().getTime()));
+ itemValue.setText(cgBase.formatDate(trackable.getReleased().getTime()));
detailsList.addView(itemLayout);
}
@@ -495,7 +495,7 @@ public class cgeotrackable extends AbstractActivity {
rowView = (RelativeLayout) inflater.inflate(R.layout.trackable_logitem, null);
if (log.date > 0) {
- ((TextView) rowView.findViewById(R.id.added)).setText(base.formatShortDate(log.date));
+ ((TextView) rowView.findViewById(R.id.added)).setText(cgBase.formatShortDate(log.date));
}
if (cgBase.logTypes1.containsKey(log.type)) {