ORM数据库-LitePal源码解析理解设计
来源:互联网 发布:mecare同步软件 编辑:程序博客网 时间:2024/06/05 11:43
基础知识:
这种方式解析是一种基于事件驱动的api,有两个部分,解析器和事件处理器,解析器就是XMLReader接口,负责读取XML文档,和向事件处理器发送事件(也是事件源),事件处理器ContentHandler接口,负责对发送的事件响应和进行XML文档处理。
下面是ContentHandler接口的常用方法
public abstract void characters (char[] ch, int start, int length)
这个方法来接收字符块通知,解析器通过这个方法来报告字符数据块,解析器为了提高解析效率把读到的所有字符串放到一个字符数组(ch)中,作为参数传递给character的方法中,如果想获取本次事件中读取到的字符数据,需要使用start和length属性。
public abstract void startDocument () 接收文档开始的通知
public abstract void endDocument () 接收文档结束的通知
public abstract void startElement (String uri, String localName, String qName, Attributes atts) 接收文档开始的标签
public abstract void endElement (String uri, String localName, String qName) 接收文档结束的标签
在一般使用中为了简化开发,在org.xml.sax.helpers提供了一个DefaultHandler类,它实现了ContentHandler的方法,我们只想继承DefaultHandler方法即可。
LitePalContentHandler handler;
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
XMLReader xmlReader = factory.newSAXParser().getXMLReader();
handler = new LitePalContentHandler();
xmlReader.setContentHandler(handler);
xmlReader.parse(new InputSource(getConfigInputStream()));
} catch (NotFoundException e) {
throw new ParseConfigurationFileException(
ParseConfigurationFileException.CAN_NOT_FIND_LITEPAL_FILE);
} catch (SAXException e) {
throw new ParseConfigurationFileException(
ParseConfigurationFileException.FILE_FORMAT_IS_NOT_CORRECT);
} catch (ParserConfigurationException e) {
throw new ParseConfigurationFileException(
ParseConfigurationFileException.PARSE_CONFIG_FAILED);
} catch (IOException e) {
throw new ParseConfigurationFileException(ParseConfigurationFileException.IO_EXCEPTION);
}
}
AssetManager assetManager = LitePalApplication.getContext().getAssets();
String[] fileNames = assetManager.list("");
if (fileNames != null && fileNames.length > 0) {
for (String fileName : fileNames) {
if (Const.LitePal.CONFIGURATION_FILE_NAME.equalsIgnoreCase(fileName)) {
return assetManager.open(fileName, AssetManager.ACCESS_BUFFER);
}
}
}
throw new ParseConfigurationFileException(
ParseConfigurationFileException.CAN_NOT_FIND_LITEPAL_FILE);
}
public void startDocument() throws SAXException {
litePalAttr = LitePalAttr.getInstance();
litePalAttr.getClassNames().clear();
}
/**
* Start analysis the litepal.xml file. Set all the parsed value into the
* LitePalAttr model.
*/
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
if (LitePalParser.NODE_DB_NAME.equalsIgnoreCase(localName)) {
for (int i = 0; i < attributes.getLength(); i++) {
if (LitePalParser.ATTR_VALUE.equalsIgnoreCase(attributes.getLocalName(i))) {
litePalAttr.setDbName(attributes.getValue(i).trim());
}
}
} else if (LitePalParser.NODE_VERSION.equalsIgnoreCase(localName)) {
for (int i = 0; i < attributes.getLength(); i++) {
if (LitePalParser.ATTR_VALUE.equalsIgnoreCase(attributes.getLocalName(i))) {
litePalAttr.setVersion(Integer.parseInt(attributes.getValue(i).trim()));
}
}
} else if (LitePalParser.NODE_MAPPING.equalsIgnoreCase(localName)) {
for (int i = 0; i < attributes.getLength(); i++) {
if (LitePalParser.ATTR_CLASS.equalsIgnoreCase(attributes.getLocalName(i))) {
litePalAttr.addClassName(attributes.getValue(i).trim());
}
}
} else if (LitePalParser.NODE_CASES.equalsIgnoreCase(localName)) {
for (int i = 0; i < attributes.getLength(); i++) {
if (LitePalParser.ATTR_VALUE.equalsIgnoreCase(attributes.getLocalName(i))) {
litePalAttr.setCases(attributes.getValue(i).trim());
}
}
} else if (LitePalParser.NODE_STORAGE.equalsIgnoreCase(localName)) {
for (int i = 0; i < attributes.getLength(); i++) {
if (LitePalParser.ATTR_VALUE.equalsIgnoreCase(attributes.getLocalName(i))) {
litePalAttr.setStorage(attributes.getValue(i).trim());
}
}
}
}
基础知识:
try {
LitePalAttr litePalAttr = LitePalAttr.getInstance();
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xmlPullParser = factory.newPullParser();
xmlPullParser.setInput(getConfigInputStream(), "UTF-8");
int eventType = xmlPullParser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
String nodeName = xmlPullParser.getName();
switch (eventType) {
case XmlPullParser.START_TAG: {
if (NODE_DB_NAME.equals(nodeName)) {
String dbName = xmlPullParser.getAttributeValue("", ATTR_VALUE);
litePalAttr.setDbName(dbName);
} else if (NODE_VERSION.equals(nodeName)) {
String version = xmlPullParser.getAttributeValue("", ATTR_VALUE);
litePalAttr.setVersion(Integer.parseInt(version));
} else if (NODE_MAPPING.equals(nodeName)) {
String className = xmlPullParser.getAttributeValue("", ATTR_CLASS);
litePalAttr.addClassName(className);
} else if (NODE_CASES.equals(nodeName)) {
String cases = xmlPullParser.getAttributeValue("", ATTR_VALUE);
litePalAttr.setCases(cases);
}
break;
}
default:
break;
}
eventType = xmlPullParser.next();
}
} catch (XmlPullParserException e) {
throw new ParseConfigurationFileException(
ParseConfigurationFileException.FILE_FORMAT_IS_NOT_CORRECT);
} catch (IOException e) {
throw new ParseConfigurationFileException(ParseConfigurationFileException.IO_EXCEPTION);
}
}
return getWritableDatabase();
}
LitePalOpenHelper litePalHelper = buildConnection();
return litePalHelper.getWritableDatabase();
}
if (mLitePalAttr == null) {
LitePalParser.parseLitePalConfiguration();
mLitePalAttr = LitePalAttr.getInstance();
}
if (mLitePalAttr.checkSelfValid()) {
if (mLitePalHelper == null) {
String dbName = mLitePalAttr.getDbName();
if ("external".equalsIgnoreCase(mLitePalAttr.getStorage())) {
dbName = LitePalApplication.getContext().getExternalFilesDir("") + "/databases/" + dbName;
}
mLitePalHelper = new LitePalOpenHelper(dbName, mLitePalAttr.getVersion());
}
return mLitePalHelper;
} else {
throw new InvalidAttributesException("Uncaught invalid attributes exception happened");
}
}
this(LitePalApplication.getContext(), dbName, null, version);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Generator.upgrade(db);
SharedUtil.updateVersion(newVersion);
}
drop(db);
create(db, false);
updateAssociations(db);
upgradeTables(db);
addAssociation(db, false);
}
try {
Class<?> dynamicClass = Class.forName(className);
Field[] fields = dynamicClass.getDeclaredFields();
for (Field field : fields) {
if (isPrivateAndNonPrimitive(field)) {
oneToAnyConditions(className, field, action);//一对多关系分析,缓存结果
manyToAnyConditions(className, field, action);//多对多关系分析,缓存结果
}
}
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
throw new DatabaseGenerateException(DatabaseGenerateException.CLASS_NOT_FOUND + className);
}
}
String classHoldsForeignKey, int associationType) {
AssociationsModel associationModel = new AssociationsModel();
associationModel.setTableName(DBUtility.getTableNameByClassName(className));
associationModel.setAssociatedTableName(DBUtility.getTableNameByClassName(associatedClassName));
associationModel.setTableHoldsForeignKey(DBUtility.getTableNameByClassName(classHoldsForeignKey));
associationModel.setAssociationType(associationType);
mAssociationModels.add(associationModel);
}
- ORM数据库-LitePal源码解析理解设计
- LitePal数据库框架 -- 源码解析之创建数据库
- LitePal源码解析——数据库的创建
- 源码探索系列4---数据库ORM框架之Ormlite解析
- android ORM框架LitePal完全解析(crud)
- android ORM框架LitePal完全解析(crud)
- 解读郭神LitePal源码-litepal.xml的解析
- android中ORM(对象关系映射)数据库LitePal的使用
- ORM-c++解析数据库
- litepal ORM框架
- LitePal数据库
- 从源码解析使用LitePal建表过程
- LitePal源码解析——框架结构及关键类说明
- 解读郭神LitePal源码-数据库的创建以及如何修改源码来创建外置数据库
- LitePal源码学习开篇
- LitePal + Gson + Volley的ORM框架尝试
- android ORM框架——Litepal
- android短彩信数据库设计源码解析(一)
- 木雨音乐 项目开发(七)我的收藏界面
- POJ 2635 The Embarrassed Cryptographer
- netlink socket编程之why & how
- Java Bean相关工具类库
- nncq---见证子系统,跨站登录
- ORM数据库-LitePal源码解析理解设计
- 【Spring学习笔记六】-Spring MVC框架
- Android的设置界面及Preference使用
- ubuntu系统下使用git克隆源码
- Linux中与内核通信的Netlink机制(实例)
- Scala中的Map和Tuple
- 适配器模式--把不适合的为己所用
- Qt5.2.1中解决QSqlDatabase:QMySQL driver not loaded
- 自定义nginx日志记录格式