Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refine ConfigurationParser #113

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,15 @@ public class ConfigurationParser {
*/
public static final String OPTIONALLY_PREFIX = "optionally";

private ConfigurationHandler handler;
protected static final String FROM_SEPARATOR = " from ";

private Properties systemProperties;
protected static final String USING_SEPARATOR = " using ";

protected static final String DEFAULT_SEPARATOR = " default ";

private final ConfigurationHandler handler;

private final Properties systemProperties;

public ConfigurationParser(ConfigurationHandler handler, Properties systemProperties) {
this.handler = handler;
Expand All @@ -74,188 +80,225 @@ public ConfigurationParser(ConfigurationHandler handler, Properties systemProper
*/
public void parse(InputStream is)
throws IOException, ConfigurationException, DuplicateRealmException, NoSuchRealmException {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {

String line;
String line;

int lineNo = 0;
int lineNo = 0;

boolean mainSet = false;
boolean mainSet = false;

String curRealm = null;
String curRealm = null;

while (true) {
line = reader.readLine();

if (line == null) {
break;
}

++lineNo;
line = line.trim();

if (canIgnore(line)) {
continue;
}
while (true) {
line = reader.readLine();

if (line.startsWith(MAIN_PREFIX)) {
if (mainSet) {
throw new ConfigurationException("Duplicate main configuration", lineNo, line);
if (line == null) {
break;
}

String conf = line.substring(MAIN_PREFIX.length()).trim();
++lineNo;
line = line.trim();

int fromLoc = conf.indexOf("from");

if (fromLoc < 0) {
throw new ConfigurationException("Missing from clause", lineNo, line);
if (canIgnore(line)) {
continue;
}

String mainClassName = filter(conf.substring(0, fromLoc).trim());
char lineFirstChar = line.charAt(0);
switch (lineFirstChar) {
case 'm': {
if (line.startsWith(MAIN_PREFIX)) {
if (mainSet) {
throw new ConfigurationException("Duplicate main configuration", lineNo, line);
}

String mainRealmName = filter(conf.substring(fromLoc + 4).trim());
int fromLoc = line.indexOf(FROM_SEPARATOR, MAIN_PREFIX.length());

this.handler.setAppMain(mainClassName, mainRealmName);
if (fromLoc < 0) {
throw new ConfigurationException("Missing from clause", lineNo, line);
}

mainSet = true;
} else if (line.startsWith(SET_PREFIX)) {
String conf = line.substring(SET_PREFIX.length()).trim();
String mainClassName = filter(line.substring(MAIN_PREFIX.length(), fromLoc)
.trim());

int usingLoc = conf.indexOf(" using") + 1;
String mainRealmName = filter(line.substring(fromLoc + FROM_SEPARATOR.length())
.trim());

String property = null;
this.handler.setAppMain(mainClassName, mainRealmName);

String propertiesFileName = null;
mainSet = true;

if (usingLoc > 0) {
property = conf.substring(0, usingLoc).trim();
break;
}
throw new ConfigurationException("Unhandled configuration", lineNo, line);
}
case 's': {
if (line.startsWith(SET_PREFIX)) {
String conf = line.substring(SET_PREFIX.length()).trim();

propertiesFileName = filter(conf.substring(usingLoc + 5).trim());
int usingLoc = conf.indexOf(USING_SEPARATOR);

conf = propertiesFileName;
}
String property = null;

String defaultValue = null;
String propertiesFileName = null;

int defaultLoc = conf.indexOf(" default") + 1;
if (usingLoc >= 0) {
property = conf.substring(0, usingLoc).trim();

if (defaultLoc > 0) {
defaultValue = filter(conf.substring(defaultLoc + 7).trim());
propertiesFileName = filter(conf.substring(usingLoc + USING_SEPARATOR.length())
.trim());

if (property == null) {
property = conf.substring(0, defaultLoc).trim();
} else {
propertiesFileName = conf.substring(0, defaultLoc).trim();
}
}
conf = propertiesFileName;
}

String value = systemProperties.getProperty(property);
String defaultValue = null;

if (value != null) {
continue;
}
int defaultLoc = conf.indexOf(DEFAULT_SEPARATOR);

if (propertiesFileName != null) {
File propertiesFile = new File(propertiesFileName);
if (defaultLoc >= 0) {
defaultValue = filter(conf.substring(defaultLoc + DEFAULT_SEPARATOR.length())
.trim());

if (propertiesFile.exists()) {
Properties properties = new Properties();
if (property == null) {
property = conf.substring(0, defaultLoc).trim();
} else {
propertiesFileName =
conf.substring(0, defaultLoc).trim();
}
}

try {
properties.load(Files.newInputStream(Paths.get(propertiesFileName)));
String value = systemProperties.getProperty(property);

value = properties.getProperty(property);
} catch (Exception e) {
// do nothing
}
}
}
if (value != null) {
continue;
}

if (value == null && defaultValue != null) {
value = defaultValue;
}
if (propertiesFileName != null) {
File propertiesFile = new File(propertiesFileName);

if (value != null) {
value = filter(value);
systemProperties.setProperty(property, value);
}
} else if (line.startsWith("[")) {
int rbrack = line.indexOf("]");
if (propertiesFile.exists()) {
Properties properties = new Properties();

if (rbrack < 0) {
throw new ConfigurationException("Invalid realm specifier", lineNo, line);
}
try (InputStream inputStream =
Files.newInputStream(Paths.get(propertiesFileName))) {
properties.load(inputStream);
value = properties.getProperty(property);
} catch (Exception e) {
// do nothing
}
}
}

String realmName = line.substring(1, rbrack);
if (value == null && defaultValue != null) {
value = defaultValue;
}

handler.addRealm(realmName);
if (value != null) {
value = filter(value);
systemProperties.setProperty(property, value);
}

curRealm = realmName;
} else if (line.startsWith(IMPORT_PREFIX)) {
if (curRealm == null) {
throw new ConfigurationException("Unhandled import", lineNo, line);
}
break;
}
throw new ConfigurationException("Unhandled configuration", lineNo, line);
}
case '[': {
int rbrack = line.indexOf("]");

String conf = line.substring(IMPORT_PREFIX.length()).trim();
if (rbrack < 0) {
throw new ConfigurationException("Invalid realm specifier", lineNo, line);
}

int fromLoc = conf.indexOf("from");
String realmName = line.substring(1, rbrack);

if (fromLoc < 0) {
throw new ConfigurationException("Missing from clause", lineNo, line);
}
handler.addRealm(realmName);

String importSpec = conf.substring(0, fromLoc).trim();
curRealm = realmName;

String relamName = conf.substring(fromLoc + 4).trim();
break;
}
case 'i': {
if (line.startsWith(IMPORT_PREFIX)) {
if (curRealm == null) {
throw new ConfigurationException("Unhandled import", lineNo, line);
}
int fromLoc = line.indexOf(FROM_SEPARATOR, IMPORT_PREFIX.length());

handler.addImportFrom(relamName, importSpec);
if (fromLoc < 0) {
throw new ConfigurationException("Missing from clause", lineNo, line);
}

} else if (line.startsWith(LOAD_PREFIX)) {
String constituent = line.substring(LOAD_PREFIX.length()).trim();
String importSpec = line.substring(IMPORT_PREFIX.length(), fromLoc)
.trim();

constituent = filter(constituent);
String relamName = line.substring(fromLoc + FROM_SEPARATOR.length())
.trim();

if (constituent.contains("*")) {
loadGlob(constituent, false /*not optionally*/);
} else {
File file = new File(constituent);
handler.addImportFrom(relamName, importSpec);

if (file.exists()) {
handler.addLoadFile(file);
} else {
try {
handler.addLoadURL(new URL(constituent));
} catch (MalformedURLException e) {
throw new FileNotFoundException(constituent);
break;
}
throw new ConfigurationException("Unhandled configuration", lineNo, line);
}
}
} else if (line.startsWith(OPTIONALLY_PREFIX)) {
String constituent = line.substring(OPTIONALLY_PREFIX.length()).trim();

constituent = filter(constituent);

if (constituent.contains("*")) {
loadGlob(constituent, true /*optionally*/);
} else {
File file = new File(constituent);

if (file.exists()) {
handler.addLoadFile(file);
} else {
try {
handler.addLoadURL(new URL(constituent));
} catch (MalformedURLException e) {
// swallow
case 'l': {
if (line.startsWith(LOAD_PREFIX)) {
String constituent =
line.substring(LOAD_PREFIX.length()).trim();

constituent = filter(constituent);

if (constituent.contains("*")) {
loadGlob(constituent, false /*not optionally*/);
} else {
File file = new File(constituent);

if (file.exists()) {
handler.addLoadFile(file);
} else {
try {
handler.addLoadURL(new URL(constituent));
} catch (MalformedURLException e) {
throw new FileNotFoundException(constituent);
}
}
}

break;
}
throw new ConfigurationException("Unhandled configuration", lineNo, line);
}
case 'o': {
if (line.startsWith(OPTIONALLY_PREFIX)) {
String constituent =
line.substring(OPTIONALLY_PREFIX.length()).trim();

constituent = filter(constituent);

if (constituent.contains("*")) {
loadGlob(constituent, true /*optionally*/);
} else {
File file = new File(constituent);

if (file.exists()) {
handler.addLoadFile(file);
} else {
try {
handler.addLoadURL(new URL(constituent));
} catch (MalformedURLException e) {
// swallow
}
}
}

break;
}
throw new ConfigurationException("Unhandled configuration", lineNo, line);
}
default:
throw new ConfigurationException("Unhandled configuration", lineNo, line);
}
} else {
throw new ConfigurationException("Unhandled configuration", lineNo, line);
}
}

reader.close();
}

/**
Expand Down Expand Up @@ -373,6 +416,6 @@ protected String filter(String text) throws ConfigurationException {
* otherwise <code>false</code>.
*/
private boolean canIgnore(String line) {
return (line.length() == 0 || line.startsWith("#"));
return (line.isEmpty() || line.startsWith("#"));
}
}
Loading