summaryrefslogtreecommitdiffstats
path: root/Ministro/src/org/kde/necessitas/ministro/Library.java
blob: 0646d8667542ed57d0c9cd0121d32000d3f22b2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
    Copyright (c) 2011, BogDan Vatra <bog_dan_ro@yahoo.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.kde.necessitas.ministro;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

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

import android.os.Bundle;


class Library
{
    public String name = null;
    public String filePath = null;
    public String[] depends = null;
    public String[] replaces = null;
    public NeedsStruct[] needs = null;
    public int level=0;
    public long size = 0;
    public String sha1 = null;
    public String url;

    public static String[] getLibNames(Element libNode)
    {
        if (libNode == null)
            return null;
        NodeList list=libNode.getElementsByTagName("lib");
        ArrayList<String> libs = new ArrayList<String>();
        for (int i=0;i<list.getLength();i++)
        {
            if (list.item(i).getNodeType() != Node.ELEMENT_NODE)
                continue;
            Element lib=(Element)list.item(i);
            if (lib!=null)
                libs.add(lib.getAttribute("name"));
        }
        String[] strings = new String[libs.size()];
        return libs.toArray(strings);
    }

    public static NeedsStruct[] getNeeds(Element libNode)
    {
        if (libNode == null)
            return null;
        NodeList list=libNode.getElementsByTagName("item");
        ArrayList<NeedsStruct> needs = new ArrayList<NeedsStruct>();

        for (int i=0;i<list.getLength();i++)
        {
            if (list.item(i).getNodeType() != Node.ELEMENT_NODE)
                continue;
            Element lib=(Element)list.item(i);
            if (lib!=null)
            {
                NeedsStruct need=new NeedsStruct();
                need.name=lib.getAttribute("name");
                need.filePath=lib.getAttribute("file");
                need.url=lib.getAttribute("url");
                need.sha1=lib.getAttribute("sha1");
                need.size=Long.valueOf(lib.getAttribute("size"));
                if ( lib.hasAttribute("type") )
                    need.type=lib.getAttribute("type");
                needs.add(need);
            }
        }
        NeedsStruct[] _needs = new NeedsStruct[needs.size()];
        return needs.toArray(_needs);
    }

    public static  Library getLibrary(Element libNode, boolean includeNeed)
    {
        Library lib= new Library();
        lib.name=libNode.getAttribute("name");
        lib.sha1=libNode.getAttribute("sha1").toUpperCase();
        lib.filePath=libNode.getAttribute("file");
        lib.url=libNode.getAttribute("url");
        try
        {
            lib.level=Integer.parseInt(libNode.getAttribute("level"));
        } catch (Exception e) {
            e.printStackTrace();
        }

        try
        {
            lib.size=Long.parseLong(libNode.getAttribute("size"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        NodeList list=libNode.getElementsByTagName("depends");
        for (int i=0;i<list.getLength();i++)
        {
            if (list.item(i).getNodeType() != Node.ELEMENT_NODE)
                continue;
            lib.depends=getLibNames((Element) list.item(i));
            break;
        }
        list=libNode.getElementsByTagName("replaces");
        for (int i=0;i<list.getLength();i++)
        {
            if (list.item(i).getNodeType() != Node.ELEMENT_NODE)
                continue;
            lib.replaces=getLibNames((Element) list.item(i));
            break;
        }

        if (!includeNeed) // don't waste time.
            return lib;

        list=libNode.getElementsByTagName("needs");
        for (int i=0;i<list.getLength();i++)
        {
            if (list.item(i).getNodeType() != Node.ELEMENT_NODE)
                continue;
            lib.needs=getNeeds((Element) list.item(i));
            break;
        }
        return lib;
    }

    public static String convertToHex(byte[] data)
    {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < data.length; i++)
        {
            int halfbyte = (data[i] >>> 4) & 0x0F;
            int two_halfs = 0;
            do
            {
                if ((0 <= halfbyte) && (halfbyte <= 9))
                    buf.append((char) ('0' + halfbyte));
                else
                    buf.append((char) ('a' + (halfbyte - 10)));
                halfbyte = data[i] & 0x0F;
            } while(two_halfs++ < 1);
        }
        return buf.toString();
    }

    public static boolean checkCRC(String fileName, String sha1) throws IOException
    {
        try
        {
            byte[] tmp = new byte[2048];
            MessageDigest digester = MessageDigest.getInstance("SHA-1");
            int downloaded;
            FileInputStream inFile = new FileInputStream(new File(fileName));
            while ((downloaded = inFile.read(tmp)) != -1)
            {
                digester.update(tmp, 0, downloaded);
            }
            return sha1.equalsIgnoreCase(convertToHex(digester.digest()));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static String mkdirParents(String rootPath, String filePath, int skip)
    {
        String[] paths=filePath.split("/");
        String path = "";
        for (int pit=0;pit<paths.length-skip;pit++)
        {
            if (paths[pit].length()==0)
                continue;
            path+="/"+paths[pit];
            File dir=new File(rootPath+path);
            dir.mkdir();
            MinistroActivity.nativeChmode(rootPath+path, 0755);
        }
        return rootPath+path;
    }

    public static void removeAllFiles(String path)
    {
        String files[]=new File(path).list();
        if (!path.endsWith("/"))
            path+="/";
        for (int i=0;i<files.length;i++)
            new File(path+files[i]).delete();
    }

    public static String join(Collection<String> s, String delimiter)
    {
        if (s == null || s.isEmpty()) return "";
        Iterator<String> iter = s.iterator();
        StringBuilder builder = new StringBuilder(iter.next());
        while( iter.hasNext() )
        {
            builder.append(delimiter).append(iter.next());
        }
        return builder.toString();
    }

    public static void mergeBundleParameters(Bundle out, String outKey, Bundle in, String inKey)
    {
        if (!in.containsKey(inKey))
            return;

        String value = null;
        if (out.containsKey(outKey))
            value=out.getString(outKey);

        if (value!=null && value.length()>0 && value.charAt(value.length()-1)!='\t')
            value=value+"\t";

        value=value+in.getString(inKey);
        out.putString(outKey, value);
    }
};

class NeedsStruct
{
    public String name = null;
    public String filePath = null;
    public String sha1 = null;
    public String url = null;
    public String type = null;
    public long size = 0;
};