/* SimpleVStreamTexter.java * * Copyright (c) 1999-2001, Ted Nelson and Tuomas Lukka * * This file is part of Gzz. * * Gzz is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Gzz is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General * Public License for more details. * * You should have received a copy of the GNU Lesser General * Public License along with Gzz; if not, write to the Free * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA * * */ /* * Written by Tuomas Lukka */ package org.fenfire.impl; import org.fenfire.*; //import org.fenfire.vocab.*; import org.fenfire.index.*; import org.fenfire.index.impl.*; import org.fenfire.util.*; import org.nongnu.alph.*; import java.util.*; //import org.fenfire.fuzzy.EnfiladeAligner; import com.hp.hpl.mesa.rdf.jena.model.*; /** A simple referential CellTexter. */ public class SimpleVStreamTexter implements VStreamNodeTexter, SearchableNodeTexter, ModularNodeTexter, IndexedVStreamNodeTexter { protected Map contents = new HashMap(); ModularSpace space; SpanMaker spanMaker; Enfilade1D.Maker enfMaker; EnfiladeOverlapIndex index; XuIndexer xuIndex; /** Aligner * @deprecated EnfiladeAligner should not be provided by Space: it's too * complicated and ill-defined for that. */ // XXX EnfiladeAligner enfAligner; /** Create a new object with given new span maker. * @param spanMaker In order to perform setText() and insertText(), * this object needs to create new spans. * The spanMaker interface allows us to polymorphize that. * @deprecated EnfiladeAligner should not be provided by Space: it's too * complicated and ill-defined for that. */ /* XXX public SimpleVStreamTexter(SpanMaker spanMaker, Enfilade1D.Maker enfMaker) // , EnfiladeAligner enfAligner) { { this.spanMaker = spanMaker; this.enfMaker = enfMaker; // XXX this.enfAligner = enfAligner; this.index = new EnfiladeOverlapIndex(); } */ /** Create a new object with given new span maker. * @param spanMaker In order to perform setText() and insertText(), * this object needs to create new spans. * The spanMaker interface allows us to polymorphize that. */ public SimpleVStreamTexter(SpanMaker spanMaker, Enfilade1D.Maker enfMaker) { this.spanMaker = spanMaker; this.enfMaker = enfMaker; this.index = new EnfiladeOverlapIndex(); } public void setSpace(ModularSpace space) { this.space = space; this.xuIndex = new SpaceXuIndexer(space); } /** The innermost routine that uses the actual map. * For keeping change lists etc, override this and setMap. */ protected Enfilade1D getFromMap(RDFNode node) { // Must be the real data node. RDFNode n = ShortRDF.getDataNode(space.getModel(), node); return (Enfilade1D)contents.get(n); } /** The innermost routine that uses the actual map. * For keeping change lists etc, override this and getFromMap. */ protected void setMap(RDFNode node, Enfilade1D enf) { RDFNode n = ShortRDF.getDataNode(space.getModel(), node); contents.put(n, enf); index.set(n, enf); } /** The innermost routine that uses the actual map. * For keeping change lists etc, override this and set/getFromMap. */ public Set getNodesWithContent() { return contents.keySet(); } public final Enfilade1D getEnfilade(RDFNode node, Obs o) { RDFNode n = ShortRDF.getDataNode(space.getModel(), node); if(o != null) space.getObsTrigger().addObs(o, this, n); Enfilade1D enf = getFromMap(n); if(enf == null) return enfMaker.makeEnfilade(); else return enf; } public final void setEnfilade(RDFNode node, Enfilade1D vstream) { RDFNode n = ShortRDF.getDataNode(space.getModel(), node); setMap(n, vstream); space.getObsTrigger().chg(this, n); } /* XXX public EnfiladeAligner.Instance startEnfiladeAligner(RDFNode node) { RDFNode n = ShortRDF.getDataNode(space.getModel(), node); return enfAligner.start(getEnfilade(n, null), spanMaker); } */ public Index getEnfiladeOverlap() { return index; } /** XXX WRONG IMPLEMENTATION!!!!!!! */ public XuIndexer getXuIndexer() { return xuIndex; } public String getText(RDFNode node, Obs o) { Enfilade1D enf = getEnfilade(node, o); if(enf == null) return ""; return enf.makeString(); } public void setText(RDFNode node, String s) { RDFNode n = ShortRDF.getDataNode(space.getModel(), node); setEnfilade(n, enfMaker.makeEnfilade(spanMaker.makeTextSpan(s))); } public void insertText(RDFNode node, int ind, String s) { Enfilade1D old = getEnfilade(node, null); if(old != null) { Enfilade1D enf = old.sub(0, ind); enf = enf.plus(spanMaker.makeTextSpan(s)); enf = enf.plus(old.sub(ind)); setEnfilade(node, enf); } else { setText(node, s); } } public void deleteText(RDFNode node, int begin, int end) { Enfilade1D old = getEnfilade(node, null); if(old != null) { Enfilade1D enf = old.sub(0, begin); enf = enf.plus(old.sub(end)); setEnfilade(node, enf); } } public void copyText(RDFNode to, int ind, RDFNode from, int begin, int end) { Enfilade1D old = getEnfilade(to, null); Enfilade1D ins = getEnfilade(from, null); // Normalize nulls if(ins == null) ins = enfMaker.makeEnfilade(); if(old == null) old = enfMaker.makeEnfilade(); if(ind < 0 || ind > old.length()) throw new IndexOutOfBoundsException("Copy to node, to index: "+ind+" "+old); if(begin < 0 || begin > ins.length()) throw new IndexOutOfBoundsException("Copy to node, from index: "+begin+" "+end+" "+ins); if(end < begin || end > ins.length()) throw new IndexOutOfBoundsException("Copy to node, from index: "+begin+" "+end+" "+ins); Enfilade1D newValue = old.sub(0,ind) .plus(ins.sub(begin,end)) .plus(old.sub(ind)); setEnfilade(to, newValue); } public void moveText(RDFNode to, int ind, RDFNode from, int begin, int end) { copyText(to, ind, from, begin, end); deleteText(from, begin, end); } public StringSearcher getStringSearcher() { // XXX inefficient StringSearcher s = new InitialStringSearcher(); for(Iterator i=getNodesWithContent().iterator(); i.hasNext();) { RDFNode node = (RDFNode)i.next(); s.addString(getText(node, null), node); } return s; } }