Android Open Source - LitePal Lite Pal Parser






From Project

Back to project page LitePal.

License

The source code is released under:

Apache License

If you think the Android project LitePal listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C)  Tony Green, Litepal Framework Open Source Project
 */*from  w ww. ja v  a 2s.co  m*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.litepal.parser;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;

import org.litepal.LitePalApplication;
import org.litepal.exceptions.ParseConfigurationFileException;
import org.litepal.util.Const;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.content.res.AssetManager;
import android.content.res.Resources.NotFoundException;

/**
 * The class is used to parse the litepal.xml file. There're three usual ways to
 * parse XML in android, SAX, Pull and DOM. LitePal use SAX as default option,
 * and DOM parser will be added soon.
 * 
 * @author Tony Green
 * @since 1.0
 */
public class LitePalParser {

  /**
   * Node name dbname.
   */
  static final String NODE_DB_NAME = "dbname";

  /**
   * Node name version.
   */
  static final String NODE_VERSION = "version";

  /**
   * Node name list. Currently not used.
   */
  static final String NODE_LIST = "list";

  /**
   * Node name mapping.
   */
  static final String NODE_MAPPING = "mapping";

  /**
   * Node name column case.
   */
  static final String NODE_CASES = "cases";

  /**
   * Attribute name value, for dbname and version node.
   */
  static final String ATTR_VALUE = "value";

  /**
   * Attribute name class, for mapping node.
   */
  static final String ATTR_CLASS = "class";

  /**
   * Store the parsed value of litepal.xml.
   */
  private static LitePalParser parser;

  /**
   * Analyze litepal.xml, and store the analyzed result in LitePalParser. Use
   * DomParse to parse the configuration file as default. SAXParser and
   * XmlPullParser is also optional, but not visible to developers.
   */
  public static void parseLitePalConfiguration() {
    if (parser == null) {
      parser = new LitePalParser();
    }
    parser.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.
   */
  void useSAXParser() {
    LitePalContentHandler handler = null;
    try {
      SAXParserFactory factory = SAXParserFactory.newInstance();
      XMLReader xmlReader = factory.newSAXParser().getXMLReader();
      handler = new LitePalContentHandler();
      xmlReader.setContentHandler(handler);
      xmlReader.parse(new InputSource(getConfigInputStream()));
      return;
    } 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 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.
   */
  void usePullParse() {
    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);
    }
  }

  /**
   * 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 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.LitePal.CONFIGURATION_FILE_NAME.equalsIgnoreCase(fileName)) {
          return assetManager.open(fileName, AssetManager.ACCESS_BUFFER);
        }
      }
    }
    throw new ParseConfigurationFileException(
        ParseConfigurationFileException.CAN_NOT_FIND_LITEPAL_FILE);
  }
}




Java Source Code List

org.litepal.LitePalApplication.java
org.litepal.LitePalBase.java
org.litepal.crud.AssociationsAnalyzer.java
org.litepal.crud.ClusterQuery.java
org.litepal.crud.DataHandler.java
org.litepal.crud.DataSupport.java
org.litepal.crud.DeleteHandler.java
org.litepal.crud.DynamicExecutor.java
org.litepal.crud.Many2ManyAnalyzer.java
org.litepal.crud.Many2OneAnalyzer.java
org.litepal.crud.One2OneAnalyzer.java
org.litepal.crud.QueryHandler.java
org.litepal.crud.SaveHandler.java
org.litepal.crud.UpdateHandler.java
org.litepal.crud.model.AssociationsInfo.java
org.litepal.exceptions.DataSupportException.java
org.litepal.exceptions.DatabaseGenerateException.java
org.litepal.exceptions.GlobalException.java
org.litepal.exceptions.InvalidAttributesException.java
org.litepal.exceptions.ParseConfigurationFileException.java
org.litepal.litepalsample.activity.AggregateActivity.java
org.litepal.litepalsample.activity.AverageSampleActivity.java
org.litepal.litepalsample.activity.CRUDActivity.java
org.litepal.litepalsample.activity.CountSampleActivity.java
org.litepal.litepalsample.activity.DeleteSampleActivity.java
org.litepal.litepalsample.activity.MainActivity.java
org.litepal.litepalsample.activity.ManageTablesActivity.java
org.litepal.litepalsample.activity.MaxSampleActivity.java
org.litepal.litepalsample.activity.MinSampleActivity.java
org.litepal.litepalsample.activity.ModelListActivity.java
org.litepal.litepalsample.activity.ModelStructureActivity.java
org.litepal.litepalsample.activity.QuerySampleActivity.java
org.litepal.litepalsample.activity.SaveSampleActivity.java
org.litepal.litepalsample.activity.SumSampleActivity.java
org.litepal.litepalsample.activity.TableListActivity.java
org.litepal.litepalsample.activity.TableStructureActivity.java
org.litepal.litepalsample.activity.UpdateSampleActivity.java
org.litepal.litepalsample.adapter.DataArrayAdapter.java
org.litepal.litepalsample.adapter.StringArrayAdapter.java
org.litepal.litepalsample.model.Album.java
org.litepal.litepalsample.model.Singer.java
org.litepal.litepalsample.model.Song.java
org.litepal.litepalsample.util.Utility.java
org.litepal.model.Table_Schema.java
org.litepal.parser.LitePalAttr.java
org.litepal.parser.LitePalContentHandler.java
org.litepal.parser.LitePalParser.java
org.litepal.tablemanager.AssociationCreator.java
org.litepal.tablemanager.AssociationUpdater.java
org.litepal.tablemanager.Connector.java
org.litepal.tablemanager.Creator.java
org.litepal.tablemanager.Dropper.java
org.litepal.tablemanager.Generator.java
org.litepal.tablemanager.LitePalOpenHelper.java
org.litepal.tablemanager.Upgrader.java
org.litepal.tablemanager.model.AssociationsModel.java
org.litepal.tablemanager.model.TableModel.java
org.litepal.tablemanager.typechange.BooleanOrm.java
org.litepal.tablemanager.typechange.DateOrm.java
org.litepal.tablemanager.typechange.DecimalOrm.java
org.litepal.tablemanager.typechange.NumericOrm.java
org.litepal.tablemanager.typechange.OrmChange.java
org.litepal.tablemanager.typechange.TextOrm.java
org.litepal.util.BaseUtility.java
org.litepal.util.Const.java
org.litepal.util.DBUtility.java
org.litepal.util.LogUtil.java
org.litepal.util.SharedUtil.java