Skip to content
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
10 changes: 6 additions & 4 deletions src/main/java/com/keildraco/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.keildraco.config.factory.TypeFactory;
import com.keildraco.config.types.*;
import com.keildraco.config.states.*;

import static com.keildraco.config.types.ParserInternalTypeBase.EmptyType;
import static com.keildraco.config.types.ParserInternalTypeBase.ItemType;

Expand Down Expand Up @@ -93,8 +94,8 @@ public static void registerParser(final String name, final Class<? extends IStat
}

public static void registerKnownParts() {
internalTypes.stream().forEach( type -> registerType(type.getType(), type.getClass()));
internalParsers.entrySet().stream().forEach( ent -> registerParser(ent.getKey(), ent.getValue()));
internalTypes.stream().forEach(type -> registerType(type.getType(), type.getClass()));
internalParsers.entrySet().stream().forEach(ent -> registerParser(ent.getKey(), ent.getValue()));
}

public static void reset() {
Expand All @@ -109,13 +110,14 @@ private static SectionType runParser(final Reader reader) {
tok.slashSlashComments(true);
tok.slashStarComments(true);
final ParserInternalTypeBase root = coreTypeFactory.getType(null, "root", "", ItemType.SECTION);
return (SectionType)coreTypeFactory.getParser("SECTION", root).getState(tok);
return (SectionType) coreTypeFactory.getParser("SECTION", root).getState(tok);
}

private static FileSystem getFilesystemForURI(final URI uri) throws IOException {
if(uri.getScheme().equalsIgnoreCase("jar")) return FileSystems.newFileSystem(uri, Collections.<String, Object>emptyMap());
if (uri.getScheme().equalsIgnoreCase("jar")) return FileSystems.newFileSystem(uri, Collections.<String, Object>emptyMap());
else return FileSystems.getDefault();
}

public static DataQuery LoadFile(final URI filePath) throws IOException {
final FileSystem fs = getFilesystemForURI(filePath);
final Path p = fs.getPath(filePath.getPath().substring(1));
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/keildraco/config/data/DataQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public static DataQuery of(final SectionType section) {

public boolean get(final String key) {
// find item, or "all"
if(this.baseSection.has(key)) {
if (this.baseSection.has(key)) {
return true;
} else if(key.indexOf('.') > 0) {
} else if (key.indexOf('.') > 0) {
final String base = String.format("%s.all", key.substring(0, key.lastIndexOf('.')));
if(this.baseSection.has(base)) {
if (this.baseSection.has(base)) {
final String term = key.substring(key.lastIndexOf('.')+1);
return new ItemMatcher(this.baseSection.get(base)).matches(term);
} else {
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/com/keildraco/config/data/ItemMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ public boolean matches(final String name) {
}

private boolean doMatch(final ItemType type, final String bn, final String xn) {
switch(type) {
switch (type) {
case IDENTIFIER:
if(xn.length() > 0) return this.identMatches((IdentifierType)this.thisItem,xn)&&this.thisItem.getName().equalsIgnoreCase(bn);
else return this.identMatches((IdentifierType)this.thisItem,bn);
if (xn.length() > 0) return this.identMatches((IdentifierType) this.thisItem,xn)&&this.thisItem.getName().equalsIgnoreCase(bn);
else return this.identMatches((IdentifierType) this.thisItem,bn);
case LIST:
if(this.thisItem.has(bn) && xn.length() > 0) return (new ItemMatcher(this.thisItem.get(bn))).matches(xn);
if (this.thisItem.has(bn) && xn.length() > 0) return (new ItemMatcher(this.thisItem.get(bn))).matches(xn);
else return this.listMatchesAny(bn);
case OPERATION:
return this.operatorMatches(xn.length()>0?String.format("%s.%s", bn, xn):bn);
case SECTION:
if(xn.length() > 0) return (new ItemMatcher(this.thisItem.get(bn))).matches(xn);
if (xn.length() > 0) return (new ItemMatcher(this.thisItem.get(bn))).matches(xn);
return this.sectionMatches(bn);
default:
return false;
Expand All @@ -56,22 +56,22 @@ private boolean sectionMatches(final SectionType sec, final String name) {
}

private boolean sectionMatches(final String name) {
return this.sectionMatches((SectionType)this.thisItem, name);
return this.sectionMatches((SectionType) this.thisItem, name);
}

private boolean matchOperator(final OperationType op, final String itemName, final String valueName) {
String matchName = itemName;
if(op.getName().equalsIgnoreCase(itemName) && valueName.length() > 0) {
if (op.getName().equalsIgnoreCase(itemName) && valueName.length() > 0) {
matchName = valueName;
}

if(op.getOperator() == '!') return !op.getValue().equalsIgnoreCase(matchName);
else if(op.getOperator() == '~') return op.getValue().equalsIgnoreCase(matchName);
if (op.getOperator() == '!') return !op.getValue().equalsIgnoreCase(matchName);
else if (op.getOperator() == '~') return op.getValue().equalsIgnoreCase(matchName);
return true;
}

private boolean operatorMatches(final OperationType op, final String name) {
if(name.indexOf('.') > 0) {
if (name.indexOf('.') > 0) {
final String in = name.substring(0, name.indexOf('.'));
final String vn = name.substring(name.indexOf('.')+1);
return this.matchOperator(op, in, vn);
Expand All @@ -81,7 +81,7 @@ private boolean operatorMatches(final OperationType op, final String name) {
}

private boolean operatorMatches(final String name) {
return this.operatorMatches((OperationType)this.thisItem, name);
return this.operatorMatches((OperationType) this.thisItem, name);
}

private boolean identMatches(final IdentifierType ident, final String name) {
Expand All @@ -93,6 +93,6 @@ private boolean listMatchesAny(final ListType theList, final String name) {
}

private boolean listMatchesAny(final String name) {
return this.listMatchesAny((ListType)this.thisItem,name);
return this.listMatchesAny((ListType) this.thisItem,name);
}
}
11 changes: 4 additions & 7 deletions src/main/java/com/keildraco/config/factory/TypeFactory.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/**
*
*/
package com.keildraco.config.factory;

import java.io.StreamTokenizer;
Expand All @@ -22,7 +19,7 @@ public class TypeFactory {
private final Map<String, IParserState> parserMap;

/**
* Private default constructor
* Private default constructor.
*/
public TypeFactory() {
this.typeMap = new ConcurrentHashMap<>();
Expand All @@ -44,14 +41,14 @@ public void registerParser(final IParserState parser, final String name) {
@Nullable
public IStateParser getParser(final String parserName, @Nullable final ParserInternalTypeBase parent) {
final IParserState parser = this.parserMap.getOrDefault(parserName, null);
if(parser == null) return null;
if (parser == null) return null;

return parser.get();
}

public ParserInternalTypeBase parseTokens(String parserName, @Nullable final ParserInternalTypeBase parent, final StreamTokenizer tok, final String itemName) {
public ParserInternalTypeBase parseTokens(final String parserName, @Nullable final ParserInternalTypeBase parent, final StreamTokenizer tok, final String itemName) {
final IStateParser parser = this.getParser(parserName, parent);
if(parser==null) return ParserInternalTypeBase.EmptyType;
if (parser==null) return ParserInternalTypeBase.EmptyType;

parser.clearErrors();
parser.setName(itemName);
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/com/keildraco/config/states/IStateParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.io.StreamTokenizer;

import static java.io.StreamTokenizer.*;

import com.keildraco.config.Config;
Expand All @@ -12,10 +13,11 @@ public interface IStateParser {
public static final String IDENTIFIER_PATTERN = "^\\s*[a-zA-Z_]{1}[a-zA-Z0-9_]*\\s*$";

public void setFactory(TypeFactory factory);

public TypeFactory getFactory();

default public String ttypeToString(final int ttype) {
switch(ttype) {
switch (ttype) {
case TT_WORD:
return "TT_WORD";
case TT_NUMBER:
Expand All @@ -30,6 +32,7 @@ default public String ttypeToString(final int ttype) {
}

public void setErrored();

public boolean errored();

default public int peekToken(final StreamTokenizer tok) {
Expand Down Expand Up @@ -57,9 +60,13 @@ default public int nextToken(final StreamTokenizer tok) {
}

public ParserInternalTypeBase getState(final StreamTokenizer tok);

public void setParent(final ParserInternalTypeBase parent);

public ParserInternalTypeBase getParent();

default public void setName(final String name) { /* this space intentionally blank */ }
public String getName();

public void clearErrors();
}
8 changes: 4 additions & 4 deletions src/main/java/com/keildraco/config/states/KeyValueParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public boolean errored() {
public ParserInternalTypeBase getState(final StreamTokenizer tok) {
final int p = this.nextToken(tok);

if(!this.errored() && p == TT_WORD && tok.sval.matches(IDENTIFIER_PATTERN)) {
if (!this.errored() && p == TT_WORD && tok.sval.matches(IDENTIFIER_PATTERN)) {
final String temp = tok.sval;
if(this.peekToken(tok) == '(') return this.factory.parseTokens("OPERATION", null, tok, temp);
if (this.peekToken(tok) == '(') return this.factory.parseTokens("OPERATION", null, tok, temp);
else return this.factory.getType(this.getParent(), this.name, temp, ItemType.IDENTIFIER);
} else if(!errored() && p != TT_WORD) {
switch(p) {
} else if (!errored() && p != TT_WORD) {
switch (p) {
case StreamTokenizer.TT_EOF:
Config.LOGGER.error("Premature End of File while parsing a key-value pair, line %d", tok.lineno());
this.setErrored();
Expand Down
16 changes: 7 additions & 9 deletions src/main/java/com/keildraco/config/states/ListParser.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/**
*
*/
package com.keildraco.config.states;

import java.io.StreamTokenizer;
Expand All @@ -13,6 +10,7 @@
import com.keildraco.config.Config;
import com.keildraco.config.factory.TypeFactory;
import com.keildraco.config.types.*;

import static com.keildraco.config.types.ParserInternalTypeBase.ItemType;
import static com.keildraco.config.types.ParserInternalTypeBase.EmptyType;

Expand Down Expand Up @@ -61,14 +59,14 @@ public ParserInternalTypeBase getState(final StreamTokenizer tok) {
int p;
final Deque<ParserInternalTypeBase> store = new LinkedList<>();
String ident;
while((p = this.nextToken(tok)) != StreamTokenizer.TT_EOF && p != ']') {
if(p=='[') continue;
if(!this.errored && p == StreamTokenizer.TT_WORD && tok.sval.matches(IDENTIFIER_PATTERN)) {
while ((p = this.nextToken(tok)) != StreamTokenizer.TT_EOF && p != ']') {
if (p=='[') continue;
if (!this.errored && p == StreamTokenizer.TT_WORD && tok.sval.matches(IDENTIFIER_PATTERN)) {
ident = tok.sval;
final ParserInternalTypeBase temp = this.getToken(tok, ident);
temp.setName(ident);
store.push(temp);
} else if(p == StreamTokenizer.TT_WORD) {
} else if (p == StreamTokenizer.TT_WORD) {
Config.LOGGER.fatal("Error loading list, did not find TT_WORD matching %s where expected (%s found)", IDENTIFIER_PATTERN, tok.sval);
return EmptyType;
}
Expand All @@ -81,9 +79,9 @@ public ParserInternalTypeBase getState(final StreamTokenizer tok) {

private ParserInternalTypeBase getToken(final StreamTokenizer tok, final String ident) {
final int n = this.peekToken(tok);
if(n != StreamTokenizer.TT_WORD && (n == ',' || n == ']')) {
if (n != StreamTokenizer.TT_WORD && (n == ',' || n == ']')) {
return this.factory.getType(this.getParent(), this.getName(), ident, ItemType.IDENTIFIER);
} else if( n == '(') {
} else if (n == '(') {
return this.factory.parseTokens("OPERATION", this.getParent(), tok, ident);
}
this.setErrored();
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/keildraco/config/states/OperationParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ public boolean errored() {
public ParserInternalTypeBase getState(final StreamTokenizer tok) {
try {
tok.nextToken();
if(tok.ttype == '(') tok.nextToken();
if (tok.ttype == '(') tok.nextToken();

if(tok.ttype != StreamTokenizer.TT_EOF) {
if (tok.ttype != StreamTokenizer.TT_EOF) {
final String operator = this.getOperator(tok);
final String value = this.getIdentifier(tok);
final int p = peekToken(tok);
if(p == ')') {
final OperationType rv = (OperationType)this.factory.getType(this.getParent(), this.name, value, ItemType.OPERATION);
if (p == ')') {
final OperationType rv = (OperationType) this.factory.getType(this.getParent(), this.name, value, ItemType.OPERATION);
rv.setName(this.name);
rv.setOperation(operator);
return rv;
Expand All @@ -77,12 +77,12 @@ public ParserInternalTypeBase getState(final StreamTokenizer tok) {

private String getIdentifier(final StreamTokenizer tok) {
this.nextToken(tok);
if(tok.ttype == StreamTokenizer.TT_WORD && tok.sval.matches(IDENTIFIER_PATTERN)) return tok.sval;
if (tok.ttype == StreamTokenizer.TT_WORD && tok.sval.matches(IDENTIFIER_PATTERN)) return tok.sval;
throw new IllegalArgumentException("IDENTIFIER not available in token stream");
}

private String getOperator(final StreamTokenizer tok) {
if(tok.ttype=='~'||tok.ttype=='!') return String.format("%c", tok.ttype);
if (tok.ttype=='~'||tok.ttype=='!') return String.format("%c", tok.ttype);
throw new IllegalArgumentException("OPERATOR not available in token stream");
}

Expand Down
19 changes: 10 additions & 9 deletions src/main/java/com/keildraco/config/states/SectionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.keildraco.config.factory.TypeFactory;
import com.keildraco.config.types.ParserInternalTypeBase;
import com.keildraco.config.types.SectionType;

import static com.keildraco.config.types.ParserInternalTypeBase.EmptyType;

import static java.io.StreamTokenizer.*;
Expand Down Expand Up @@ -50,12 +51,12 @@ public boolean errored() {
@Override
public ParserInternalTypeBase getState(final StreamTokenizer tok) {
String ident = "";
while( this.nextToken(tok) != TT_EOF && !this.errored()) {
while (this.nextToken(tok) != TT_EOF && !this.errored()) {
int tt = getTokenType(tok);

switch(tt) {
switch (tt) {
case '=':
if(ident.equals("")) {
if (ident.equals("")) {
this.setErrored();
Config.LOGGER.error("Found a store operation (=) where I was expecting an identifier");
return EmptyType;
Expand All @@ -80,13 +81,13 @@ public ParserInternalTypeBase getState(final StreamTokenizer tok) {
return EmptyType;
}
}
if(!this.errored()) return this.section;
if (!this.errored()) return this.section;
return EmptyType;
}

private void getSection(final StreamTokenizer tok, final String ident) {
final ParserInternalTypeBase sk = this.factory.parseTokens("SECTION", this.section, tok, ident);
if(EmptyType.equals(sk)) {
if (EmptyType.equals(sk)) {
this.setErrored();
} else {
this.section.addItem(sk);
Expand All @@ -95,21 +96,21 @@ private void getSection(final StreamTokenizer tok, final String ident) {

private void getKeyValue(final StreamTokenizer tok, final String ident) {
final ParserInternalTypeBase kv = this.factory.parseTokens("KEYVALUE", this.section, tok, ident);
if(EmptyType.equals(kv)) {
if (EmptyType.equals(kv)) {
this.setErrored();
} else {
this.section.addItem(kv);
}
}

private String itToString(final int tt) {
if(tt == -1) return "an Identifier";
if (tt == -1) return "an Identifier";
return String.format("'%c'", tt);
}

private static int getTokenType(final StreamTokenizer tok) {
if(tok.ttype == TT_WORD) {
if(tok.sval.matches(IDENTIFIER_PATTERN)) return -1;
if (tok.ttype == TT_WORD) {
if (tok.sval.matches(IDENTIFIER_PATTERN)) return -1;
return -4;
} else {
return tok.ttype;
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/com/keildraco/config/states/package-info.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/**
*
*/
/**
* @author Daniel Hazelton
*
*/
package com.keildraco.config.states;
package com.keildraco.config.states;
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public ParserInternalTypeBase get(final String s) {

@Override
public String asString() {
if(this.getName().equals("")) return this.ident;
if (this.getName().equals("")) return this.ident;
return String.format("%s = %s", this.getName(), this.ident);
}

Expand Down
Loading