000001002003004005006007008009010011012013014015016017018019020021022023024025026027028029030031032033034035036037038039040041042043044045046047048049050051052053054055056057058059060061062063064065066067068069070071072073074075076077078079080081082083084085086087088089090091092093094095096097098099100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
package br.com.hkp.whatsappwebfix;
import static br.com.hkp.whatsappwebfix.global.Global.EMOJIS_DIRNAME;
import static br.com.hkp.whatsappwebfix.global.Global.FILENAME_DIFF;
import static br.com.hkp.whatsappwebfix.global.Global.PASTA_BASE;
import br.com.hkp.whatsappwebfix.util.FileTools;
import static br.com.hkp.whatsappwebfix.util.FileTools.writeTextFile;
import br.com.hkp.whatsappwebfix.util.Normalizer;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class WhatsAppEditor
{
private static final Pattern ALT_ATTR_PATTERN =
Pattern.compile("alt\\s*=\\s*\".+?\"");
private static final Pattern EMOJI_TAG_PATTERN =
Pattern.compile
(
"<img src=\""
+ PASTA_BASE + "\\/"
+ "d5fceb6532643d0d84ffe09c40c481ecdf59e15a\\.gif.+?>"
);
private String htmlContent;
private static String absolutePath;
private final File inputFile;
private final File outputFile;
private final File reportFile;
private final HashMap<String, String> emojisReport;
public WhatsAppEditor(final File file) throws IOException
{
inputFile = file;
absolutePath = inputFile.getParent() + '/';
String absoluteFileName = absolutePath + inputFile.getName();
htmlContent = FileTools.readTextFile(file);
outputFile =
new File
(
absoluteFileName.replace(".html", FILENAME_DIFF + ".html")
);
reportFile = new File(absoluteFileName.replace(".html",".report"));
emojisReport = new HashMap<>(1000);
}
private String utf8EmojiToFilename(final String utf8Emoji)
{
String codepoints = Normalizer.utf8ToCodepoints(utf8Emoji);
String filename =
PASTA_BASE + '/' + EMOJIS_DIRNAME + '/' + codepoints + ".png";
File test = new File(absolutePath + filename);
if (test.exists())
{
emojisReport.put
(
utf8Emoji,
"Renderizado : " + codepoints
);
}
else
{
filename = filename.replaceAll("-[-a-f0-9]+[.]png", ".png");
test = new File(absolutePath + filename);
if (test.exists())
{
emojisReport.put
(
utf8Emoji,
"Renderizado com emoji raiz : " + codepoints
);
return ("!" + filename);
}
else
{
emojisReport.put
(
utf8Emoji,
"Faltando arquivo PNG : " + codepoints
);
return null;
}
}
return filename;
}
private String getNewTag(final String oldTag)
{
Matcher m = ALT_ATTR_PATTERN.matcher(oldTag);
if (m.find())
{
String utf8Emoji = m.group();
utf8Emoji = utf8Emoji.substring
(
utf8Emoji.indexOf('"') + 1, utf8Emoji.length() - 1
).trim();
String filename = utf8EmojiToFilename(utf8Emoji);
if (filename == null)
{
return "<span>" + utf8Emoji + "</span>";
}
else if (filename.charAt(0) == '!')
{
return
"<img src=\"" + filename.substring(1, filename.length()) +
"\" width=\"20px\" height=\"20px\"" +
"style=\"border: solid 1px red;\">";
}
else
{
return
"<img src=\"" + filename + "\" alt=\"" + utf8Emoji +
"\" width=\"20px\" height=\"20px\">";
}
}
else
return oldTag;
}
private HashMap<String, String> getMap() throws IOException
{
Matcher m = EMOJI_TAG_PATTERN.matcher(htmlContent);
HashMap<String, String> map = new HashMap<>(1000);
while (m.find()) map.put(m.group(), getNewTag(m.group()));
return map;
}
public void createNewFile() throws IOException
{
HashMap<String, String> tagMap = getMap();
for(String oldTag: tagMap.keySet())
htmlContent = htmlContent.replace(oldTag, tagMap.get(oldTag));
writeTextFile(outputFile, htmlContent);
StringBuilder sb = new StringBuilder(emojisReport.size() * 100);
for (String utf8Emoji: emojisReport.keySet())
{
sb.
append(utf8Emoji).
append(" : ").
append(emojisReport.get(utf8Emoji)).
append("\n");
}
writeTextFile(reportFile, sb.toString());
}
}//classe WhatsAppEditor