1 19 package org.netbeans.api.retouche.source; 20 21 import java.io.IOException ; 22 import java.util.ArrayList ; 23 import java.util.List ; 24 import org.netbeans.api.gsf.Error; 25 import org.netbeans.api.gsf.ParseEvent; 26 import org.netbeans.api.gsf.ParseListener; 27 import org.netbeans.api.gsf.Parser; 28 import org.netbeans.api.gsf.ParserFile; 29 import org.netbeans.api.gsf.ParserResult; 30 import org.netbeans.api.gsf.SourceFileReader; 31 import org.netbeans.api.retouche.source.CompilationUnitTree; 32 import org.netbeans.modules.gsf.Language; 33 import org.netbeans.modules.retouche.source.parsing.SourceFileObject; 34 import org.openide.filesystems.FileObject; 35 36 37 public class ParserTaskImpl { 38 private Parser parser; 39 private ParseListener listener; 40 41 public ParserTaskImpl(Language language) { 42 } 43 44 public void setParser(Parser parser) { 45 this.parser = parser; 46 } 47 48 public void setParseListener(ParseListener listener) { 49 this.listener = listener; 50 } 51 52 public void finish() { 53 } 54 55 public Iterable <ParserResult> parse(ParserFile... files) throws IOException { 56 List <ParserResult> results = new ArrayList <ParserResult>(files.length); 57 58 for (ParserFile file : files) { 59 if (file == null) { 60 continue; 61 } 62 63 final ParserResult[] resultHolder = new ParserResult[1]; 65 ParseListener listener = 66 new ParseListener() { 67 public void started(ParseEvent e) { 68 ParserTaskImpl.this.listener.started(e); 69 } 70 71 public void error(Error e) { 72 ParserTaskImpl.this.listener.error(e); 73 } 74 75 public void exception(Exception e) { 76 ParserTaskImpl.this.listener.exception(e); 77 } 78 79 public void finished(ParseEvent e) { 80 if (e.getKind() == ParseEvent.Kind.PARSE) { 82 resultHolder[0] = e.getResult(); 83 } 84 ParserTaskImpl.this.listener.finished(e); 85 } 86 }; 87 88 List <ParserFile> sourceFiles = new ArrayList <ParserFile>(1); 89 sourceFiles.add(file); 90 91 SourceFileReader reader = 92 new SourceFileReader() { 93 public CharSequence read(ParserFile file) throws IOException { 94 return SourceFileObject.create(file.getFileObject()).getCharContent(false).toString(); 97 } 98 public int getCaretOffset(ParserFile fileObject) { 99 return -1; 100 } 101 }; 102 103 parser.parseFiles(sourceFiles, listener, reader); 104 105 ParserResult result = resultHolder[0]; 106 assert result != null; 107 results.add(result); 108 } 109 110 return results; 111 } 112 } 113 | Popular Tags |