KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > runtime > content > BinarySignatureDescriber


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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.core.runtime.content;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.util.*;
16 import org.eclipse.core.internal.content.ContentMessages;
17 import org.eclipse.core.runtime.*;
18 import org.eclipse.osgi.util.NLS;
19
20 /**
21  * A content describer for binary formats that present some
22  * simple signature at a known, fixed offset.
23  * <p>
24  * This executable extension supports three parameters:
25  * "signature", "offset" and "required", the first one being mandatory.
26  * If the
27  * <code>":-"</code> method is used, then the value is treated as the
28  * "signature".
29  * </p>
30  * <p>
31  * The "signature" parameter is a sequence of hex codes, one for each byte in
32  * the signature. For example, "CA FE BA BE" would be a signature for Java
33  * class files.
34  * </p>
35  * <p>
36  * The "offset" parameter is an integer indicating the offset where the
37  * signature's first byte is found.
38  * </p>
39  * <p>
40  * The "required" parameter is a boolean (default is " true") indicating whether
41  * the absence of a signature should deem the contents validity status as
42  * IContentDescriber.INVALID or IContentDescriber.INDETERMINATE.
43  * </p>
44  * <p>
45  * This class is not intended to be subclassed or instantiated by clients,
46  * only to be referenced by the "describer" configuration element in
47  * extensions to the <code>org.eclipse.core.runtime.contentTypes</code>
48  * extension point.
49  * </p>
50  *
51  * @since 3.0
52  */

53 public final class BinarySignatureDescriber implements IContentDescriber, IExecutableExtension {
54     private final static String JavaDoc SIGNATURE = "signature"; //$NON-NLS-1$
55
private final static String JavaDoc OFFSET = "offset"; //$NON-NLS-1$
56
private static final Object JavaDoc REQUIRED = "required"; //$NON-NLS-1$
57
private byte[] signature;
58     private int offset;
59     private boolean required = true;
60
61     /* (Intentionally not included in javadoc)
62      * @see IContentDescriber#describe(InputStream, IContentDescription)
63      */

64     public int describe(InputStream JavaDoc contents, IContentDescription description) throws IOException JavaDoc {
65         byte[] buffer = new byte[signature.length];
66         int notValid = required ? INVALID : INDETERMINATE;
67         if (contents.skip(offset) < offset)
68             return notValid;
69         if (contents.read(buffer) != buffer.length)
70             return notValid;
71         for (int i = 0; i < signature.length; i++)
72             if (signature[i] != buffer[i])
73                 return notValid;
74         return VALID;
75     }
76
77     /* (Intentionally not included in javadoc)
78      * @see IContentDescriber#getSupportedOptions
79      */

80     public QualifiedName[] getSupportedOptions() {
81         return new QualifiedName[0];
82     }
83
84     /* (Intentionally not included in javadoc)
85      * @see IExecutableExtension#setInitializationData
86      */

87     public void setInitializationData(IConfigurationElement config, String JavaDoc propertyName, Object JavaDoc data) throws CoreException {
88         try {
89             if (data instanceof String JavaDoc)
90                 signature = parseSignature((String JavaDoc) data);
91             else if (data instanceof Hashtable) {
92                 Hashtable parameters = (Hashtable) data;
93                 if (!parameters.containsKey(SIGNATURE)) {
94                     String JavaDoc message = NLS.bind(ContentMessages.content_badInitializationData, XMLRootElementContentDescriber.class.getName());
95                     throw new CoreException(new Status(IStatus.ERROR, ContentMessages.OWNER_NAME, 0, message, null));
96                 }
97                 signature = parseSignature((String JavaDoc) parameters.get(SIGNATURE));
98                 if (parameters.containsKey(OFFSET))
99                     offset = Integer.parseInt((String JavaDoc) parameters.get(OFFSET));
100                 if (parameters.containsKey(REQUIRED))
101                     required = Boolean.valueOf((String JavaDoc) parameters.get(REQUIRED)).booleanValue();
102             }
103         } catch (NumberFormatException JavaDoc nfe) {
104             String JavaDoc message = NLS.bind(ContentMessages.content_badInitializationData, BinarySignatureDescriber.class.getName());
105             throw new CoreException(new Status(IStatus.ERROR, ContentMessages.OWNER_NAME, 0, message, nfe));
106         }
107     }
108
109     private static byte[] parseSignature(String JavaDoc data) {
110         List bytes = new ArrayList();
111         StringTokenizer tokenizer = new StringTokenizer(data, " \t\n\r\f,"); //$NON-NLS-1$
112
while (tokenizer.hasMoreTokens())
113             bytes.add(new Byte JavaDoc((byte) Integer.parseInt(tokenizer.nextToken().trim(), 16)));
114         byte[] signature = new byte[bytes.size()];
115         for (int i = 0; i < signature.length; i++)
116             signature[i] = ((Byte JavaDoc) bytes.get(i)).byteValue();
117         return signature;
118     }
119 }
120
Popular Tags