Reformat of parse package

This commit is contained in:
Jeroen Ketelaar
2014-02-14 03:41:41 +01:00
parent b97c022784
commit a5b1977731
6 changed files with 742 additions and 760 deletions
@@ -1,488 +1,480 @@
package org.parabot.core.parsers;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.parabot.core.asm.adapters.AddInterfaceAdapter;
import org.parabot.core.asm.interfaces.Injectable;
import org.parabot.core.asm.wrappers.Callback;
import org.parabot.core.asm.wrappers.Getter;
import org.parabot.core.asm.wrappers.Interface;
import org.parabot.core.asm.wrappers.Invoker;
import org.parabot.core.asm.wrappers.Setter;
import org.parabot.core.asm.wrappers.Super;
import org.parabot.core.asm.wrappers.*;
import org.parabot.environment.api.utils.WebUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
/**
*
* Parses an XML files which injects the hooks and other bytecode manipulation
* methods
*
*
* @author Everel
*
*/
public class HookParser {
private Document doc = null;
private boolean parsedInterfaces = false;
private HashMap<String, String> interfaceMap = new HashMap<String, String>();
private Document doc = null;
private boolean parsedInterfaces = false;
private HashMap<String, String> interfaceMap = new HashMap<String, String>();
private HashMap<String, String> constants = new HashMap<String, String>();
private HashMap<String, String> constants = new HashMap<String, String>();
public HookParser(URL url) {
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
doc = dBuilder.parse(WebUtil.getInputStream(url));
doc.getDocumentElement().normalize();
if (!doc.getDocumentElement().getNodeName().equals("injector")) {
throw new RuntimeException("Incorrect hook file.");
}
} catch (Throwable t) {
throw new RuntimeException("Unable to parse hooks " + t);
}
}
public HookParser(URL url) {
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
doc = dBuilder.parse(WebUtil.getInputStream(url));
doc.getDocumentElement().normalize();
if (!doc.getDocumentElement().getNodeName().equals("injector")) {
throw new RuntimeException("Incorrect hook file.");
}
} catch (Throwable t) {
throw new RuntimeException("Unable to parse hooks " + t);
}
}
public final Interface[] getInterfaces() {
parsedInterfaces = true;
final NodeList interfaceRootList = doc
.getElementsByTagName("interfaces");
switch (interfaceRootList.getLength()) {
case 0:
return null;
case 1:
break;
default:
throw new RuntimeException(
"Hook file may not contains multiple <interfaces> tags ");
}
final Node node = interfaceRootList.item(0);
if (node.getNodeType() != Node.ELEMENT_NODE) {
return null;
}
final Element interfaceRoot = (Element) node;
final NodeList interfaces = interfaceRoot.getElementsByTagName("add");
if (interfaces.getLength() == 0) {
return null;
}
final ArrayList<Interface> interfaceList = new ArrayList<Interface>();
for (int x = 0; x < interfaces.getLength(); x++) {
final Node n = interfaces.item(x);
if (n.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
final Element addInterface = (Element) n;
final String className = getValue("classname", addInterface);
final String interfaceClass = getValue("interface", addInterface);
interfaceMap.put(interfaceClass, className);
final Interface inf = new Interface(className, interfaceClass);
interfaceList.add(inf);
}
return interfaceList.toArray(new Interface[interfaceList.size()]);
}
public final Interface[] getInterfaces() {
parsedInterfaces = true;
final NodeList interfaceRootList = doc
.getElementsByTagName("interfaces");
switch (interfaceRootList.getLength()) {
case 0:
return null;
case 1:
break;
default:
throw new RuntimeException(
"Hook file may not contains multiple <interfaces> tags ");
}
final Node node = interfaceRootList.item(0);
if (node.getNodeType() != Node.ELEMENT_NODE) {
return null;
}
final Element interfaceRoot = (Element) node;
final NodeList interfaces = interfaceRoot.getElementsByTagName("add");
if (interfaces.getLength() == 0) {
return null;
}
final ArrayList<Interface> interfaceList = new ArrayList<Interface>();
for (int x = 0; x < interfaces.getLength(); x++) {
final Node n = interfaces.item(x);
if (n.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
final Element addInterface = (Element) n;
final String className = getValue("classname", addInterface);
final String interfaceClass = getValue("interface", addInterface);
interfaceMap.put(interfaceClass, className);
final Interface inf = new Interface(className, interfaceClass);
interfaceList.add(inf);
}
return interfaceList.toArray(new Interface[interfaceList.size()]);
}
public final Super[] getSupers() {
final NodeList interfaceRootList = doc.getElementsByTagName("supers");
switch (interfaceRootList.getLength()) {
case 0:
return null;
case 1:
break;
default:
throw new RuntimeException(
"Hook file may not contains multiple <supers> tags ");
}
final Node node = interfaceRootList.item(0);
if (node.getNodeType() != Node.ELEMENT_NODE) {
return null;
}
final Element superRoot = (Element) node;
final NodeList supers = superRoot.getElementsByTagName("add");
if (supers.getLength() == 0) {
return null;
}
final ArrayList<Super> superList = new ArrayList<Super>();
for (int x = 0; x < supers.getLength(); x++) {
final Node n = supers.item(x);
if (n.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
final Element addSuper = (Element) n;
final String className = getValue("classname", addSuper);
final String superClass = getValue("super", addSuper);
final Super sup = new Super(className, superClass);
superList.add(sup);
}
return superList.toArray(new Super[superList.size()]);
}
public final Super[] getSupers() {
final NodeList interfaceRootList = doc.getElementsByTagName("supers");
switch (interfaceRootList.getLength()) {
case 0:
return null;
case 1:
break;
default:
throw new RuntimeException(
"Hook file may not contains multiple <supers> tags ");
}
final Node node = interfaceRootList.item(0);
if (node.getNodeType() != Node.ELEMENT_NODE) {
return null;
}
final Element superRoot = (Element) node;
final NodeList supers = superRoot.getElementsByTagName("add");
if (supers.getLength() == 0) {
return null;
}
final ArrayList<Super> superList = new ArrayList<Super>();
for (int x = 0; x < supers.getLength(); x++) {
final Node n = supers.item(x);
if (n.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
final Element addSuper = (Element) n;
final String className = getValue("classname", addSuper);
final String superClass = getValue("super", addSuper);
final Super sup = new Super(className, superClass);
superList.add(sup);
}
return superList.toArray(new Super[superList.size()]);
}
public final Getter[] getGetters() {
final NodeList getterRootList = doc.getElementsByTagName("getters");
switch (getterRootList.getLength()) {
case 0:
return null;
case 1:
break;
default:
throw new RuntimeException(
"Hook file may not contains multiple <getters> tags ");
}
final Node node = getterRootList.item(0);
if (node.getNodeType() != Node.ELEMENT_NODE) {
return null;
}
final Element getterRoot = (Element) node;
final NodeList getters = getterRoot.getElementsByTagName("add");
if (getters.getLength() == 0) {
return null;
}
final ArrayList<Getter> getterList = new ArrayList<Getter>();
for (int x = 0; x < getters.getLength(); x++) {
final Node n = getters.item(x);
if (n.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
final Element addGetter = (Element) n;
if (isSet("classname", addGetter) && isSet("accessor", addGetter)) {
throw new RuntimeException(
"Can't set classname and accessor tag together.");
}
if (isSet("accessor", addGetter) && !parsedInterfaces) {
throw new RuntimeException(
"You'll need to parse interfaces first.");
}
final String className = isSet("classname", addGetter) ? getValue(
"classname", addGetter) : interfaceMap.get(getValue(
"accessor", addGetter));
final String into = isSet("into", addGetter) ? getValue("into",
addGetter) : className;
final String fieldName = getValue("field", addGetter);
final String methodName = getValue("methodname", addGetter);
boolean staticMethod = isSet("methstatic", addGetter) ? (getValue(
"methstatic", addGetter).equals("true")) : false;
String returnDesc = isSet("desc", addGetter) ? getValue("desc",
addGetter) : null;
String array = "";
if (returnDesc != null && returnDesc.contains("%s")) {
StringBuilder str = new StringBuilder();
if (returnDesc.startsWith("[")) {
for (int i = 0; i < returnDesc.length(); i++) {
if (returnDesc.charAt(i) == '[') {
array += '[';
}
}
returnDesc = returnDesc.replaceAll("\\[", "");
}
str.append(array)
.append('L')
.append(String.format(returnDesc,
AddInterfaceAdapter.getAccessorPackage()))
.append(";");
returnDesc = str.toString();
}
final Getter get = new Getter(into, className, fieldName,
methodName, returnDesc, staticMethod);
getterList.add(get);
}
return getterList.toArray(new Getter[getterList.size()]);
}
public final Getter[] getGetters() {
final NodeList getterRootList = doc.getElementsByTagName("getters");
switch (getterRootList.getLength()) {
case 0:
return null;
case 1:
break;
default:
throw new RuntimeException(
"Hook file may not contains multiple <getters> tags ");
}
final Node node = getterRootList.item(0);
if (node.getNodeType() != Node.ELEMENT_NODE) {
return null;
}
final Element getterRoot = (Element) node;
final NodeList getters = getterRoot.getElementsByTagName("add");
if (getters.getLength() == 0) {
return null;
}
final ArrayList<Getter> getterList = new ArrayList<Getter>();
for (int x = 0; x < getters.getLength(); x++) {
final Node n = getters.item(x);
if (n.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
final Element addGetter = (Element) n;
if (isSet("classname", addGetter) && isSet("accessor", addGetter)) {
throw new RuntimeException(
"Can't set classname and accessor tag together.");
}
if (isSet("accessor", addGetter) && !parsedInterfaces) {
throw new RuntimeException(
"You'll need to parse interfaces first.");
}
final String className = isSet("classname", addGetter) ? getValue(
"classname", addGetter) : interfaceMap.get(getValue(
"accessor", addGetter));
final String into = isSet("into", addGetter) ? getValue("into",
addGetter) : className;
final String fieldName = getValue("field", addGetter);
final String methodName = getValue("methodname", addGetter);
boolean staticMethod = isSet("methstatic", addGetter) ? (getValue(
"methstatic", addGetter).equals("true")) : false;
String returnDesc = isSet("desc", addGetter) ? getValue("desc",
addGetter) : null;
String array = "";
if (returnDesc != null && returnDesc.contains("%s")) {
StringBuilder str = new StringBuilder();
if (returnDesc.startsWith("[")) {
for (int i = 0; i < returnDesc.length(); i++) {
if (returnDesc.charAt(i) == '[') {
array += '[';
}
}
returnDesc = returnDesc.replaceAll("\\[", "");
}
str.append(array)
.append('L')
.append(String.format(returnDesc,
AddInterfaceAdapter.getAccessorPackage()))
.append(";");
returnDesc = str.toString();
}
final Getter get = new Getter(into, className, fieldName,
methodName, returnDesc, staticMethod);
getterList.add(get);
}
return getterList.toArray(new Getter[getterList.size()]);
}
public Injectable[] getInjectables() {
ArrayList<Injectable> injectables = new ArrayList<Injectable>();
Interface[] interfaces = getInterfaces();
if (interfaces != null) {
for (Interface inf : interfaces) {
injectables.add(inf);
}
}
Getter[] getters = getGetters();
if (getters != null) {
for (Getter get : getters) {
injectables.add(get);
}
}
Setter[] setters = getSetters();
if (setters != null) {
for (Setter set : setters) {
injectables.add(set);
}
}
Super[] supers = getSupers();
if (supers != null) {
for (Super sup : supers) {
injectables.add(sup);
}
}
Invoker[] invokers = getInvokers();
if (invokers != null) {
for (Invoker vok : invokers) {
injectables.add(vok);
}
}
Callback[] callbacks = getCallbacks();
if (callbacks != null) {
for (Callback callback : callbacks) {
injectables.add(callback);
}
}
return injectables.toArray(new Injectable[injectables.size()]);
}
public Injectable[] getInjectables() {
ArrayList<Injectable> injectables = new ArrayList<Injectable>();
Interface[] interfaces = getInterfaces();
if (interfaces != null) {
for (Interface inf : interfaces) {
injectables.add(inf);
}
}
Getter[] getters = getGetters();
if (getters != null) {
for (Getter get : getters) {
injectables.add(get);
}
}
Setter[] setters = getSetters();
if (setters != null) {
for (Setter set : setters) {
injectables.add(set);
}
}
Super[] supers = getSupers();
if (supers != null) {
for (Super sup : supers) {
injectables.add(sup);
}
}
Invoker[] invokers = getInvokers();
if (invokers != null) {
for (Invoker vok : invokers) {
injectables.add(vok);
}
}
Callback[] callbacks = getCallbacks();
if (callbacks != null) {
for (Callback callback : callbacks) {
injectables.add(callback);
}
}
return injectables.toArray(new Injectable[injectables.size()]);
}
public final Setter[] getSetters() {
final NodeList setterRootList = doc.getElementsByTagName("setters");
switch (setterRootList.getLength()) {
case 0:
return null;
case 1:
break;
default:
throw new RuntimeException(
"Hook file may not contains multiple <setters> tags ");
}
final Node node = setterRootList.item(0);
if (node.getNodeType() != Node.ELEMENT_NODE) {
return null;
}
final Element setterRoot = (Element) node;
final NodeList setters = setterRoot.getElementsByTagName("add");
if (setters.getLength() == 0) {
return null;
}
final ArrayList<Setter> setterList = new ArrayList<Setter>();
for (int x = 0; x < setters.getLength(); x++) {
final Node n = setters.item(x);
if (n.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
final Element addSetter = (Element) n;
if (isSet("classname", addSetter) && isSet("accessor", addSetter)) {
throw new RuntimeException(
"Can't set classname and accessor tag together.");
}
if (isSet("accessor", addSetter) && !parsedInterfaces) {
throw new RuntimeException(
"You'll need to parse interfaces first.");
}
final String className = isSet("classname", addSetter) ? getValue(
"classname", addSetter) : interfaceMap.get(getValue(
"accessor", addSetter));
final String into = isSet("into", addSetter) ? getValue("into",
addSetter) : className;
final String fieldName = getValue("field", addSetter);
final String methodName = getValue("methodname", addSetter);
boolean staticMethod = isSet("methstatic", addSetter) ? (getValue(
"methstatic", addSetter).equals("true")) : false;
String returnDesc = isSet("desc", addSetter) ? getValue("desc",
addSetter) : null;
String array = "";
if (returnDesc != null && returnDesc.contains("%s")) {
StringBuilder str = new StringBuilder();
if (returnDesc.startsWith("[")) {
for (int i = 0; i < returnDesc.length(); i++) {
if (returnDesc.charAt(i) == '[') {
array += '[';
}
}
returnDesc = returnDesc.replaceAll("\\[", "");
}
str.append(array)
.append('L')
.append(String.format(returnDesc,
AddInterfaceAdapter.getAccessorPackage()))
.append(";");
returnDesc = str.toString();
}
final Setter get = new Setter(className, into, fieldName,
methodName, returnDesc, staticMethod);
setterList.add(get);
}
return setterList.toArray(new Setter[setterList.size()]);
}
public final Setter[] getSetters() {
final NodeList setterRootList = doc.getElementsByTagName("setters");
switch (setterRootList.getLength()) {
case 0:
return null;
case 1:
break;
default:
throw new RuntimeException(
"Hook file may not contains multiple <setters> tags ");
}
final Node node = setterRootList.item(0);
if (node.getNodeType() != Node.ELEMENT_NODE) {
return null;
}
final Element setterRoot = (Element) node;
final NodeList setters = setterRoot.getElementsByTagName("add");
if (setters.getLength() == 0) {
return null;
}
final ArrayList<Setter> setterList = new ArrayList<Setter>();
for (int x = 0; x < setters.getLength(); x++) {
final Node n = setters.item(x);
if (n.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
final Element addSetter = (Element) n;
if (isSet("classname", addSetter) && isSet("accessor", addSetter)) {
throw new RuntimeException(
"Can't set classname and accessor tag together.");
}
if (isSet("accessor", addSetter) && !parsedInterfaces) {
throw new RuntimeException(
"You'll need to parse interfaces first.");
}
final String className = isSet("classname", addSetter) ? getValue(
"classname", addSetter) : interfaceMap.get(getValue(
"accessor", addSetter));
final String into = isSet("into", addSetter) ? getValue("into",
addSetter) : className;
final String fieldName = getValue("field", addSetter);
final String methodName = getValue("methodname", addSetter);
boolean staticMethod = isSet("methstatic", addSetter) ? (getValue(
"methstatic", addSetter).equals("true")) : false;
String returnDesc = isSet("desc", addSetter) ? getValue("desc",
addSetter) : null;
String array = "";
if (returnDesc != null && returnDesc.contains("%s")) {
StringBuilder str = new StringBuilder();
if (returnDesc.startsWith("[")) {
for (int i = 0; i < returnDesc.length(); i++) {
if (returnDesc.charAt(i) == '[') {
array += '[';
}
}
returnDesc = returnDesc.replaceAll("\\[", "");
}
str.append(array)
.append('L')
.append(String.format(returnDesc,
AddInterfaceAdapter.getAccessorPackage()))
.append(";");
returnDesc = str.toString();
}
final Setter get = new Setter(className, into, fieldName,
methodName, returnDesc, staticMethod);
setterList.add(get);
}
return setterList.toArray(new Setter[setterList.size()]);
}
public final Invoker[] getInvokers() {
final NodeList invokerRootList = doc.getElementsByTagName("invokers");
switch (invokerRootList.getLength()) {
case 0:
return null;
case 1:
break;
default:
throw new RuntimeException(
"Hook file may not contains multiple <invokers> tags ");
}
final Node node = invokerRootList.item(0);
if (node.getNodeType() != Node.ELEMENT_NODE) {
return null;
}
final Element invokerRoot = (Element) node;
final NodeList invokers = invokerRoot.getElementsByTagName("add");
if (invokers.getLength() == 0) {
return null;
}
final ArrayList<Invoker> invokerList = new ArrayList<Invoker>();
for (int x = 0; x < invokers.getLength(); x++) {
final Node n = invokers.item(x);
if (n.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
final Element addInvoker = (Element) n;
if (isSet("classname", addInvoker) && isSet("accessor", addInvoker)) {
throw new RuntimeException(
"Can't set classname and accessor tag together.");
}
if (isSet("accessor", addInvoker) && !parsedInterfaces) {
throw new RuntimeException(
"You'll need to parse interfaces first.");
}
final String className = isSet("classname", addInvoker) ? getValue(
"classname", addInvoker) : interfaceMap.get(getValue(
"accessor", addInvoker));
final String into = isSet("into", addInvoker) ? getValue("into",
addInvoker) : className;
final String methodName = getValue("methodname", addInvoker);
final String invMethodName = getValue("invokemethod", addInvoker);
final String argsDesc = getValue("argsdesc", addInvoker);
String returnDesc = isSet("desc", addInvoker) ? resolveDesc(getValue(
"desc", addInvoker)) : null;
public final Invoker[] getInvokers() {
final NodeList invokerRootList = doc.getElementsByTagName("invokers");
switch (invokerRootList.getLength()) {
case 0:
return null;
case 1:
break;
default:
throw new RuntimeException(
"Hook file may not contains multiple <invokers> tags ");
}
final Node node = invokerRootList.item(0);
if (node.getNodeType() != Node.ELEMENT_NODE) {
return null;
}
final Element invokerRoot = (Element) node;
final NodeList invokers = invokerRoot.getElementsByTagName("add");
if (invokers.getLength() == 0) {
return null;
}
final ArrayList<Invoker> invokerList = new ArrayList<Invoker>();
for (int x = 0; x < invokers.getLength(); x++) {
final Node n = invokers.item(x);
if (n.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
final Element addInvoker = (Element) n;
if (isSet("classname", addInvoker) && isSet("accessor", addInvoker)) {
throw new RuntimeException(
"Can't set classname and accessor tag together.");
}
if (isSet("accessor", addInvoker) && !parsedInterfaces) {
throw new RuntimeException(
"You'll need to parse interfaces first.");
}
final String className = isSet("classname", addInvoker) ? getValue(
"classname", addInvoker) : interfaceMap.get(getValue(
"accessor", addInvoker));
final String into = isSet("into", addInvoker) ? getValue("into",
addInvoker) : className;
final String methodName = getValue("methodname", addInvoker);
final String invMethodName = getValue("invokemethod", addInvoker);
final String argsDesc = getValue("argsdesc", addInvoker);
String returnDesc = isSet("desc", addInvoker) ? resolveDesc(getValue(
"desc", addInvoker)) : null;
final Invoker invoker = new Invoker(into, className, invMethodName,
argsDesc, returnDesc, methodName);
invokerList.add(invoker);
}
return invokerList.toArray(new Invoker[invokerList.size()]);
}
final Invoker invoker = new Invoker(into, className, invMethodName,
argsDesc, returnDesc, methodName);
invokerList.add(invoker);
}
return invokerList.toArray(new Invoker[invokerList.size()]);
}
public final Callback[] getCallbacks() {
final NodeList callbackRootList = doc.getElementsByTagName("callbacks");
switch (callbackRootList.getLength()) {
case 0:
return null;
case 1:
break;
default:
throw new RuntimeException(
"Hook file may not contains multiple <callbacks> tags ");
}
final Node node = callbackRootList.item(0);
if (node.getNodeType() != Node.ELEMENT_NODE) {
return null;
}
final Element callbackRoot = (Element) node;
final NodeList callbacks = callbackRoot.getElementsByTagName("add");
if (callbacks.getLength() == 0) {
return null;
}
final ArrayList<Callback> callbackList = new ArrayList<Callback>();
for (int x = 0; x < callbacks.getLength(); x++) {
final Node n = callbacks.item(x);
if (n.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
final Element addCallback = (Element) n;
if (isSet("classname", addCallback)
&& isSet("accessor", addCallback)) {
throw new RuntimeException(
"Can't set classname and accessor tag together.");
}
if (isSet("accessor", addCallback) && !parsedInterfaces) {
throw new RuntimeException(
"You'll need to parse interfaces first.");
}
final String className = isSet("classname", addCallback) ? getValue(
"classname", addCallback) : interfaceMap.get(getValue(
"accessor", addCallback));
public final Callback[] getCallbacks() {
final NodeList callbackRootList = doc.getElementsByTagName("callbacks");
switch (callbackRootList.getLength()) {
case 0:
return null;
case 1:
break;
default:
throw new RuntimeException(
"Hook file may not contains multiple <callbacks> tags ");
}
final Node node = callbackRootList.item(0);
if (node.getNodeType() != Node.ELEMENT_NODE) {
return null;
}
final Element callbackRoot = (Element) node;
final NodeList callbacks = callbackRoot.getElementsByTagName("add");
if (callbacks.getLength() == 0) {
return null;
}
final ArrayList<Callback> callbackList = new ArrayList<Callback>();
for (int x = 0; x < callbacks.getLength(); x++) {
final Node n = callbacks.item(x);
if (n.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
final Element addCallback = (Element) n;
if (isSet("classname", addCallback)
&& isSet("accessor", addCallback)) {
throw new RuntimeException(
"Can't set classname and accessor tag together.");
}
if (isSet("accessor", addCallback) && !parsedInterfaces) {
throw new RuntimeException(
"You'll need to parse interfaces first.");
}
final String className = isSet("classname", addCallback) ? getValue(
"classname", addCallback) : interfaceMap.get(getValue(
"accessor", addCallback));
final String methodName = getValue("methodname", addCallback);
final String callClass = getValue("callclass", addCallback);
final String callMethod = getValue("callmethod", addCallback);
final String callDesc = getValue("calldesc", addCallback);
final String callArgs = getValue("callargs", addCallback);
final String desc = getValue("desc", addCallback);
final String methodName = getValue("methodname", addCallback);
final String callClass = getValue("callclass", addCallback);
final String callMethod = getValue("callmethod", addCallback);
final String callDesc = getValue("calldesc", addCallback);
final String callArgs = getValue("callargs", addCallback);
final String desc = getValue("desc", addCallback);
final Callback callback = new Callback(className, methodName, desc,
callClass, callMethod, callDesc, callArgs);
callbackList.add(callback);
}
return callbackList.toArray(new Callback[callbackList.size()]);
}
final Callback callback = new Callback(className, methodName, desc,
callClass, callMethod, callDesc, callArgs);
callbackList.add(callback);
}
return callbackList.toArray(new Callback[callbackList.size()]);
}
private static String resolveDesc(String returnDesc) {
String array = "";
if (returnDesc != null && returnDesc.contains("%s")) {
StringBuilder str = new StringBuilder();
if (returnDesc.startsWith("[")) {
for (int i = 0; i < returnDesc.length(); i++) {
if (returnDesc.charAt(i) == '[') {
array += '[';
}
}
returnDesc = returnDesc.replaceAll("\\[", "");
}
str.append(array)
.append('L')
.append(String.format(returnDesc,
AddInterfaceAdapter.getAccessorPackage()))
.append(";");
returnDesc = str.toString();
}
return returnDesc;
}
private static String resolveDesc(String returnDesc) {
String array = "";
if (returnDesc != null && returnDesc.contains("%s")) {
StringBuilder str = new StringBuilder();
if (returnDesc.startsWith("[")) {
for (int i = 0; i < returnDesc.length(); i++) {
if (returnDesc.charAt(i) == '[') {
array += '[';
}
}
returnDesc = returnDesc.replaceAll("\\[", "");
}
str.append(array)
.append('L')
.append(String.format(returnDesc,
AddInterfaceAdapter.getAccessorPackage()))
.append(";");
returnDesc = str.toString();
}
return returnDesc;
}
private static final boolean isSet(String tag, Element element) {
return element.getElementsByTagName(tag).getLength() > 0;
}
private static final boolean isSet(String tag, Element element) {
return element.getElementsByTagName(tag).getLength() > 0;
}
private static final String getValue(String tag, Element element) {
NodeList nodes = element.getElementsByTagName(tag).item(0)
.getChildNodes();
Node node = (Node) nodes.item(0);
return node.getNodeValue();
}
private static final String getValue(String tag, Element element) {
NodeList nodes = element.getElementsByTagName(tag).item(0)
.getChildNodes();
Node node = (Node) nodes.item(0);
return node.getNodeValue();
}
public final HashMap<String, String> getConstants() {
if (!constants.isEmpty()) {
return constants;
}
final NodeList constantsRootList = doc
.getElementsByTagName("constants");
switch (constantsRootList.getLength()) {
case 0:
return null;
case 1:
break;
default:
throw new RuntimeException(
"Hook file may not contains multiple <constants> tags ");
}
final Node node = constantsRootList.item(0);
if (node.getNodeType() != Node.ELEMENT_NODE) {
return null;
}
final Element constantRoot = (Element) node;
final NodeList constantsList = constantRoot.getElementsByTagName("add");
if (constantsList.getLength() == 0) {
// return empty hashmap
return constants;
}
for (int x = 0; x < constantsList.getLength(); x++) {
final Node n = constantsList.item(x);
if (n.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
final Element addConstant = (Element) n;
final String key = getValue("key", addConstant);
final String value = getValue("value", addConstant);
constants.put(key, value);
}
return constants;
}
public final HashMap<String, String> getConstants() {
if (!constants.isEmpty()) {
return constants;
}
final NodeList constantsRootList = doc
.getElementsByTagName("constants");
switch (constantsRootList.getLength()) {
case 0:
return null;
case 1:
break;
default:
throw new RuntimeException(
"Hook file may not contains multiple <constants> tags ");
}
final Node node = constantsRootList.item(0);
if (node.getNodeType() != Node.ELEMENT_NODE) {
return null;
}
final Element constantRoot = (Element) node;
final NodeList constantsList = constantRoot.getElementsByTagName("add");
if (constantsList.getLength() == 0) {
// return empty hashmap
return constants;
}
for (int x = 0; x < constantsList.getLength(); x++) {
final Node n = constantsList.item(x);
if (n.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
final Element addConstant = (Element) n;
final String key = getValue("key", addConstant);
final String value = getValue("value", addConstant);
constants.put(key, value);
}
return constants;
}
}
@@ -1,9 +1,5 @@
package org.parabot.core.parsers.scripts;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
import org.parabot.core.Directories;
import org.parabot.core.classpath.ClassPath;
import org.parabot.core.desc.ScriptDescription;
@@ -12,69 +8,71 @@ import org.parabot.environment.scripts.Script;
import org.parabot.environment.scripts.ScriptManifest;
import org.parabot.environment.scripts.loader.JavaScriptLoader;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
/**
*
* Parses locally stored java scripts
*
* @author Everel
*
* @author Everel
*/
public class LocalJavaScripts extends ScriptParser {
@Override
public void execute() {
// parse classes in server directories
final ClassPath path = new ClassPath();
path.addClasses(Directories.getScriptCompiledPath());
@Override
public void execute() {
// parse classes in server directories
final ClassPath path = new ClassPath();
path.addClasses(Directories.getScriptCompiledPath());
// init the script loader
final JavaScriptLoader loader = new JavaScriptLoader(path);
// init the script loader
final JavaScriptLoader loader = new JavaScriptLoader(path);
// list of scripts
final List<Script> scripts = new ArrayList<Script>();
// list of scripts
final List<Script> scripts = new ArrayList<Script>();
// list of descriptions
final List<ScriptDescription> descs = new ArrayList<ScriptDescription>();
// list of descriptions
final List<ScriptDescription> descs = new ArrayList<ScriptDescription>();
// loop through all classes which extends the 'Script' class
for (final String className : loader.getScriptClassNames()) {
try {
// get class
final Class<?> scriptClass;
try {
scriptClass = loader.loadClass(className);
} catch (NoClassDefFoundError ignored) {
// script for an other server provider
continue;
}
// get annotation
final Object annotation = scriptClass
.getAnnotation(ScriptManifest.class);
if (annotation == null) {
throw new RuntimeException("Missing manifest at "
+ className);
}
// cast object annotation to script manifest annotation
final ScriptManifest manifest = (ScriptManifest) annotation;
// get constructor
final Constructor<?> con = scriptClass.getConstructor();
final Script script = (Script) con.newInstance();
scripts.add(script);
final ScriptDescription desc = new ScriptDescription(
manifest.name(), manifest.author(), manifest.category()
.toString(), manifest.version(),
manifest.description(), manifest.servers(),
manifest.vip() ? "yes" : "no",
manifest.premium() ? "yes" : "no");
SCRIPT_CACHE.put(desc, new LocalScriptExecuter(script));
descs.add(desc);
} catch (ClassNotFoundException ignored) {
} catch (NoClassDefFoundError ignored) {
} catch (Throwable t) {
t.printStackTrace();
}
}
// loop through all classes which extends the 'Script' class
for (final String className : loader.getScriptClassNames()) {
try {
// get class
final Class<?> scriptClass;
try {
scriptClass = loader.loadClass(className);
} catch (NoClassDefFoundError ignored) {
// script for an other server provider
continue;
}
// get annotation
final Object annotation = scriptClass
.getAnnotation(ScriptManifest.class);
if (annotation == null) {
throw new RuntimeException("Missing manifest at "
+ className);
}
// cast object annotation to script manifest annotation
final ScriptManifest manifest = (ScriptManifest) annotation;
// get constructor
final Constructor<?> con = scriptClass.getConstructor();
final Script script = (Script) con.newInstance();
scripts.add(script);
final ScriptDescription desc = new ScriptDescription(
manifest.name(), manifest.author(), manifest.category()
.toString(), manifest.version(),
manifest.description(), manifest.servers(),
manifest.vip() ? "yes" : "no",
manifest.premium() ? "yes" : "no");
SCRIPT_CACHE.put(desc, new LocalScriptExecuter(script));
descs.add(desc);
} catch (ClassNotFoundException ignored) {
} catch (NoClassDefFoundError ignored) {
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}
}
@@ -1,9 +1,5 @@
package org.parabot.core.parsers.scripts;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import org.parabot.core.Directories;
import org.parabot.core.desc.ScriptDescription;
import org.parabot.environment.scripts.Category;
@@ -12,91 +8,91 @@ import org.parabot.environment.scripts.framework.PythonScript;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
/**
*
* Parses python scripts
*
* @author Everel
*
* @author Everel
*/
public class LocalPythonScripts extends ScriptParser {
private PythonInterpreter interpreter = new PythonInterpreter();
private PythonInterpreter interpreter = new PythonInterpreter();
private static final FilenameFilter PYTHON_SCRIPT_FILTER = new FilenameFilter() {
private static final FilenameFilter PYTHON_SCRIPT_FILTER = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".py");
}
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".py");
}
};
};
/**
*
* @param name
* - local var name
* @return script instance
*/
public PythonScript getScript(String name) {
PyObject clazz = interpreter.get(name);
if (clazz.toString().startsWith("<class '__main__.")) {
final PyObject instanceClass = clazz.__call__();
final Object javaInstance = instanceClass
.__tojava__(PythonScript.class);
if (javaInstance instanceof PythonScript) {
return (PythonScript) javaInstance;
}
}
return null;
}
/**
* @param name - local var name
* @return script instance
*/
public PythonScript getScript(String name) {
PyObject clazz = interpreter.get(name);
if (clazz.toString().startsWith("<class '__main__.")) {
final PyObject instanceClass = clazz.__call__();
final Object javaInstance = instanceClass
.__tojava__(PythonScript.class);
if (javaInstance instanceof PythonScript) {
return (PythonScript) javaInstance;
}
}
return null;
}
@Override
public void execute() {
for (final File scriptFile : Directories.getScriptSourcesPath()
.listFiles(PYTHON_SCRIPT_FILTER)) {
try {
interpreter.execfile(new FileInputStream(scriptFile));
final String name = interpreter.get("__scriptname__")
.asString();
final String author = interpreter.get("__author__").asString();
final Category cat = (Category) interpreter.get("__category__")
.__tojava__(Category.class);
final double version = interpreter.get("__version__")
.asDouble();
final String description = interpreter.get("__description__")
.asString();
final String[] servers = (String[]) interpreter.get(
"__servers__").__tojava__(String[].class);
Object ob;
String vip = "no";
if( (ob = interpreter.get("__vip__")) != null) {
final String isVip = ob.toString();
vip = isVip.equals("True") ? "yes" : "no";
}
String prem = "no";
if( (ob = interpreter.get("__premium__")) != null) {
final String isPrem = ob.toString();
prem = isPrem.equals("True") ? "yes" : "no";
}
final ScriptDescription desc = new ScriptDescription(name,
author, cat.toString(), version, description, servers, vip, prem);
for (final PyObject o : interpreter.getLocals().asIterable()) {
PythonScript script = getScript(o.asString());
if (script != null) {
SCRIPT_CACHE.put(desc, new LocalScriptExecuter(script));
break;
}
}
interpreter.cleanup();
} catch (Throwable t) {
t.printStackTrace();
}
}
@Override
public void execute() {
for (final File scriptFile : Directories.getScriptSourcesPath()
.listFiles(PYTHON_SCRIPT_FILTER)) {
try {
interpreter.execfile(new FileInputStream(scriptFile));
final String name = interpreter.get("__scriptname__")
.asString();
final String author = interpreter.get("__author__").asString();
final Category cat = (Category) interpreter.get("__category__")
.__tojava__(Category.class);
final double version = interpreter.get("__version__")
.asDouble();
final String description = interpreter.get("__description__")
.asString();
final String[] servers = (String[]) interpreter.get(
"__servers__").__tojava__(String[].class);
}
Object ob;
String vip = "no";
if ((ob = interpreter.get("__vip__")) != null) {
final String isVip = ob.toString();
vip = isVip.equals("True") ? "yes" : "no";
}
String prem = "no";
if ((ob = interpreter.get("__premium__")) != null) {
final String isPrem = ob.toString();
prem = isPrem.equals("True") ? "yes" : "no";
}
final ScriptDescription desc = new ScriptDescription(name,
author, cat.toString(), version, description, servers, vip, prem);
for (final PyObject o : interpreter.getLocals().asIterable()) {
PythonScript script = getScript(o.asString());
if (script != null) {
SCRIPT_CACHE.put(desc, new LocalScriptExecuter(script));
break;
}
}
interpreter.cleanup();
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}
@@ -1,7 +1,5 @@
package org.parabot.core.parsers.scripts;
import java.io.BufferedReader;
import java.net.URL;
import org.parabot.core.Configuration;
import org.parabot.core.desc.ScriptDescription;
import org.parabot.core.forum.AccountManager;
@@ -9,90 +7,91 @@ import org.parabot.core.forum.AccountManagerAccess;
import org.parabot.environment.api.utils.WebUtil;
import org.parabot.environment.scripts.SDNScriptExecuter;
import java.io.BufferedReader;
import java.net.URL;
/**
*
* Parses scripts stored at parabot´s sdn
*
* @author Everel
* Parses scripts stored at parabots sdn
*
* @author Everel
*/
public class SDNScripts extends ScriptParser {
private static AccountManager manager = null;
private static AccountManager manager = null;
public static final AccountManagerAccess MANAGER_FETCHER = new AccountManagerAccess() {
public static final AccountManagerAccess MANAGER_FETCHER = new AccountManagerAccess() {
@Override
public final void setManager(AccountManager manager) {
SDNScripts.manager = manager;
}
@Override
public final void setManager(AccountManager manager) {
SDNScripts.manager = manager;
}
};
};
@Override
public void execute() {
if (!manager.isLoggedIn()) {
System.err.println("Not logged in...");
return;
}
try {
BufferedReader br = WebUtil.getReader(new URL(String.format(Configuration.SDN_SCRIPTS, manager.getAccount()
.getUsername())));
int count = 0;
String line;
@Override
public void execute() {
if (!manager.isLoggedIn()) {
System.err.println("Not logged in...");
return;
}
try {
BufferedReader br = WebUtil.getReader(new URL(String.format(Configuration.SDN_SCRIPTS, manager.getAccount()
.getUsername())));
int count = 0;
String line;
String jarName = null;
int sdnId = -1;
String scriptName = null;
String author = null;
double version = 0D;
String category = null;
String description = null;
String[] servers = null;
while ((line = br.readLine()) != null) {
count++;
String jarName = null;
int sdnId = -1;
String scriptName = null;
String author = null;
double version = 0D;
String category = null;
String description = null;
String[] servers = null;
while ((line = br.readLine()) != null) {
count++;
switch (count % 8) {
case 1:
// jarname
jarName = line;
break;
case 2:
// sdn id
sdnId = Integer.parseInt(line);
break;
case 3:
scriptName = line;
break;
case 4:
author = line;
break;
case 5:
version = Double.parseDouble(line);
break;
case 6:
category = line;
break;
case 7:
description = line;
break;
case 0:
if (line.contains(", ")) {
servers = line.split(", ");
} else {
servers = new String[] { line };
}
final ScriptDescription desc = new ScriptDescription(jarName, scriptName,
author, category, version, description,
servers, sdnId);
SCRIPT_CACHE.put(desc, new SDNScriptExecuter(sdnId));
}
}
br.close();
switch (count % 8) {
case 1:
// jarname
jarName = line;
break;
case 2:
// sdn id
sdnId = Integer.parseInt(line);
break;
case 3:
scriptName = line;
break;
case 4:
author = line;
break;
case 5:
version = Double.parseDouble(line);
break;
case 6:
category = line;
break;
case 7:
description = line;
break;
case 0:
if (line.contains(", ")) {
servers = line.split(", ");
} else {
servers = new String[]{line};
}
final ScriptDescription desc = new ScriptDescription(jarName, scriptName,
author, category, version, description,
servers, sdnId);
SCRIPT_CACHE.put(desc, new SDNScriptExecuter(sdnId));
}
}
} catch (Throwable t) {
t.printStackTrace();
}
}
br.close();
} catch (Throwable t) {
t.printStackTrace();
}
}
}
@@ -1,7 +1,5 @@
package org.parabot.core.parsers.servers;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import org.parabot.core.Directories;
import org.parabot.core.classpath.ClassPath;
import org.parabot.core.desc.ServerDescription;
@@ -10,64 +8,65 @@ import org.parabot.environment.servers.ServerManifest;
import org.parabot.environment.servers.ServerProvider;
import org.parabot.environment.servers.loader.ServerLoader;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
/**
*
* Parses local server providers located in the servers directory
*
*
* @author Everel
*
*/
public class LocalServers extends ServerParser {
@Override
public void execute() {
// parse classes in server directories
final ClassPath basePath = new ClassPath();
basePath.parseJarFiles(false);
basePath.addClasses(Directories.getServerPath());
@Override
public void execute() {
// parse classes in server directories
final ClassPath basePath = new ClassPath();
basePath.parseJarFiles(false);
basePath.addClasses(Directories.getServerPath());
final ArrayList<ClassPath> classPaths = new ArrayList<ClassPath>();
classPaths.add(basePath);
for (final ClassPath classPath : basePath.getJarFiles()) {
classPaths.add(classPath);
}
final ArrayList<ClassPath> classPaths = new ArrayList<ClassPath>();
classPaths.add(basePath);
for (final ClassPath classPath : basePath.getJarFiles()) {
classPaths.add(classPath);
}
for (final ClassPath path : classPaths) {
// init the server loader
final ServerLoader loader = new ServerLoader(path);
for (final ClassPath path : classPaths) {
// init the server loader
final ServerLoader loader = new ServerLoader(path);
// loop through all classes which extends the 'ServerProvider' class
for (final String className : loader.getServerClassNames()) {
try {
// get class
final Class<?> serverProviderClass = loader
.loadClass(className);
// get annotation
final Object annotation = serverProviderClass
.getAnnotation(ServerManifest.class);
if (annotation == null) {
throw new RuntimeException("Missing manifest at "
+ className);
}
// cast object annotation to server manifest annotation
final ServerManifest manifest = (ServerManifest) annotation;
// get constructor
final Constructor<?> con = serverProviderClass
.getConstructor();
final ServerProvider serverProvider = (ServerProvider) con
.newInstance();
// loop through all classes which extends the 'ServerProvider' class
for (final String className : loader.getServerClassNames()) {
try {
// get class
final Class<?> serverProviderClass = loader
.loadClass(className);
// get annotation
final Object annotation = serverProviderClass
.getAnnotation(ServerManifest.class);
if (annotation == null) {
throw new RuntimeException("Missing manifest at "
+ className);
}
// cast object annotation to server manifest annotation
final ServerManifest manifest = (ServerManifest) annotation;
// get constructor
final Constructor<?> con = serverProviderClass
.getConstructor();
final ServerProvider serverProvider = (ServerProvider) con
.newInstance();
SERVER_CACHE.put(
new ServerDescription(manifest.name(), manifest
.author(), manifest.version()),
new LocalServerExecuter(serverProvider, path,
manifest.name()));
} catch (Throwable t) {
t.printStackTrace();
}
}
}
SERVER_CACHE.put(
new ServerDescription(manifest.name(), manifest
.author(), manifest.version()),
new LocalServerExecuter(serverProvider, path,
manifest.name()));
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}
}
}
@@ -1,62 +1,60 @@
package org.parabot.core.parsers.servers;
import java.io.BufferedReader;
import java.net.URL;
import org.parabot.core.Configuration;
import org.parabot.core.desc.ServerDescription;
import org.parabot.environment.api.utils.WebUtil;
import org.parabot.environment.servers.PublicServerExecuter;
import java.io.BufferedReader;
import java.net.URL;
/**
*
* Parses servers hosted on parabot
*
*
* @author Everel
*
*/
public class PublicServers extends ServerParser {
@Override
public void execute() {
try {
BufferedReader br = WebUtil.getReader(new URL(
Configuration.GET_SERVER_PROVIDERS));
int count = 0;
String line;
@Override
public void execute() {
try {
BufferedReader br = WebUtil.getReader(new URL(
Configuration.GET_SERVER_PROVIDERS));
int count = 0;
String line;
String name = null;
String author = null;
double version = 0D;
String name = null;
String author = null;
double version = 0D;
while ((line = br.readLine()) != null) {
count++;
switch (count % 4) {
case 1:
// server name
name = line;
break;
case 2:
// author
author = line;
break;
case 3:
// version
version = Double.parseDouble(line);
break;
case 0:
// jarName
ServerDescription desc = new ServerDescription(name,
author, version);
SERVER_CACHE.put(desc, new PublicServerExecuter(name, line));
}
}
br.close();
while ((line = br.readLine()) != null) {
count++;
switch (count % 4) {
case 1:
// server name
name = line;
break;
case 2:
// author
author = line;
break;
case 3:
// version
version = Double.parseDouble(line);
break;
case 0:
// jarName
ServerDescription desc = new ServerDescription(name,
author, version);
SERVER_CACHE.put(desc, new PublicServerExecuter(name, line));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}