1b3a58c573031d896a1d49cad96216fe1d87e2e9
[phpeclipse.git] /
1 /*
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
7  *
8  * Contributors:
9  *     Igor Malinin - initial contribution
10  *
11  * $Id: AbstractDocumentProvider.java,v 1.1 2004-09-02 18:28:03 jsurfer Exp $
12  */
13
14 package net.sourceforge.phpeclipse.xml.ui.internal.text;
15
16 import java.io.BufferedInputStream;
17 import java.io.IOException;
18 import java.io.InputStream;
19
20 import org.eclipse.jface.text.rules.IWhitespaceDetector;
21
22 import net.sourceforge.phpeclipse.ui.editor.I18NDocumentProvider;
23
24
25 /**
26  * 
27  * 
28  * @author Igor Malinin
29  */
30 public abstract class AbstractDocumentProvider extends I18NDocumentProvider {
31         protected IWhitespaceDetector detector = new WhitespaceDetector();
32
33         /*
34          * @see org.eclipse.ui.editors.text.IStorageDocumentProvider#getDefaultEncoding()
35          */
36         public String getDefaultEncoding() {
37                 return "UTF-8";
38         }
39
40         public String getDeclaredEncoding( InputStream in ) throws IOException {
41                 if ( !in.markSupported() ) {
42                         in = new BufferedInputStream( in, 512 );
43                 }
44
45                 in.mark( 512 );
46                 String encoding = super.getDeclaredEncoding( in );
47                 if ( encoding != null ) {
48                         return encoding;
49                 }
50
51                 in.reset();
52
53                 // check Prolog-Start <?xml
54                 if ( !skipXMLDecl(in) ) {
55                         return null;
56                 }
57
58                 // detect 'encoding'
59                 skipEncoding( in );
60
61                 // read encoding
62                 int delimiter;
63
64                 while ( true ) {
65                         int ch = in.read();
66                         if ( ch < 0 ) {
67                                 return null;
68                         }
69
70                         if ( detector.isWhitespace((char) ch) ) {
71                                 continue;
72                         }
73
74                         if ( ch == '"' || ch == '\'' ) {
75                                 delimiter = ch;
76                                 break;
77                         }
78
79                         return null;
80                 }
81
82                 StringBuffer buf = new StringBuffer();
83
84                 while ( true ) {
85                         int ch = in.read();
86                         if ( ch < 0 ) {
87                                 return null;
88                         }
89
90                         if ( ch == delimiter ) {
91                                 break;
92                         }
93
94                         buf.append( (char) ch );
95                 }
96
97                 return buf.toString();
98         }
99
100         private boolean skipXMLDecl( InputStream in ) throws IOException {
101                 int ch = in.read();
102                 if ( ch != '<' ) {
103                         return false;
104                 }
105
106                 ch = in.read();
107                 if ( ch != '?' ) {
108                         return false;
109                 }
110
111                 ch = in.read();
112                 if ( ch != 'x' ) {
113                         return false;
114                 }
115
116                 ch = in.read();
117                 if ( ch != 'm' ) {
118                         return false;
119                 }
120
121                 ch = in.read();
122                 if ( ch != 'l' ) {
123                         return false;
124                 }
125
126                 return true;
127         }
128
129         private boolean skipEncoding( InputStream in ) throws IOException {
130                 int ch = in.read();
131
132                 boolean whitespace = false;
133
134                 while ( true ) {
135                         if ( ch < 0 ) {
136                                 return false;
137                         }
138
139                         if ( detector.isWhitespace((char) ch) ) {
140                                 ch = in.read();
141                                 whitespace = true;
142                                 continue;
143                         }
144
145                         if ( ch == '?' || ch == '<' ) {
146                                 return false;
147                         }
148
149                         if ( ch != 'e' ) {
150                                 ch = in.read();
151                                 whitespace = false;
152                                 continue;
153                         }
154
155                         if ( !whitespace ) {
156                                 ch = in.read();
157                                 continue;
158                         }
159
160                         if ( (ch = in.read()) == 'n' &&
161                                         (ch = in.read()) == 'c' &&
162                                         (ch = in.read()) == 'o' &&
163                                         (ch = in.read()) == 'd' &&
164                                         (ch = in.read()) == 'i' &&
165                                         (ch = in.read()) == 'n' &&
166                                         (ch = in.read()) == 'g' ) {
167                                 break;
168                         }
169
170                         whitespace = false;
171                 }
172
173                 // '='
174                 while ( true ) {
175                         ch = in.read();
176                         if ( ch < 0 ) {
177                                 return false;
178                         }
179
180                         if ( detector.isWhitespace((char) ch) ) {
181                                 continue;
182                         }
183
184                         if ( ch == '=' ) {
185                                 break;
186                         }
187
188                         return false;
189                 }
190
191                 return true;
192         }
193 }