KickJava   Java API By Example, From Geeks To Geeks.

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


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  * A content interpreter for XML files.
20  * This class provides internal basis for XML-based content describers.
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  * @see "http://www.w3.org/TR/REC-xml *"
28  */

29 public class XMLContentDescriber extends TextContentDescriber implements ITextContentDescriber {
30     private static final QualifiedName[] SUPPORTED_OPTIONS = new QualifiedName[] {IContentDescription.CHARSET, IContentDescription.BYTE_ORDER_MARK};
31     private static final String JavaDoc ENCODING = "encoding="; //$NON-NLS-1$
32
private static final String JavaDoc XML_PREFIX = "<?xml "; //$NON-NLS-1$
33

34     public int describe(InputStream input, IContentDescription description) throws IOException {
35         byte[] bom = getByteOrderMark(input);
36         String JavaDoc xmlDeclEncoding = "UTF-8"; //$NON-NLS-1$
37
input.reset();
38         if (bom != null) {
39             if (bom == IContentDescription.BOM_UTF_16BE)
40                 xmlDeclEncoding = "UTF-16BE"; //$NON-NLS-1$
41
else if (bom == IContentDescription.BOM_UTF_16LE)
42                 xmlDeclEncoding = "UTF-16LE"; //$NON-NLS-1$
43
// skip BOM to make comparison simpler
44
input.skip(bom.length);
45             // set the BOM in the description if requested
46
if (description != null && description.isRequested(IContentDescription.BYTE_ORDER_MARK))
47                 description.setProperty(IContentDescription.BYTE_ORDER_MARK, bom);
48         }
49         byte[] xmlPrefixBytes = XML_PREFIX.getBytes(xmlDeclEncoding);
50         byte[] prefix = new byte[xmlPrefixBytes.length];
51         if (input.read(prefix) < prefix.length)
52             // there is not enough info to say anything
53
return INDETERMINATE;
54         for (int i = 0; i < prefix.length; i++)
55             if (prefix[i] != xmlPrefixBytes[i])
56                 // we don't have a XMLDecl... there is not enough info to say anything
57
return INDETERMINATE;
58         if (description == null)
59             return VALID;
60         // describe charset if requested
61
if (description.isRequested(IContentDescription.CHARSET)) {
62             String JavaDoc fullXMLDecl = readFullXMLDecl(input, xmlDeclEncoding);
63             if (fullXMLDecl != null) {
64                 String JavaDoc charset = getCharset(fullXMLDecl);
65                 if (charset != null && !"UTF-8".equalsIgnoreCase(charset)) //$NON-NLS-1$
66
// only set property if value is not default (avoid using a non-default content description)
67
description.setProperty(IContentDescription.CHARSET, getCharset(fullXMLDecl));
68             }
69         }
70         return VALID;
71     }
72
73     private String JavaDoc readFullXMLDecl(InputStream input, String JavaDoc unicodeEncoding) throws IOException {
74         byte[] xmlDecl = new byte[100];
75         int c = 0;
76         // looks for XMLDecl ending char (?)
77
int read = 0;
78         while (read < xmlDecl.length && (c = input.read()) != -1 && c != '?')
79             xmlDecl[read++] = (byte) c;
80         return c == '?' ? new String JavaDoc(xmlDecl, 0, read, unicodeEncoding) : null;
81     }
82
83     public int describe(Reader input, IContentDescription description) throws IOException {
84         BufferedReader reader = new BufferedReader(input);
85         String JavaDoc line = reader.readLine();
86         // end of stream
87
if (line == null)
88             return INDETERMINATE;
89         // XMLDecl should be the first string (no blanks allowed)
90
if (!line.startsWith(XML_PREFIX))
91             return INDETERMINATE;
92         if (description == null)
93             return VALID;
94         // describe charset if requested
95
if ((description.isRequested(IContentDescription.CHARSET)))
96             description.setProperty(IContentDescription.CHARSET, getCharset(line));
97         return VALID;
98     }
99
100     private String JavaDoc getCharset(String JavaDoc firstLine) {
101         int encodingPos = firstLine.indexOf(ENCODING);
102         if (encodingPos == -1)
103             return null;
104         char quoteChar = '"';
105         int firstQuote = firstLine.indexOf(quoteChar, encodingPos);
106         if (firstQuote == -1) {
107             quoteChar = '\'';
108             firstQuote = firstLine.indexOf(quoteChar, encodingPos);
109         }
110         if (firstQuote == -1 || firstLine.length() == firstQuote - 1)
111             return null;
112         int secondQuote = firstLine.indexOf(quoteChar, firstQuote + 1);
113         if (secondQuote == -1)
114             return null;
115         return firstLine.substring(firstQuote + 1, secondQuote);
116     }
117
118     public QualifiedName[] getSupportedOptions() {
119         return SUPPORTED_OPTIONS;
120     }
121 }
122
Popular Tags