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

[#1764]feature: add advanced urlcheck #1157

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

flybyray
Copy link
Contributor

replacement for #706

@asolntsev asolntsev self-assigned this Jun 1, 2017
@asolntsev asolntsev requested a review from xael-fry June 1, 2017 21:26
@asolntsev asolntsev added this to the 1.5.0 milestone Jun 1, 2017
@asolntsev asolntsev self-requested a review June 1, 2017 21:27

@Override
public void configure(URL url) {
setMessage(url.message());
this.tldMandatory = url.tldMandatory();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to store 2 fields? Can't we just store instance of URL?
Like

public void configure(URL url) {
      this.url = url;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not know if that is a good way. Maybe ask the author of OVal why he not just copies a reference of the annotations in all of his checks.
https://github.com/sebthom/oval/tree/1.32/src/main/java/net/sf/oval/constraint

}

@Override
public boolean isSatisfied(Object validatedObject, Object value, OValContext context, Validator validator) {
if (value == null || value.toString().length() == 0) {
return true;
}
return urlPattern.matcher(value.toString()).matches();
if (!tldMandatory || !excludeLoopback) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to change order of if/else. It would make code easier to read.

if (tldMandatory && excludeLoopback) {
  return urlPattern.matcher(value.toString()).matches();
}
// ... and here come all the other magic...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes maybe nicer but that is cosmetic and for me i like the most frequent return at the end of the method.

if (!tldMandatory || !excludeLoopback) {
//slow for special cases
String[] localRegexFragments = new String[regexFragments.length];
System.arraycopy(regexFragments, 0, localRegexFragments, 0, regexFragments.length);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating every time a new array seems to be too resource-consuming. And the code is not really simple.

Can't we just hold 4 regexps in 4 constants? I mean

Pattern localUrlPattern_false_false = Pattern.compile(...);
Pattern localUrlPattern_false_true = Pattern.compile(...);
Pattern localUrlPattern_true_false = Pattern.compile(...);
Pattern localUrlPattern_true_true = Pattern.compile(...);

Copy link
Contributor Author

@flybyray flybyray Jun 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the if part is only the special case, it will not be called every time

see URL annotation.
tldMandatory default true
excludeLoopback default true

And yes you can do x-regexps. but maybe someone will add someday additional special cases and we need more patterns, then i will ask a similar question Do we really need to store x-fields? for all those patterns?
But maybe it is better to use the configure method to initiate only once the correct pattern and just use it in satisfy.

@flybyray
Copy link
Contributor Author

flybyray commented Jun 2, 2017

@asolntsev
Copy link
Contributor

asolntsev commented Jun 2, 2017

@flybyray I added a comment to the gist:

May be this could be a shorter solution:

private static String[] origRegex = { ... };
private static String[][] regexFragments = {
  origRegex,
  replace(origRegex, 4, "(?!(?:10)(?:\\.\\d{1,3}){3})"),
  replace(origRegex, 13, ""),
  replace(origRegex, 13, "")
};

P.S. I agree, all my comments are quite minor (cosmetic).

@flybyray
Copy link
Contributor Author

flybyray commented Jun 2, 2017

would need something like this. but is this nicer to read?

    private static String[] origRegex = { ... };
    private static IntFunction[] intf = {
            x -> (x == 4) ? "(?!(?:10)(?:\\.\\d{1,3}){3})" : null,
            x -> (x == 13) ? "" : null
    };
    private static String[][] regexFragments = {
            origRegex,
            replace(origRegex.clone(), intf[0]),
            replace(origRegex.clone(), intf[1]),
            replace(replace(origRegex.clone(), intf[0]), intf[1])};
    private static String[] replace(String[] array, IntFunction<? extends String> generator) {
        for (int i = 0; i < array.length; i++) {
            final String object = generator.apply(i);
            if (object != null) array[i] = object;
        }
        return array;
    }

@xael-fry xael-fry removed this from the 1.5.0 milestone Sep 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants