1 11 package org.eclipse.jdt.internal.compiler.tool; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.io.OutputStream ; 17 import java.io.Reader ; 18 import java.io.Writer ; 19 import java.net.URI ; 20 import java.net.URISyntaxException ; 21 import java.nio.charset.Charset ; 22 import java.util.zip.ZipEntry ; 23 import java.util.zip.ZipFile ; 24 25 import javax.lang.model.element.Modifier; 26 import javax.lang.model.element.NestingKind; 27 import javax.tools.JavaFileObject; 28 29 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; 30 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader; 31 import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException; 32 33 36 public class ArchiveFileObject implements JavaFileObject { 37 private ZipEntry zipEntry; 38 private ZipFile zipFile; 39 private String entryName; 40 private File file; 41 private Charset charset; 42 43 public ArchiveFileObject(File file, ZipFile zipFile, String entryName, Charset charset) { 44 this.zipFile = zipFile; 45 this.zipEntry = zipFile.getEntry(entryName); 46 this.entryName = entryName; 47 this.file = file; 48 this.charset = charset; 49 } 50 51 54 public Modifier getAccessLevel() { 55 if (getKind() != Kind.CLASS) { 57 return null; 58 } 59 ClassFileReader reader = null; 60 try { 61 reader = ClassFileReader.read(this.zipFile, this.entryName); 62 } catch (ClassFormatException e) { 63 } catch (IOException e) { 65 } 67 if (reader == null) { 68 return null; 69 } 70 final int accessFlags = reader.accessFlags(); 71 if ((accessFlags & ClassFileConstants.AccPublic) != 0) { 72 return Modifier.PUBLIC; 73 } 74 if ((accessFlags & ClassFileConstants.AccAbstract) != 0) { 75 return Modifier.ABSTRACT; 76 } 77 if ((accessFlags & ClassFileConstants.AccFinal) != 0) { 78 return Modifier.FINAL; 79 } 80 return null; 81 } 82 83 86 public Kind getKind() { 87 String name = this.entryName.toLowerCase(); 88 if (name.endsWith(Kind.CLASS.extension)) { 89 return Kind.CLASS; 90 } else if (name.endsWith(Kind.SOURCE.extension)) { 91 return Kind.SOURCE; 92 } else if (name.endsWith(Kind.HTML.extension)) { 93 return Kind.HTML; 94 } 95 return Kind.OTHER; 96 } 97 98 101 public NestingKind getNestingKind() { 102 switch(getKind()) { 103 case SOURCE : 104 return NestingKind.TOP_LEVEL; 105 case CLASS : 106 ClassFileReader reader = null; 107 try { 108 reader = ClassFileReader.read(this.zipFile, this.entryName); 109 } catch (ClassFormatException e) { 110 } catch (IOException e) { 112 } 114 if (reader == null) { 115 return null; 116 } 117 if (reader.isAnonymous()) { 118 return NestingKind.ANONYMOUS; 119 } 120 if (reader.isLocal()) { 121 return NestingKind.LOCAL; 122 } 123 if (reader.isMember()) { 124 return NestingKind.MEMBER; 125 } 126 return NestingKind.TOP_LEVEL; 127 default: 128 return null; 129 } 130 } 131 132 135 public boolean isNameCompatible(String simpleName, Kind kind) { 136 return this.zipEntry.getName().endsWith(simpleName + kind.extension); 137 } 138 139 142 public boolean delete() { 143 throw new UnsupportedOperationException (); 144 } 145 146 public boolean equals(Object o) { 147 if (!(o instanceof ArchiveFileObject)) { 148 return false; 149 } 150 ArchiveFileObject archiveFileObject = (ArchiveFileObject) o; 151 return archiveFileObject.toUri().equals(this.toUri()); 152 } 153 154 157 public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { 158 if (getKind() == Kind.SOURCE) { 159 return Util.getCharContents(this, ignoreEncodingErrors, org.eclipse.jdt.internal.compiler.util.Util.getZipEntryByteContent(this.zipEntry, this.zipFile), this.charset.toString()); 160 } 161 return null; 162 } 163 164 167 public long getLastModified() { 168 return this.zipEntry.getTime(); } 170 171 174 public String getName() { 175 return this.zipEntry.getName(); 176 } 177 178 181 public InputStream openInputStream() throws IOException { 182 return this.zipFile.getInputStream(this.zipEntry); 183 } 184 185 188 public OutputStream openOutputStream() throws IOException { 189 throw new UnsupportedOperationException (); 190 } 191 192 195 public Reader openReader(boolean ignoreEncodingErrors) throws IOException { 196 throw new UnsupportedOperationException (); 197 } 198 199 202 public Writer openWriter() throws IOException { 203 throw new UnsupportedOperationException (); 204 } 205 206 209 public URI toUri() { 210 try { 211 return new URI ("jar:" + this.file.toURI().getPath() + "!" + this.zipEntry.getName()); } catch (URISyntaxException e) { 213 return null; 214 } 215 } 216 217 218 @Override 219 public String toString() { 220 return this.file.getAbsolutePath() + "[" + this.zipEntry.getName() + "]"; } 222 } 223 | Popular Tags |