KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > content > TextContentDescriber


1 /*******************************************************************************
2  * Copyright (c) 2004, 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.core.internal.content;
12
13 import java.io.*;
14 import org.eclipse.core.runtime.QualifiedName;
15 import org.eclipse.core.runtime.content.IContentDescription;
16 import org.eclipse.core.runtime.content.ITextContentDescriber;
17
18 /**
19  * This class provides internal basis for text-based content describers.
20  *
21  * <p>
22  * Note: do not add protected/public members to this class if you don't intend to
23  * make them public API.
24  * </p>
25  *
26  * @see org.eclipse.core.runtime.content.XMLRootElementContentDescriber
27  * @since 3.0
28  */

29 public class TextContentDescriber implements ITextContentDescriber {
30
31     private final static QualifiedName[] SUPPORTED_OPTIONS = {IContentDescription.BYTE_ORDER_MARK};
32
33     /*
34      * (non-Javadoc)
35      * @see org.eclipse.core.runtime.content.ITextContentDescriber#describe(java.io.Reader, org.eclipse.core.runtime.content.IContentDescription)
36      */

37     public int describe(Reader contents, IContentDescription description) throws IOException {
38         // we want to be pretty loose on detecting the text content type
39
return INDETERMINATE;
40     }
41
42     /*
43      * (non-Javadoc)
44      * @see org.eclipse.core.runtime.content.IContentDescriber#describe(java.io.InputStream, org.eclipse.core.runtime.content.IContentDescription)
45      */

46     public int describe(InputStream contents, IContentDescription description) throws IOException {
47         if (description == null || !description.isRequested(IContentDescription.BYTE_ORDER_MARK))
48             return INDETERMINATE;
49         byte[] bom = getByteOrderMark(contents);
50         if (bom != null)
51             description.setProperty(IContentDescription.BYTE_ORDER_MARK, bom);
52         // we want to be pretty loose on detecting the text content type
53
return INDETERMINATE;
54     }
55
56     /*
57      * (non-Javadoc)
58      * @see org.eclipse.core.runtime.content.IContentDescriber#getSupportedOptions()
59      */

60     public QualifiedName[] getSupportedOptions() {
61         return SUPPORTED_OPTIONS;
62     }
63
64     byte[] getByteOrderMark(InputStream input) throws IOException {
65         int first = (input.read() & 0xFF);//converts unsigned byte to int
66
int second = (input.read() & 0xFF);
67         if (first == -1 || second == -1)
68             return null;
69         //look for the UTF-16 Byte Order Mark (BOM)
70
if (first == 0xFE && second == 0xFF)
71             return IContentDescription.BOM_UTF_16BE;
72         if (first == 0xFF && second == 0xFE)
73             return IContentDescription.BOM_UTF_16LE;
74         int third = (input.read() & 0xFF);
75         if (third == -1)
76             return null;
77         //look for the UTF-8 BOM
78
if (first == 0xEF && second == 0xBB && third == 0xBF)
79             return IContentDescription.BOM_UTF_8;
80         return null;
81     }
82
83 }
84
Popular Tags