OSDN Git Service

a9b3364010f26890154add95c142745fc5188912
[nicownn/NicoWnn.git] / src / com / hiroshica / android / input / nicownn2 / UserDicImportExport.java
1 package com.hiroshica.android.input.nicownn2;
2
3 import android.os.AsyncTask;
4 import android.os.Environment;
5 import android.util.Log;
6
7 import java.util.ArrayList;
8 //import java.io.File;
9 //import java.io.FileInputStream;
10 //import java.io.FileOutputStream;
11 import java.io.*;
12 import org.xmlpull.v1.XmlPullParser;
13 import org.xmlpull.v1.XmlPullParserFactory;
14
15
16
17
18 public class UserDicImportExport extends AsyncTask<String, String, String[]>{
19         private UserDictionaryToolsList mActivity;
20         
21         public UserDicImportExport(UserDictionaryToolsList activity) {
22                 mActivity  = activity;
23         }
24         @Override
25         protected void onPreExecute() {
26                 super.onPreExecute();
27                 mActivity.createProgressDialog();
28         }
29
30         @Override
31         protected String[] doInBackground(String... params)  {
32                 boolean result = false;
33                 String[]  resultString = new String[2];
34                 if (params[0].equals("import")) {
35                         result = importUserDic(params[1], params[2], params[3]);
36                         if (true == result) {
37                                 resultString[0] = "true";
38                                 resultString[1] = mActivity.getString(R.string.dialog_import_dic_message_done);
39                         }
40                         else{
41                                 resultString[0] = "false";
42                                 resultString[1] = mActivity.getString(R.string.dialog_import_dic_message_failed);
43                         }
44                 }
45                 else{
46                         resultString = exportUserDic(params[1], params[2], params[3]);
47                         /*
48                         if (true == result) {
49                                 resultString[0] = "true";
50                                 resultString[1] = mActivity.getString(R.string.dialog_export_dic_message_done);
51                         }
52                         else{
53                                 resultString[0] = "false";
54                                 resultString[1] = mActivity.getString(R.string.dialog_export_dic_message_failed);
55                         }
56                         */
57                 }
58                 return resultString;
59         }
60         @Override
61         protected void onPostExecute(String[] result) {
62                 mActivity.removeProgressDialog(result);
63         }
64         @Override
65         protected void onCancelled() {
66                 super.onCancelled();
67         }
68
69         /*
70          *
71          */
72         private boolean importUserDic(String file, String sd, String flash) {
73                 File fileSdCard = getExternalStorageDirectory();
74                 File fileBase = null;
75                 if (fileSdCard != null) {
76                         //fileBase = new File(fileSdCard, mActivity.getPackageName());
77                         fileBase = new File(fileSdCard, "nicoWnn");
78                         if (!fileBase.exists()) {
79                                 return false;
80                         }
81                 }
82                 // import learn dic
83                 File sFile = new File(fileBase, sd);
84                 File dFile = new File(flash);
85                 if (false == copyFile(dFile, sFile)) {
86                         return false;
87                 }
88                 Log.d("load", "finish import learn dic!!\n");
89
90                 ArrayList<WnnWord> importList = new ArrayList<WnnWord>();
91                 try {
92                         File fileLoad = new File(fileBase, file);
93                         Log.d("load", "create fileload\n");
94                         FileInputStream fin = new FileInputStream(fileLoad);
95                         Log.d("load", "create inputstream\n");
96                         final XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
97                         final XmlPullParser parser = factory.newPullParser();
98                         parser.setInput(fin, "UTF8");
99                         Log.d("load", "create XML parser\n");
100                         int eventType;                  
101                         String tagName;
102                         String tagText;
103                         for(eventType = parser.getEventType(); eventType != XmlPullParser.END_DOCUMENT; eventType = parser.next()){
104                                 tagName = parser.getName();
105                                 if (eventType != XmlPullParser.START_TAG)    continue;
106                                 if (tagName == null)    continue;
107                                 if (!tagName.equals("dicword"))    continue;
108                                 // entry word
109                                 WnnWord newword = new WnnWord();
110                                 newword.stroke    = parser.getAttributeValue(null,"stroke").replaceAll("\"", "");
111                                 Log.d("load", "get stroke = "+newword.stroke+"\n");
112                                 do {
113                                         eventType = parser.next();
114                                         if (eventType == XmlPullParser.TEXT) {
115                                                 newword.candidate = parser.getText().replaceAll("\"", "");;
116                                                 Log.d("load", "get candidate = "+newword.candidate+"\n");
117                                                 break;
118                                         }
119                                 } while (eventType == XmlPullParser.END_DOCUMENT);
120                                 if (eventType == XmlPullParser.END_DOCUMENT) {
121                                         break;
122                                 }
123                                 importList.add(importList.size(), newword);
124                         }
125                 } catch (Exception e) {
126                         return false;
127                 } finally {
128                         //
129                 }
130                 // set new wordlist
131                 Log.d("load", "set new wordlist\n");
132                 int size = importList.size();
133                 for (int iI = 0; iI < size; ++iI) {
134                         WnnWord getword = importList.get(iI);
135                         boolean result = mActivity.addImportWord(getword);
136                         if (false == result) {
137                                 return false;
138                         }
139                 }
140                 Log.d("load", "finish import!!\n");
141                 return true;
142         }
143         /*
144          *
145          */
146         private String[] exportUserDic(String file, String flash, String sd) {
147                 String[] resultString = new String[2];
148                 resultString[0] = "true";
149                 resultString[1] = mActivity.getString(R.string.dialog_export_dic_message_done);
150
151                 File fileSdCard = getExternalStorageDirectory();
152                 File fileBase = null;
153                 if (fileSdCard != null) {
154                         //fileBase = new File(fileSdCard, mActivity.getPackageName());
155                         fileBase = new File(fileSdCard, "nicoWnn");
156                         if (!fileBase.exists()) {
157                                 if (!fileBase.mkdir()) {
158                                         resultString[0] = "false";
159                                         resultString[1] = mActivity.getString(R.string.dialog_export_dic_message_failed);
160                                         return resultString;
161                                 } // mkdir
162                         } // exists
163                 } // sd card
164                 else{
165                         resultString[0] = "false";
166                         resultString[1] = mActivity.getString(R.string.dialog_export_dic_message_failed);
167                         return resultString;
168                 }
169                 try {
170                         File fileSave = new File(fileBase, file);
171                         FileOutputStream fout = new FileOutputStream(fileSave);
172                         // output XML header
173                         String header = new String("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
174                         String top    = new String("<wordlist>\n");
175                         String end    = new String("</wordlist>\n");
176
177                         fout.write(header.getBytes());
178                         fout.write(top.getBytes());
179                         // create data
180                         int size = mActivity.getWordListSize();
181                         WnnWord getword;
182                         for (int iI = 0; iI < size; ++iI) {
183                                 getword = mActivity.getWnnWord(iI);
184                                 String outstring = new String("  <dicword stroke=\"" + getword.stroke + "\">\"" + getword.candidate + "\"</dicword>\n");
185                                 fout.write(outstring.getBytes());
186                         }
187                         fout.write(end.getBytes());
188                 } catch (Exception e) {
189                         resultString[0] = "false";
190                         resultString[1] = mActivity.getString(R.string.dialog_export_dic_message_failed);
191                         return resultString;
192                 } finally {
193                         //
194                 }
195                 // export learn dic
196                 File sFile = new File(flash);
197                 File dFile = new File(fileBase, sd);
198                 copyFile(dFile, sFile);
199                 return resultString;
200         }
201         /*************************************************************************************/
202     /* file load/save                                                                    */
203     /*************************************************************************************/
204         /*
205          * 
206          */
207         private File getExternalStorageDirectory() {
208                 boolean state = Environment.getExternalStorageState().contains(Environment.MEDIA_MOUNTED);
209                 if (false == state) {
210                         Log.d("sdcard", "not mount sdcard!!\n");
211                         return null;
212                 }
213                 Log.d("sdcard", "mount sdcard!!\n");
214                 return Environment.getExternalStorageDirectory();
215         }
216         private boolean copyFile(File dFile, File sFile) {
217                 if (!sFile.exists()) {
218                         return false;
219                 }
220                 InputStream input = null;
221                 OutputStream output = null;
222                 try {
223                         input  = new FileInputStream(sFile);
224                         output = new FileOutputStream(dFile);
225                         int DEFAULT_BUFFER_SIZE = 1024 * 4;
226                         byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
227                         int n = 0;
228                         while (-1 != (n = input.read(buffer))) {
229                                 output.write(buffer, 0, n);
230                         }
231                         input.close();
232                         output.close();
233                 } catch (Exception e) {
234                         return false;
235                 }
236                 return true;
237         }
238 }
239 /****************************** end of file ******************************/