better JS metablock parsing...returned map now contains all key/values of metablock
This commit is contained in:
parent
a86872d051
commit
e1c047c4b4
@ -22,6 +22,7 @@ import com.cradle.iitc_mobile.IITC_Mobile.ResponseHandler;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
@ -33,6 +34,7 @@ import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PipedInputStream;
|
||||
import java.io.PipedOutputStream;
|
||||
import java.io.StringReader;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.net.URLEncoder;
|
||||
@ -81,38 +83,32 @@ public class IITC_FileManager {
|
||||
public static HashMap<String, String> getScriptInfo(final String js) {
|
||||
final HashMap<String, String> map = new HashMap<String, String>();
|
||||
String header = "";
|
||||
// get metadata of javascript file
|
||||
if (js != null && js.contains("==UserScript==") && js.contains("==/UserScript==")) {
|
||||
header = js.substring(js.indexOf("==UserScript=="),
|
||||
js.indexOf("==/UserScript=="));
|
||||
}
|
||||
// remove new line comments
|
||||
header = header.replace("\n//", " ");
|
||||
// get a list of key-value
|
||||
final String[] attributes = header.split(" +");
|
||||
// add default values
|
||||
map.put("id", "unknown");
|
||||
map.put("version", "not found");
|
||||
map.put("name", "unknown");
|
||||
map.put("description", "");
|
||||
map.put("category", "Misc");
|
||||
// add parsed values
|
||||
for (int i = 0; i < attributes.length; i++) {
|
||||
// search for attributes and use the value
|
||||
if (attributes[i].equals("@id")) {
|
||||
map.put("id", attributes[i + 1]);
|
||||
}
|
||||
if (attributes[i].equals("@version")) {
|
||||
map.put("version", attributes[i + 1]);
|
||||
}
|
||||
if (attributes[i].equals("@name")) {
|
||||
map.put("name", attributes[i + 1]);
|
||||
}
|
||||
if (attributes[i].equals("@description")) {
|
||||
map.put("description", attributes[i + 1]);
|
||||
}
|
||||
if (attributes[i].equals("@category")) {
|
||||
map.put("category", attributes[i + 1]);
|
||||
BufferedReader reader = new BufferedReader(new StringReader(header));
|
||||
String headerLine;
|
||||
try {
|
||||
while ((headerLine = reader.readLine()) != null) {
|
||||
if (headerLine.matches("//.*@.*")) {
|
||||
// get start of key name (first @ in line)
|
||||
String[] keyStart = headerLine.split("@", 2);
|
||||
// split key value
|
||||
String[] keyValue = keyStart[1].split(" ", 2);
|
||||
// remove whitespaces from string begin and end and push to map
|
||||
map.put(keyValue[0].trim(), keyValue[1].trim());
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Log.w(e);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user