-
Notifications
You must be signed in to change notification settings - Fork 99
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
Validate if the statement ID is of the right format #500
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,6 +74,36 @@ public class StatementImpl implements Statement { | |
|
||
private List<SnakGroup> qualifiersGroups; | ||
|
||
/** | ||
* Checks if the statement ID consists of a first part | ||
* with the entity id of the item the statement belongs to, the separator $, plus | ||
* a random hash of the form | ||
* /^\{?[A-Z\d]{8}-[A-Z\d]{4}-[A-Z\d]{4}-[A-Z\d]{4}-[A-Z\d]{12}\}?\z/ | ||
* | ||
* @param statementID | ||
* @return true if the statement ID is of the valid format | ||
*/ | ||
public static boolean validateStatementID(String statementID) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please put curly brace in the same line like all the code base. |
||
if(statementID.equals("")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please always use curly braces with if to avoid ambiguities. |
||
return true; | ||
|
||
String separator = "\\$"; | ||
String statementFormat ="^\\{?[A-Za-z0-9]{8}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{12}\\}?$"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We will validate millions of IDs. I belive it would be better to precompile the pattern using |
||
|
||
String[] statementParts = statementID.split(separator); | ||
if(statementParts.length != 2) | ||
throw new IllegalArgumentException("Statement ID does not have the correct number of parts"); | ||
|
||
if(statementParts[0].charAt(0) != 'Q' && statementParts[0].charAt(0) != 'P' && statementParts[0].charAt(0) != 'L' && statementParts[0].charAt(0) != 'M') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are actually more types (forms, senses, media-info...). You could use |
||
throw new TypeNotPresentException("Query,Property or Lexeme", null); | ||
|
||
if(!statementParts[1].matches(statementFormat)) | ||
throw new IllegalStateException("Statement part does not have a correct format"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The indentation of this line is inconsistent with the rest. Also, I do not think |
||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Constructor. | ||
* <p> | ||
|
@@ -102,6 +132,7 @@ public StatementImpl( | |
List<Reference> references, | ||
EntityIdValue subjectId) { | ||
this.statementId = (statementId == null) ? "" : statementId; | ||
boolean check = validateStatementID(this.statementId); | ||
|
||
Validate.notNull(rank, "No rank provided to create a statement."); | ||
this.rank = rank; | ||
|
@@ -131,6 +162,7 @@ public StatementImpl( | |
List<Reference> references, | ||
EntityIdValue subjectId) { | ||
this.statementId = (statementId == null) ? "" : statementId; | ||
boolean check = validateStatementID(statementId); | ||
Validate.notNull(rank, "No rank provided to create a statement."); | ||
this.rank = rank; | ||
Validate.notNull(mainSnak, "No main snak provided to create a statement."); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you raise an exception if the ID is not valid, then returning a boolean is not useful. If the function returns, then you aready know the ID is valid.