Search in sources :

Example 1 with ParseConfigurationFileException

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);
}
Also used : AssetManager(android.content.res.AssetManager) ParseConfigurationFileException(org.litepal.exceptions.ParseConfigurationFileException)

Example 2 with ParseConfigurationFileException

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);
    }
}
Also used : XmlPullParserFactory(org.xmlpull.v1.XmlPullParserFactory) ParseConfigurationFileException(org.litepal.exceptions.ParseConfigurationFileException) XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 3 with ParseConfigurationFileException

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);
}
Also used : AssetManager(android.content.res.AssetManager) ParseConfigurationFileException(org.litepal.exceptions.ParseConfigurationFileException)

Example 4 with ParseConfigurationFileException

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);
    }
}
Also used : InputSource(org.xml.sax.InputSource) ParseConfigurationFileException(org.litepal.exceptions.ParseConfigurationFileException) NotFoundException(android.content.res.Resources.NotFoundException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 5 with ParseConfigurationFileException

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);
    }
}
Also used : XmlPullParserFactory(org.xmlpull.v1.XmlPullParserFactory) ParseConfigurationFileException(org.litepal.exceptions.ParseConfigurationFileException) XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Aggregations

ParseConfigurationFileException (org.litepal.exceptions.ParseConfigurationFileException)5 IOException (java.io.IOException)3 AssetManager (android.content.res.AssetManager)2 XmlPullParser (org.xmlpull.v1.XmlPullParser)2 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)2 XmlPullParserFactory (org.xmlpull.v1.XmlPullParserFactory)2 NotFoundException (android.content.res.Resources.NotFoundException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 SAXParserFactory (javax.xml.parsers.SAXParserFactory)1 InputSource (org.xml.sax.InputSource)1 SAXException (org.xml.sax.SAXException)1 XMLReader (org.xml.sax.XMLReader)1