jscl.converter.Converter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of txt2xhtml Show documentation
Show all versions of txt2xhtml Show documentation
java symbolic computing library and mathematical editor
The newest version!
package jscl.converter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Converter {
public final Pattern pattern = Pattern.compile("(?s:)|(?s:)");
protected final String XML = "";
private final Pattern word = Pattern.compile("[\\w_\\-\\.]+");
private final Pattern http = Pattern.compile("https?://[\\w_\\-\\.:/~\\?=]*");
private final Pattern txt = Pattern.compile("(" + word.pattern() + ")(?:/(" + word.pattern() + "))*\\.txt");
private final Pattern mvn = Pattern.compile("(" + word.pattern() + ")#(" + word.pattern() + ");(" + word.pattern() + ")");
private final Pattern mail = Pattern.compile(word.pattern() + "@" + word.pattern());
private final Pattern links = Pattern.compile("(" + http.pattern() + ")|(" + txt.pattern() + ")|(" + mvn.pattern() + ")|(" + mail.pattern() + ")");
private final Pattern newlines = Pattern.compile("\n");
private final Pattern spaces = Pattern.compile("(?m:^ +)|( +)|(\\t)");
private final String mml = " xmlns=\"http://www.w3.org/1998/Math/MathML\"";
private final String svg = " xmlns=\"http://www.w3.org/2000/svg\"";
public String apply(final Reader reader, final String stylesheet, final String title, final String feed, final String icon, final String url, final boolean extension) throws IOException {
final String str = stringFromReader(reader);
final Matcher pm = pattern.matcher(str);
final StringBuffer b = new StringBuffer(XML);
if (stylesheet != null && !stylesheet.isEmpty()) {
b.append("");
}
b.append("\n");
b.append("\n");
if (title != null && !title.isEmpty()) {
b.append("").append(title).append(" \n");
}
if (feed != null && !feed.isEmpty()) {
b.append("\n");
}
if (icon != null && !icon.isEmpty()) {
b.append("\n");
}
b.append("\n");
b.append("\n");
b.append("\n");
int n = 0;
while (pm.find()) {
final int m = pm.start();
final String s = pm.group();
b.append(links(str.substring(n, m), extension));
b.append(insertNameSpace(s));
n = pm.end();
}
b.append(links(str.substring(n), extension));
b.append("\n");
if (url != null) {
b.append("
").append("source\n");
}
b.append("\n");
b.append("\n");
return b.toString();
}
private String links(final String str, final boolean extension) {
final StringBuffer buffer = new StringBuffer();
final Matcher pm = links.matcher(str);
int n = 0;
while (pm.find()) {
final int m = pm.start();
final String s = pm.group();
final Matcher pm0 = txt.matcher(s);
final Matcher pm1 = mvn.matcher(s);
final Matcher pm2 = mail.matcher(s);
buffer.append(newlines(str.substring(n, m)));
if (pm0.matches()) {
buffer.append("");
if (ss != null) {
if (ss.endsWith("/index.txt")) {
buffer.append(ss.substring(0, ss.lastIndexOf("/index.txt")));
} else {
buffer.append(ss.substring(0, ss.lastIndexOf(".txt")));
}
}
buffer.append("");
} else if (pm1.matches()) {
buffer.append(" 1) {
buffer.append("/");
}
buffer.append(ss);
}
buffer.append("\">");
for (int i = 1 ; i <= pm1.groupCount() ; i++) {
final String ss = pm1.group(i);
if (i > 1) {
buffer.append(i == 2?"#":";");
}
buffer.append(ss);
}
buffer.append("");
} else if (pm2.matches()) {
final String ss = pm2.group();
buffer.append("");
buffer.append(ss);
buffer.append("");
} else {
final String ss = special(s);
buffer.append("");
buffer.append(ss);
buffer.append("");
}
n = pm.end();
}
buffer.append(newlines(str.substring(n)));
return buffer.toString();
}
private String newlines(final String str) {
final StringBuffer buffer = new StringBuffer();
final Matcher pm = newlines.matcher(str);
int n = 0;
while (pm.find()) {
final int m = pm.start();
final String s = pm.group();
final String t = spaces(str.substring(n, m));
buffer.append(t.matches("-+")?"
":t + "
").append("\n");
n = pm.end();
}
buffer.append(spaces(str.substring(n)));
return buffer.toString();
}
private String spaces(final String str) {
final StringBuffer buffer = new StringBuffer();
final Matcher pm = spaces.matcher(str);
int n = 0;
while (pm.find()) {
final int m = pm.start();
final String s = pm.group();
buffer.append(special(str.substring(n, m)));
for (int i = 0; i < s.length(); i++) {
buffer.append(s.charAt(i) == '\t'?"\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0":"\u00a0");
}
n = pm.end();
}
buffer.append(special(str.substring(n)));
return buffer.toString();
}
private String special(final String str) {
return str.replaceAll("&","&").replaceAll("<","<").replaceAll(">",">").replaceAll("\"",""");
}
protected String insertNameSpace(final String str) {
return isSvg(str)?str.indexOf(svg) < 0?str.substring(0, 4) + svg + str.substring(4):str:str.indexOf(mml) < 0?str.substring(0, 5) + mml + str.substring(5):str;
}
protected String stripNameSpace(final String str) {
final int n = str.indexOf(mml);
return str.substring(0, n) + str.substring(n + mml.length());
}
protected boolean isSvg(final String str) {
return str.substring(1, 4).compareTo("svg") == 0;
}
private String stringFromReader(final Reader reader) throws IOException {
try (final Writer writer = new StringWriter()) {
pipe(reader, writer);
return writer.toString();
}
}
private char buffer[] = new char[8192];
protected void pipe(final Reader in, final Writer out) throws IOException {
int n = in.read(buffer);
while (n > -1) {
out.write(buffer, 0, n);
n = in.read(buffer);
}
}
public void apply(final Reader reader, final String stylesheet, final String title, final String feed, final String icon, final String url, final boolean extension, final Writer writer) throws IOException {
pipe(new StringReader(apply(reader, stylesheet, title, feed, icon, url, extension)), writer);
}
}