au.com.integradev.delphi.antlr.DelphiFileStream Maven / Gradle / Ivy
The newest version!
/*
* Sonar Delphi Plugin
* Copyright (C) 2019 Integrated Application Development
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package au.com.integradev.delphi.antlr;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import org.antlr.runtime.ANTLRStringStream;
import org.apache.commons.io.ByteOrderMark;
import org.apache.commons.io.input.BOMInputStream;
public class DelphiFileStream extends ANTLRStringStream {
private final String fileName;
private final String encoding;
public DelphiFileStream(String fileName, String encoding) throws IOException {
this.fileName = fileName;
this.encoding = this.load(fileName, encoding);
}
private String load(String fileName, String encoding) throws IOException {
if (fileName != null) {
File f = new File(fileName);
int size = (int) f.length();
try (BOMInputStream input = bomInputStream(fileName).get()) {
ByteOrderMark bom = input.getBOM();
if (bom != null) {
encoding = bom.getCharsetName();
}
if (encoding == null) {
encoding = Charset.defaultCharset().name();
}
try (InputStreamReader reader = new InputStreamReader(input, encoding)) {
this.data = new char[size];
super.n = reader.read(this.data);
}
}
}
return encoding;
}
@Override
public String getSourceName() {
return this.fileName;
}
public String getEncoding() {
return this.encoding;
}
private static BOMInputStream.Builder bomInputStream(String fileName) throws IOException {
return BOMInputStream.builder()
.setInputStream(new FileInputStream(fileName))
.setInclude(false)
.setByteOrderMarks(
ByteOrderMark.UTF_8,
ByteOrderMark.UTF_16LE,
ByteOrderMark.UTF_16BE,
ByteOrderMark.UTF_32LE,
ByteOrderMark.UTF_32BE);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy