KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > xhtml > XHTMLContentDescriber


1 /*******************************************************************************
2  * Copyright (c) 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.help.internal.xhtml;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.Reader JavaDoc;
16
17 import org.eclipse.core.runtime.QualifiedName;
18 import org.eclipse.core.runtime.content.IContentDescriber;
19 import org.eclipse.core.runtime.content.IContentDescription;
20 import org.eclipse.help.internal.search.ASCIIReader;
21
22 /**
23  * A content describer for XHTML.
24  */

25 public class XHTMLContentDescriber implements IContentDescriber {
26
27     private static final String JavaDoc XHTML_DTD_PREFIX = "http://www.w3.org/TR/xhtml"; //$NON-NLS-1$
28

29     public static final int BUFFER_SIZE = 4096;
30
31     /* (non-Javadoc)
32      * @see org.eclipse.core.runtime.content.IContentDescriber#describe(java.io.InputStream, org.eclipse.core.runtime.content.IContentDescription)
33      */

34     public int describe(InputStream JavaDoc contents, IContentDescription description) throws IOException JavaDoc {
35         Reader JavaDoc reader = null;
36         try {
37             reader = new ASCIIReader(contents, BUFFER_SIZE);
38             char[] chars = new char[BUFFER_SIZE];
39             reader.read(chars);
40             String JavaDoc str = new String JavaDoc(chars);
41             return (str.indexOf(XHTML_DTD_PREFIX) != -1) ? VALID : INVALID;
42         }
43         catch (Exception JavaDoc e) {
44             return INDETERMINATE;
45         }
46         finally {
47             if (reader != null) {
48                 try {
49                     reader.close();
50                 }
51                 catch (IOException JavaDoc e) {
52                 }
53             }
54         }
55     }
56
57     /* (non-Javadoc)
58      * @see org.eclipse.core.runtime.content.IContentDescriber#getSupportedOptions()
59      */

60     public QualifiedName[] getSupportedOptions() {
61         return new QualifiedName[0];
62     }
63 }
64
Popular Tags