2 * Copyright (c) 2002-2004 Widespace, OU and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * Igor Malinin - initial contribution
11 * $Id: AbstractDocumentProvider.java,v 1.3 2006-10-21 23:14:13 pombredanne Exp $
14 package net.sourceforge.phpeclipse.xml.ui.internal.text;
16 import java.io.BufferedInputStream;
17 import java.io.IOException;
18 import java.io.InputStream;
20 import net.sourceforge.phpeclipse.ui.editor.I18NDocumentProvider;
22 import org.eclipse.jface.text.rules.IWhitespaceDetector;
27 * @author Igor Malinin
29 public abstract class AbstractDocumentProvider extends I18NDocumentProvider {
30 protected IWhitespaceDetector detector = new WhitespaceDetector();
33 * @see org.eclipse.ui.editors.text.IStorageDocumentProvider#getDefaultEncoding()
35 public String getDefaultEncoding() {
39 public String getDeclaredEncoding(InputStream in) throws IOException {
40 if (!in.markSupported()) {
41 in = new BufferedInputStream(in, 512);
45 String encoding = super.getDeclaredEncoding(in);
46 if (encoding != null) {
52 // check Prolog-Start <?xml
53 if (!skipXMLDecl(in)) {
69 if (detector.isWhitespace((char) ch)) {
73 if (ch == '"' || ch == '\'') {
81 StringBuffer buf = new StringBuffer();
89 if (ch == delimiter) {
93 buf.append((char) ch);
96 return buf.toString();
99 private boolean skipXMLDecl(InputStream in) throws IOException {
128 private boolean skipEncoding(InputStream in) throws IOException {
131 boolean whitespace = false;
138 if (detector.isWhitespace((char) ch)) {
144 if (ch == '?' || ch == '<') {
159 if ((ch = in.read()) == 'n' && (ch = in.read()) == 'c'
160 && (ch = in.read()) == 'o' && (ch = in.read()) == 'd'
161 && (ch = in.read()) == 'i' && (ch = in.read()) == 'n'
162 && (ch = in.read()) == 'g') {
176 if (detector.isWhitespace((char) ch)) {