<commit_before>package com.parsa_plm.Layout;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;

import com.jointelementinspector.main.ExpandableListHeader;
import com.jointelementinspector.main.ExpandableListItem;
import com.jointelementinspector.main.MainActivity;
import com.jointelementinspector.main.R;
import com.jointelementinspector.main.WeldPoint;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

public class OpenFileActivity extends Activity implements IFolderItemListener {
    private FolderLayout localFolders;
    // 20170106: we use external storage path to open xml file
    // private final String filePath = "/sdcard/Download";
    private Context mContext;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.folders);
        this.mContext = getApplicationContext();
        localFolders = (FolderLayout) findViewById(R.id.localFolders);
        localFolders.setIFolderItemListener(this);
        // 20161223: test internal storage path
        File path = this.getExternalFilesDir(null);
        localFolders.setDir(path.toString());
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void OnCannotFileRead(File file) {
        new AlertDialog.Builder(this)
                .setIcon(R.drawable.viewfile48)
                .setTitle(file.getName() + "folder can't be read!")
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                }).show();
    }

    @Override
    public void OnFileClicked(final File file) {
        final ExpandableListHeader expandableListHeader = null;
        AlertDialog.Builder adb = new AlertDialog.Builder(this);
        if (!file.getName().toLowerCase().endsWith("xml")) {
            adb.setIcon(R.drawable.viewfile48);
            adb.setMessage("Only File with .xml extension is accepted.");
            adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.cancel();
                }
            });
            adb.show();
        } else {
            adb.setIcon(R.drawable.viewfile48);
            adb.setTitle("Open File: " + file.getName());
            adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // 20161125: use async task to process file
                    new ParseXMLFileTask(OpenFileActivity.this, file.getPath()).execute();
                }
            });
            adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            adb.show();
        }
    }

    public boolean notNullAndEmpty(String s) {
        return s != null && !s.isEmpty();
    }

    // TRIM() !!!
    public String findFormNameById(NodeList nodeList, String id) {
        String formName = null;

        if (notNullAndEmpty(id)) return null;
        if (nodeList.getLength() == 0) return null;
        for (int k = 0; k < nodeList.getLength(); k++) {
            Element ele = (Element) nodeList.item(k);
            String idOfForm = ele.getAttribute("id");
            if (notNullAndEmpty(idOfForm)) {
                if (id.trim().equalsIgnoreCase(idOfForm.trim())) {
                    Log.d("xml", "form gefunden");
                    formName = ele.getAttribute("name");
                    break;
                }
            }
        }
        return formName;
    }

    private class ParseXMLFileTask extends AsyncTask<File, Void, ExpandableListHeader> {
        private ProgressDialog mProgressDialog;
        private String mFilePath;
        private Context mContext;

        ParseXMLFileTask(Context context, String filePath) {
            this.mFilePath = filePath;
            this.mContext = context;
        }

        protected void onPreExecute() {
            this.mProgressDialog = new ProgressDialog(this.mContext);
            this.mProgressDialog.setMessage("   wird geladen ...   ");
            this.mProgressDialog.setCancelable(false);
            this.mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            this.mProgressDialog.show();
        }

        // 20161214 need to expand later, method exacting for better maintain later
        protected ExpandableListHeader doInBackground(File... file) {
            // using Parcelable to send custom data
            // set default parameter to NotFound, if there is some thing wrong with xml parse<commit_msg>add visual viewer fragment for 3d viewer<commit_after>package com.parsa_plm.Layout;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;

import com.jointelementinspector.main.ExpandableListHeader;
import com.jointelementinspector.main.ExpandableListItem;
import com.jointelementinspector.main.MainActivity;
import com.jointelementinspector.main.R;
import com.jointelementinspector.main.WeldPoint;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

public class OpenFileActivity extends Activity implements IFolderItemListener {
    private FolderLayout localFolders;
    // 20170106: we use external storage path to open xml file
    // private final String filePath = "/sdcard/Download";
    private Context mContext;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.folders);
        this.mContext = getApplicationContext();
        localFolders = (FolderLayout) findViewById(R.id.localFolders);
        localFolders.setIFolderItemListener(this);
        // 20161223: test internal storage path
        File path = this.getExternalFilesDir(null);
        localFolders.setDir(path.toString());
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void OnCannotFileRead(File file) {
        new AlertDialog.Builder(this)
                .setIcon(R.drawable.viewfile48)
                .setTitle(file.getName() + "folder can't be read!")
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                }).show();
    }

    @Override
    public void OnFileClicked(final File file) {
        final ExpandableListHeader expandableListHeader = null;
        AlertDialog.Builder adb = new AlertDialog.Builder(this);
        if (!file.getName().toLowerCase().endsWith("xml")) {
            adb.setIcon(R.drawable.viewfile48);
            adb.setMessage("Only File with .xml extension is accepted.");
            adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.cancel();
                }
            });
            adb.show();
        } else {
            adb.setIcon(R.drawable.viewfile48);
            adb.setTitle("Open File: " + file.getName());
            adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // 20161125: use async task to process file
                    new ParseXMLFileTask(OpenFileActivity.this, file.getPath()).execute();
                }
            });
            adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            adb.show();
        }
    }

    public boolean notNullAndEmpty(String s) {
        return s != null && !s.isEmpty();
    }

    // TRIM() !!!
    public String findFormNameById(NodeList nodeList, String id) {
        String formName = null;

        if (notNullAndEmpty(id)) return null;
        if (nodeList.getLength() == 0) return null;
        for (int k = 0; k < nodeList.getLength(); k++) {
            Element ele = (Element) nodeList.item(k);
            String idOfForm = ele.getAttribute("id");
            if (notNullAndEmpty(idOfForm)) {
                if (id.trim().equalsIgnoreCase(idOfForm.trim())) {
                    Log.d("xml", "form gefunden");
                    formName = ele.getAttribute("name");
                    break;
                }
            }
        }
        return formName;
    }

    private class ParseXMLFileTask extends AsyncTask<File, Void, ExpandableListHeader> {
        private ProgressDialog mProgressDialog;
        private String mFilePath;
        private Context mContext;

        ParseXMLFileTask(Context context, String filePath) {
            this.mFilePath = filePath;
            this.mContext = context;
        }

        protected void onPreExecute() {
            this.mProgressDialog = new ProgressDialog(this.mContext);
            this.mProgressDialog.setMessage("   wird geladen ...   ");
            this.mProgressDialog.setCancelable(false);
            this.mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            this.mProgressDialog.show();
        }

        // 20161214 need to expand later, method exacting for better maintain later
        // 20170113: two methods extracted, better readable code later
        protected ExpandableListHeader doInBackground(File... file) {
            // using Parcelable to send custom data
            // set default parameter to NotFound, if there is some thing wrong with xml parse
{'fraction_non_alphanumeric': 0.09596687072380267, 'fraction_numerical': 0.008732445084623695, 'mean_word_length': 2.3684050939963615, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 16}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3400457', 'n_tokens_mistral': 3041, 'n_tokens_neox': 2860, 'n_words': 669}