Android Open Source - sodf Sodf Framework






From Project

Back to project page sodf.

License

The source code is released under:

Copyright (c) 2013 Lorenz Lehmann Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Sof...

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

package lal.sodf.framework;
//from   w ww.j  a v  a2 s .  c  o  m
import java.io.IOException;
import java.nio.charset.Charset;

import lal.sodf.framework.compressor.CompressionAlgorithm;
import lal.sodf.framework.compressor.Compressor;
import lal.sodf.framework.compressor.Gzip;
import lal.sodf.framework.exceptions.CompressionAlgorithmNotFoundException;
import lal.sodf.framework.nfc.NfcContentWrapper;
import lal.sodf.framework.nfc.NfcHandler;
import lal.sodf.framework.ontology.SodfTree;
import lal.sodf.framework.parser.SodfParser;
import android.nfc.FormatException;
import android.nfc.Tag;

/**
 * The main class of the framework, which takes care of the entire program flow.
 * @author Lorenz Lehmann
 *
 */
public class SodfFramework {
  /**
   * Write data on a NFC tag
   * @param tag The tag where the data is written on
   * @param data The payload, which will be written on the tag
   * @param app A string to identify the app. This should be the package name of the application under which it is listend in the Google Play store
   */
  public static void writeData(Tag tag, SodfTree data, String app, SodfCallback callback){
    SodfFramework.writeData(tag, data, app, callback, null);
  }
  
  /**
   * Write data on a NFC tag. The data will be compressed using GZIP
   * @param tag The tag where the data is written on
   * @param data The payload, which will be written on the tag
   * @param app A string to identify the app. This should be the package name of the application under which it is listend in the Google Play store
   */
  public static void writeDataCompressed(Tag tag, SodfTree data, String app, SodfCallback callback){
    CompressionAlgorithm ca = new Gzip();
    SodfFramework.writeData(tag, data, app, callback, ca);        
  }
  
  /**
   * Write data on a NFC tag.
   * @param tag The tag where the data is written on
   * @param data The data set, which is written on the tag
   * @throws CompressionAlgorithmNotFoundException If the specified compression in the data was not found and is not null
   */
  public static void writeData(Tag tag, SodfWrapper data, SodfCallback callback) throws CompressionAlgorithmNotFoundException{
    if (data.compressionAlgorithm == null) SodfFramework.writeData(tag, data.data, data.app, callback, null);
    else SodfFramework.writeData(tag, data.data, data.app, callback, Compressor.alogrithmSelector(data.compressionAlgorithm));    
  }
  
  /**
   * Write data on a NFC tag.
   * @param tag The tag where the data is written on
   * @param data The data set, which is written on the tag
   */
  public static void writeDataUncompressed(Tag tag, SodfWrapper data, SodfCallback callback){
    data.compressionAlgorithm = null;//compression has to be null
    SodfFramework.writeData(tag, data.data, data.app, callback, null);  
  }
  
  /** Internal function that wraps all write calls */
  private static void writeData(final Tag tag, final SodfTree data, final String app, final SodfCallback callback, final CompressionAlgorithm ca){
    //prepare a runable to run all the code
    Runnable task = new Runnable(){
      @Override
      public void run() {
        try {
          byte[] writeData = null;
          //use the parser to serialize the data tree
          String serializedTree = SodfParser.serializeTree(data);
          //set the compression parameter
          String compressionName = null;
          if (ca != null){//compress the strings
            Compressor compressor = new Compressor();
            writeData = compressor.compress(serializedTree, ca);
            compressionName = ca.getIdentifier();
          }
          else {//without any compression just write the data as byte array
            writeData = serializedTree.getBytes(Charset.forName("UTF-8"));
          }
          //let the NfcHandler write the data
          NfcHandler.writeData(tag, app, writeData, compressionName);
          //if the write data handler did not throw an exception everything went smooth
          if (callback != null) callback.writeComplete(SodfCallback.WRITE_SUCCESSFUL);
        }
        catch (IOException e){
          if (callback != null) callback.writeComplete(SodfCallback.WRITE_IO_EXCEPTION);
        }
        catch (FormatException e){
          if (callback != null) callback.writeComplete(SodfCallback.WRITE_FORMAT_EXCEPTION);
        }        
      }      
    };
    
    //start the runable in a new thread so it doesn't block UI
    new Thread(task).start();  
  }


  /**
   * Read data from a SODF formatted tag
   * @param tag The tag, which contains the data
   * @param callback The callback, which receives the data from the tag
   */
  public static void readData(final Tag tag, final SodfCallback callback){
    //prepare a runable to run all the code
    Runnable task = new Runnable(){
      @Override
      public void run() {
        try {
          //use the NFC reader to read the data
          NfcContentWrapper nfcData = NfcHandler.readData(tag);
          //if the data was compressed, decompress it
          String data;
          if (nfcData.compression != null){
            Compressor compressor = new Compressor();
            data = compressor.decompress(nfcData.payload, Compressor.alogrithmSelector(nfcData.compression));
          }
          else {
            data = new String(nfcData.payload, Charset.forName("UTF-8"));
          }
          //parse the JSON payload to a tree
          SodfTree tree = SodfParser.deserializeTree(data);
          //pack everything in a wrapper object
          SodfWrapper wrapper = new SodfWrapper(tree, nfcData.app, nfcData.compression);
          if (callback != null) callback.readComplete(wrapper);
        }
        catch (Exception e){
          if (callback != null) callback.readComplete(new SodfWrapper(e));
        }
        }
    };

    //start the runable in a new thread so it doesn't block UI
    new Thread(task).start();  
  }
}




