Skip to content

Instantly share code, notes, and snippets.

@mauricioaniche
Created October 19, 2012 01:10
Show Gist options
  • Save mauricioaniche/3915690 to your computer and use it in GitHub Desktop.
Save mauricioaniche/3915690 to your computer and use it in GitHub Desktop.
Consegue refatorar?
package br.com.caelum.gnarus.apostila;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import br.com.caelum.gnarus.apostila.dominio.Exercicio;
import br.com.caelum.gnarus.apostila.dominio.Secao;
import br.com.caelum.gnarus.apostila.dominio.SecaoConvertida;
public class Conversor {
private StringBuilder convertido;
public Conversor() {
}
public SecaoConvertida converte(Secao s) {
convertido = new StringBuilder();
convertido.append("[chapter " + s.getTitulo() + "]\n\n");
toAfc(s.getId(), s.getTexto());
exercicios(s);
return new SecaoConvertida(s.getNumero(), convertido.toString());
}
private void exercicios(Secao s) {
if(s.getExercicios().isEmpty()) return;
convertido.append("\n\n[section Exercícios]\n\n");
convertido.append("[exercise]\n");
for(Exercicio e : s.getExercicios()) {
convertido.append("[question]\n");
toAfc(s.getId(), e.getEnunciado());
convertido.append("[answer]\n");
toAfc(s.getId(), e.getResposta());
convertido.append("[/answer]\n");
convertido.append("[/question]\n\n");
}
convertido.append("[/exercise]\n");
}
private String toAfc(int secaoId, String textoOriginal) {
String texto = textoOriginal.replace("<b>", "**").replace("</b>", "**");
texto = texto.replace("<br/>", "\n\n").replace("<br>", "\n\n").replace("<br />", "\n\n");
texto = texto.replace("<i>", "::").replace("</i>", "::");
texto = texto.replace("<h1>", "[section ").replace("</h1>", "]");
texto = texto.replace("<h2>", "[section ").replace("</h2>", "]");
texto = texto.replace("<code>", "%%").replace("</code>", "%%");
texto = texto.replace("[java]", "[code java]").replace("[/java]", "[/code]");
texto = texto.replace("[xml]", "[code xml]").replace("[/xml]", "[/code]");
texto = texto.replace("[ruby]", "[code ruby]").replace("[/ruby]", "[/code]");
Pattern pattern = Pattern.compile("<img src=\"([a-zA-Z0-9.:/-]*)\" />");
Matcher matcher = pattern.matcher(texto);
int i = 0;
while(matcher.find()) {
String originalImg = matcher.group(0);
String imgUrl = matcher.group(1);
String imgFileName = "s" + secaoId + "i" + i + "." + extensao(imgUrl);
download(imgUrl, imgFileName);
texto = texto.replace(originalImg, "[img " + imgFileName + " w=60%]");
i++;
}
convertido.append(texto);
return convertido.toString();
}
private String extensao(String imgUrl) {
String[] ponto = imgUrl.split("\\.");
return ponto[ponto.length-1];
}
private void download(String imageUrl, String destinationFile) {
try {
URL url = new URL(imageUrl);
InputStream is = url.openStream();
OutputStream os = new FileOutputStream(destinationFile);
byte[] b = new byte[2048];
int length;
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
is.close();
os.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment