@@ -145,6 +145,9 @@ private void parse(String s, String ext){
145
145
146
146
if (ext .equals ("js" ) || ext .equals ("javascript" )){
147
147
classes = getClasses (s );
148
+ if (classes .isEmpty ()){
149
+ classes = getStaticFunctions (s );
150
+ }
148
151
}
149
152
else if (ext .equals ("java" )) {
150
153
classes = new ArrayList <>();
@@ -948,11 +951,18 @@ private static Comment parseComment(String comment){
948
951
//**************************************************************************
949
952
//** getClasses
950
953
//**************************************************************************
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>
952
963
*/
953
964
private static ArrayList <Class > getClasses (String s ){
954
965
ArrayList <Class > classes = new ArrayList <>();
955
- ArrayList <JSONObject > orphanedFunctions = new ArrayList <>();
956
966
957
967
int i =0 ;
958
968
Word word , p1 = null , p2 = null ;
@@ -979,7 +989,7 @@ private static ArrayList<Class> getClasses(String s){
979
989
980
990
if (fn !=null ){
981
991
String functionName = fn .get ("name" ).toString ();
982
- if (functionName .contains ("." )){ //javaxt-style class
992
+ if (functionName .contains ("." )){ //function with a namespace
983
993
984
994
//Update functionName
985
995
String namespace = functionName .substring (0 , functionName .lastIndexOf ("." ));
@@ -1018,11 +1028,6 @@ private static ArrayList<Class> getClasses(String s){
1018
1028
}
1019
1029
1020
1030
}
1021
- else {
1022
-
1023
- //TODO: add static functions to anonymous class (e.g. Utils.js)
1024
-
1025
- }
1026
1031
}
1027
1032
1028
1033
i = end +1 ;
@@ -1039,6 +1044,116 @@ private static ArrayList<Class> getClasses(String s){
1039
1044
}
1040
1045
1041
1046
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
+
1042
1157
//**************************************************************************
1043
1158
//** getFunctions
1044
1159
//**************************************************************************
0 commit comments