source.setByteStream(new URL(source.getURI()).openStream());
} catch (Exception e) {
try {
source.setByteStream(new FileInputStream(source.getURI()));
} catch (IOException ex) {
throw new CSSException("invalid url ?");
}
}
}
String encoding = "ASCII";
InputStream input = source.getByteStream();
char c = ' ';
if (!input.markSupported()) {
input = new BufferedInputStream(input);
source.setByteStream(input);
}
input.mark(100);
c = (char) input.read();
if (c == '@') {
// hum, is it a charset ?
int size = 100;
byte[] buf = new byte[size];
input.read(buf, 0, 7);
String keyword = new String(buf, 0, 7);
if (keyword.equals("charset")) {
// Yes, this is the charset declaration !
// here I don't use the right declaration : white space are ' '.
while ((c = (char) input.read()) == ' ') {
// find the first quote
}
char endChar = c;
int i = 0;
if ((endChar != '"') && (endChar != '\'')) {
// hum this is not a quote.
throw new CSSException("invalid charset declaration");
}
while ((c = (char) input.read()) != endChar) {
buf[i++] = (byte) c;
if (i == size) {
byte[] old = buf;
buf = new byte[size + 100];
System.arraycopy(old, 0, buf, 0, size);
size += 100;
}
}
while ((c = (char) input.read()) == ' ') {
// find the next relevant character
}
if (c != ';') {
// no semi colon at the end ?
throw new CSSException("invalid charset declaration: "
+ "missing semi colon");
}
encoding = new String(buf, 0, i);
if (source.getEncoding() != null) {
// compare the two encoding informations.
// For example, I don't accept to have ASCII and after UTF-8.
// Is it really good ? That is the question.
if (!encoding.equals(source.getEncoding())) {
throw new CSSException("invalid encoding information.");
}
}
} // else no charset declaration available
}
// ok set the real encoding of this source.