19
19
20
20
package com .as3mxml .vscode .asdoc ;
21
21
22
+ import java .io .BufferedReader ;
23
+ import java .io .StringReader ;
22
24
import java .util .ArrayList ;
23
25
import java .util .Collection ;
24
26
import java .util .HashMap ;
31
33
import org .apache .royale .compiler .asdoc .IASDocTag ;
32
34
import org .apache .royale .compiler .common .ISourceLocation ;
33
35
import org .apache .royale .compiler .common .SourceLocation ;
36
+ import org .dom4j .Element ;
34
37
35
38
import antlr .Token ;
36
39
@@ -43,6 +46,114 @@ public class VSCodeASDocComment extends SourceLocation implements IASDocComment
43
46
private static final Pattern markdownUnderscorePattern = Pattern .compile ("(_{1,2})(\\ w(?:[\\ w ]+\\ w)?)\\ 1" );
44
47
private static final Pattern markdownBacktickPattern = Pattern .compile ("(`)(\\ w(?:[\\ w ]+\\ w)?)\\ 1" );
45
48
49
+ public static VSCodeASDocComment getComment (Element defElement ) {
50
+ try {
51
+ String defName = defElement .getName ();
52
+ String description = null ;
53
+ Element descriptionElement = defElement .element ("description" );
54
+ if (descriptionElement != null ) {
55
+ description = descriptionElement .asXML ();
56
+ } else {
57
+ Element apiDetailElement = defElement .element (defName + "Detail" );
58
+ if (apiDetailElement == null ) {
59
+ return null ;
60
+ }
61
+ Element apiDescElement = apiDetailElement .element ("apiDesc" );
62
+ if (apiDescElement == null ) {
63
+ return null ;
64
+ }
65
+ description = apiDescElement .asXML ();
66
+ }
67
+ StringBuilder builder = new StringBuilder ();
68
+ builder .append ("/**" );
69
+ BufferedReader reader = new BufferedReader (new StringReader (description ));
70
+ String line = null ;
71
+ while ((line = reader .readLine ()) != null ) {
72
+ builder .append ("\n * " );
73
+ builder .append (line );
74
+ }
75
+ if ("apiValue" .equals (defName )) {
76
+ Element apiDetailElement = defElement .element (defName + "Detail" );
77
+ if (apiDetailElement != null ) {
78
+ Element apiDefElement = apiDetailElement .element (defName + "Def" );
79
+ if (apiDefElement != null ) {
80
+ Element apiDefaultValueElement = apiDefElement .element ("apiDefaultValue" );
81
+ if (apiDefaultValueElement != null ) {
82
+ String defaultDescription = apiDefaultValueElement .getStringValue ();
83
+ defaultDescription = defaultDescription .replaceAll ("\n " , " " );
84
+ builder .append ("\n * @default " );
85
+ builder .append (defaultDescription );
86
+ }
87
+ }
88
+ }
89
+ }
90
+ if ("apiValue" .equals (defName ) || "apiOperation" .equals (defName ) || "apiConstructor" .equals (defName )) {
91
+ Element apiDetailElement = defElement .element (defName + "Detail" );
92
+ if (apiDetailElement != null ) {
93
+ Element apiDefElement = apiDetailElement .element (defName + "Def" );
94
+ if (apiDefElement != null ) {
95
+ for (Object element : apiDefElement .elements ("apiException" )) {
96
+ Element apiExceptionElement = (Element ) element ;
97
+ Element apiItemNameElement = apiExceptionElement .element ("apiItemName" );
98
+ builder .append ("\n * @throws " );
99
+ if (apiItemNameElement == null ) {
100
+ builder .append ("_" );
101
+ } else {
102
+ builder .append (apiItemNameElement .getStringValue ());
103
+ Element paramApiDescElement = apiExceptionElement .element ("apiDesc" );
104
+ if (paramApiDescElement != null ) {
105
+ String paramDescription = paramApiDescElement .getStringValue ();
106
+ paramDescription = paramDescription .replaceAll ("\n " , " " );
107
+ builder .append (" " );
108
+ builder .append (paramDescription );
109
+ }
110
+ }
111
+ }
112
+ }
113
+ }
114
+ }
115
+ if ("apiOperation" .equals (defName ) || "apiConstructor" .equals (defName )) {
116
+ Element apiDetailElement = defElement .element (defName + "Detail" );
117
+ if (apiDetailElement != null ) {
118
+ Element apiDefElement = apiDetailElement .element (defName + "Def" );
119
+ if (apiDefElement != null ) {
120
+ for (Object element : apiDefElement .elements ("apiParam" )) {
121
+ Element apiParamElement = (Element ) element ;
122
+ Element apiItemNameElement = apiParamElement .element ("apiItemName" );
123
+ builder .append ("\n * @param " );
124
+ if (apiItemNameElement == null ) {
125
+ builder .append ("_" );
126
+ } else {
127
+ builder .append (apiItemNameElement .getStringValue ());
128
+ Element paramApiDescElement = apiParamElement .element ("apiDesc" );
129
+ if (paramApiDescElement != null ) {
130
+ String paramDescription = paramApiDescElement .getStringValue ();
131
+ paramDescription = paramDescription .replaceAll ("\n " , " " );
132
+ builder .append (" " );
133
+ builder .append (paramDescription );
134
+ }
135
+ }
136
+ }
137
+ Element apiReturnElement = apiDefElement .element ("apiReturn" );
138
+ if (apiReturnElement != null ) {
139
+ Element returnApiDescElement = apiReturnElement .element ("apiDesc" );
140
+ if (returnApiDescElement != null ) {
141
+ String returnDescription = returnApiDescElement .getStringValue ();
142
+ returnDescription = returnDescription .replaceAll ("\n " , " " );
143
+ builder .append ("\n * @return " );
144
+ builder .append (returnDescription );
145
+ }
146
+ }
147
+ }
148
+ }
149
+ }
150
+ builder .append ("\n */" );
151
+ return new VSCodeASDocComment (builder .toString ());
152
+ } catch (Exception e ) {
153
+ return null ;
154
+ }
155
+ }
156
+
46
157
public VSCodeASDocComment (Token t ) {
47
158
super ((ISourceLocation ) t );
48
159
token = t ;
0 commit comments