45 lines
1.7 KiB
Java
45 lines
1.7 KiB
Java
package yg;
|
||
|
||
import java.io.File;
|
||
import java.io.IOException;
|
||
import java.util.Arrays;
|
||
import java.util.List;
|
||
import java.util.regex.Matcher;
|
||
import java.util.regex.Pattern;
|
||
import java.util.stream.Collectors;
|
||
|
||
import com.fasterxml.jackson.core.JsonParseException;
|
||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||
|
||
public class Smses2Json {
|
||
private static final ObjectMapper JACKSON = new ObjectMapper();
|
||
private static final Pattern XML_ENTITY_PATTERN = Pattern.compile("&#(\\d+);");
|
||
|
||
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
|
||
List<XmlMessage> messages = Arrays.asList(JACKSON.readValue(new File(args[0]), XmlMessage[].class));
|
||
|
||
Backup backup = new Backup();
|
||
backup.setMessages(messages
|
||
.stream()
|
||
.map(x -> new Message(
|
||
x.getProtocol(), x.getAddress(), x.getDate(),
|
||
x.getType() == 1 ? x.getDate() : 0, // a date for an SMS received; 0 for an SMS sent
|
||
x.getType(), x.getBody(), x.getRead() == 1, x.getStatus(), x.getLocked() == 1,
|
||
x.getType() == 1 ? 1 : -1)) // 1 for an SMS received; −1 for an SMS sent
|
||
.peek(msg -> {
|
||
Matcher matcher = XML_ENTITY_PATTERN.matcher(msg.getBody());
|
||
msg.setBody(matcher.replaceAll(match -> {
|
||
return new StringBuilder().appendCodePoint(Integer.valueOf(match.group(1))).toString();
|
||
}));
|
||
})
|
||
.collect(Collectors.toList()));
|
||
backup.setMessageCount(backup.getMessages().size());
|
||
|
||
if (args.length == 1) {
|
||
JACKSON.writeValue(System.out, backup);
|
||
} else {
|
||
JACKSON.writeValue(new File(args[1]), backup);
|
||
}
|
||
}
|
||
} |