1 package com.ochafik.lang.compiler; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.io.InputStreamReader; 7 import java.io.OutputStream; 8 import java.io.Reader; 9 import java.io.Writer; 10 import java.net.URI; 11 import java.net.URISyntaxException; 12 import java.net.URL; 13 import java.net.URLConnection; 14 15 import javax.tools.FileObject; 16 17 import com.ochafik.io.ReadText; 18 19 public class URLFileObject implements FileObject { 20 URL url; 21 22 public URLFileObject(URL url) { 23 this.url = url; 24 } 25 26 public String getPath() { 27 return url.getFile(); 28 } 29 30 @Override 31 public boolean delete() { 32 return false; 33 } 34 35 @Override 36 public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { 37 return ReadText.readText(url); 38 } 39 40 @Override 41 public long getLastModified() { 42 try { 43 URLConnection con = url.openConnection(); 44 long lastModified = con.getLastModified(); 45 con.getInputStream().close(); 46 return lastModified; 47 } catch (Exception ex) { 48 ex.printStackTrace(); 49 return -1; 50 } 51 } 52 53 @Override 54 public String getName() { 55 return new File(url.getFile()).getName(); 56 } 57 58 @Override 59 public InputStream openInputStream() throws IOException { 60 return url.openStream(); 61 } 62 63 @Override 64 public OutputStream openOutputStream() throws IOException { 65 return null; 66 } 67 68 @Override 69 public Reader openReader(boolean arg0) throws IOException { 70 return new InputStreamReader(openInputStream()); 71 } 72 73 @Override 74 public Writer openWriter() throws IOException { 75 return null; 76 } 77 78 @Override 79 public URI toUri() { 80 try { 81 return url.toURI(); 82 } catch (URISyntaxException e) { 83 e.printStackTrace(); 84 return null; 85 } 86 } 87 }