Android Open Source - android-core Application Descriptor Reader






From Project

Back to project page android-core.

License

The source code is released under:

Apache License

If you think the Android project android-core 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

/** 
 * [SIMINOV FRAMEWORK]//  www.  j  av  a 2 s  .com
 * Copyright [2015] [Siminov Software Solution LLP|support@siminov.com]
 *
 * 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 siminov.core.reader;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import siminov.core.Constants;
import siminov.core.exception.DeploymentException;
import siminov.core.log.Log;
import siminov.core.model.ApplicationDescriptor;
import siminov.core.resource.ResourceManager;
import android.content.Context;



/**
 * Exposes methods to parse Application Descriptor information as per define in ApplicationDescriptor.si.xml file by application.
  <p>
    <pre>
    
Example:
  {@code
  
  <siminov>
      
    <!-- General Application Description Properties -->
    
      <!-- Mandatory Field -->
    <property name="name">application_name</property>  
    
      <!-- Optional Field -->
    <property name="description">application_description</property>
    
      <!-- Mandatory Field (Default is 0.0) -->
    <property name="version">application_version</property>
  
  
    <!-- Database Descriptors Used By Application (zero-to-many) -->  
      <!-- Optional Field's -->
    <database-descriptors>
      <database-descriptor>full_path_of_database_descriptor_file</database-descriptor>
    </database-descriptors>
      
  
    <!-- Library Descriptors Used By Application (zero-to-many) -->
      <!-- Optional Field's -->
    <library-descriptors>
       <library-descriptor>full_path_of_library_descriptor_file</library-descriptor>   
    </library-descriptors>
    
      
    <!-- Event Handlers Implemented By Application (zero-to-many) -->
    
      <!-- Optional Field's -->
    <event-handlers>
      <event-handler>full_class_path_of_event_handler_(ISiminovHandler/IDatabaseHandler)</event-handler>
    </event-handlers>
  
  </siminov>

  }
  
    </pre>
  </p>
 *
 */
public class ApplicationDescriptorReader extends SiminovSAXDefaultHandler implements Constants {

  private ApplicationDescriptor applicationDescriptor = null;
  
  private ResourceManager resourceManager = ResourceManager.getInstance();

  private StringBuilder tempValue = new StringBuilder();
  private String propertyName = null;
  
  /**
   * ApplicationDescriptorReader Constructor
   */
  public ApplicationDescriptorReader() {
    
    Context context = resourceManager.getApplicationContext();
    if(context == null) {
      Log.error(getClass().getName(), "Constructor", "Invalid Application Context found.");
      throw new DeploymentException(getClass().getName(), "Constructor", "Invalid Application Context found.");
    }

    /*
     * Parse ApplicationDescriptor.
     */
    InputStream applicationDescriptorStream = null;
    
    try {
      applicationDescriptorStream = context.getAssets().open(APPLICATION_DESCRIPTOR_FILE_NAME);
    } catch(IOException ioException) {
      Log.error(getClass().getName(), "Constructor", "IOException caught while getting input stream of application descriptor, " + ioException.getMessage());
      throw new DeploymentException(getClass().getName(), "Constructor", "IOException caught while getting input stream of application descriptor, " + ioException.getMessage());
    }
    
    try {
      parseMessage(applicationDescriptorStream);
    } catch(Exception exception) {
      Log.error(getClass().getName(), "Constructor", "Exception caught while parsing APPLICATION-DESCRIPTOR, " + exception.getMessage());
      throw new DeploymentException(getClass().getName(), "Constructor", "Exception caught while parsing APPLICATION-DESCRIPTOR, " + exception.getMessage());
    }
    
    doValidation();
  }

  public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

    tempValue = new StringBuilder();
    
