();
WordWithParallelCombiningHintsTokenizer tokenizer = new WordWithParallelCombiningHintsTokenizer();
boolean stripOmitted = false;
boolean stripLacuna = false;
// "text" is for legacy purposes; remove when sure it is not used
if ("apphtml".equals(format) || "text".equals(format) || "apptext".equals(format) || "atable".equals(format) || "csv".equals(format)) {
stripOmitted = true;
stripLacuna = true;
}
if ("graph".equals(format)) {
/*
stripOmitted = true;
stripLacuna = true;
*/
}
for (WitnessReading wr : witnessReadings) {
if (primaryLang == null && wr.getDocumentID() > 0) {
Document d = Document.getDocument(wr.getDocumentID());
primaryLang = d.getLanguage();
}
if ("(([ Omitted ]))".equals(wr.getText())) {
omittedWitnesses.add(wr.getLabel());
if (stripOmitted) continue;
}
else if ("[lac]".equals(wr.getText())) {
lacunaWitnesses.add(wr.getLabel());
if (stripLacuna) continue;
}
else if (givenBaseText || !wr.getLabel().equals(params.baseText)) presentWitnesses.add(wr.getLabel());
witnesses.add(new SimpleWitness(wr.getLabel(), wr.getText(), tokenizer, SimpleTokenNormalizers.LC_TRIM_WS));
}
String algorithm = (requestJSON != null) ? requestJSON.getString("algorithm") : params.algorithm;
CollationAlgorithm collationAlgorithm = null;
if ("medite".equals(algorithm)) {
collationAlgorithm = CollationAlgorithmFactory.medite(new EqualityTokenComparator(), SimpleToken.TOKEN_MATCH_EVALUATOR);
} else if ("needleman-wunsch".equals(algorithm)) {
collationAlgorithm = CollationAlgorithmFactory.needlemanWunsch(new EqualityTokenComparator());
} else {
collationAlgorithm = CollationAlgorithmFactory.dekker(new EqualityTokenComparator());
}
boolean joined = false;
graph = new SimpleCollation(witnesses, collationAlgorithm, joined).collate(new JungVariantGraph());
/*
// from old method, already done in the SimpleCollation class above
//collationAlgorithm.collate(graph, witnesses.toArray(new SimpleWitness[0]));
for (String wl : omittedWitnesses) {
graph.register(new SimpleWitness(wl, ""));
}
*/
if ("graphml".equals(format)) {
response.setContentType("text/xml");
StringWriter writer = new StringWriter();
XMLStreamWriter swriter = javax.xml.stream.XMLOutputFactory.newFactory().createXMLStreamWriter(writer);
SimpleVariantGraphSerializer serializer = new SimpleVariantGraphSerializer(graph);
serializer.toGraphML(swriter);
%><%=writer.toString()%><%
}
else if ("graph".equals(format)) {
response.setContentType("text/xml");
SimpleVariantGraphSerializer serializer = new SimpleVariantGraphSerializer(graph);
serializer.toSVG(out);
}
else if ("dot".equals(format)) {
response.setContentType("text/plain");
SimpleVariantGraphSerializer serializer = new SimpleVariantGraphSerializer(graph);
serializer.toDot(out);
}
// "text" is for legacy purposes; remove when sure it is not used anymore
else if ("apptext".equals(format) || "text".equals(format)) {
response.setContentType("text/plain");
SimpleVariantGraphSerializer serializer = new SimpleVariantGraphSerializer(graph);
out.write("Not Present in " + SimpleVariantGraphSerializer.toWitnessesLabel(lacunaWitnesses, true)+"\n\n");
out.write("Present in " + SimpleVariantGraphSerializer.toWitnessesLabel(presentWitnesses, true)+"\n\n");
try {
serializer.toPlainTextApparatus(out, params.baseText);
}
catch (Exception e) {
params.getLogger().error("ERROR in apptext display: ");
e.printStackTrace();
}
}
else if ("apphtml".equals(format)) {
response.setContentType("text/html");
SimpleVariantGraphSerializer serializer = new SimpleVariantGraphSerializer(graph);
out.write("Not Present in " + SimpleVariantGraphSerializer.toWitnessesLabelHTML(lacunaWitnesses, true)+"
");
out.write("Present in " + SimpleVariantGraphSerializer.toWitnessesLabelHTML(presentWitnesses, true)+"
");
out.write("");
try {
serializer.toHTMLApparatus(out, params.baseText);
}
catch (Exception e) {
params.getLogger().error("ERROR in apphtml display: ");
e.printStackTrace();
}
out.write("
");
}
else if ("atable".equals(format) || "csv".equals(format)) {
boolean csv = "csv".equals(format);
table = VariantGraphRanking.of(graph).asTable();
if (!csv) {
%>
<%
}
else {
response.setContentType("text/csv");
response.setHeader("Content-Disposition", "attachment; filename=collation"+(params.indexContent != null ? ("-"+params.indexContent) : "")+".csv");
out.print("\uFEFF");
}
int row = 0;
for (Witness witness : table.columnKeySet()) {
++row;
String sigil = witness.getSigil().trim();
sigil.replace("\n", "");
if (!csv) {
%> <%= sigil %> | <%
}
else {
out.print("\""+sigil+"\"");
}
int col = 0;
for (Integer wordNum : table.rowKeySet()) {
++col;
if (!csv) {
%> <%
}
else {
out.print(",\"");
}
Set tokens = table.get(wordNum, witness);
if (tokens != null) {
for (Token token : tokens) {
out.print(((SimpleToken)token).getContent().trim());
}
}
if (!csv) {
%> | <%
}
else {
out.print("\"");
}
}
if (!csv) out.print("
");
else out.print("\n");
}
if (!csv) {
%>
<%
}
}
else if ("tei".equals(format)) {
response.setContentType("text/xml");
StringWriter writer = new StringWriter();
XMLStreamWriter swriter = XMLOutputFactory.newFactory().createXMLStreamWriter(writer);
SimpleVariantGraphSerializer serializer = new SimpleVariantGraphSerializer(graph);
serializer.toTEI(swriter);
%>
<%=writer.toString()%>
<%
}
// This is stuff Bham wants as input to their Apparatus editing tool
else if ("json".equals(format)) {
params.getLogger().debug("*** Collate returning JSON ***");
response.setContentType("application/json");
table = VariantGraphRanking.of(graph).asTable();
StringBuffer apparatus = new StringBuffer();
// "[
// {
// start:0,
// end:0,
// readings:[
// {
// witnesses:['w1', 'w2'],
// text:[
// {
// index:0,
// t:agaph,
// reading:['w1', 'w2'],
// manuscipts:[20001, 20001]
// },{...word2...}
// ]
// },
// {... reading 2 ... }
// ]
// }
// ]"
//
apparatus.append("[");
HashMap>> app = new HashMap>>();
int segment = 2;
Vector aTextReadings = new Vector();
for (Integer wordNum : table.rowKeySet()) {
apparatus.append("{");
apparatus.append("\"start\":"+segment+",");
apparatus.append("\"end\":"+segment+",");
final HashMap> readings = new HashMap>();
for (Witness witness : table.columnKeySet()) {
Set tokens = table.get(wordNum, witness);
String reading = "|lac|";
if (tokens != null) {
reading = "";
for (Token token : tokens) {
reading += ((SimpleToken)token).getContent();
}
}
Vector wits = readings.get(reading);
if (wits == null) {
readings.put(reading, new Vector());
wits = readings.get(reading);
}
wits.add(witness.getSigil());
}
final Vector rdngs = new Vector();
rdngs.addAll(readings.keySet());
Collections.sort(rdngs, new Comparator() {
public int compare(String o1, String o2) {
return readings.get(o2).size() - readings.get(o1).size();
}
});
apparatus.append("\"readings\":[");
for (String read : rdngs) {
Vector wits = readings.get(read);
apparatus.append("{ \"witnesses\":");
String ws = "[";
boolean first = true;
for (String w: wits) {
if (!first) ws +=", ";
first = false;
ws+= ("\""+w+"\"");
}
ws += "]";
apparatus.append(ws+",\"text\":["+(!"|lac|".equals(read)?("{\"index\":"+segment+", \"t\":\""+read+"\",\"reading\":"+ws+", \"manuscript\":"+ws+"}"):"")+"]");
apparatus.append("},");
}
apparatus.append("]");
apparatus.append("},");
String r = rdngs.get(0);
if ("|lac|".equals(r)) r = "";
aTextReadings.add(r);
app.put(segment, readings);
segment+=2;
}
apparatus.append("]");
String appts = apparatus.toString().replaceAll(",]","]");
appts = appts.replaceAll(",}","}");
params.getLogger().debug("*** Collate returning: " + appts);
out.print("{");
out.print("\"apparatus\":"+appts+",");
out.print("\"overtext\":[{\"tokens\":[");
int seg = 2;
String overtext = "";
for (String aText : aTextReadings) {
overtext += "{\"index\":"+seg+", \"t\":\""+aText+"\", \"reading\":\"aText\",\"manuscript\":\"aText\"},";
seg+=2;
}
overtext += "]}]}";
overtext = overtext.replaceAll(",]","]");
overtext = overtext.replaceAll(",}","}");
out.print(overtext);
}
else {
usage = true;
}
}
else {
if (params.format == null) usage = true;
}
if (!usage) return;
else ((Parameters)params).format = "html";
}
else ((Parameters)params).format = "html";
Serializer.reportErrors(request, response, out, params, true);
%>
<%!
static HashMap specialFonts = new HashMap();
static HashSet rightJustify = new HashSet();
static {
specialFonts.put("bo", "Antinoou, AntinoouWeb");
specialFonts.put("sa", "Antinoou, AntinoouWeb");
specialFonts.put("fa", "Antinoou, AntinoouWeb");
specialFonts.put("mae", "Antinoou, AntinoouWeb");
specialFonts.put("ly", "Antinoou, AntinoouWeb");
specialFonts.put("cw", "Antinoou, AntinoouWeb");
specialFonts.put("syc", "Estrangelo Edessa, EstreWeb");
specialFonts.put("chu", "BukyvedeWeb");
rightJustify.add("syc");
rightJustify.add("he");
rightJustify.add("arb");
}
public static WitnessReading loadModuleReading(String moduleName, String key, boolean supplied, boolean unclear, boolean punct, boolean accents, Logger logger) {
String moduleServiceURL = "http://crosswire.org/study/fetchdata.jsp";
String params
= "mod="+java.net.URLEncoder.encode(moduleName)
+ "&key="+java.net.URLEncoder.encode(key)
+ "&format=strip";
StringBuffer result = HTTPUtils.postURL(moduleServiceURL, params);
String t = result.toString().toLowerCase();
WitnessReading retVal = null;
try {
Vector wits = Convert.getTEITranscriptionText(t, supplied, unclear, punct, accents);
retVal = (wits.size() > 0) ? wits.get(0) : new WitnessReading("", t);
}
catch (Exception e) {
logger.error("Error looking up module: " + moduleName, e);
retVal = new WitnessReading("", t);
}
retVal.setDocumentName(moduleName);
return retVal;
}
public static class WordWithParallelCombiningHintsTokenizer implements Function> {
@Override
public Iterable apply(String input) {
final List tokens = new Vector();
final StringTokenizer tokenizer = new StringTokenizer(input.trim(), " ,.-?;:\n", true);
boolean inSeg = false;
String segToken = "";
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
if (inSeg) {
if (token.indexOf("))") > -1) {
token = segToken + token.replaceFirst(java.util.regex.Pattern.quote("))"), "");
inSeg = false;
}
else {
segToken += token;
continue;
}
}
else if (token.indexOf("((") > -1) {
segToken = token.replaceFirst(java.util.regex.Pattern.quote("(("), "");
inSeg = true;
continue;
}
token = token.trim();
if (token.length() > 0) {
tokens.add(token);
}
}
return tokens;
}
}
%>