- added assets folder and Quran content
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
package com.mhalka.qurantranslationdisplay;
|
||||
|
||||
public class InvalidChapterException extends Exception {
|
||||
|
||||
}
|
||||
61
src/com/mhalka/qurantranslationdisplay/QuranReader.java
Normal file
61
src/com/mhalka/qurantranslationdisplay/QuranReader.java
Normal file
@@ -0,0 +1,61 @@
|
||||
package com.mhalka.qurantranslationdisplay;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.xpath.XPath;
|
||||
import javax.xml.xpath.XPathExpressionException;
|
||||
import javax.xml.xpath.XPathFactory;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import android.content.res.AssetManager;
|
||||
|
||||
public class QuranReader {
|
||||
private AssetManager assetManager;
|
||||
private XPath xPath;
|
||||
private DocumentBuilder builder = null;
|
||||
private Document document;
|
||||
private String folder;
|
||||
|
||||
public QuranReader(AssetManager assetManager) throws ParserConfigurationException{
|
||||
this(assetManager,null);
|
||||
}
|
||||
public QuranReader(AssetManager assetManager, String folder) throws ParserConfigurationException {
|
||||
this.assetManager = assetManager;
|
||||
this.folder = folder;
|
||||
|
||||
DocumentBuilderFactory builderFactory =
|
||||
DocumentBuilderFactory.newInstance();
|
||||
xPath = XPathFactory.newInstance().newXPath();
|
||||
|
||||
|
||||
builder = builderFactory.newDocumentBuilder();
|
||||
|
||||
}
|
||||
public void setChapter(int num) throws IOException, InvalidChapterException, SAXException {
|
||||
if (num < 1 || num > 114)
|
||||
throw new InvalidChapterException();
|
||||
|
||||
String chapterNum = String.format("%03d", num);
|
||||
String folderPrefix = "";
|
||||
if(this.folder != null && !this.folder.trim().equals("")){
|
||||
folderPrefix = this.folder + "/";
|
||||
}
|
||||
|
||||
InputStream is = this.assetManager.open(String.format("%sChapter%s.xml",folderPrefix,chapterNum ));;
|
||||
|
||||
document = builder.parse(is);
|
||||
|
||||
}
|
||||
|
||||
public String getVerse(int num) throws XPathExpressionException {
|
||||
String expression = "/Chapter/Verse[@VerseID='"+num+"']/text()";
|
||||
String text = xPath.compile(expression).evaluate(this.document);
|
||||
return text;
|
||||
}
|
||||
}
|
||||
@@ -25,11 +25,10 @@ public class ShowAyah extends Activity {
|
||||
final TextView ayah = (TextView) findViewById(R.id.ayah);
|
||||
ayah.setMovementMethod(new ScrollingMovementMethod());
|
||||
|
||||
// Example of use of QuranReader
|
||||
// AssetManager assetManager = getAssets();
|
||||
// QuranReader reader = new QuranReader(assetManager, "quran_content");
|
||||
// reader.setChapter(2);
|
||||
// String verseText = reader.getVerse(16);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user