package de.tvo.tools;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Schneidele
* @version 1.0
*/
public class HtmlUtil {
public HtmlUtil() {
}
public static void main(String[] args) {
}
/**
* @author Schneidele
* @param source String zum umwandeln
* @return Gibt umgewandelten String mit HTML Tags als Liste zurück.
* @see #createListWithBold(String) Liste mit Fettschrift
*/
public static String createList(String source) {
return source.replaceFirst("-", "
") + "";
}
/**
* @author Schneidele
* @param source String zum umwandeln
* @return Gibt umgewandelten String mit HTML Tags als Liste und Fettschrift zurück.
* @see #createList(String) Liste ohne Fettschrift
* @see #createBoldText(String) Paragraph mit Fettschrift
*/
public static String createListWithBold(String source) {
if (source.contains(" **")) {
return source.replaceFirst("-", "").replace(" **", " ").replace("** ", " ") + "";
} else {
final String regex = "\\*\\*(.*)\\*\\*";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(source);
if (matcher.find()) {
return source.replaceFirst("-", "").replace(matcher.group(0), "" + matcher.group(1) + "") + "";
} else {
return source.replaceFirst("-", "") + "";
}
}
}
/**
* @author Schneidele
* @param source String zum umwandeln
* @return Gibt umgewandelten String mit HTML Tags als Paragraph zurück.
*/
public static String createParagraph(String source) {
return "" + source + "
";
}
/**
* @author Schneidele
* @param source String zum umwandeln
* @return Gibt umgewandelten String mit HTML Tags als Header zurück.
* "#" --> "h1"
* "##" --> "h2"
* "###" --> "h3"
* "####" --> "h4"
* "#####" --> "h5"
* "######" --> "h6"
*/
public static String createHeader(String source) {
int hashCounter = 0;
for (int i = 0; i < source.length(); i++) {
if (source.substring(i, i+1).equals("#")) {
hashCounter++;
}
}
if (hashCounter == 1) {
return source.replaceFirst("#", "") + "
";
} else if (hashCounter == 2) {
return source.replaceFirst("##", "") + "
";
} else if (hashCounter == 3) {
return source.replaceFirst("###", "") + "
";
} else if (hashCounter == 4) {
return source.replaceFirst("####", "") + "
";
} else if (hashCounter == 5) {
return source.replaceFirst("#####", "") + "
";
} else {
return source.replaceFirst("######", "") + "
";
}
}
/**
* @author Schneidele
* @param source String zum umwandeln
* @return Gibt eine "Breakline" "_______" über die gesamte Seite zurück.
*/
public static String createBreakLine(String source) {
return source.replace("___", "
");
}
/**
* @author Schneidele
* @param source String zum umwandeln
* @return Gibt umgewandelten String mit HTML Tags als Fettschrift zurück.
*/
public static String createBoldText(String source) {
if (source.contains(" **")) {
return source.replace(" **", " ").replace("** ", " ");
} else {
final String regex = "\\*\\*(.*)\\*\\*";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(source);
if (matcher.find()) {
return source.replace(matcher.group(0), "" + matcher.group(1) + "");
} else {
return source;
}
}
}
/**
* @author Schneidele
* @param source String zum umwandeln
* @return Gibt umgewandelten String mit HTML Tags als Bild zurück.
*/
public static String createImage(String source) {
final String regex = ".*\\((.*)\\).*";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(source);
if (matcher.find()) {
return "
";
} else {
return " [!ERROR!] Bild konnte nicht geladen werden
";
}
}
}