001 package plugins.JavaRolePluginModule;
002
003 import sharpster.common.*;
004
005 public class Extractor {
006
007 public static ResponseCollection extractPartsForDoc(SharedFile file) {
008 //returvariabeln
009 ResponseCollection returnResponseCollection = new ResponseCollection();
010
011 Body fileBody = file.getBody();
012
013 /*test om någon av filern är binärfil (behövs kanske inte)
014 if (fileBody.isBinary()) {
015 JavaRoleResponse res = new JavaRoleResponse();
016 res.setError(true);
017 res.setSubError(SubError.SYNTAX_ERROR);
018 res.setFilename(file.getFileName());
019 returnResponseCollection.addResponse(res);
020 return returnResponseCollection;
021 }*/
022
023 String fileStr = new String(fileBody.getData());
024
025 ParseTree tree = new ParseTree(fileStr);
026 if (tree.hasSyntaxError()) {
027 JavaRoleResponse res = new JavaRoleResponse();
028 res.setError(true);
029 res.setSubError(SubError.SYNTAX_ERROR);
030 res.setFilename(file.getFileName());
031 returnResponseCollection.addResponse(res);
032 return returnResponseCollection;
033
034 }
035
036 Entity current = tree.getFirstChildOfFile();
037 while(current!=null) {
038 //RMTester.print(current);
039 if(current.type==Entity.DOCCOMMENT) {
040 Entity previous=tree.getPrevious();
041 if(previous!=null && previous.type==Entity.DOCCOMMENT) {
042 tree.removeAndGetNext();
043 current=tree.getNext();
044 }else{
045 tree.getNext();
046 current=tree.getNext();
047 }
048 } else if(current.type==Entity.METHODBODY) {
049 current.type=Entity.SEMICOLON;
050 current.endPos=current.contentStartPos=current.startPos;
051 current=tree.getNext();
052 } else if(current.type==Entity.EXTRA) {
053 current=tree.removeAndGetNext();
054 } else {
055 current=tree.getNext();
056 }
057 }
058 SharedFilesResponse res = new SharedFilesResponse();
059 FileCollection fc = new FileCollection();
060 SharedFile sf = new SharedFile(file);
061 Body body = new Body();
062
063 body.setData(tree.toString().getBytes());
064 sf.setBody(body);
065 fc.addFile(sf);
066
067 res.setFiles(fc);
068
069 returnResponseCollection.addResponse(res);
070 return returnResponseCollection;
071 }
072
073 /** Att göra:
074 * 1. undersöka om filen är tom och hantera detta
075 * 2. säkerställa att inte flera DOCCOMMENT finns på rad
076 * och ta bort överflödiga.
077 */
078 public static ResponseCollection extractPartsForRole(SharedFile file, String role) {
079 //returvariabeln
080 ResponseCollection returnResponseCollection = new ResponseCollection();
081
082 Body fileBody = file.getBody();
083
084 /*test om någon av filern är binärfil (behövs kanske inte)
085 if (fileBody.isBinary()) {
086 JavaRoleResponse res = new JavaRoleResponse();
087 res.setError(true);
088 res.setSubError(SubError.SYNTAX_ERROR);
089 res.setFilename(file.getFileName());
090 returnResponseCollection.addResponse(res);
091 return returnResponseCollection;
092 }*/
093
094 String fileStr = new String(fileBody.getData());
095
096 ParseTree tree = new ParseTree(fileStr);
097 if (tree.hasSyntaxError()) {
098 JavaRoleResponse res = new JavaRoleResponse();
099 res.setError(true);
100 res.setSubError(SubError.SYNTAX_ERROR);
101 res.setFilename(file.getFileName());
102 returnResponseCollection.addResponse(res);
103 return returnResponseCollection;
104
105 }
106
107 Entity current=tree.getFirstChildOfFile();
108 while(current!=null) {
109 //System.out.print("Strip:");RMTester.print(current);
110 switch(current.type) {
111 case Entity.DOCCOMMENT:
112 /* Ensures no adjacent DOCCOMMENTs */
113 if(tree.getPrevious().type==Entity.DOCCOMMENT) {
114 current=tree.removeAndGetNext();
115 }else {
116 current=tree.getNext();
117 }
118
119 if(current.getContent().indexOf("@sharpster."+role)>0) {
120 current=tree.getNextSkipHeadAndBody();
121 }else{
122 current.type=Entity.EXTRA;
123 current.startPos=current.contentStartPos=current.endPos;
124 current=tree.getNext();
125 }
126 break;
127 case Entity.METHODHEAD:
128 case Entity.METHODBODY:
129 case Entity.SEMICOLON:
130 case Entity.VARIABLE:
131 current.type=Entity.EXTRA;
132 current.startPos=current.contentStartPos=current.endPos;
133 current=tree.getNext();
134 break;
135 case Entity.EXTRA:
136 current.startPos=current.contentStartPos=current.endPos;
137 current=tree.getNext();
138 break;
139 case Entity.ENDOFCLASSBODY:
140 current=removeClassIfEmptyAndGetNext(tree);
141 default:
142 current=tree.getNext();
143 }
144 }
145 SharedFilesResponse res = new SharedFilesResponse();
146 FileCollection fc = new FileCollection();
147 SharedFile sf = new SharedFile(file);
148 Body body = new Body();
149
150 body.setData(tree.toString().getBytes());
151 sf.setBody(body);
152 fc.addFile(sf);
153
154 res.setFiles(fc);
155
156 returnResponseCollection.addResponse(res);
157 return returnResponseCollection;
158
159
160 }
161
162 /** The cursor in the parse tree should be at ENDOFCLASS.
163 * @return The next entity
164 */
165 private static Entity removeClassIfEmptyAndGetNext(ParseTree tree) {
166 tree.getParent();
167 Entity current=tree.getFirstChild();
168 while(current.type!=Entity.ENDOFCLASSBODY) {
169 //System.out.print("remClIfEmpty:");
170 //RMTester.print(current);
171 if(current.type==Entity.EXTRA) {
172 Entity previous=tree.getPrevious();
173 if(previous!=null & previous.type==Entity.EXTRA) {
174 tree.removeAndGetNext();
175 } else {
176 tree.getNext(); // current
177 }
178 current=tree.getNext();
179 } else {
180 //behåll klassen
181 tree.getParent();
182 current=tree.getNextSkipHeadAndBody();
183 return current;
184 }
185 }
186 tree.getPrevious(); // classhead
187 if(tree.removeAndGetPrevious().type==Entity.DOCCOMMENT) {
188 return tree.removeAndGetNext();
189 }else{
190 return tree.getNext();
191 }
192 }
193
194 /** The cursor is at ENDOFFILE
195 * @return True if the file is "empty"
196 */
197 private static boolean fileIsEmpty(ParseTree tree) {
198 tree.getParent(); // file
199 Entity current=tree.getFirstChildOfFile();
200 while(current.type!=Entity.ENDOFFILE) {
201 if(current.type!=Entity.EXTRA) {
202 return false;
203 }
204 current=tree.getNextSkipHeadAndBody();
205 }
206 return true;
207 }
208
209 }