silence-xml-to-qksms-json/src/main/java/yg/Smses2Json.java

45 lines
1.7 KiB
Java
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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);
}
}
}