1 11 package org.eclipse.pde.internal.content; 12 13 import java.io.BufferedReader ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.io.InputStreamReader ; 17 import java.io.Reader ; 18 19 import org.eclipse.core.runtime.QualifiedName; 20 import org.eclipse.core.runtime.content.IContentDescription; 21 import org.eclipse.core.runtime.content.ITextContentDescriber; 22 import org.osgi.framework.Constants; 23 24 public class BundleManifestDescriber implements ITextContentDescriber { 25 26 private final static String [] HEADERS = {Constants.BUNDLE_MANIFESTVERSION, Constants.BUNDLE_NAME, Constants.BUNDLE_VERSION, Constants.BUNDLE_SYMBOLICNAME, Constants.BUNDLE_VENDOR, Constants.BUNDLE_ACTIVATOR}; 27 private final static int LINES = 50; 29 private final static QualifiedName[] SUPPORTED_OPTIONS = {IContentDescription.BYTE_ORDER_MARK}; 30 31 34 public int describe(InputStream contents, IContentDescription description) throws IOException { 35 byte[] bom = getByteOrderMark(contents); 36 contents.reset(); 37 String charset = "UTF-8"; if (bom != null) { 39 contents.skip(bom.length); 42 if (bom == IContentDescription.BOM_UTF_8) 44 charset = "UTF-8"; else if (bom == IContentDescription.BOM_UTF_16BE || bom == IContentDescription.BOM_UTF_16LE) 46 charset = "UTF-16"; if (description != null && description.isRequested(IContentDescription.BYTE_ORDER_MARK)) 50 description.setProperty(IContentDescription.BYTE_ORDER_MARK, bom); 51 } 52 BufferedReader reader = new BufferedReader (new InputStreamReader (contents, charset)); 53 String line; 54 for (int i = 0; ((line = reader.readLine()) != null) && i < LINES; i++) 55 if (matches(line)) 56 return VALID; 58 return INDETERMINATE; 60 } 61 62 66 public int describe(Reader contents, IContentDescription description) throws IOException { 67 BufferedReader reader = new BufferedReader (contents); 68 String line; 69 for (int i = 0; ((line = reader.readLine()) != null) && i < LINES; i++) 70 if (matches(line)) 71 return VALID; 72 return INDETERMINATE; 73 } 74 75 byte[] getByteOrderMark(InputStream input) throws IOException { 76 int first = (input.read() & 0xFF); int second = (input.read() & 0xFF); 78 if (first == -1 || second == -1) 79 return null; 80 if (first == 0xFE && second == 0xFF) 82 return IContentDescription.BOM_UTF_16BE; 83 if (first == 0xFF && second == 0xFE) 84 return IContentDescription.BOM_UTF_16LE; 85 int third = (input.read() & 0xFF); 86 if (third == -1) 87 return null; 88 if (first == 0xEF && second == 0xBB && third == 0xBF) 90 return IContentDescription.BOM_UTF_8; 91 return null; 92 } 93 94 98 public QualifiedName[] getSupportedOptions() { 99 return SUPPORTED_OPTIONS; 100 } 101 102 private boolean matches(String line) { 103 for (int i = 0; i < HEADERS.length; i++) { 104 int length = HEADERS[i].length(); 105 if (line.length() >= length) 106 if (line.substring(0, length).equalsIgnoreCase(HEADERS[i])) 107 return true; 108 } 109 return false; 110 } 111 } 112 | Popular Tags |