better JS metablock parsing...returned map now contains all key/values of metablock

This commit is contained in:
Philipp Schaefer 2014-02-26 18:27:35 +01:00
parent a86872d051
commit e1c047c4b4

View File

@ -22,6 +22,7 @@ import com.cradle.iitc_mobile.IITC_Mobile.ResponseHandler;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
@ -33,6 +34,7 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.PipedInputStream; import java.io.PipedInputStream;
import java.io.PipedOutputStream; import java.io.PipedOutputStream;
import java.io.StringReader;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.net.URLEncoder; import java.net.URLEncoder;
@ -81,38 +83,32 @@ public class IITC_FileManager {
public static HashMap<String, String> getScriptInfo(final String js) { public static HashMap<String, String> getScriptInfo(final String js) {
final HashMap<String, String> map = new HashMap<String, String>(); final HashMap<String, String> map = new HashMap<String, String>();
String header = ""; String header = "";
// get metadata of javascript file
if (js != null && js.contains("==UserScript==") && js.contains("==/UserScript==")) { if (js != null && js.contains("==UserScript==") && js.contains("==/UserScript==")) {
header = js.substring(js.indexOf("==UserScript=="), header = js.substring(js.indexOf("==UserScript=="),
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 // add default values
map.put("id", "unknown"); map.put("id", "unknown");
map.put("version", "not found"); map.put("version", "not found");
map.put("name", "unknown"); map.put("name", "unknown");
map.put("description", ""); map.put("description", "");
map.put("category", "Misc"); map.put("category", "Misc");
// add parsed values BufferedReader reader = new BufferedReader(new StringReader(header));
for (int i = 0; i < attributes.length; i++) { String headerLine;
// search for attributes and use the value try {
if (attributes[i].equals("@id")) { while ((headerLine = reader.readLine()) != null) {
map.put("id", attributes[i + 1]); 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());
} }
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]);
} }
} catch (IOException e) {
Log.w(e);
} }
return map; return map;
} }