    if(localName.equalsIgnoreCase(APPLICATION_DESCRIPTOR_SIMINOV)) {
      applicationDescriptor = new ApplicationDescriptor();
    } else if(localName.equalsIgnoreCase(APPLICATION_DESCRIPTOR_PROPERTY)) {
      initializeProperty(attributes);
    } 
  }
  
  public void characters(char[] ch, int start, int length) throws SAXException {
    String value = new String(ch,start,length);
    
    if(value == null || value.length() <= 0 || value.equalsIgnoreCase(NEW_LINE)) {
      return;
    }
    
    value = value.trim();
    tempValue.append(value);
  }

  public void endElement(String uri, String localName, String qName) throws SAXException {
    
    if(localName.equalsIgnoreCase(APPLICATION_DESCRIPTOR_PROPERTY)) {
      applicationDescriptor.addProperty(propertyName, tempValue.toString());
    } else if(localName.equalsIgnoreCase(APPLICATION_DESCRIPTOR_DATABASE_DESCRIPTOR)) {
      applicationDescriptor.addDatabaseDescriptorPath(tempValue.toString());
    } else if(localName.equalsIgnoreCase(APPLICATION_DESCRIPTOR_EVENT_HANDLER)) {
      
      if(tempValue == null || tempValue.length() <= 0) {
        return;
      }
      
      applicationDescriptor.addEvent(tempValue.toString());
    } else if(localName.equalsIgnoreCase(APPLICATION_DESCRIPTOR_LIBRARY_DESCRIPTOR)) {
      
      if(tempValue == null || tempValue.length() <= 0) {
        return;
      }
      
      applicationDescriptor.addLibraryDescriptorPath(tempValue.toString());
    }
  }
  
  private void initializeProperty(final Attributes attributes) {
    propertyName = attributes.getValue(APPLICATION_DESCRIPTOR_NAME);
  }
  
  private void doValidation() throws DeploymentException {
    
    /*
     * Validate Application Name field.
     */
    String name = applicationDescriptor.getName();
    if(name == null || name.length() <= 0) {
      Log.error(getClass().getName(), "doValidation", "NAME IS MANDATORY FIELD - APPLICATION-DESCRIPTOR");
      throw new DeploymentException(getClass().getName(), "doValidation", "NAME IS MANDATORY FIELD - APPLICATION-DESCRIPTOR");
    }
    
    Iterator<String> databaseDescriptorPaths = applicationDescriptor.getDatabaseDescriptorPaths();
    while(databaseDescriptorPaths.hasNext()) {
      String databaseDescriptorPath = databaseDescriptorPaths.next();
      
      if(!databaseDescriptorPath.contains(SIMINOV_DESCRIPTOR_EXTENSION)) {
        Log.error(getClass().getName(), "doValidation", "INVALID DATABASE DESCRIPTOR PATH FOUND, it should contain .core extension in path, PATH-DEFINED: " + databaseDescriptorPath);
        throw new DeploymentException(getClass().getName(), "doValidation", "INVALID DATABASE DESCRIPTOR PATH FOUND, it should contain .core extension in path, PATH-DEFINED: " + databaseDescriptorPath);
      }
    }
  }

  /**
   * Get application descriptor object. 
   * @return Application Descriptor Object.
   */
  public ApplicationDescriptor getApplicationDescriptor() {
    return this.applicationDescriptor;
  }

}




Java Source Code List

siminov.core.Constants.java
siminov.core.IInitializer.java
siminov.core.Initializer.java
siminov.core.Siminov.java
siminov.core.database.Clause.java
siminov.core.database.DatabaseBundle.java
siminov.core.database.DatabaseFactory.java
siminov.core.database.DatabaseHelper.java
siminov.core.database.DatabaseUtils.java
siminov.core.database.Database.java
siminov.core.database.Where.java
siminov.core.database.design.IAverageClause.java
siminov.core.database.design.IAverage.java
siminov.core.database.design.ICountClause.java
siminov.core.database.design.ICount.java
siminov.core.database.design.IDataTypeHandler.java
siminov.core.database.design.IDatabaseImpl.java
siminov.core.database.design.IDatabase.java
siminov.core.database.design.IDeleteClause.java
siminov.core.database.design.IDelete.java
siminov.core.database.design.IGroupConcatClause.java
siminov.core.database.design.IGroupConcat.java
siminov.core.database.design.IMaxClause.java
siminov.core.database.design.IMax.java
siminov.core.database.design.IMinClause.java
siminov.core.database.design.IMin.java
siminov.core.database.design.IQueryBuilder.java
siminov.core.database.design.ISelectClause.java
siminov.core.database.design.ISelect.java
siminov.core.database.design.ISumClause.java
siminov.core.database.design.ISum.java
siminov.core.database.design.ITotalClause.java
siminov.core.database.design.ITotal.java
siminov.core.database.sqlite.DataTypeHandler.java
siminov.core.database.sqlite.DatabaseImpl.java
siminov.core.database.sqlite.QueryBuilder.java
siminov.core.events.EventHandler.java
siminov.core.events.IDatabaseEvents.java
siminov.core.events.ISiminovEvents.java
siminov.core.exception.DatabaseException.java
siminov.core.exception.DeploymentException.java
siminov.core.exception.IException.java
siminov.core.exception.PrematureEndOfParseException.java
siminov.core.exception.SiminovCriticalException.java
siminov.core.exception.SiminovException.java
siminov.core.log.Log.java
siminov.core.model.ApplicationDescriptor.java
siminov.core.model.DatabaseDescriptor.java
siminov.core.model.DatabaseMappingDescriptor.java
siminov.core.model.IDescriptor.java
siminov.core.model.LibraryDescriptor.java
siminov.core.reader.ApplicationDescriptorReader.java
siminov.core.reader.DatabaseDescriptorReader.java
siminov.core.reader.DatabaseMappingDescriptorReader.java
siminov.core.reader.LibraryDescriptorReader.java
siminov.core.reader.QuickDatabaseMappingDescriptorReader.java
siminov.core.reader.SiminovSAXDefaultHandler.java
siminov.core.resource.ResourceManager.java
siminov.core.utils.ClassUtils.java
siminov.core.utils.EmptyIterator.java
siminov.core.utils.Utils.java