use of org.litepal.exceptions.ParseConfigurationFileException in project LitePal by LitePalFramework.
the class LitePalParser method getConfigInputStream.
/**
* Iterates all files in the root of assets folder. If find litepal.xml,
* open this file and return the input stream. Or throw
* ParseConfigurationFileException.
*
* @return The input stream of litepal.xml.
* @throws java.io.IOException
*/
private InputStream getConfigInputStream() throws IOException {
AssetManager assetManager = LitePalApplication.getContext().getAssets();
String[] fileNames = assetManager.list("");
if (fileNames != null && fileNames.length > 0) {
for (String fileName : fileNames) {
if (Const.Config.CONFIGURATION_FILE_NAME.equalsIgnoreCase(fileName)) {
return assetManager.open(fileName, AssetManager.ACCESS_BUFFER);
}
}
}
throw new ParseConfigurationFileException(ParseConfigurationFileException.CAN_NOT_FIND_LITEPAL_FILE);
}
use of org.litepal.exceptions.ParseConfigurationFileException in project LitePal by LitePalFramework.
the class LitePalParser method usePullParse.
/**
* Use XmlPullParser to parse the litepal.xml file. It will store the result
* in the instance of LitePalAttr.
*
* Note while analyzing litepal.xml file, ParseConfigurationFileException
* could be thrown. Be careful of writing litepal.xml file, or developer's
* application may be crash.
*/
private LitePalConfig usePullParse() {
try {
LitePalConfig litePalConfig = new LitePalConfig();
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);
litePalConfig.setDbName(dbName);
} else if (NODE_VERSION.equals(nodeName)) {
String version = xmlPullParser.getAttributeValue("", ATTR_VALUE);
litePalConfig.setVersion(Integer.parseInt(version));
} else if (NODE_MAPPING.equals(nodeName)) {
String className = xmlPullParser.getAttributeValue("", ATTR_CLASS);
litePalConfig.addClassName(className);
} else if (NODE_CASES.equals(nodeName)) {
String cases = xmlPullParser.getAttributeValue("", ATTR_VALUE);
litePalConfig.setCases(cases);
} else if (NODE_STORAGE.equals(nodeName)) {
String storage = xmlPullParser.getAttributeValue("", ATTR_VALUE);
litePalConfig.setStorage(storage);
}
break;
}
default:
break;
}
eventType = xmlPullParser.next();
}
return litePalConfig;
} catch (XmlPullParserException e) {
throw new ParseConfigurationFileException(ParseConfigurationFileException.FILE_FORMAT_IS_NOT_CORRECT);
} catch (IOException e) {
throw new ParseConfigurationFileException(ParseConfigurationFileException.IO_EXCEPTION);
}
}
use of org.litepal.exceptions.ParseConfigurationFileException in project LitePal by LitePalFramework.
the class ModelListActivity method getInputStream.
private InputStream getInputStream() throws IOException {
AssetManager assetManager = LitePalApplication.getContext().getAssets();
String[] fileNames = assetManager.list("");
if (fileNames != null && fileNames.length > 0) {
for (String fileName : fileNames) {
if (Const.Config.CONFIGURATION_FILE_NAME.equalsIgnoreCase(fileName)) {
return assetManager.open(fileName, AssetManager.ACCESS_BUFFER);
}
}
}
throw new ParseConfigurationFileException(ParseConfigurationFileException.CAN_NOT_FIND_LITEPAL_FILE);
}
use of org.litepal.exceptions.ParseConfigurationFileException in project LitePal by LitePalFramework.
the class LitePalParser method useSAXParser.
/**
* Use SAXParser to parse the litepal.xml file. It will get the parsed
* result from LitePalContentHandler and stored in the instance of
* LitePalAttr.
*
* Note while analyzing litepal.xml file, ParseConfigurationFileException
* could be thrown. Be careful of writing litepal.xml file, or developer's
* application may be crash.
*/
private void useSAXParser() {
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);
}
}
use of org.litepal.exceptions.ParseConfigurationFileException in project LitePal by LitePalFramework.
the class ModelListActivity method populateMappingClasses.
private void populateMappingClasses() {
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xmlPullParser = factory.newPullParser();
xmlPullParser.setInput(getInputStream(), "UTF-8");
int eventType = xmlPullParser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
String nodeName = xmlPullParser.getName();
switch(eventType) {
case XmlPullParser.START_TAG:
{
if ("mapping".equals(nodeName)) {
String className = xmlPullParser.getAttributeValue("", "class");
mList.add(className);
}
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);
}
}
Aggregations