• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.eclipse.org/org/documents/epl-v10.php
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.ide.eclipse.adt.internal.editors.binaryxml;
18 
19 import org.eclipse.core.runtime.QualifiedName;
20 import org.eclipse.core.runtime.content.IContentDescriber;
21 import org.eclipse.core.runtime.content.IContentDescription;
22 
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.nio.ByteBuffer;
26 import java.nio.ByteOrder;
27 
28 /**
29  * A content describer for Android binary xml files
30  *
31  * <p>
32  * This class referenced by the "describer" configuration element in
33  * extensions to the <code>org.eclipse.core.runtime.contentTypes</code>
34  * extension point.
35  * </p>
36  *
37  * References :
38  * <a>http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;
39  * f=include/utils/ResourceTypes.h</a>
40  *
41  */
42 public class BinaryXMLDescriber implements IContentDescriber {
43 
44     private static final int RES_XML_HEADER_SIZE = 8;
45     private final static short RES_XML_TYPE = 0x0003;
46 
47     /*
48      * (non-Javadoc)
49      * @see org.eclipse.core.runtime.content.IContentDescriber#describe(java.io.
50      * InputStream, org.eclipse.core.runtime.content.IContentDescription)
51      */
52     @Override
describe(InputStream contents, IContentDescription description)53     public int describe(InputStream contents, IContentDescription description) throws IOException {
54         int status = INVALID;
55         int length = 8;
56         byte[] bytes = new byte[length];
57         if (contents.read(bytes, 0, length) == length) {
58             ByteBuffer buf = ByteBuffer.wrap(bytes);
59             buf.order(ByteOrder.LITTLE_ENDIAN);
60             short type = buf.getShort();
61             short headerSize = buf.getShort();
62             int size = buf.getInt(); // chunk size
63             if (type == RES_XML_TYPE && headerSize == RES_XML_HEADER_SIZE) {
64                 status = VALID;
65             }
66         }
67         return status;
68     }
69 
70     /*
71      * (non-Javadoc)
72      * @see
73      * org.eclipse.core.runtime.content.IContentDescriber#getSupportedOptions()
74      */
75     @Override
getSupportedOptions()76     public QualifiedName[] getSupportedOptions() {
77         return new QualifiedName[0];
78     }
79 
80 }
81