[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: New libquran architecture and features



Salam,

Data base is a good idea, but i think that a compressed xml file will solve data size problem.

For data fetching or searching xml path solve all requirement , I send a java implementation of this (just for demo).

Regards


Ahmed Ghoneim a écrit :
On 7/23/07, nabil ben khalifa <nabil_benkhalifa at yahoo dot fr> wrote:
Are plugins really needed ? why we don't offer
compilation options so that everyone can compile
libquran with the adequate options for his platform ?
Good point. But the reason i would lean more towards the plugins
options is that, not all the expected user base (at least from what
i've seen) will be in "compile your program" camp.
A problem that's easily solved by maintaining a combination of builds
that would satisfy most people, then again it's more work/headache for
you (the maintainers) :)


I have another suggestion: instead of using xml or
binary files, why we don't use to databases instead to
store quranic data (text, tafsir, ...) which will
facilitate access to data and searching of info
within.  We can use sqlite for example which is a very
light and portable sql database based in files, what
do you think ?
excellent point, i totally agree, sqlite wuold be great for extras
such as tafsir (in a plugin arch).
but do we really need it for The Quran text? i think for a smaller
footprint (mobile devices), XML or sqlite should not be used, the
quran text can be stored in a binary record format file and indexed by
aya number.
of course sqlite search capabilities will be much more superior to
such a scheme, so it's still a very good idea to consider.

i haven't used sqlite on symbian. anybody has some experience there?

Salam,

Ahmed Ghoneim
_______________________________________________
Developer mailing list
Developer at arabeyes dot org
http://lists.arabeyes.org/mailman/listinfo/developer
------------------------------------------------------------------------

No virus found in this incoming message.
Checked by AVG Free Edition. Version: 7.5.467 / Virus Database: 269.10.9/907 - Release Date: 18/07/2007 15:30

/*
 * Created on 16 janv. 2006
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.linuxtunisie.jquran.xml;

import org.dom4j.Node;

import com.linuxtunisie.jquran.Aya;

public class AyaXml extends QuranElement implements Aya {

	private String text;

	private String spText;

	private int number;

	public AyaXml(Node node) {
		super(node);
	}

	protected void build() {
		text = node.selectSingleNode("qurantext").getText();
		Node spNode = node.selectSingleNode("searchtext");
		if (spNode != null)
			spText = spNode.getText();
		else
			spText = text;
		if (text == null)
			throw STATE_EXCEPTION;
		try {
			number = Integer.valueOf(node.selectSingleNode("@id").getText())
					.intValue();
		} catch (NumberFormatException e) {
			throw STATE_EXCEPTION;
		}
	}

	public String getText() {
		return text;
	}

	public String toString() {
		return text;
	}

	public String getSpecialText() {
		return spText;
	}

	public int getNumber() {
		return number;
	}

}
/*
 * Created on 15 janv. 2006
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.linuxtunisie.jquran.xml;

import org.dom4j.Node;

public abstract class QuranElement {
	protected static final IllegalStateException STATE_EXCEPTION = new IllegalStateException(
	"xml file was corrupted");
	protected Node node;
	protected QuranElement(Node node) {
		this.node = node;
		build();
	}
	abstract protected void build();

}
/*
 * Created on 13 janv. 2006
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.linuxtunisie.jquran.xml;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

import com.linuxtunisie.jquran.Quran;

import de.netseeker.ejoe.io.ByteBufferInputStream;

public class QuranReader {

	private static final String DEFAULT_ENCODING = "UTF-8";

	private Document document;

	private ByteBuffer data;

	private Quran quran;

	/**
	 * 
	 * @param zipedFile
	 */
	public QuranReader(String zipedFile) {
		this(zipedFile, DEFAULT_ENCODING);
	}
	
	/**
	 * 
	 * @param zipedFile
	 * @param encoding
	 */
	public QuranReader(String zipedFile, String encoding) {

		try {
			FileInputStream inputStream = new FileInputStream(zipedFile);
			FileChannel channel = inputStream.getChannel();
			long fileSize = channel.size();
			data = channel.map(FileChannel.MapMode.READ_ONLY, 0, fileSize);
			channel.close();
			ByteBufferInputStream ins = new ByteBufferInputStream(data);
			ZipInputStream zipInputStream = new ZipInputStream(ins);
			ZipEntry zipEntry = zipInputStream.getNextEntry();
			if (zipEntry == null)
				throw new IllegalStateException("fichier incompatible");
			BufferedReader in = new BufferedReader(new InputStreamReader(
					zipInputStream, encoding));
			SAXReader reader = new SAXReader();
			try {
				document = reader.read(in);
				quran = new QuranXml(document);
			} catch (DocumentException e) {
				e.printStackTrace();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Quran getQuran() {
		return quran;
	}
}
/*
 * Created on 15 janv. 2006
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.linuxtunisie.jquran.xml;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;

import org.dom4j.Element;
import org.dom4j.Node;

import com.linuxtunisie.jquran.Aya;
import com.linuxtunisie.jquran.Quran;
import com.linuxtunisie.jquran.QuranException;
import com.linuxtunisie.jquran.QuranResult;
import com.linuxtunisie.jquran.Sura;
import com.linuxtunisie.jquran.SuraResult;

public class QuranXml extends QuranElement implements Quran {

	private ArrayList suras;

	QuranXml(Node node) {
		super(node);
	}

	public Collection getAllSuras() {
		return new ArrayList(suras);
	}

	public Sura getSura(int number) throws QuranException {
		if (number < 1 && number > Quran.MAX_SURA_NUMBER)
			throw new IllegalArgumentException("invalid number");
		Sura sura = (Sura) suras.get(number - 1);
		if (sura == null)
			throw new QuranException("no sura for number " + number);
		return sura;
	}

	protected void build() {
		suras = new ArrayList();
		List surusElmt = node.selectNodes("//quran/sura");
		for (Iterator iter = surusElmt.iterator(); iter.hasNext();) {
			Element element = (Element) iter.next();
			suras.add(new SuraXml(element));
		}
	}

	public String toString() {
		return node.asXML();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.linuxtunisie.jquran.Quran#search(java.lang.String)
	 */
	public QuranResult search(String text) {
		Collection results = new ArrayList();
		int number = 0;
		Pattern pattern = Pattern.compile(".*" + text + ".*",
				Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
		for (Iterator iter = suras.iterator(); iter.hasNext();) {
			Sura sura = (Sura) iter.next();
			XmlSuraResult result = null;
			Collection ayas = sura.getAllAyas();
			for (Iterator iterator = ayas.iterator(); iterator.hasNext();) {
				Aya aya = (Aya) iterator.next();
				if (pattern.matcher(aya.getSpecialText()).matches()) {
					if (result == null)
						result = new XmlSuraResult(sura);
					result.addAya(aya);
					number++;
				}
			}
			if (result != null)
				results.add(result);
		}
		XmlQuranResult result = new XmlQuranResult();
		SuraResult[] suraResults = new SuraResult[results.size()];
		result.setNumber(number);
		result.setResults((SuraResult[]) results.toArray(suraResults));
		return result;
	}
}
/*
 * Created on 15 janv. 2006
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.linuxtunisie.jquran.xml;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import org.dom4j.Element;
import org.dom4j.Node;

import com.linuxtunisie.jquran.Aya;
import com.linuxtunisie.jquran.QuranException;
import com.linuxtunisie.jquran.Sura;

public class SuraXml extends QuranElement implements Sura {

	private String title;

	private int number;

	private ArrayList ayas;

	private int numberOfAyas;

	SuraXml(Node node) {
		super(node);
	}

	public Collection getAllAyas() {
		return new ArrayList(ayas);
	}

	public Aya getAya(int number) throws QuranException {
		if (number < 1 || number > numberOfAyas)
			throw new QuranException("no aya for number " + number);
		Aya aya = (Aya) ayas.get(number - 1);
		if (aya == null)
			new QuranException("aya not found");
		return aya;
	}

	public int getNumber() {
		return number;
	}

	public String getTitle() {
		return title;
	}

	public String toString() {
		return node.asXML();
	}

	protected void build() {
		title = node.selectSingleNode("@name").getText();
		if (title == null)
			throw STATE_EXCEPTION;
		try {
			number = Integer.valueOf(node.selectSingleNode("@id").getText())
					.intValue();
		} catch (NumberFormatException e) {
			throw STATE_EXCEPTION;
		}
		ayas = new ArrayList();
		java.util.List list = node.selectNodes("aya");
		for (Iterator iter = list.iterator(); iter.hasNext();) {
			Element element = (Element) iter.next();
			ayas.add(new AyaXml(element));
		}
		numberOfAyas = ayas.size();
		if (numberOfAyas == 0)
			throw STATE_EXCEPTION;
	}

	public int getNumberOfAyas() {
		return numberOfAyas;
	}

}
/*
 * Created on 27 janv. 2006
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.linuxtunisie.jquran.xml;

import com.linuxtunisie.jquran.QuranResult;
import com.linuxtunisie.jquran.SuraResult;

public class XmlQuranResult implements QuranResult {

	private SuraResult [] results;
	private int number;
	
	public void setNumber(int number) {
		this.number = number;
	}

	public void setResults(SuraResult[] results) {
		this.results = results;
	}

	public XmlQuranResult() {
		super();
	}

	public SuraResult[] getSuraResults() {
		return results;
	}

	public int getNumber() {
		return number;
	}
	

}
/*
 * Created on 26 janv. 2006
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.linuxtunisie.jquran.xml;

import java.util.ArrayList;
import java.util.Collection;

import com.linuxtunisie.jquran.Aya;
import com.linuxtunisie.jquran.SuraResult;
import com.linuxtunisie.jquran.Sura;

public class XmlSuraResult implements SuraResult {
	private Sura sura;

	private final Collection ayas = new ArrayList();

	public XmlSuraResult(Sura sura) {
		this.sura = sura;
	}

	public Sura getSura() {
		return sura;
	}

	public Aya[] getAyas() {
		Aya [] ayas = new Aya[this.ayas.size()];
		ayas = (Aya[]) this.ayas.toArray(ayas);
		return ayas;
	}

	void addAya(Aya aya) {
		if (aya != null)
			ayas.add(aya);
	}

	public int getNumber() {
		return ayas.size();
	}

}