Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 719ab85

Browse files
committed
Added new getStaticFunctions() method for parsing javascript files with static methods (e.g. Utils.js)
1 parent 868a486 commit 719ab85

File tree

1 file changed

+123
-8
lines changed

1 file changed

+123
-8
lines changed

src/javaxt/utils/src/Parser.java

Lines changed: 123 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ private void parse(String s, String ext){
145145

146146
if (ext.equals("js") || ext.equals("javascript")){
147147
classes = getClasses(s);
148+
if (classes.isEmpty()){
149+
classes = getStaticFunctions(s);
150+
}
148151
}
149152
else if (ext.equals("java")) {
150153
classes = new ArrayList<>();
@@ -948,11 +951,18 @@ private static Comment parseComment(String comment){
948951
//**************************************************************************
949952
//** getClasses
950953
//**************************************************************************
951-
/** Used to extract classes from a given block of JavaScript
954+
/** Used to extract classes from a given block of JavaScript. This parser
955+
* actually looks for functions with nested functions that javaxt and other
956+
* libraries use to represent a class. Example:
957+
<pre>
958+
javaxt.dhtml.Window = function(parent, config) {
959+
this.open = function(){};
960+
this.close = function(){};
961+
}
962+
</pre>
952963
*/
953964
private static ArrayList<Class> getClasses(String s){
954965
ArrayList<Class> classes = new ArrayList<>();
955-
ArrayList<JSONObject> orphanedFunctions = new ArrayList<>();
956966

957967
int i=0;
958968
Word word, p1 = null, p2 = null;
@@ -979,7 +989,7 @@ private static ArrayList<Class> getClasses(String s){
979989

980990
if (fn!=null){
981991
String functionName = fn.get("name").toString();
982-
if (functionName.contains(".")){ //javaxt-style class
992+
if (functionName.contains(".")){ //function with a namespace
983993

984994
//Update functionName
985995
String namespace = functionName.substring(0, functionName.lastIndexOf("."));
@@ -1018,11 +1028,6 @@ private static ArrayList<Class> getClasses(String s){
10181028
}
10191029

10201030
}
1021-
else{
1022-
1023-
//TODO: add static functions to anonymous class (e.g. Utils.js)
1024-
1025-
}
10261031
}
10271032

10281033
i = end+1;
@@ -1039,6 +1044,116 @@ private static ArrayList<Class> getClasses(String s){
10391044
}
10401045

10411046

1047+
//**************************************************************************
1048+
//** getStaticFunctions
1049+
//**************************************************************************
1050+
/** Used to extract static functions from a given block of JavaScript. This
1051+
* parser looks for functions that are encapsulated in a property with a
1052+
* namespace. Example:
1053+
<pre>
1054+
javaxt.dhtml.utils = {
1055+
isString: function(obj){};
1056+
isNumber: function(obj){};
1057+
}
1058+
</pre>
1059+
*/
1060+
private static ArrayList<Class> getStaticFunctions(String s){
1061+
ArrayList<Class> classes = new ArrayList<>();
1062+
1063+
int i=0;
1064+
Word word, p1 = null, p2 = null;
1065+
while ((word = getNextWord(s, i))!=null){
1066+
1067+
String str = word.toString();
1068+
if (str.contains("{")){
1069+
1070+
1071+
//Find the position of the start and end brackets
1072+
int start = getStartBacket(s, i, '{');
1073+
int end = getEndBacket(s, start, '{');
1074+
1075+
1076+
if (end==-1){
1077+
i = word.end+1;
1078+
continue;
1079+
}
1080+
else{
1081+
i = end+1;
1082+
}
1083+
1084+
1085+
String body = s.substring(start, end);
1086+
1087+
1088+
ArrayList<Method> methods = getFunctions(body);
1089+
if (!methods.isEmpty()){
1090+
1091+
1092+
1093+
//Try to find the name of the property holding these functions
1094+
ArrayList<Word> words = new ArrayList<>();
1095+
if (p1!=null){
1096+
words.add(p1);
1097+
if (p2!=null) words.add(p2);
1098+
}
1099+
int idx = str.indexOf("{");
1100+
str = str.substring(0, idx).trim();
1101+
idx = str.lastIndexOf("=");
1102+
if (idx==-1){
1103+
word = words.remove(0);
1104+
str = word.toString();
1105+
idx = str.lastIndexOf("=");
1106+
if (idx==-1) continue;
1107+
}
1108+
str = str.substring(0, idx).trim();
1109+
if (str.isEmpty()){
1110+
word = words.remove(0);
1111+
str = word.toString();
1112+
}
1113+
1114+
1115+
1116+
//Create a class and add members
1117+
if (str.contains(".")){ //property with a namespace
1118+
1119+
1120+
//Get namespace and use the last key in the property as
1121+
//the class name
1122+
String namespace = str.substring(0, str.lastIndexOf("."));
1123+
str = str.substring(namespace.length()+1);
1124+
1125+
1126+
//Create class
1127+
Class cls = new Class(str);
1128+
cls.setNamespace(namespace);
1129+
1130+
1131+
Comment comment = parseComment(word.lastComment);
1132+
cls.setDescription(comment.getDescription());
1133+
1134+
1135+
for (Method method : methods){
1136+
cls.addMember(method);
1137+
}
1138+
classes.add(cls);
1139+
1140+
1141+
}
1142+
1143+
}
1144+
}
1145+
else{
1146+
i = word.end+1;
1147+
}
1148+
1149+
p2 = p1;
1150+
p1 = word;
1151+
}
1152+
1153+
return classes;
1154+
}
1155+
1156+
10421157
//**************************************************************************
10431158
//** getFunctions
10441159
//**************************************************************************

0 commit comments

Comments
 (0)