Wiki

Clone wiki

SuperSimpleSemantics / Example custom file manager

Because the SSS needs to load files from either local or remote sources, or both, it needs functions supplied to load them.

Desktop, Android and Web all need different file access code. Potentially other platforms might need different ones too. For this reason you can supply your own.

If you just want to use the inbuilt one, however, you can just use;

#!java

SuperSimpleSemantics.setFileManager(JavaFileManager.internalFileManager);

To set it to use its internal file manager thats suitable for desktop java.

Example code of a file manager:

Note; SSSGenericFileManager is the interface your required to implement for SSS to understand your file loader.

#!java


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.logging.Logger;

import com.darkflame.client.interfaces.SSSGenericFileManager;


public class FileManager implements SSSGenericFileManager {

    static Logger Log = Logger.getLogger("FileManager");
    @Override
    public void getText(String location,
            FileCallbackRunnable runoncomplete, FileCallbackError runonerror,
            boolean forcePost, boolean useCache) {
        // TODO Auto-generated method stub

    }

    @Override
    public void getText(String location,
            FileCallbackRunnable runoncomplete, FileCallbackError runonerror,
            boolean forcePost) {

        if (!location.contains("://")){
           getLocalFile(location, runoncomplete, runonerror);
        } else {
           getFromURL(location, runoncomplete, runonerror);
        }



    }

    private void getFromURL(String location,
            FileCallbackRunnable runoncomplete, FileCallbackError runonerror) {

        URL myUrl;
        try {
            myUrl = new URL(location);
        } catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            Log.info(" couldn't get remote file;"+e1.getLocalizedMessage());
            runonerror.run(e1.getLocalizedMessage(), e1);
            return;
        }



        try {

            BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                    myUrl.openStream()));

            String line="";
            String file="";

            while ((line = in.readLine()) != null){

                file=file+"\n"+line;

            }
              in.close();

              runoncomplete.run(file, 200);

        } catch (IOException e) {
            Log.info(" couldn't get remote file;"+e.getLocalizedMessage());
            runonerror.run(e.getLocalizedMessage(), e);
        }



    }

    private void getLocalFile(String location,
            FileCallbackRunnable runoncomplete, FileCallbackError runonerror) {
        try {

            String contents = new String(Files.readAllBytes(Paths.get(location)));



            Log.info(" got contents="+contents);

            //fire runnable
            runoncomplete.run(contents, 200);

        } catch (IOException e) {


            Log.info(" couldn't get file;"+e.getLocalizedMessage());
            runonerror.run(e.getLocalizedMessage(), e);

        }
    }

}

Updated