mirror of
https://github.com/leanote/leanote-android.git
synced 2025-10-17 16:07:48 +00:00
fixed time parse error
This commit is contained in:
@@ -25,8 +25,6 @@ import java.util.HashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
import okhttp3.MediaType;
|
import okhttp3.MediaType;
|
||||||
import okhttp3.MultipartBody;
|
import okhttp3.MultipartBody;
|
||||||
@@ -152,57 +150,11 @@ public class NoteService {
|
|||||||
AppDataBase.deleteFileExcept(noteLocalId, excepts);
|
AppDataBase.deleteFileExcept(noteLocalId, excepts);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String replace(String content, String tagExp, String targetExp, Replacer replacer, Object... extraData) {
|
|
||||||
Pattern tagPattern = Pattern.compile(tagExp);
|
|
||||||
Pattern targetPattern = Pattern.compile(targetExp);
|
|
||||||
Matcher tagMather = tagPattern.matcher(content);
|
|
||||||
StringBuilder contentBuilder = new StringBuilder(content);
|
|
||||||
int offset = 0;
|
|
||||||
while (tagMather.find()) {
|
|
||||||
String tag = tagMather.group();
|
|
||||||
Matcher targetMatcher = targetPattern.matcher(tag);
|
|
||||||
if (!targetMatcher.find()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
String original = targetMatcher.group();
|
|
||||||
int originalLen = original.length();
|
|
||||||
String modified = replacer.replaceWith(original, extraData);
|
|
||||||
contentBuilder.replace(tagMather.start() + targetMatcher.start() + offset,
|
|
||||||
tagMather.end() - (tag.length() - targetMatcher.end()) + offset,
|
|
||||||
modified);
|
|
||||||
offset += modified.length() - originalLen;
|
|
||||||
}
|
|
||||||
return contentBuilder.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void find(String content, String tagExp, String targetExp, Finder finder, Object... extraData) {
|
|
||||||
Pattern tagPattern = Pattern.compile(tagExp);
|
|
||||||
Pattern targetPattern = Pattern.compile(targetExp);
|
|
||||||
Matcher tagMather = tagPattern.matcher(content);
|
|
||||||
while (tagMather.find()) {
|
|
||||||
String tag = tagMather.group();
|
|
||||||
Matcher targetMatcher = targetPattern.matcher(tag);
|
|
||||||
if (!targetMatcher.find()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
String original = targetMatcher.group();
|
|
||||||
finder.onFound(original);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public interface Finder {
|
|
||||||
void onFound(String original, Object... extraData);
|
|
||||||
}
|
|
||||||
|
|
||||||
public interface Replacer {
|
|
||||||
String replaceWith(String original, Object... extraData);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String convertToLocalImageLinkForRichText(long noteLocalId, String noteContent) {
|
private static String convertToLocalImageLinkForRichText(long noteLocalId, String noteContent) {
|
||||||
return replace(noteContent,
|
return StringUtils.replace(noteContent,
|
||||||
"<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>",
|
"<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>",
|
||||||
String.format(Locale.US, "\\ssrc\\s*=\\s*\"%s/api/file/getImage\\?fileId=.*?\"", AccountService.getCurrent().getHost()),
|
String.format(Locale.US, "\\ssrc\\s*=\\s*\"%s/api/file/getImage\\?fileId=.*?\"", AccountService.getCurrent().getHost()),
|
||||||
new Replacer() {
|
new StringUtils.Replacer() {
|
||||||
@Override
|
@Override
|
||||||
public String replaceWith(String original, Object... extraData) {
|
public String replaceWith(String original, Object... extraData) {
|
||||||
Log.i(TAG, "in=" + original);
|
Log.i(TAG, "in=" + original);
|
||||||
@@ -225,10 +177,10 @@ public class NoteService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static String convertToLocalImageLinkForMD(long noteLocalId, String noteContent) {
|
private static String convertToLocalImageLinkForMD(long noteLocalId, String noteContent) {
|
||||||
return replace(noteContent,
|
return StringUtils.replace(noteContent,
|
||||||
String.format(Locale.US, "!\\[.*?\\]\\(%s/api/file/getImage\\?fileId=.*?\\)", AccountService.getCurrent().getHost()),
|
String.format(Locale.US, "!\\[.*?\\]\\(%s/api/file/getImage\\?fileId=.*?\\)", AccountService.getCurrent().getHost()),
|
||||||
String.format(Locale.US, "\\(%s/api/file/getImage\\?fileId=.*?\\)", AccountService.getCurrent().getHost()),
|
String.format(Locale.US, "\\(%s/api/file/getImage\\?fileId=.*?\\)", AccountService.getCurrent().getHost()),
|
||||||
new Replacer() {
|
new StringUtils.Replacer() {
|
||||||
@Override
|
@Override
|
||||||
public String replaceWith(String original, Object... extraData) {
|
public String replaceWith(String original, Object... extraData) {
|
||||||
Uri linkUri = Uri.parse(original.substring(1, original.length() - 1));
|
Uri linkUri = Uri.parse(original.substring(1, original.length() - 1));
|
||||||
@@ -276,10 +228,10 @@ public class NoteService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static String convertToServerImageLinkForMD(String noteContent) {
|
private static String convertToServerImageLinkForMD(String noteContent) {
|
||||||
return replace(noteContent,
|
return StringUtils.replace(noteContent,
|
||||||
"!\\[.*?\\]\\(file:/getImage\\?id=.*?\\)",
|
"!\\[.*?\\]\\(file:/getImage\\?id=.*?\\)",
|
||||||
"\\(file:/getImage\\?id=.*?\\)",
|
"\\(file:/getImage\\?id=.*?\\)",
|
||||||
new Replacer() {
|
new StringUtils.Replacer() {
|
||||||
@Override
|
@Override
|
||||||
public String replaceWith(String original, Object... extraData) {
|
public String replaceWith(String original, Object... extraData) {
|
||||||
Uri linkUri = Uri.parse(original.substring(1, original.length() - 1));
|
Uri linkUri = Uri.parse(original.substring(1, original.length() - 1));
|
||||||
@@ -294,10 +246,10 @@ public class NoteService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static String convertToServerImageLinkForRichText(String noteContent) {
|
private static String convertToServerImageLinkForRichText(String noteContent) {
|
||||||
return replace(noteContent,
|
return StringUtils.replace(noteContent,
|
||||||
"<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>",
|
"<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>",
|
||||||
"\\ssrc\\s*=\\s*\"file:/getImage\\?id=.*?\"",
|
"\\ssrc\\s*=\\s*\"file:/getImage\\?id=.*?\"",
|
||||||
new Replacer() {
|
new StringUtils.Replacer() {
|
||||||
@Override
|
@Override
|
||||||
public String replaceWith(String original, Object... extraData) {
|
public String replaceWith(String original, Object... extraData) {
|
||||||
Uri linkUri = Uri.parse(original.substring(6, original.length() - 1));
|
Uri linkUri = Uri.parse(original.substring(6, original.length() - 1));
|
||||||
@@ -361,9 +313,8 @@ public class NoteService {
|
|||||||
requestBodyMap.put("Content", createPartFromString(content));
|
requestBodyMap.put("Content", createPartFromString(content));
|
||||||
requestBodyMap.put("IsMarkdown", createPartFromString(getBooleanString(note.isMarkDown())));
|
requestBodyMap.put("IsMarkdown", createPartFromString(getBooleanString(note.isMarkDown())));
|
||||||
requestBodyMap.put("IsBlog", createPartFromString(getBooleanString(note.isPublicBlog())));
|
requestBodyMap.put("IsBlog", createPartFromString(getBooleanString(note.isPublicBlog())));
|
||||||
long current = System.currentTimeMillis();
|
requestBodyMap.put("CreatedTime", createPartFromString(TimeUtils.toServerTime(note.getCreatedTimeVal())));
|
||||||
requestBodyMap.put("CreatedTime", createPartFromString(TimeUtils.toServerTime(current)));
|
requestBodyMap.put("UpdatedTime", createPartFromString(TimeUtils.toServerTime(note.getUpdatedTimeVal())));
|
||||||
requestBodyMap.put("UpdatedTime", createPartFromString(TimeUtils.toServerTime(current)));
|
|
||||||
|
|
||||||
List<String> imageLocalIds;
|
List<String> imageLocalIds;
|
||||||
if (note.isMarkDown()) {
|
if (note.isMarkDown()) {
|
||||||
@@ -392,10 +343,10 @@ public class NoteService {
|
|||||||
|
|
||||||
private static List<String> getImagesFromContentForRichText(String noteContent) {
|
private static List<String> getImagesFromContentForRichText(String noteContent) {
|
||||||
final List<String> localIds = new ArrayList<>();
|
final List<String> localIds = new ArrayList<>();
|
||||||
find(noteContent,
|
StringUtils.find(noteContent,
|
||||||
"<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>",
|
"<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>",
|
||||||
"\\ssrc\\s*=\\s*\"file:/getImage\\?id=.*?\"",
|
"\\ssrc\\s*=\\s*\"file:/getImage\\?id=.*?\"",
|
||||||
new Finder() {
|
new StringUtils.Finder() {
|
||||||
@Override
|
@Override
|
||||||
public void onFound(String original, Object... extraData) {
|
public void onFound(String original, Object... extraData) {
|
||||||
Uri linkUri = Uri.parse(original.substring(6, original.length() - 1));
|
Uri linkUri = Uri.parse(original.substring(6, original.length() - 1));
|
||||||
@@ -408,10 +359,10 @@ public class NoteService {
|
|||||||
|
|
||||||
private static List<String> getImagesFromContentForMD(String noteContent) {
|
private static List<String> getImagesFromContentForMD(String noteContent) {
|
||||||
final List<String> localIds = new ArrayList<>();
|
final List<String> localIds = new ArrayList<>();
|
||||||
find(noteContent,
|
StringUtils.find(noteContent,
|
||||||
"!\\[.*?\\]\\(file:/getImage\\?id=.*?\\)",
|
"!\\[.*?\\]\\(file:/getImage\\?id=.*?\\)",
|
||||||
"\\(file:/getImage\\?id=.*?\\)",
|
"\\(file:/getImage\\?id=.*?\\)",
|
||||||
new Finder() {
|
new StringUtils.Finder() {
|
||||||
@Override
|
@Override
|
||||||
public void onFound(String original, Object... extraData) {
|
public void onFound(String original, Object... extraData) {
|
||||||
Uri linkUri = Uri.parse(original.substring(1, original.length() - 1));
|
Uri linkUri = Uri.parse(original.substring(1, original.length() - 1));
|
||||||
|
@@ -1,8 +1,57 @@
|
|||||||
package org.houxg.leanotelite.utils;
|
package org.houxg.leanotelite.utils;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class StringUtils {
|
public class StringUtils {
|
||||||
public static String notNullStr(String str) {
|
public static String notNullStr(String str) {
|
||||||
return str == null ? "" : str;
|
return str == null ? "" : str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void find(String content, String tagExp, String targetExp, Finder finder, Object... extraData) {
|
||||||
|
Pattern tagPattern = Pattern.compile(tagExp);
|
||||||
|
Pattern targetPattern = Pattern.compile(targetExp);
|
||||||
|
Matcher tagMather = tagPattern.matcher(content);
|
||||||
|
while (tagMather.find()) {
|
||||||
|
String tag = tagMather.group();
|
||||||
|
Matcher targetMatcher = targetPattern.matcher(tag);
|
||||||
|
if (!targetMatcher.find()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String original = targetMatcher.group();
|
||||||
|
finder.onFound(original);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String replace(String content, String tagExp, String targetExp, Replacer replacer, Object... extraData) {
|
||||||
|
Pattern tagPattern = Pattern.compile(tagExp);
|
||||||
|
Pattern targetPattern = Pattern.compile(targetExp);
|
||||||
|
Matcher tagMather = tagPattern.matcher(content);
|
||||||
|
StringBuilder contentBuilder = new StringBuilder(content);
|
||||||
|
int offset = 0;
|
||||||
|
while (tagMather.find()) {
|
||||||
|
String tag = tagMather.group();
|
||||||
|
Matcher targetMatcher = targetPattern.matcher(tag);
|
||||||
|
if (!targetMatcher.find()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String original = targetMatcher.group();
|
||||||
|
int originalLen = original.length();
|
||||||
|
String modified = replacer.replaceWith(original, extraData);
|
||||||
|
contentBuilder.replace(tagMather.start() + targetMatcher.start() + offset,
|
||||||
|
tagMather.end() - (tag.length() - targetMatcher.end()) + offset,
|
||||||
|
modified);
|
||||||
|
offset += modified.length() - originalLen;
|
||||||
|
}
|
||||||
|
return contentBuilder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface Finder {
|
||||||
|
void onFound(String original, Object... extraData);
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface Replacer {
|
||||||
|
String replaceWith(String original, Object... extraData);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -9,14 +9,29 @@ import java.util.Locale;
|
|||||||
|
|
||||||
public class TimeUtils {
|
public class TimeUtils {
|
||||||
|
|
||||||
private static final SimpleDateFormat mServerWithMillsFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX", Locale.US);
|
private static final SimpleDateFormat mServerWithMillsFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.US);
|
||||||
private static final SimpleDateFormat mServerFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX", Locale.US);
|
private static final SimpleDateFormat mServerFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX", Locale.US);
|
||||||
private static final SimpleDateFormat mTimeFormat = new SimpleDateFormat("H:mm:ss", Locale.US);
|
private static final SimpleDateFormat mTimeFormat = new SimpleDateFormat("H:mm:ss", Locale.US);
|
||||||
private static final SimpleDateFormat mDateFormat = new SimpleDateFormat("M-dd H:mm:ss", Locale.US);
|
private static final SimpleDateFormat mDateFormat = new SimpleDateFormat("M-dd H:mm:ss", Locale.US);
|
||||||
private static final SimpleDateFormat mYearFormat = new SimpleDateFormat("yyyy-M-dd H:mm:ss", Locale.US);
|
private static final SimpleDateFormat mYearFormat = new SimpleDateFormat("yyyy-M-dd H:mm:ss", Locale.US);
|
||||||
|
|
||||||
public static long toTimestamp(String serverTime) {
|
public static long toTimestamp(String serverTime) {
|
||||||
try {
|
try {
|
||||||
|
serverTime = StringUtils.replace(serverTime,
|
||||||
|
"T\\d{2}:\\d{2}:\\d{2}.\\d+\\+",
|
||||||
|
"\\.\\d+",
|
||||||
|
new StringUtils.Replacer() {
|
||||||
|
@Override
|
||||||
|
public String replaceWith(String original, Object... extraData) {
|
||||||
|
String modified;
|
||||||
|
if (original.length() > 4) {
|
||||||
|
modified = original.substring(0, 4);
|
||||||
|
} else {
|
||||||
|
modified = original;
|
||||||
|
}
|
||||||
|
return modified;
|
||||||
|
}
|
||||||
|
});
|
||||||
Date date = mServerWithMillsFormat.parse(serverTime);
|
Date date = mServerWithMillsFormat.parse(serverTime);
|
||||||
return date.getTime();
|
return date.getTime();
|
||||||
} catch (ParseException e) {
|
} catch (ParseException e) {
|
||||||
|
Reference in New Issue
Block a user