1 |
//(c): Matti J. Katila and Tuomas J. Lukka |
// (c) Matti J. Katila and Tuomas J. Lukka |
2 |
|
|
3 |
package org.fenfire.util; |
package org.fenfire.util; |
4 |
import org.fenfire.vocab.ALPH; |
import org.fenfire.vocab.ALPH; |
5 |
|
import org.fenfire.*; |
6 |
import org.nongnu.alph.*; |
import org.nongnu.alph.*; |
7 |
import org.nongnu.alph.xml.*; |
import org.nongnu.alph.xml.*; |
|
import com.hp.hpl.mesa.rdf.jena.vocabulary.RDF; |
|
|
import com.hp.hpl.mesa.rdf.jena.model.*; |
|
|
import com.hp.hpl.mesa.rdf.jena.common.*; |
|
8 |
|
|
9 |
import java.util.*; |
import java.util.*; |
10 |
import java.io.*; |
import java.io.*; |
|
import javax.xml.parsers.*; |
|
11 |
|
|
12 |
|
|
13 |
/** Help class to work with Alph |
/** Help class to work with Alph |
17 |
private static void pa(String s) { System.out.println("AlphContent: "+s); } |
private static void pa(String s) { System.out.println("AlphContent: "+s); } |
18 |
|
|
19 |
|
|
|
// Static helpers.. |
|
20 |
|
|
21 |
|
|
|
/** helper to determinate the type of span, i.e. PageSpan or TextSpan |
|
|
* @return the first span in enfilade. |
|
|
*/ |
|
|
public Span getSpan(Model model, RDFNode node) |
|
|
{ try { |
|
|
|
|
|
NodeIterator iter = model.listObjectsOfProperty((Resource)node, ALPH.content); |
|
|
if (iter.hasNext()) { |
|
|
Literal lit = (Literal)iter.next(); |
|
|
|
|
|
try { |
|
|
SpanReader sr = new SpanReader(); |
|
|
SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); |
|
|
parser.parse(new StringBufferInputStream(lit.getString()), sr); |
|
|
|
|
|
ArrayList l = (ArrayList)sr.getSpans(); |
|
|
return (Span)l.get(0); |
|
|
|
|
|
} catch (Exception e) { |
|
|
pa("Exception!: "+e); |
|
|
throw new Error("getspan has an Exception!"); |
|
|
} |
|
|
} |
|
|
else throw new Error("No available content!"); |
|
|
|
|
|
} catch (RDFException e) { |
|
|
pa("Exception ocurred!: "+e); |
|
|
throw new Error("Alph has an RDFException!"); |
|
|
}} |
|
|
|
|
|
|
|
|
/** helper to present text contents on screen |
|
|
* Content MUST be TextSpan so check it put first. |
|
|
*/ |
|
|
public String getString(Model model, RDFNode node) |
|
|
{ try { |
|
|
|
|
|
NodeIterator iter = model.listObjectsOfProperty((Resource)node, ALPH.content); |
|
|
if (iter.hasNext()) { |
|
|
Literal lit = (Literal)iter.next(); |
|
|
|
|
|
try { |
|
|
SpanReader sr = new SpanReader(); |
|
|
SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); |
|
|
parser.parse(new StringBufferInputStream(lit.getString()), sr); |
|
|
|
|
|
String text=""; |
|
|
|
|
|
ArrayList l = (ArrayList)sr.getSpans(); |
|
|
for (int i=0; i<l.size(); i++) { |
|
|
text += ((TextSpan)l.get(i)).getText(); |
|
|
pa(i+" Xml: "+ text); |
|
|
} |
|
|
return text; |
|
|
|
|
|
} catch (Exception e) { |
|
|
pa("Exception!: "+e); |
|
|
throw new Error("getspan has an Exception!"); |
|
|
} |
|
|
} |
|
|
else throw new Error("No available content!"); |
|
|
|
|
|
} catch (RDFException e) { |
|
|
pa("Exception ocurred!: "+e); |
|
|
throw new Error("Alph has an RDFException!"); |
|
|
}} |
|
22 |
|
|
23 |
|
|
|
|
|
|
// Old VStreamer or something... |
|
|
|
|
24 |
protected Map contents = new HashMap(); |
protected Map contents = new HashMap(); |
25 |
private SpanMaker spanMaker; |
private SpanMaker spanMaker; |
26 |
private Enfilade1D.Maker enfMaker; |
private Enfilade1D.Maker enfMaker; |
27 |
//private XuIndexer xuIndex; |
//private XuIndexer xuIndex; |
28 |
private Model model; |
private Fen fen; |
29 |
|
|
30 |
public AlphContent(SpanMaker spanMaker, Enfilade1D.Maker enfMaker, Model model) { |
public AlphContent(SpanMaker spanMaker, Enfilade1D.Maker enfMaker, |
31 |
|
Fen fen) { |
32 |
this.spanMaker = spanMaker; |
this.spanMaker = spanMaker; |
33 |
this.enfMaker = enfMaker; |
this.enfMaker = enfMaker; |
34 |
this.model = model; |
this.fen = fen; |
|
} |
|
|
|
|
|
|
|
|
/** The innermost routine that uses the actual map. |
|
|
* For keeping change lists etc, override this and getFromMap. |
|
|
*/ |
|
|
protected void setMap(RDFNode node, Enfilade1D enf) { |
|
|
contents.put(node, enf); |
|
|
} |
|
|
|
|
|
public final Enfilade1D getEnfilade(RDFNode node) { |
|
|
Enfilade1D enf = (Enfilade1D)contents.get(node); |
|
|
if(enf == null) return enfMaker.makeEnfilade(); |
|
|
else return enf; |
|
|
} |
|
|
|
|
|
private final void setEnfilade(RDFNode node, Enfilade1D vstream) { |
|
|
setMap(node, vstream); |
|
|
|
|
|
try { |
|
|
SpanSerializer sr = new SpanSerializer(); |
|
|
|
|
|
String xml = ""; |
|
|
ArrayList l = (ArrayList)vstream.getList(); |
|
|
for (int i=0; i<l.size(); i++) { |
|
|
xml = sr.span2xml((Span)l.get(i)); |
|
|
xml += " "; |
|
|
} |
|
|
pa("Xml: "+xml); |
|
|
|
|
|
Literal lit = new LiteralImpl(xml); |
|
|
model.add(new StatementImpl((Resource)node, ALPH.content, (RDFNode)lit)); |
|
|
|
|
|
} catch (RDFException e) { |
|
|
pa("Exception ocurred!: "+e); |
|
|
throw new Error("setTextContent got RDFException!"); |
|
|
} |
|
35 |
} |
} |
36 |
|
|
37 |
public String getText(RDFNode node) { |
public String getText(Object node) { |
38 |
Enfilade1D enf = getEnfilade(node); |
Enfilade1D enf = fen.txt.get(node); |
39 |
if(enf == null) return ""; |
if(enf == null) return ""; |
40 |
return enf.makeString(); |
return enf.makeString(); |
41 |
} |
} |
42 |
|
|
43 |
public void setText(RDFNode node, String s) { |
public void setText(Object node, String s) { |
44 |
setEnfilade(node, enfMaker.makeEnfilade(spanMaker.makeTextSpan(s))); |
fen.txt.set(node, enfMaker.makeEnfilade(spanMaker.makeTextSpan(s))); |
45 |
} |
} |
46 |
|
|
47 |
public void insertText(RDFNode node, int ind, String s) { |
public void insertText(Object node, int ind, String s) { |
48 |
Enfilade1D old = getEnfilade(node); |
Enfilade1D old = fen.txt.get(node); |
49 |
|
|
50 |
if(old != null) { |
if(old != null) { |
51 |
Enfilade1D enf = old.sub(0, ind); |
Enfilade1D enf = old.sub(0, ind); |
52 |
enf = enf.plus(spanMaker.makeTextSpan(s)); |
enf = enf.plus(spanMaker.makeTextSpan(s)); |
53 |
enf = enf.plus(old.sub(ind)); |
enf = enf.plus(old.sub(ind)); |
54 |
|
|
55 |
setEnfilade(node, enf); |
fen.txt.set(node, enf); |
56 |
} else { |
} else { |
57 |
setText(node, s); |
setText(node, s); |
58 |
} |
} |
59 |
} |
} |
60 |
|
|
61 |
public void deleteText(RDFNode node, int begin, int end) { |
public void deleteText(Object node, int begin, int end) { |
62 |
Enfilade1D old = getEnfilade(node); |
Enfilade1D old = fen.txt.get(node); |
63 |
|
|
64 |
if(old != null) { |
if(old != null) { |
65 |
Enfilade1D enf = old.sub(0, begin); |
Enfilade1D enf = old.sub(0, begin); |
66 |
enf = enf.plus(old.sub(end)); |
enf = enf.plus(old.sub(end)); |
67 |
|
|
68 |
setEnfilade(node, enf); |
fen.txt.set(node, enf); |
69 |
} |
} |
70 |
} |
} |
71 |
|
|