KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > content > BundleManifestDescriber


1 /*******************************************************************************
2  * Copyright (c) 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.content;
12
13 import java.io.BufferedReader JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.io.InputStreamReader JavaDoc;
17 import java.io.Reader JavaDoc;
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 JavaDoc[] 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; //$NON-NLS-1$
28

29     private final static QualifiedName[] SUPPORTED_OPTIONS = {IContentDescription.BYTE_ORDER_MARK};
30
31     /* (Intentionally not included in javadoc)
32      * @see IContentDescriber#describe(InputStream, IContentDescription)
33      */

34     public int describe(InputStream JavaDoc contents, IContentDescription description) throws IOException JavaDoc {
35         byte[] bom = getByteOrderMark(contents);
36         contents.reset();
37         String JavaDoc charset = "UTF-8"; //$NON-NLS-1$
38
if (bom != null) {
39             // has a bom
40
// remember to skip it
41
contents.skip(bom.length);
42             // compute a corresponding charset
43
if (bom == IContentDescription.BOM_UTF_8)
44                 charset = "UTF-8"; //$NON-NLS-1$
45
else if (bom == IContentDescription.BOM_UTF_16BE || bom == IContentDescription.BOM_UTF_16LE)
46                 // UTF-16 will properly recognize the BOM
47
charset = "UTF-16"; //$NON-NLS-1$
48
// fill description if requesed
49
if (description != null && description.isRequested(IContentDescription.BYTE_ORDER_MARK))
50                 description.setProperty(IContentDescription.BYTE_ORDER_MARK, bom);
51         }
52         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(contents, charset));
53         String JavaDoc line;
54         for (int i = 0; ((line = reader.readLine()) != null) && i < LINES; i++)
55             if (matches(line))
56                 // found signature
57
return VALID;
58         // could not find signature
59
return INDETERMINATE;
60     }
61
62     /*
63      * (non-Javadoc)
64      * @see org.eclipse.core.runtime.content.ITextContentDescriber#describe(java.io.Reader, org.eclipse.core.runtime.content.IContentDescription)
65      */

66     public int describe(Reader JavaDoc contents, IContentDescription description) throws IOException JavaDoc {
67         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(contents);
68         String JavaDoc 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 JavaDoc input) throws IOException JavaDoc {
76         int first = (input.read() & 0xFF);//converts unsigned byte to int
77
int second = (input.read() & 0xFF);
78         if (first == -1 || second == -1)
79             return null;
80         //look for the UTF-16 Byte Order Mark (BOM)
81
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         //look for the UTF-8 BOM
89
if (first == 0xEF && second == 0xBB && third == 0xBF)
90             return IContentDescription.BOM_UTF_8;
91         return null;
92     }
93
94     /*
95      * (non-Javadoc)
96      * @see org.eclipse.core.runtime.content.IContentDescriber#getSupportedOptions()
97      */

98     public QualifiedName[] getSupportedOptions() {
99         return SUPPORTED_OPTIONS;
100     }
101
102     private boolean matches(String JavaDoc 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