Java Source Code List

lal.apps.ontap.MainActivity.java
lal.apps.ontap.WifiVocabulary.java
lal.apps.smartfoodenvironment.activities.ExpiredActivity.java
lal.apps.smartfoodenvironment.activities.FactoryActivity.java
lal.apps.smartfoodenvironment.activities.MicrowaveActivity.java
lal.apps.smartfoodenvironment.activities.StartActivity.java
lal.apps.smartfoodenvironment.model.MicrowaveVocabulary.java
lal.apps.smartfoodenvironment.model.ProductFactory.java
lal.apps.smartfoodenvironment.model.ProductVocabulary.java
lal.sodf.example.MainActivity.java
lal.sodf.framework.SodfCallback.java
lal.sodf.framework.SodfCallback.java
lal.sodf.framework.SodfCallback.java
lal.sodf.framework.SodfFramework.java
lal.sodf.framework.SodfFramework.java
lal.sodf.framework.SodfFramework.java
lal.sodf.framework.SodfWrapper.java
lal.sodf.framework.SodfWrapper.java
lal.sodf.framework.SodfWrapper.java
lal.sodf.framework.compressor.CompressionAlgorithm.java
lal.sodf.framework.compressor.CompressionAlgorithm.java
lal.sodf.framework.compressor.CompressionAlgorithm.java
lal.sodf.framework.compressor.Compressor.java
lal.sodf.framework.compressor.Compressor.java
lal.sodf.framework.compressor.Compressor.java
lal.sodf.framework.compressor.Gzip.java
lal.sodf.framework.compressor.Gzip.java
lal.sodf.framework.compressor.Gzip.java
lal.sodf.framework.exceptions.CompressionAlgorithmNotFoundException.java
lal.sodf.framework.exceptions.CompressionAlgorithmNotFoundException.java
lal.sodf.framework.exceptions.CompressionAlgorithmNotFoundException.java
lal.sodf.framework.exceptions.DuplicateKeyException.java
lal.sodf.framework.exceptions.DuplicateKeyException.java
lal.sodf.framework.exceptions.DuplicateKeyException.java
lal.sodf.framework.exceptions.MalformedTypeException.java
lal.sodf.framework.exceptions.MalformedTypeException.java
lal.sodf.framework.exceptions.MalformedTypeException.java
lal.sodf.framework.exceptions.TagEmptyException.java
lal.sodf.framework.exceptions.TagEmptyException.java
lal.sodf.framework.exceptions.TagEmptyException.java
lal.sodf.framework.exceptions.UnformattedTagException.java
lal.sodf.framework.exceptions.UnformattedTagException.java
lal.sodf.framework.exceptions.UnformattedTagException.java
lal.sodf.framework.nfc.NfcContentWrapper.java
lal.sodf.framework.nfc.NfcContentWrapper.java
lal.sodf.framework.nfc.NfcContentWrapper.java
lal.sodf.framework.nfc.NfcHandler.java
lal.sodf.framework.nfc.NfcHandler.java
lal.sodf.framework.nfc.NfcHandler.java
lal.sodf.framework.ontology.FunctionsNode.java
lal.sodf.framework.ontology.FunctionsNode.java
lal.sodf.framework.ontology.FunctionsNode.java
lal.sodf.framework.ontology.KeyNode.java
lal.sodf.framework.ontology.KeyNode.java
lal.sodf.framework.ontology.KeyNode.java
lal.sodf.framework.ontology.KeyValueNode.java
lal.sodf.framework.ontology.KeyValueNode.java
lal.sodf.framework.ontology.KeyValueNode.java
lal.sodf.framework.ontology.MetadataNode.java
lal.sodf.framework.ontology.MetadataNode.java
lal.sodf.framework.ontology.MetadataNode.java
lal.sodf.framework.ontology.Node.java
lal.sodf.framework.ontology.Node.java
lal.sodf.framework.ontology.Node.java
lal.sodf.framework.ontology.PropertiesNode.java
lal.sodf.framework.ontology.PropertiesNode.java
lal.sodf.framework.ontology.PropertiesNode.java
lal.sodf.framework.ontology.RestInteractionNode.java
lal.sodf.framework.ontology.RestInteractionNode.java
lal.sodf.framework.ontology.RestInteractionNode.java
lal.sodf.framework.ontology.RestParameters.java
lal.sodf.framework.ontology.RestParameters.java
lal.sodf.framework.ontology.RestParameters.java
lal.sodf.framework.ontology.SodfTree.java
lal.sodf.framework.ontology.SodfTree.java
lal.sodf.framework.ontology.SodfTree.java
lal.sodf.framework.ontology.SodfVocabulary.java
lal.sodf.framework.ontology.SodfVocabulary.java
lal.sodf.framework.ontology.SodfVocabulary.java
lal.sodf.framework.parser.SodfParser.java
lal.sodf.framework.parser.SodfParser.java
lal.sodf.framework.parser.SodfParser.java