com.google.re2j.Pattern

Here are the examples of the java api com.google.re2j.Pattern taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

67 Examples 7

19 Source : NcepMnemonic.java
with BSD 3-Clause "New" or "Revised" License
from Unidata

/**
 * A clreplaced that reads a mnemonic table. It doesn't include X < 48 and Y < 192 type of
 * descriptors because they are already stored in the latest WMO tables.
 */
public clreplaced NcepMnemonic {

    // | HEADR | 362001 | TABLE D ENTRY - PROFILE COORDINATES | |
    private static final Pattern fields3 = Pattern.compile("^\\|\\s+(.*)\\s+\\|\\s+(.*)\\s+\\|\\s+(.*)\\s*\\|");

    private static final Pattern fields2 = Pattern.compile("^\\|\\s+(.*)\\s+\\|\\s+(.*)\\s+\\|");

    private static final Pattern fields5 = Pattern.compile("^\\|\\s+(.*)\\s+\\|\\s+(.*)\\s+\\|\\s+(.*)\\s+\\|\\s+(.*)\\s+\\|\\s+(.*)\\s+\\|");

    /**
     * Pattern to get 3 integers from beginning of line.
     */
    private static final Pattern ints6 = Pattern.compile("^\\d{6}");

    private static final int XlocalCutoff = 48;

    private static final int YlocalCutoff = 192;

    private static final boolean debugTable = false;

    /**
     * Read NCEP mnemonic BUFR tables.
     *
     * @return true on success.
     */
    public static boolean read(InputStream ios, BufrTables.Tables tables) throws IOException {
        if (ios == null)
            return false;
        if (tables.b == null)
            tables.b = new TableB("fake", "fake");
        if (tables.d == null)
            tables.d = new TableD("fake", "fake");
        // key = mnemonic value = fxy
        HashMap<String, String> number = new HashMap<>();
        // key = mnemonic value = description
        HashMap<String, String> desc = new HashMap<>();
        HashMap<String, String> mnseq = new HashMap<>();
        try {
            BufferedReader dataIS = new BufferedReader(new InputStreamReader(ios, StandardCharsets.UTF_8));
            // read mnemonic table
            Matcher m;
            // read header info and disregard
            while (true) {
                String line = dataIS.readLine();
                if (line == null)
                    throw new RuntimeException("Bad NCEP mnemonic BUFR table ");
                if (line.contains("MNEMONIC"))
                    break;
            }
            // read mnemonic, number, and description
            // | HEADR | 362001 | TABLE D ENTRY - PROFILE COORDINATES |
            while (true) {
                String line = dataIS.readLine();
                if (line == null)
                    break;
                if (line.contains("MNEMONIC"))
                    break;
                if (line.contains("----"))
                    continue;
                if (line.startsWith("*"))
                    continue;
                if (line.startsWith("|       "))
                    continue;
                m = fields3.matcher(line);
                if (m.find()) {
                    String mnu = m.group(1).trim();
                    String fxy = m.group(2).trim();
                    if (fxy.startsWith("3")) {
                        number.put(mnu, fxy);
                        desc.put(mnu, m.group(3).replace("TABLE D ENTRY - ", "").trim());
                    } else if (fxy.startsWith("0")) {
                        number.put(mnu, fxy);
                        desc.put(mnu, m.group(3).replace("TABLE B ENTRY - ", "").trim());
                    } else if (fxy.startsWith("A")) {
                        number.put(mnu, fxy);
                        desc.put(mnu, m.group(3).replace("TABLE A ENTRY - ", "").trim());
                    }
                } else if (debugTable) {
                    System.out.println("bad mnemonic, number, and description: " + line);
                }
            }
            // read in sequences using mnemonics
            // | ETACLS1 | HEADR {PROFILE} SURF FLUX HYDR D10M {SLYR} XTRA |
            while (true) {
                String line = dataIS.readLine();
                if (line == null)
                    break;
                if (line.contains("MNEMONIC"))
                    break;
                if (line.contains("----"))
                    continue;
                if (line.startsWith("|       "))
                    continue;
                if (line.startsWith("*"))
                    continue;
                m = fields2.matcher(line);
                if (m.find()) {
                    String mnu = m.group(1).trim();
                    if (mnseq.containsKey(mnu)) {
                        // concat lines with same mnu
                        String value = mnseq.get(mnu);
                        value = value + " " + m.group(2);
                        mnseq.put(mnu, value);
                    } else {
                        mnseq.put(mnu, m.group(2));
                    }
                } else if (debugTable) {
                    System.out.println("bad sequence mnemonic: " + line);
                }
            }
            // create sequences, replacing mnemonics with numbers
            for (Map.Entry<String, String> ent : mnseq.entrySet()) {
                String seq = ent.getValue();
                seq = seq.replaceAll("\\<", "1-1-0 0-31-0 ");
                seq = seq.replaceAll("\\>", "");
                seq = seq.replaceAll("\\{", "1-1-0 0-31-1 ");
                seq = seq.replaceAll("\\}", "");
                seq = seq.replaceAll("\\(", "1-1-0 0-31-2 ");
                seq = seq.replaceAll("\\)", "");
                StringTokenizer stoke = new StringTokenizer(seq, " ");
                List<Short> list = new ArrayList<>();
                while (stoke.hasMoreTokens()) {
                    String mn = stoke.nextToken();
                    if (mn.charAt(1) == '-') {
                        list.add(Descriptor.getFxy(mn));
                        continue;
                    }
                    // element descriptor needs hyphens
                    m = ints6.matcher(mn);
                    if (m.find()) {
                        String F = mn.substring(0, 1);
                        String X = removeLeading0(mn.substring(1, 3));
                        String Y = removeLeading0(mn.substring(3));
                        list.add(Descriptor.getFxy(F + "-" + X + "-" + Y));
                        continue;
                    }
                    if (mn.startsWith("\"")) {
                        int idx = mn.lastIndexOf('"');
                        String count = mn.substring(idx + 1);
                        list.add(Descriptor.getFxy("1-1-" + count));
                        mn = mn.substring(1, idx);
                    }
                    if (mn.startsWith(".")) {
                        String des = mn.substring(mn.length() - 4);
                        mn = mn.replace(des, "....");
                    }
                    String fxy = number.get(mn);
                    String F = fxy.substring(0, 1);
                    String X = removeLeading0(fxy.substring(1, 3));
                    String Y = removeLeading0(fxy.substring(3));
                    list.add(Descriptor.getFxy(F + "-" + X + "-" + Y));
                }
                String fxy = number.get(ent.getKey());
                String X = removeLeading0(fxy.substring(1, 3));
                String Y = removeLeading0(fxy.substring(3));
                // these are in latest tables
                if (XlocalCutoff > Integer.parseInt(X) && YlocalCutoff > Integer.parseInt(Y))
                    continue;
                // key = F + "-" + X + "-" + Y;
                short seqX = Short.parseShort(X.trim());
                short seqY = Short.parseShort(Y.trim());
                tables.d.addDescriptor(seqX, seqY, ent.getKey(), list);
            // short id = Descriptor.getFxy(key);
            // sequences.put(Short.valueOf(id), tableD);
            }
            // add some static repereplacedion sequences
            // LOOK why?
            List<Short> list = new ArrayList<>();
            // 16 bit delayed repereplacedion
            list.add(Descriptor.getFxy("1-1-0"));
            list.add(Descriptor.getFxy("0-31-2"));
            tables.d.addDescriptor((short) 60, (short) 1, "", list);
            // tableD = new DescriptorTableD("", "3-60-1", list, false);
            // tableD.put( "3-60-1", d);
            // short id = Descriptor.getFxy("3-60-1");
            // sequences.put(Short.valueOf(id), tableD);
            list = new ArrayList<>();
            // 8 bit delayed repereplacedion
            list.add(Descriptor.getFxy("1-1-0"));
            list.add(Descriptor.getFxy("0-31-1"));
            tables.d.addDescriptor((short) 60, (short) 2, "", list);
            // tableD = new DescriptorTableD("", "3-60-2", list, false);
            // tableD.put( "3-60-2", d);
            // id = Descriptor.getFxy("3-60-2");
            // sequences.put(Short.valueOf(id), tableD);
            list = new ArrayList<>();
            // 8 bit delayed repereplacedion
            list.add(Descriptor.getFxy("1-1-0"));
            list.add(Descriptor.getFxy("0-31-1"));
            tables.d.addDescriptor((short) 60, (short) 3, "", list);
            // tableD = new DescriptorTableD("", "3-60-3", list, false);
            // tableD.put( "3-60-3", d);
            // id = Descriptor.getFxy("3-60-3");
            // sequences.put(Short.valueOf(id), tableD);
            list = new ArrayList<>();
            // 1 bit delayed repereplacedion
            list.add(Descriptor.getFxy("1-1-0"));
            list.add(Descriptor.getFxy("0-31-0"));
            tables.d.addDescriptor((short) 60, (short) 4, "", list);
            // tableD = new DescriptorTableD("", "3-60-4", list, false);
            // tableD.put( "3-60-4", d);
            // id = Descriptor.getFxy("3-60-4");
            // sequences.put(Short.valueOf(id), tableD);
            // add in element descriptors
            // MNEMONIC | SCAL | REFERENCE | BIT | UNITS
            // | FTIM | 0 | 0 | 24 | SECONDS |-------------|
            // tableB = new TableB(tablename, tablename);
            while (true) {
                String line = dataIS.readLine();
                if (line == null)
                    break;
                if (line.contains("MNEMONIC"))
                    break;
                if (line.startsWith("|       "))
                    continue;
                if (line.startsWith("*"))
                    continue;
                m = fields5.matcher(line);
                if (m.find()) {
                    if (m.group(1).equals("")) {
                    // do nothing
                    } else if (number.containsKey(m.group(1).trim())) {
                        // add descriptor to tableB
                        String fxy = number.get(m.group(1).trim());
                        String X = fxy.substring(1, 3);
                        String Y = fxy.substring(3);
                        String mnu = m.group(1).trim();
                        String descr = desc.get(mnu);
                        short x = Short.parseShort(X.trim());
                        short y = Short.parseShort(Y.trim());
                        // these are in latest tables so skip LOOK WHY
                        if (XlocalCutoff > x && YlocalCutoff > y)
                            continue;
                        int scale = Integer.parseInt(m.group(2).trim());
                        int refVal = Integer.parseInt(m.group(3).trim());
                        int width = Integer.parseInt(m.group(4).trim());
                        String units = m.group(5).trim();
                        tables.b.addDescriptor(x, y, scale, refVal, width, mnu, units, descr);
                    } else if (debugTable) {
                        System.out.println("bad element descriptors: " + line);
                    }
                }
            }
        } finally {
            ios.close();
        }
        // LOOK why ?
        // default for NCEP
        // 0; 63; 0; 0; 0; 16; Numeric; Byte count
        tables.b.addDescriptor((short) 63, (short) 0, 0, 0, 16, "Byte count", "Numeric", null);
        return true;
    }

    private static String removeLeading0(String number) {
        if (number.length() == 2 && number.startsWith("0")) {
            number = number.substring(1);
        } else if (number.length() == 3 && number.startsWith("00")) {
            number = number.substring(2);
        } else if (number.length() == 3 && number.startsWith("0")) {
            number = number.substring(1);
        }
        return number;
    }
}

19 Source : Glob.java
with Apache License 2.0
from google

/**
 * A {@link PathMatcher} builder that creates a PathMatcher relative to a {@link Path}.
 *
 * <p>The returned {@link PathMatcher} returns true if any of the {@code paths} expressions match.
 * If {@code paths} is empty it will no match any file.
 */
@StarlarkBuiltin(name = "glob", doc = "Glob returns a list of every file in the workdir that matches at least one" + " pattern in include and does not match any of the patterns in exclude.", doreplacedented = false)
public abstract clreplaced Glob implements StarlarkValue, HasBinary {

    private static final Pattern UNESCAPE = Pattern.compile("\\\\(.)");

    public static final Glob ALL_FILES = createGlob(ImmutableList.of("**"));

    @Override
    public final UnionGlob binaryOp(TokenKind op, Object that, boolean thisLeft) throws EvalException {
        if (op == TokenKind.PLUS && that instanceof Glob) {
            return new UnionGlob(this, (Glob) that);
        }
        throw Starlark.errorf("Cannot concatenate %s with %s. Only a glob can be concatenated to a glob", this, that);
    }

    /**
     * Checks if the given {@code changedFiles} are or are descendants of the {@code roots}.
     */
    public static boolean affectsRoots(ImmutableSet<String> roots, ImmutableCollection<String> changedFiles) {
        if (changedFiles == null || isEmptyRoot(roots)) {
            return true;
        }
        // This is O(changes * files * roots) in the worse case. roots shouldn't be big and
        // files shouldn't be big for 99% of the changes.
        for (String file : changedFiles) {
            for (String root : roots) {
                if (file.equals(root) || file.startsWith(root + "/")) {
                    return true;
                }
            }
        }
        return false;
    }

    public abstract PathMatcher relativeTo(Path path);

    /**
     * Creates a function {@link Glob} that when a {@link Path} is preplaceded it returns a
     * {@link PathMatcher} relative to the path.
     *
     * @param include list of strings representing the globs to include/match
     * @param exclude list of strings representing the globs to exclude from the include set
     * @throws IllegalArgumentException if any glob is not valid
     */
    public static Glob createGlob(Iterable<String> include, Iterable<String> exclude) {
        return new SimpleGlob(ImmutableList.copyOf(include), Iterables.isEmpty(exclude) ? null : createGlob(exclude));
    }

    /**
     * Creates a function {@link Glob} that when a {@link Path} is preplaceded it returns a
     * {@link PathMatcher} relative to the path.
     *
     * @param include list of strings representing the globs to include/match
     * @throws IllegalArgumentException if any glob is not valid
     */
    public static Glob createGlob(Iterable<String> include) {
        return new SimpleGlob(include, null);
    }

    /**
     * Calculates a set of paths which recursively contain all files that could possibly match a file
     * in this glob. Generally, this returns the include paths but removing all segments that have a
     * metacharacter and following segments.
     *
     * <p>This can be used by an {@code Origin} or {@code Destination} implementation to determine
     * which directories to query from the repo. For instance, if the <em>include</em> paths are:
     *
     * <ul>
     * <li>foo/bar.jar
     * <li>foo/baz/**
     * </ul>
     *
     * This function will return a single string: {@code "foo"}.
     *
     * <p>If the include paths potentially include files in the root directory or use metacharacters
     * to specify the top level directory, a set with only the empty string is returned. For
     * instance, the following include globs will cause {@code [""]} (and no other string) to be
     * returned:
     *
     * <ul>
     * <li>{@code *.java}
     * <li>{@code {foo,bar}/baz/**}
     * <ul>
     *
     * <p>Note that in the case of {@code origin_files} or {@code destination_files}, the origin or
     * destination may give special meaning to the roots of the glob. For instance, the destination
     * may store metadata at {root}/INFO for every root. See the doreplacedentation of the destination or
     * origin you are using for more information.
     */
    public abstract ImmutableSet<String> roots();

    /**
     * If roots is empty or contains a single elemnent that is not a subdirectory. See
     * {@link #roots()} for detail.
     */
    public static boolean isEmptyRoot(Iterable<String> roots) {
        return Iterables.isEmpty(roots) || Objects.equals(roots.iterator().next(), "");
    }

    protected abstract Iterable<String> getIncludes();

    static ImmutableSet<String> computeRootsFromIncludes(Iterable<String> includes) {
        List<String> roots = new ArrayList<>();
        for (String includePath : includes) {
            ArrayDeque<String> components = new ArrayDeque<>();
            for (String component : Splitter.on('/').split(includePath)) {
                components.add(unescape(component));
                if (isMeta(component)) {
                    break;
                }
            }
            components.removeLast();
            if (components.isEmpty()) {
                return ImmutableSet.of("");
            }
            roots.add(Joiner.on('/').join(components));
        }
        // Remove redundant roots - e.g. "foo" covers all paths that start with "foo/"
        Collections.sort(roots);
        int r = 0;
        while (r < roots.size() - 1) {
            if (roots.get(r + 1).startsWith(roots.get(r) + "/")) {
                roots.remove(r + 1);
            } else {
                r++;
            }
        }
        return ImmutableSet.copyOf(roots);
    }

    private static String unescape(String pathComponent) {
        return UNESCAPE.matcher(pathComponent).replaceAll("$1");
    }

    private static boolean isMeta(String pathComponent) {
        int c = 0;
        while (c < pathComponent.length()) {
            switch(pathComponent.charAt(c)) {
                case '*':
                case '{':
                case '[':
                case '?':
                    return true;
                case '\\':
                    c++;
                    break;
                // fall out
                default:
            }
            c++;
        }
        return false;
    }
}

19 Source : LabelTemplate.java
with Apache License 2.0
from google

/**
 * A template system that for texts like "This ${LABEL} is a template"
 * TODO(malcon): Consolidate this clreplaced and Parser/Token.
 */
public clreplaced LabelTemplate {

    // ([\w-]+) is coming from LabelFinder.VALID_LABEL_EXPR. Dues to a a dependency
    // issue we have it here inlined. It is not a big deal as the labels need to exist
    // and also will be refactored into Parser/Token.
    private static final Pattern VAR_PATTERN = Pattern.compile("\\$\\{([\\w-]+)}");

    private final Set<String> labels = new HashSet<>();

    private final String template;

    /**
     * Construct the template object from a String
     * @param template a String in the from of "Foo ${LABEL} ${OTHER} Bar"
     */
    public LabelTemplate(String template) {
        this.template = template;
        Matcher matcher = VAR_PATTERN.matcher(template);
        while (matcher.find()) {
            labels.add(matcher.group(1));
        }
    }

    /**
     * Resolve the template string for a particular set of labels
     */
    public String resolve(Function<String, String> labelFinder) throws LabelNotFoundException {
        Map<String, String> labelValues = new HashMap<>();
        for (String label : labels) {
            String value = labelFinder.apply(label);
            if (value == null) {
                throw new LabelNotFoundException(label);
            }
            labelValues.put(label, value);
        }
        String result = template;
        for (Entry<String, String> entry : labelValues.entrySet()) {
            result = result.replace("${" + entry.getKey() + "}", entry.getValue());
        }
        return result;
    }

    /**
     * Thrown when a label cannot be found in the message
     */
    public clreplaced LabelNotFoundException extends Exception {

        private final String label;

        LabelNotFoundException(String label) {
            super("Cannot find label " + label);
            this.label = label;
        }

        /**
         * Get the label that couldn't be found
         */
        public String getLabel() {
            return label;
        }
    }
}

19 Source : GitHubUtil.java
with Apache License 2.0
from google

/**
 * General utilities for manipulating GitHub urls and data
 */
public clreplaced GitHubUtil {

    private GitHubUtil() {
    }

    /**
     * Returns a valid branch name by replacing invalid character with "_"
     * throw ValidationException when branchName starts with "/" or "refs/"
     */
    public static String getValidBranchName(String branchName) throws ValidationException {
        checkCondition(!branchName.startsWith("/") && !branchName.startsWith("refs/"), "Branch name has invalid prefix: \"/\" or \"refs/\"");
        return branchName.replaceAll("[^A-Za-z0-9/_-]", "_");
    }

    /**
     * The variable name of the list of the required status context names.
     */
    public static final String REQUIRED_STATUS_CONTEXT_NAMES = "required_status_context_names";

    /**
     * The variable name of the list of the required check runs.
     */
    public static final String REQUIRED_CHECK_RUNS = "required_check_runs";

    /**
     * The variable name of the list of the required labels.
     */
    public static final String REQUIRED_LABELS = "required_labels";

    /**
     * The variable name of the list of the retryable labels.
     */
    public static final String RETRYABLE_LABELS = "retryable_labels";

    private static final Pattern GITHUB_PULL_REQUEST_REF = Pattern.compile("refs/pull/([0-9]+)/(head|merge)");

    /**
     * Given a ref like 'refs/pull/12345/head' returns 12345 or null it not a GitHub PR ref
     */
    public static Optional<Integer> maybeParseGithubPrFromHeadRef(String ref) {
        Matcher matcher = GITHUB_PULL_REQUEST_REF.matcher(ref);
        return (matcher.matches() && "head".equals(matcher.group(2))) ? Optional.of(Integer.parseInt(matcher.group(1))) : Optional.empty();
    }

    /**
     * Given a ref like 'refs/pull/12345/merge' returns 12345 or null it not a GitHub PR ref
     */
    public static Optional<Integer> maybeParseGithubPrFromMergeOrHeadRef(String ref) {
        Matcher matcher = GITHUB_PULL_REQUEST_REF.matcher(ref);
        return matcher.matches() ? Optional.of(Integer.parseInt(matcher.group(1))) : Optional.empty();
    }

    /**
     * Given a prNumber return a git reference like 'refs/pull/12345/head'
     */
    public static String asHeadRef(int prNumber) {
        return "refs/pull/" + prNumber + "/head";
    }

    /**
     * Given a prNumber return a git reference like 'refs/pull/12345/merge'
     */
    public static String asMergeRef(int prNumber) {
        return "refs/pull/" + prNumber + "/merge";
    }
}

19 Source : GitHubHost.java
with Apache License 2.0
from google

/**
 * An object that parses GitHub urls in their components (project, name, etc.)
 */
public clreplaced GitHubHost {

    /**
     * Host for http://github.com (Non-Enterprise)
     */
    public static final GitHubHost GITHUB_COM = new GitHubHost("github.com");

    private final Pattern gitHubPrUrlPattern;

    private final String host;

    public GitHubHost(String host) {
        this.host = checkNotNull(host);
        this.gitHubPrUrlPattern = Pattern.compile("https://\\Q" + host + "\\E/(.+)/pull/([0-9]+)");
    }

    /**
     * Return the username part of a github url. For example in https://github.com/foo/bar/baz, 'foo'
     * would be the user.
     */
    public String getUserNameFromUrl(String url) throws ValidationException {
        String project = getProjectNameFromUrl(url);
        int i = project.indexOf("/");
        return i == -1 ? project : project.substring(0, i);
    }

    /**
     * Given a GitHub host name and a url that represents a GitHub repository, return the project
     * name, e.g. org/repo.
     */
    public String getProjectNameFromUrl(String url) throws ValidationException {
        checkCondition(!Strings.isNullOrEmpty(url), "Empty url");
        String gitProtocolPrefix = "git@" + host + ":";
        if (url.startsWith(gitProtocolPrefix)) {
            return url.substring(gitProtocolPrefix.length()).replaceAll("([.]git|/)$", "");
        }
        URI uri;
        try {
            uri = URI.create(url);
        } catch (IllegalArgumentException e) {
            throw new ValidationException("Cannot find project name from url " + url);
        }
        if (uri.getScheme() == null) {
            uri = URI.create("notimportant://" + url);
        }
        checkCondition(host.equals(uri.getHost()), "Not a github url: %s. Expected host: %s", url, host);
        String name = uri.getPath().replaceAll("^/", "").replaceAll("([.]git|/)$", "");
        Matcher firstTwo = Pattern.compile("^([^/]+/[^/]+).*$").matcher(name);
        if (firstTwo.matches()) {
            name = firstTwo.group(1);
        }
        checkCondition(!Strings.isNullOrEmpty(name), "Cannot find project name from url %s", url);
        return name;
    }

    /**
     * Returns true if url is a GitHub url for a given GitHub or Enterprise host.
     */
    public boolean isGitHubUrl(String url) {
        try {
            getProjectNameFromUrl(url);
            return true;
        } catch (ValidationException e) {
            return false;
        }
    }

    public String projectAsUrl(String project) {
        return "https://" + host + "/" + project;
    }

    public String normalizeUrl(String url) throws ValidationException {
        return projectAsUrl(getProjectNameFromUrl(url));
    }

    /**
     * Given a reference, parse it as a Github PR data if it is a url for a PR.
     */
    public Optional<GitHubPrUrl> maybeParseGithubPrUrl(String ref) {
        Matcher matcher = gitHubPrUrlPattern.matcher(ref);
        return matcher.matches() ? Optional.of(new GitHubPrUrl(matcher.group(1), Integer.parseInt(matcher.group(2)))) : Optional.empty();
    }

    /**
     * A GitHub PR project and number
     */
    public static clreplaced GitHubPrUrl {

        private final String project;

        private final int prNumber;

        GitHubPrUrl(String project, int prNumber) {
            this.project = project;
            this.prNumber = prNumber;
        }

        public String getProject() {
            return project;
        }

        public int getPrNumber() {
            return prNumber;
        }

        @Override
        public String toString() {
            return MoreObjects.toStringHelper(this).add("project", project).add("prNumber", prNumber).toString();
        }
    }
}

19 Source : GerritChange.java
with Apache License 2.0
from google

/**
 * A clreplaced that represents a Gerrit change. It contains all the necessary objects to do a
 * fetch when {@code {@link #fetch(ImmutableMultimap)}} is invoked.
 */
clreplaced GerritChange {

    public static final String GERRIT_CHANGE_NUMBER_LABEL = "GERRIT_CHANGE_NUMBER";

    public static final String GERRIT_CHANGE_ID_LABEL = "GERRIT_CHANGE_ID";

    public static final String GERRIT_COMPLETE_CHANGE_ID_LABEL = "GERRIT_COMPLETE_CHANGE_ID";

    // TODO(danielromero): Implement (and refer from gerrit_origin doreplacedentation in GitModule)
    public static final String GERRIT_CHANGE_URL_LABEL = "GERRIT_CHANGE_URL";

    public static final String GERRIT_CHANGE_BRANCH = "GERRIT_CHANGE_BRANCH";

    public static final String GERRIT_CHANGE_TOPIC = "GERRIT_CHANGE_TOPIC";

    public static final String GERRIT_CHANGE_DESCRIPTION_LABEL = "GERRIT_CHANGE_DESCRIPTION";

    public static final String GERRIT_OWNER_EMAIL_LABEL = "GERRIT_OWNER_EMAIL";

    public static final String GERRIT_OWNER_USERNAME_LABEL = "GERRIT_OWNER_USERNAME";

    private static final String GERRIT_PATCH_SET_REF_PREFIX = "PatchSet ";

    private static final Pattern WHOLE_GERRIT_REF = Pattern.compile("refs/changes/[0-9]{2}/([0-9]+)/([0-9]+)");

    private static final Pattern URL_PATTERN = Pattern.compile("https?://.*?/([0-9]+)(?:/([0-9]+))?/?");

    private final GitRepository repository;

    private final GeneralOptions generalOptions;

    private final String repoUrl;

    private final int change;

    private final int patchSet;

    private final String ref;

    private GerritChange(GitRepository repository, GeneralOptions generalOptions, String repoUrl, int change, int patchSet, String ref) {
        this.repository = Preconditions.checkNotNull(repository);
        this.generalOptions = Preconditions.checkNotNull(generalOptions);
        this.repoUrl = repoUrl;
        this.change = change;
        this.patchSet = patchSet;
        this.ref = ref;
    }

    /**
     * Get the change number
     */
    public int getChange() {
        return change;
    }

    /**
     * Gets the specific PatchSet of the Change
     */
    public int getPatchSet() {
        return patchSet;
    }

    /**
     * Context reference for creating GitRevision
     */
    public String getRef() {
        return ref;
    }

    /**
     * Given a local repository, a repo url and a reference, it tries to do its best to resolve the
     * reference to a Gerrit Change.
     *
     * <p>Note that if the PatchSet is not found in the ref, it will go to Gerrit to get the latest
     * PatchSet number.
     *
     * @return a Gerrit change if it can be resolved. Null otherwise.
     */
    @Nullable
    public static GerritChange resolve(GitRepository repository, String repoUrl, String ref, GeneralOptions options) throws RepoException, ValidationException {
        if (Strings.isNullOrEmpty(ref)) {
            return null;
        }
        Matcher refMatcher = WHOLE_GERRIT_REF.matcher(ref);
        if (refMatcher.matches()) {
            return new GerritChange(repository, options, repoUrl, Ints.tryParse(refMatcher.group(1)), Ints.tryParse(refMatcher.group(2)), ref);
        }
        // A change number like '23423'
        if (CharMatcher.javaDigit().matchesAllOf(ref)) {
            return resolveLatestPatchSet(repository, options, repoUrl, Ints.tryParse(ref));
        }
        Matcher urlMatcher = URL_PATTERN.matcher(ref);
        if (!urlMatcher.matches()) {
            return null;
        }
        if (!ref.startsWith(repoUrl)) {
            // replacedume it is our url. We can make this more strict later
            options.console().warn(String.format("replaceduming repository '%s' for looking for review '%s'", repoUrl, ref));
        }
        int change = Ints.tryParse(urlMatcher.group(1));
        Integer patchSet = urlMatcher.group(2) == null ? null : Ints.tryParse(urlMatcher.group(2));
        if (patchSet == null) {
            return resolveLatestPatchSet(repository, options, repoUrl, change);
        }
        Map<Integer, GitRevision> patchSets = getGerritPatchSets(repository, repoUrl, change);
        if (!patchSets.containsKey(patchSet)) {
            throw new CannotResolveRevisionException(String.format("Cannot find patch set %d for change %d in %s. Available Patch sets: %s", patchSet, change, repoUrl, patchSets.keySet()));
        }
        return new GerritChange(repository, options, repoUrl, change, patchSet, patchSets.get(patchSet).contextReference());
    }

    /**
     * Fetch the change from Gerrit
     *
     * @param additionalLabels additional labels to add to the GitRevision labels
     * @return The resolved and fetched SHA-1 of the change.
     */
    GitRevision fetch(ImmutableMultimap<String, String> additionalLabels) throws RepoException, ValidationException {
        String metaRef = String.format("refs/changes/%02d/%d/meta", change % 100, change);
        repository.fetch(repoUrl, /*prune=*/
        true, /*force=*/
        true, ImmutableList.of(ref + ":refs/gerrit/" + ref, metaRef + ":refs/gerrit/" + metaRef), false);
        GitRevision gitRevision = repository.resolveReference("refs/gerrit/" + ref);
        GitRevision metaRevision = repository.resolveReference("refs/gerrit/" + metaRef);
        String changeId = getChangeIdFromMeta(repository, metaRevision, metaRef);
        String changeNumber = Integer.toString(change);
        String changeDescription = getDescriptionFromMeta(repository, metaRevision, metaRef);
        return new GitRevision(repository, gitRevision.getSha1(), gerritPatchSetAsReviewReference(patchSet), changeNumber, ImmutableListMultimap.<String, String>builder().put(GERRIT_CHANGE_NUMBER_LABEL, changeNumber).put(GERRIT_CHANGE_ID_LABEL, changeId).put(GERRIT_CHANGE_DESCRIPTION_LABEL, changeDescription).put(DEFAULT_INTEGRATE_LABEL, new GerritIntegrateLabel(repository, generalOptions, repoUrl, change, patchSet, changeId).toString()).putAll(additionalLabels).putAll(generalOptions.cliLabels().entrySet()).build(), repoUrl);
    }

    private static GerritChange resolveLatestPatchSet(GitRepository repository, GeneralOptions options, String repoUrl, int changeNumber) throws RepoException, ValidationException {
        Entry<Integer, GitRevision> lastPatchset = // Last entry is the latest patchset, since it is ordered by patchsetId.
        getGerritPatchSets(repository, repoUrl, changeNumber).lastEntry();
        return new GerritChange(repository, options, repoUrl, changeNumber, lastPatchset.getKey(), lastPatchset.getValue().contextReference());
    }

    /**
     * Use NoteDB for extracting the Change-id. It should be the first commit in the log
     * of the meta reference.
     *
     * TODO(malcon): Remove usage and use Gerrit API in GerritOrigin
     */
    private String getChangeIdFromMeta(GitRepository repo, GitRevision metaRevision, String metaRef) throws RepoException {
        List<ChangeMessage> changes = getChanges(repo, metaRevision, metaRef);
        String changeId = null;
        for (LabelFinder change : Iterables.getLast(changes).getLabels()) {
            if (change.isLabel() && change.getName().equals("Change-id") && change.getSeparator().equals(": ")) {
                changeId = change.getValue();
            }
        }
        if (changeId == null) {
            throw new RepoException(String.format("Cannot find Change-id in %s. Not present in: \n%s", metaRef, Iterables.getLast(changes).getText()));
        }
        return changeId;
    }

    private String getDescriptionFromMeta(GitRepository repo, GitRevision metaRevision, String metaRef) throws RepoException {
        List<ChangeMessage> changes = getChanges(repo, metaRevision, metaRef);
        return changes.get(0).getText();
    }

    /**
     * Returns the list of {@link ChangeMessage}s. Guarantees that there is at least one change.
     */
    private List<ChangeMessage> getChanges(GitRepository repo, GitRevision metaRevision, String metaRef) throws RepoException {
        List<ChangeMessage> changes = Lists.transform(repo.log(metaRevision.getSha1()).run(), e -> ChangeMessage.parseMessage(e.getBody()));
        if (changes.isEmpty()) {
            throw new RepoException("Cannot find any PatchSet in " + metaRef);
        }
        return changes;
    }

    /**
     * Get all the patchsets for a change ordered by the patchset number. Last is the most recent
     * one.
     */
    static TreeMap<Integer, GitRevision> getGerritPatchSets(GitRepository repository, String url, int changeNumber) throws RepoException, ValidationException {
        TreeMap<Integer, GitRevision> patchSets = new TreeMap<>();
        String basePath = String.format("refs/changes/%02d/%d", changeNumber % 100, changeNumber);
        Map<String, String> refsToSha1 = repository.lsRemote(url, ImmutableList.of(basePath + "/*"));
        if (refsToSha1.isEmpty()) {
            throw new CannotResolveRevisionException(String.format("Cannot find change number %d in '%s'", changeNumber, url));
        }
        for (Entry<String, String> e : refsToSha1.entrySet()) {
            if (e.getKey().endsWith("/meta") || e.getKey().endsWith("/robot-comments")) {
                continue;
            }
            Preconditions.checkState(e.getKey().startsWith(basePath + "/"), String.format("Unexpected response reference %s for %s", e.getKey(), basePath));
            Matcher matcher = WHOLE_GERRIT_REF.matcher(e.getKey());
            Preconditions.checkArgument(matcher.matches(), "Unexpected format for response reference %s for %s", e.getKey(), basePath);
            int patchSet = Ints.tryParse(matcher.group(2));
            patchSets.put(patchSet, new GitRevision(repository, e.getValue(), gerritPatchSetAsReviewReference(patchSet), e.getKey(), ImmutableListMultimap.of(), url));
        }
        return patchSets;
    }

    @VisibleForTesting
    static String gerritPatchSetAsReviewReference(int patchSet) {
        return GERRIT_PATCH_SET_REF_PREFIX + patchSet;
    }
}

19 Source : AuthorParser.java
with Apache License 2.0
from google

/**
 * A parser for the standard author format {@code "Name <email>"}.
 *
 * <p>This is the format used by most VCS (Git, Mercurial) and also by the Copybara configuration
 * itself. The parser is lenient: {@code email} can be empty, and it doesn't validate that is an
 * actual email.
 */
public clreplaced AuthorParser {

    private static final Pattern AUTHOR_PATTERN = Pattern.compile("(?P<name>[^<]+)<(?P<email>[^>]*)>");

    private static final Pattern IN_QUOTES = Pattern.compile("(\".+\")|(\'.+\')");

    /**
     * Parses a Git author {@code string} into an {@link Author}.
     */
    public static Author parse(String author) throws InvalidAutreplacedxception {
        Preconditions.checkNotNull(author);
        if (IN_QUOTES.matcher(author).matches()) {
            // strip quotes
            author = author.substring(1, author.length() - 1);
        }
        Matcher matcher = AUTHOR_PATTERN.matcher(author);
        if (matcher.matches()) {
            return new Author(matcher.group(1).trim(), matcher.group(2).trim());
        }
        throw new InvalidAutreplacedxception(String.format("Invalid author '%s'. Must be in the form of 'Name <email>'", author));
    }
}

19 Source : CommentChecker.java
with MIT License
from Daimler

public clreplaced CommentChecker {

    private String noSecHubLabel;

    private String endNoSecHubLabel;

    private Pattern pattern;

    private CommentChecker(String noSecHubLabel, String endNoSecHubLabel) {
        this.noSecHubLabel = noSecHubLabel;
        this.endNoSecHubLabel = endNoSecHubLabel;
        initialize();
    }

    private void initialize() {
        /*
         * Explanation:
         * 
         * ^(\\+)? -> the string can start with a white spaces
         * 
         * (<!--|--|#|/\\*|//) -> the string has to contain a comment. Different programming languages use different comment styles.
         * 
         * ([-#/*\\s]+)? -> the string can contain further comment symbols or white spaces. ATTENTION: The `-` has to be at the beginning of the group. 
         * 
         * (" + noSecHubLabel + "|" + endNoSecHubLabel + ") -> look for SecHub marker labels.
         * 
         * (\\s|-|$)+ -> accepts a line end as well as any whitespace or `-` minus symbols after the SecHub marker label.
         */
        String regex = "^(\\s+)?(<!--|--|#|/\\*|//|\\(\\*|<#|;|!|%|REM|::|\\*|'|{)([-#/*\\s]+)?(" + noSecHubLabel + "|" + endNoSecHubLabel + ")(\\s|-|$)+";
        pattern = Pattern.compile(regex);
    }

    /**
     * Builds a new comment checker based on the given labels
     *
     * The comment check compiles a regular expression.
     * Make sure you do not call the buildFrom method too many times,
     * otherwise it may cause performance issues.
     *
     * @param noSecHubLabel  the marker start label
     * @param endNoSecHubLabel  the marker end label
     * @return CommentChecker
     */
    public static CommentChecker buildFrom(String noSecHubLabel, String endNoSecHubLabel) {
        return new CommentChecker(noSecHubLabel, endNoSecHubLabel);
    }

    /**
     * Checks if their is a comment in the given line.
     *
     * @param line  a string to check for a label
     * @return true if the string contains a comment otherwise false
     */
    public boolean isCommentInLine(String line) {
        Matcher matcher = pattern.matcher(line);
        return matcher.lookingAt();
    }

    public String getNoSecHubLabel() {
        return noSecHubLabel;
    }
}

19 Source : ProjectClassToRuleResolverTest.java
with Apache License 2.0
from bazelbuild

/**
 * Constructs a ProjectClreplacedToRuleResolver using default workspace path and arguments
 */
private ProjectClreplacedToRuleResolver newResolver(Graph<String> clreplacedGraph, Pattern whiteList, ImmutableMap<String, Path> clreplacedToFile) {
    return new ProjectClreplacedToRuleResolver(ImmutableGraph.copyOf(clreplacedGraph), whiteList, clreplacedToFile, workspace);
}

19 Source : ClassGraphPreprocessor.java
with Apache License 2.0
from bazelbuild

/**
 * Produces a clreplaced graph that only contains top level clreplaced names that either preplaced the white list
 * pattern or have an edge to node that preplacedes the white list. Any clreplaced names that preplaced the black
 * list or do not preplaced our white list criteria are filtered from the graph.
 *
 * <p>In addition, all inner clreplaced names are collapsed into their top level parent clreplaced name.
 */
static ImmutableGraph<String> preProcessClreplacedGraph(ImmutableGraph<String> clreplacedGraph, Pattern whiteList, Pattern blackList) {
    return collapseInnerClreplacedes(trimClreplacedGraph(clreplacedGraph, whiteList, blackList));
}

18 Source : Message.java
with BSD 3-Clause "New" or "Revised" License
from Unidata

/**
 * Encapsolates a complete BUFR message.
 * A message has a DataDescriptor and one or more "datasets" aka "data subsets" aka "observations" aka "obs".
 * Table lookup is done through getLookup().
 */
public clreplaced Message {

    private static final Pattern wmoPattern = Pattern.compile(".*([IJ]..... ....) .*");

    public BufrIndicatorSection is;

    public BufrIdentificationSection ids;

    public BufrDataDescriptionSection dds;

    public BufrDataSection dataSection;

    private RandomAccessFile raf;

    private BufrTableLookup lookup;

    private DataDescriptor root;

    // wmo header
    private String header;

    // starting pos in raf
    private long startPos;

    // raw bytes
    private byte[] raw;

    // bit counting
    // uncompressed: one for each dataset
    BitCounterUncompressed[] counterDatasets;

    int msg_nbits;

    public Message(RandomAccessFile raf, BufrIndicatorSection is, BufrIdentificationSection ids, BufrDataDescriptionSection dds, BufrDataSection dataSection) throws IOException {
        this.raf = raf;
        this.is = is;
        this.ids = ids;
        this.dds = dds;
        this.dataSection = dataSection;
        lookup = BufrTableLookup.factory(this);
    }

    void setTableLookup(TableLookup lookup) {
        this.lookup.setTableLookup(lookup);
    }

    public void close() throws IOException {
        if (raf != null)
            raf.close();
    }

    /**
     * Get number of datasets in this message.
     *
     * @return number of datasets in this message
     */
    public int getNumberDatasets() {
        return dds.getNumberDatasets();
    }

    public CalendarDate getReferenceTime() {
        return ids.getReferenceTime();
    }

    // /////////////////////////////////////////////////////////////////////////
    // the WMO header is in here somewhere when the message comes over the IDD
    public void setHeader(String header) {
        this.header = header;
    }

    public String getHeader() {
        return header;
    }

    // where the message starts in the file
    public void setStartPos(long startPos) {
        this.startPos = startPos;
    }

    public long getStartPos() {
        return startPos;
    }

    public void setRawBytes(byte[] raw) {
        this.raw = raw;
    }

    public byte[] getRawBytes() {
        return raw;
    }

    public String extractWMO() {
        Matcher matcher = wmoPattern.matcher(header);
        if (!matcher.matches()) {
            return "";
        }
        return matcher.group(1);
    }

    /**
     * Get the byte length of the entire BUFR record.
     *
     * @return length in bytes of BUFR record
     */
    public long getMessageSize() {
        return is.getBufrLength();
    }

    /**
     * Get the root of the DataDescriptor tree.
     *
     * @return root DataDescriptor
     */
    public DataDescriptor getRootDataDescriptor() {
        if (root == null)
            root = new DataDescriptorTreeConstructor().factory(lookup, dds);
        return root;
    }

    public boolean usesLocalTable() throws IOException {
        DataDescriptor root = getRootDataDescriptor();
        return usesLocalTable(root);
    }

    private boolean usesLocalTable(DataDescriptor dds) {
        for (DataDescriptor key : dds.getSubKeys()) {
            if (key.isLocal())
                return true;
            if ((key.getSubKeys() != null) && usesLocalTable(key))
                return true;
        }
        return false;
    }

    /**
     * Check if this message contains a BUFR table
     *
     * @return true if message contains a BUFR table
     */
    public boolean containsBufrTable() {
        for (Short key : dds.getDataDescriptors()) {
            if (Descriptor.isBufrTable(key))
                return true;
        }
        return false;
    }

    /**
     * Check if all descriptors were found in the tables.
     *
     * @return true if all dds were found.
     */
    public boolean isTablesComplete() {
        DataDescriptor root = getRootDataDescriptor();
        return !root.isBad;
    }

    public BufrTableLookup getLookup() {
        return lookup;
    }

    // //////////////////////////////////////////////////////////////////////
    // bit counting
    public boolean isBitCountOk() {
        // make sure root is calculated
        getRootDataDescriptor();
        // make sure bits are counted
        getTotalBits();
        // int nbitsGiven = 8 * (dataSection.getDataLength() - 4);
        int nbytesCounted = getCountedDataBytes();
        int nbytesGiven = dataSection.getDataLength();
        // radiosondes dataLen not even number of bytes
        return Math.abs(nbytesCounted - nbytesGiven) <= 1;
    }

    public int getCountedDataBytes() {
        int msg_nbytes = msg_nbits / 8;
        if (msg_nbits % 8 != 0)
            msg_nbytes++;
        msg_nbytes += 4;
        if (msg_nbytes % 2 != 0)
            // LOOK seems to be violated by some messages
            msg_nbytes++;
        return msg_nbytes;
    }

    public int getCountedDataBits() {
        return msg_nbits;
    }

    /**
     * Get the offset of this obs from the start of the message data.
     * Use only for non compressed data
     *
     * @param obsOffsetInMessage index of obs in the message
     * @return offset in bits
     *         <p/>
     *         public int getBitOffset(int obsOffsetInMessage) {
     *         if (dds.isCompressed())
     *         throw new IllegalArgumentException("cant call BufrMessage.getBitOffset() on compressed message");
     *         <p/>
     *         if (!root.isVarLength)
     *         return root.total_nbits * obsOffsetInMessage;
     *         <p/>
     *         getTotalBits(); // make sure its been set
     *         return nestedTableCounter[obsOffsetInMessage].getStartBit();
     *         }
     */
    public BitCounterUncompressed getBitCounterUncompressed(int obsOffsetInMessage) {
        if (dds.isCompressed())
            throw new IllegalArgumentException("cant call BufrMessage.getBitOffset() on compressed message");
        // make sure its been set
        calcTotalBits(null);
        return counterDatasets[obsOffsetInMessage];
    }

    /**
     * This is the total number of bits taken by the data in the data section of the message.
     * This is the counted number.
     *
     * @return total number of bits
     */
    public int getTotalBits() {
        if (msg_nbits == 0)
            calcTotalBits(null);
        return msg_nbits;
    }

    // sets msg_nbits as side-effect
    public int calcTotalBits(Formatter out) {
        try {
            if (!dds.isCompressed()) {
                MessageUncompressedDataReader reader = new MessageUncompressedDataReader();
                reader.readData(null, this, raf, null, false, out);
            } else {
                MessageCompressedDataReader reader = new MessageCompressedDataReader();
                reader.readData(null, this, raf, null, out);
            }
        } catch (IOException ioe) {
            return 0;
        }
        return msg_nbits;
    }

    // /////////////////////////////////////////////////////////////////
    /**
     * Override hashcode to be consistent with equals.
     *
     * @return the hash code of dds.getDescriptors()
     */
    public int hashCode() {
        int result = 17;
        result += 37 * result + getDDShashcode();
        // result += 37 * result + ids.getCenterId();
        // result += 37 * result + ids.getSubCenter_id();
        result += 37 * result + ids.getCategory();
        result += 37 * result + ids.getSubCategory();
        return result;
    }

    public int getDDShashcode() {
        root = getRootDataDescriptor();
        return root.hashCode2();
    }

    /**
     * BufrMessage is equal if they have the same dds.
     *
     * @param obj other BufrMessage
     * @return true if equals
     */
    public boolean equals(Object obj) {
        if (!(obj instanceof Message))
            return false;
        Message o = (Message) obj;
        if (!dds.getDataDescriptors().equals(o.dds.getDataDescriptors()))
            return false;
        if (ids.getCenterId() != o.ids.getCenterId())
            return false;
        // if (ids.getSubCenter_id() != o.ids.getSubCenter_id()) return false;
        if (ids.getCategory() != o.ids.getCategory())
            return false;
        return ids.getSubCategory() == o.ids.getSubCategory();
    }

    // //////////////////////////////////////////////////////////////////
    // perhaps move this into a helper clreplaced - started from ucar.bufr.Dump
    public void showMissingFields(Formatter out) throws IOException {
        lookup.showMissingFields(dds.getDataDescriptors(), out);
    }

    public void dump(Formatter out) {
        // throws IOException {
        int listHash = dds.getDataDescriptors().hashCode();
        out.format(" BUFR edition %d time= %s wmoHeader=%s hash=[0x%x] listHash=[0x%x] (%d) %n", is.getBufrEdition(), getReferenceTime(), getHeader(), hashCode(), listHash, listHash);
        out.format("   Category= %s %n", lookup.getCategoryFullName());
        out.format("   Center= %s %n", lookup.getCenterName());
        out.format("   Table= %s %n", lookup.getTableName());
        out.format("    Table B= wmoTable= %s localTable= %s mode=%s%n", lookup.getWmoTableBName(), lookup.getLocalTableBName(), lookup.getMode());
        out.format("    Table D= wmoTable= %s localTable= %s%n", lookup.getWmoTableDName(), lookup.getLocalTableDName());
        out.format("  DDS nsubsets=%d type=0x%x isObs=%b isCompressed=%b%n", dds.getNumberDatasets(), dds.getDataType(), dds.isObserved(), dds.isCompressed());
        long startPos = is.getStartPos();
        long startData = dataSection.getDataPos();
        out.format("  startPos=%d len=%d endPos=%d dataStart=%d dataLen=%d dataEnd=%d %n", startPos, is.getBufrLength(), (startPos + is.getBufrLength()), startData, dataSection.getDataLength(), startData + dataSection.getDataLength());
        dumpDesc(out, dds.getDataDescriptors(), lookup, 4);
        out.format("%n  CDM Nested Table=%n");
        DataDescriptor root = new DataDescriptorTreeConstructor().factory(lookup, dds);
        dumpKeys(out, root, 4);
    /*
     * int nbits = m.getTotalBits();
     * int nbytes = (nbits % 8 == 0) ? nbits / 8 : nbits / 8 + 1;
     * out.format("  totalBits = %d (%d bytes) outputBytes= %d isVarLen=%s isCompressed=%s\n\n",
     * nbits, nbytes, root.getByteWidthCDM(), root.isVarLength(), m.dds.isCompressed());
     */
    }

    private void dumpDesc(Formatter out, List<Short> desc, BufrTableLookup table, int indent) {
        if (desc == null)
            return;
        for (Short fxy : desc) {
            for (int i = 0; i < indent; i++) out.format(" ");
            Descriptor.show(out, fxy, table);
            out.format("%n");
            int f = (fxy & 0xC000) >> 14;
            if (f == 3) {
                List<Short> sublist = table.getDescriptorListTableD(fxy);
                dumpDesc(out, sublist, table, indent + 2);
            }
        }
    }

    private void dumpKeys(Formatter out, DataDescriptor tree, int indent) {
        for (DataDescriptor key : tree.subKeys) {
            for (int i = 0; i < indent; i++) out.format(" ");
            out.format("%s%n", key);
            if (key.getSubKeys() != null)
                dumpKeys(out, key, indent + 2);
        }
    }

    public void dumpHeader(Formatter out) {
        out.format(" BUFR edition %d time= %s wmoHeader=%s %n", is.getBufrEdition(), getReferenceTime(), getHeader());
        out.format("   Category= %d %s %s %n", lookup.getCategory(), lookup.getCategoryName(), lookup.getCategoryNo());
        out.format("   Center= %s %s %n", lookup.getCenterName(), lookup.getCenterNo());
        out.format("   Table= %d.%d local= %d wmoTables= %s,%s localTables= %s,%s %n", ids.getMasterTableId(), ids.getMasterTableVersion(), ids.getLocalTableVersion(), lookup.getWmoTableBName(), lookup.getWmoTableDName(), lookup.getLocalTableBName(), lookup.getLocalTableDName());
        out.format("  DDS nsubsets=%d type=0x%x isObs=%b isCompressed=%b%n", dds.getNumberDatasets(), dds.getDataType(), dds.isObserved(), dds.isCompressed());
    }

    public void dumpHeaderShort(Formatter out) {
        out.format(" %s, Cat= %s, Center= %s (%s), Table= %d.%d.%d %n", getHeader(), lookup.getCategoryName(), lookup.getCenterName(), lookup.getCenterNo(), ids.getMasterTableId(), ids.getMasterTableVersion(), ids.getLocalTableVersion());
    }
}

18 Source : Re2JRegexp.java
with Apache License 2.0
from trinodb

public final clreplaced Re2JRegexp {

    private static final Logger log = Logger.get(Re2JRegexp.clreplaced);

    private static final java.util.regex.Pattern DOT_STAR_PREFIX_PATTERN = java.util.regex.Pattern.compile("(?s)^(\\.\\*\\??)?(.*)");

    private static final int CORE_PATTERN_INDEX = 2;

    private final int dfaStatesLimit;

    private final int dfaRetries;

    private final Pattern re2jPattern;

    private final Pattern re2jPatternWithoutDotStartPrefix;

    public Re2JRegexp(int dfaStatesLimit, int dfaRetries, Slice pattern) {
        this.dfaStatesLimit = dfaStatesLimit;
        this.dfaRetries = dfaRetries;
        Options options = Options.builder().setAlgorithm(DFA_FALLBACK_TO_NFA).setMaximumNumberOfDFAStates(dfaStatesLimit).setNumberOfDFARetries(dfaRetries).setEventsListener(new RE2JEventsListener()).build();
        String patternString = pattern.toStringUtf8();
        re2jPattern = Pattern.compile(patternString, options);
        // Remove .*? prefix. DFA has optimization which does fast lookup for first byte of a potential match.
        // When pattern is prefixed with .*? this optimization doesn't work in Pattern.find() function.
        java.util.regex.Matcher dotStarPrefixMatcher = DOT_STAR_PREFIX_PATTERN.matcher(patternString);
        checkState(dotStarPrefixMatcher.matches());
        String patternStringWithoutDotStartPrefix = dotStarPrefixMatcher.group(CORE_PATTERN_INDEX);
        if (!patternStringWithoutDotStartPrefix.equals(patternString)) {
            re2jPatternWithoutDotStartPrefix = Pattern.compile(patternStringWithoutDotStartPrefix, options);
        } else {
            re2jPatternWithoutDotStartPrefix = re2jPattern;
        }
    }

    public String pattern() {
        return re2jPattern.pattern();
    }

    @Override
    public String toString() {
        return pattern();
    }

    public boolean matches(Slice source) {
        return re2jPatternWithoutDotStartPrefix.find(source);
    }

    public Matcher matcher(Slice source) {
        return re2jPattern.matcher(source);
    }

    public Slice replace(Slice source, Slice replacement) {
        Matcher matcher = re2jPattern.matcher(source);
        try {
            return matcher.replaceAll(replacement);
        } catch (IndexOutOfBoundsException | IllegalArgumentException e) {
            throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "Illegal replacement sequence: " + replacement.toStringUtf8());
        }
    }

    public Block extractAll(Slice source, long groupIndex) {
        Matcher matcher = re2jPattern.matcher(source);
        int group = toIntExact(groupIndex);
        validateGroup(group, matcher.groupCount());
        BlockBuilder blockBuilder = VARCHAR.createBlockBuilder(null, 32);
        while (true) {
            if (!matcher.find()) {
                break;
            }
            Slice searchedGroup = matcher.group(group);
            if (searchedGroup == null) {
                blockBuilder.appendNull();
                continue;
            }
            VARCHAR.writeSlice(blockBuilder, searchedGroup);
        }
        return blockBuilder.build();
    }

    public Slice extract(Slice source, long groupIndex) {
        Matcher matcher = re2jPattern.matcher(source);
        int group = toIntExact(groupIndex);
        validateGroup(group, matcher.groupCount());
        if (!matcher.find()) {
            return null;
        }
        return matcher.group(group);
    }

    public Block split(Slice source) {
        Matcher matcher = re2jPattern.matcher(source);
        BlockBuilder blockBuilder = VARCHAR.createBlockBuilder(null, 32);
        int lastEnd = 0;
        while (matcher.find()) {
            Slice slice = source.slice(lastEnd, matcher.start() - lastEnd);
            lastEnd = matcher.end();
            VARCHAR.writeSlice(blockBuilder, slice);
        }
        VARCHAR.writeSlice(blockBuilder, source.slice(lastEnd, source.length() - lastEnd));
        return blockBuilder.build();
    }

    private static void validateGroup(int group, int groupCount) {
        if (group < 0) {
            throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "Group cannot be negative");
        }
        if (group > groupCount) {
            throw new TrinoException(INVALID_FUNCTION_ARGUMENT, format("Pattern has %d groups. Cannot access group %d", groupCount, group));
        }
    }

    private clreplaced RE2JEventsListener implements Options.EventsListener {

        @Override
        public void fallbackToNFA() {
            log.debug("Fallback to NFA, pattern: %s, DFA states limit: %d, DFA retries: %d", re2jPattern.pattern(), dfaStatesLimit, dfaRetries);
        }
    }
}

18 Source : Re2JRegexp.java
with Apache License 2.0
from openlookeng

public final clreplaced Re2JRegexp {

    private static final Logger log = Logger.get(Re2JRegexp.clreplaced);

    private static final java.util.regex.Pattern DOT_STAR_PREFIX_PATTERN = java.util.regex.Pattern.compile("(?s)^(\\.\\*\\??)?(.*)");

    private static final int CORE_PATTERN_INDEX = 2;

    public final int dfaStatesLimit;

    public final int dfaRetries;

    public final Pattern re2jPattern;

    public final Pattern re2jPatternWithoutDotStartPrefix;

    public Re2JRegexp(int dfaStatesLimit, int dfaRetries, Slice pattern) {
        this.dfaStatesLimit = dfaStatesLimit;
        this.dfaRetries = dfaRetries;
        Options options = Options.builder().setAlgorithm(DFA_FALLBACK_TO_NFA).setMaximumNumberOfDFAStates(dfaStatesLimit).setNumberOfDFARetries(dfaRetries).setEventsListener(new RE2JEventsListener()).build();
        String patternString = pattern.toStringUtf8();
        re2jPattern = Pattern.compile(patternString, options);
        // Remove .*? prefix. DFA has optimization which does fast lookup for first byte of a potential match.
        // When pattern is prefixed with .*? this optimization doesn't work in Pattern.find() function.
        java.util.regex.Matcher dotStarPrefixMatcher = DOT_STAR_PREFIX_PATTERN.matcher(patternString);
        checkState(dotStarPrefixMatcher.matches());
        String patternStringWithoutDotStartPrefix = dotStarPrefixMatcher.group(CORE_PATTERN_INDEX);
        if (!patternStringWithoutDotStartPrefix.equals(patternString)) {
            re2jPatternWithoutDotStartPrefix = Pattern.compile(patternStringWithoutDotStartPrefix, options);
        } else {
            re2jPatternWithoutDotStartPrefix = re2jPattern;
        }
    }

    public boolean matches(Slice source) {
        return re2jPatternWithoutDotStartPrefix.find(source);
    }

    public Slice replace(Slice source, Slice replacement) {
        Matcher matcher = re2jPattern.matcher(source);
        try {
            return matcher.replaceAll(replacement);
        } catch (IndexOutOfBoundsException | IllegalArgumentException e) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Illegal replacement sequence: " + replacement.toStringUtf8());
        }
    }

    public Block extractAll(Slice source, long groupIndex) {
        Matcher matcher = re2jPattern.matcher(source);
        int group = toIntExact(groupIndex);
        validateGroup(group, matcher.groupCount());
        BlockBuilder blockBuilder = VARCHAR.createBlockBuilder(null, 32);
        while (true) {
            if (!matcher.find()) {
                break;
            }
            Slice searchedGroup = matcher.group(group);
            if (searchedGroup == null) {
                blockBuilder.appendNull();
                continue;
            }
            VARCHAR.writeSlice(blockBuilder, searchedGroup);
        }
        return blockBuilder.build();
    }

    public Slice extract(Slice source, long groupIndex) {
        Matcher matcher = re2jPattern.matcher(source);
        int group = toIntExact(groupIndex);
        validateGroup(group, matcher.groupCount());
        if (!matcher.find()) {
            return null;
        }
        return matcher.group(group);
    }

    public Block split(Slice source) {
        Matcher matcher = re2jPattern.matcher(source);
        BlockBuilder blockBuilder = VARCHAR.createBlockBuilder(null, 32);
        int lastEnd = 0;
        while (matcher.find()) {
            Slice slice = source.slice(lastEnd, matcher.start() - lastEnd);
            lastEnd = matcher.end();
            VARCHAR.writeSlice(blockBuilder, slice);
        }
        VARCHAR.writeSlice(blockBuilder, source.slice(lastEnd, source.length() - lastEnd));
        return blockBuilder.build();
    }

    private static void validateGroup(int group, int groupCount) {
        if (group < 0) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Group cannot be negative");
        }
        if (group > groupCount) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Pattern has %d groups. Cannot access group %d", groupCount, group));
        }
    }

    private clreplaced RE2JEventsListener implements Options.EventsListener {

        @Override
        public void fallbackToNFA() {
            log.debug("Fallback to NFA, pattern: %s, DFA states limit: %d, DFA retries: %d", re2jPattern.pattern(), dfaStatesLimit, dfaRetries);
        }
    }
}

18 Source : Re2jRegexDeserializer.java
with Apache License 2.0
from Nextdoor

public clreplaced Re2jRegexDeserializer extends Deserializer {

    private final Pattern pattern;

    private final List<ReFieldConfig> fields;

    public Re2jRegexDeserializer(final Pattern pattern, final List<ReFieldConfig> fields) {
        this.pattern = pattern;
        this.fields = fields;
    }

    @Override
    public DeserializedEvent deserialize(String raw) {
        Matcher m = this.pattern.matcher(raw);
        if (!m.matches()) {
            throw new DeserializationException("raw event does not match string");
        }
        int groups = m.groupCount();
        JsonObject obj = new JsonObject();
        for (int i = 0; i < groups && i < fields.size(); i++) {
            String str = m.group(i + 1);
            ReFieldConfig field = this.fields.get(i);
            switch(field.getType()) {
                case BOOLEAN:
                    obj.addProperty(field.getName(), Boolean.parseBoolean(str));
                    break;
                case NUMBER:
                    obj.addProperty(field.getName(), NumberUtils.createNumber(str));
                    break;
                case STRING:
                    obj.addProperty(field.getName(), str);
                    break;
                default:
                    obj.addProperty(field.getName(), str);
                    break;
            }
        }
        return new GenericJsonEvent(obj);
    }

    @Override
    public void init() {
    }
}

18 Source : LabelDescriptor.java
with Apache License 2.0
from google

/**
 * Definition of a metric label.
 *
 * <p>If a metric is created with labels, corresponding label values must be provided when setting
 * values on the metric.
 */
@AutoValue
public abstract clreplaced LabelDescriptor {

    private static final Pattern ALLOWED_LABEL_PATTERN = Pattern.compile("\\w+");

    LabelDescriptor() {
    }

    /**
     * Returns a new {@link LabelDescriptor}.
     *
     * @param name identifier for label
     * @param description human-readable description of label
     * @throws IllegalArgumentException if {@code name} isn't {@code \w+} or {@code description} is
     *     blank
     */
    public static LabelDescriptor create(String name, String description) {
        checkArgument(!name.isEmpty(), "Name must not be empty");
        checkArgument(!description.isEmpty(), "Description must not be empty");
        checkArgument(ALLOWED_LABEL_PATTERN.matches(name), "Label name must match the regex %s", ALLOWED_LABEL_PATTERN);
        return new AutoValue_LabelDescriptor(name, description);
    }

    public abstract String name();

    public abstract String description();
}

18 Source : SolidContactsExport.java
with Apache License 2.0
from google

public clreplaced SolidContactsExport implements Exporter<CookiesAndUrlAuthData, ContactsModelWrapper> {

    private static final Pattern MAIL_TO_PATTERN = Pattern.compile("mailto:(.+@.+\\..+)");

    private static final Logger logger = LoggerFactory.getLogger(SolidContactsExport.clreplaced);

    private static final Property NAME_EMAIL_INDEX_PROPERTY = ModelFactory.createDefaultModel().createProperty(VCARD4.NS, "nameEmailIndex");

    @Override
    public ExportResult<ContactsModelWrapper> export(UUID jobId, CookiesAndUrlAuthData authData, Optional<ExportInformation> exportInformation) throws Exception {
        if (exportInformation.isPresent()) {
            throw new IllegalStateException("Currently solid doesn't support paged exports");
        }
        checkState(authData.getCookies().size() == 1, "Exactly 1 cookie expected: %s", authData.getCookies());
        SolidUtilities solidUtilities = new SolidUtilities(authData.getCookies().get(0));
        String url = authData.getUrl();
        List<List<VCard>> vCards = explore(url, solidUtilities);
        // TODO: This flattens all the address books together, which isn't perfect.
        List<VCard> allCards = new ArrayList<>();
        vCards.forEach(allCards::addAll);
        return new ExportResult<>(ResultType.END, new ContactsModelWrapper(Ezvcard.write(allCards).go()));
    }

    private List<List<VCard>> explore(String url, SolidUtilities utilities) throws IOException {
        logger.debug("Exploring: %s", url);
        List<List<VCard>> results = new ArrayList<>();
        utilities.explore(url, r -> {
            try {
                List<VCard> cards = parseAddressBookIfApplicable(r, utilities);
                if (cards != null) {
                    results.add(cards);
                }
            } catch (IOException e) {
                throw new IllegalStateException("Problem parsing " + r.getURI(), e);
            }
        });
        return results;
    }

    private List<VCard> parseAddressBookIfApplicable(Resource resource, SolidUtilities utilities) throws IOException {
        if (SolidUtilities.isType(resource, "http://www.w3.org/2006/vcard/ns#AddressBook")) {
            logger.debug("Got Address book at %s", resource.getURI());
            return parseAddressBook(resource, utilities);
        }
        return null;
    }

    private List<VCard> parseAddressBook(Resource selfResource, SolidUtilities utilities) throws IOException {
        String peopleUri = selfResource.getProperty(NAME_EMAIL_INDEX_PROPERTY).getResource().getURI();
        Model peopleModel = utilities.getModel(peopleUri);
        List<VCard> vcards = new ArrayList<>();
        ResIterator subjects = peopleModel.listSubjects();
        while (subjects.hasNext()) {
            Resource subject = subjects.nextResource();
            Model personModel = utilities.getModel(subject.getURI());
            Resource personResource = SolidUtilities.getResource(subject.getURI(), personModel);
            if (personResource == null) {
                throw new IllegalStateException(subject.getURI() + " not found in " + subject.toString());
            }
            vcards.add(parsePerson(personResource));
        }
        return vcards;
    }

    final static VCard parsePerson(Resource r) {
        VCard vcard = new VCard();
        if (r.hasProperty(VCARD4.fn)) {
            vcard.setFormattedName(r.getProperty(VCARD4.fn).getString());
        }
        if (r.hasProperty(VCARD4.hasName)) {
            Stmreplacederator nameIterator = r.listProperties(VCARD4.hasName);
            while (nameIterator.hasNext()) {
                Statement nameStatement = nameIterator.nextStatement();
                Resource structuredNameResource = nameStatement.getResource();
                StructuredName structuredName = new StructuredName();
                if (structuredNameResource.hasProperty(VCARD4.family_name)) {
                    structuredName.setFamily(structuredNameResource.getProperty(VCARD4.family_name).getString());
                }
                if (structuredNameResource.hasProperty(VCARD4.given_name)) {
                    structuredName.setGiven(structuredNameResource.getProperty(VCARD4.given_name).getString());
                }
                structuredNameResource.listProperties(VCARD4.hasHonorificPrefix).forEachRemaining(prefix -> structuredName.getPrefixes().add(prefix.getString()));
                structuredNameResource.listProperties(VCARD4.hasHonorificSuffix).forEachRemaining(suffix -> structuredName.getSuffixes().add(suffix.getString()));
                structuredNameResource.listProperties(VCARD4.hasAdditionalName).forEachRemaining(additional -> structuredName.getAdditionalNames().add(additional.getString()));
                vcard.getStructuredNames().add(structuredName);
            }
        }
        if (r.hasProperty(VCARD4.organization_name)) {
            vcard.setOrganization(r.getProperty(VCARD4.organization_name).getString());
        }
        if (r.hasProperty(VCARD4.hasEmail)) {
            r.listProperties(VCARD4.hasEmail).forEachRemaining(emailStatement -> {
                Resource emailResource = emailStatement.getResource();
                if (emailResource.isResource()) {
                    Statement valueStatement = getValueStatement(emailResource);
                    String mailTo = valueStatement.getObject().toString();
                    Matcher matcher = MAIL_TO_PATTERN.matcher(mailTo);
                    checkState(matcher.matches(), "%s mail to address doesn't match", mailTo);
                    String emailAddress = matcher.group(1);
                    Email email = new Email(emailAddress);
                    if (emailResource.hasProperty(RDF.type)) {
                        emailResource.listProperties(RDF.type).forEachRemaining(typeProperty -> email.getTypes().add(EmailType.find(typeProperty.getResource().getLocalName())));
                    }
                    vcard.addEmail(email);
                } else {
                    String mailTo = emailResource.getURI();
                    Matcher matcher = MAIL_TO_PATTERN.matcher(mailTo);
                    checkState(matcher.matches(), "%s mail to address doesn't match", mailTo);
                    String emailAddress = matcher.group(1);
                    Email email = new Email(emailAddress);
                    vcard.addEmail(email);
                }
            });
        }
        if (r.hasProperty(VCARD4.hasTelephone)) {
            r.listProperties(VCARD4.hasTelephone).forEachRemaining(telephoneStatement -> {
                Resource telephoneResource = telephoneStatement.getResource();
                if (telephoneResource.isResource()) {
                    Statement valueStatement = getValueStatement(telephoneResource);
                    String telephoneNumber = valueStatement.getObject().toString();
                    Telephone telephoneObject = new Telephone(telephoneNumber);
                    if (telephoneResource.hasProperty(RDF.type)) {
                        telephoneResource.listProperties(RDF.type).forEachRemaining(typeProperty -> telephoneObject.getTypes().add(TelephoneType.find(typeProperty.getResource().getLocalName())));
                    }
                    vcard.addTelephoneNumber(telephoneObject);
                } else {
                    String telephoneNumber = telephoneResource.getURI();
                    Telephone telephoneObject = new Telephone(telephoneNumber);
                    vcard.addTelephoneNumber(telephoneObject);
                }
            });
        }
        if (r.hasProperty(VCARD4.note)) {
            vcard.addNote(r.getProperty(VCARD4.note).getString());
        }
        r.listProperties(VCARD4.hasNote).forEachRemaining(noteStatement -> vcard.getNotes().add(new Note(noteStatement.getString())));
        r.listProperties(VCARD4.hasURL).forEachRemaining(urlStatement -> vcard.getUrls().add(new Url(urlStatement.getString())));
        r.listProperties(VCARD4.hasPhoto).forEachRemaining(photoStatement -> vcard.getPhotos().add(new Photo(photoStatement.getString(), ImageType.JPEG)));
        return vcard;
    }

    private static Statement getValueStatement(Resource r) {
        Statement valueStatement = r.getProperty(VCARD4.hasValue);
        if (valueStatement == null) {
            valueStatement = r.getProperty(VCARD4.value);
        }
        if (valueStatement == null) {
            throw new IllegalStateException("Couldn't find value property in: " + r);
        }
        return valueStatement;
    }
}

18 Source : MetadataVerifyMatch.java
with Apache License 2.0
from google

/**
 * A checker that validates that the change description satisfies a Regex or that it doesn't
 * if verifyNoMatch is set.
 */
public clreplaced MetadataVerifyMatch implements Transformation {

    private final Pattern pattern;

    private final boolean verifyNoMatch;

    private final Location location;

    MetadataVerifyMatch(Pattern pattern, boolean verifyNoMatch, Location location) {
        this.pattern = Preconditions.checkNotNull(pattern);
        this.verifyNoMatch = verifyNoMatch;
        this.location = Preconditions.checkNotNull(location);
    }

    @Override
    public void transform(TransformWork work) throws IOException, ValidationException {
        boolean found = pattern.matcher(work.getMessage()).find();
        checkCondition(found || verifyNoMatch, "Could not find '%s' in the change message. Message was:\n%s", pattern, work.getMessage());
        checkCondition(!found || !verifyNoMatch, "'%s' found in the change message. Message was:\n%s", pattern, work.getMessage());
    }

    @Override
    public Transformation reverse() throws NonReversibleValidationException {
        return new ExplicitReversal(IntentionalNoop.INSTANCE, this);
    }

    @Override
    public String describe() {
        return String.format("Verify message %s '%s'", (verifyNoMatch ? "does not match" : "matches"), pattern);
    }

    @Override
    public Location location() {
        return location;
    }
}

18 Source : LabelFinder.java
with Apache License 2.0
from google

/**
 * A simple line finder/parser for labels like:
 * <ul>
 *   <li>foo = bar</li>
 *   <li>baz : foo</li>
 * </ul>
 *
 * <p>In general this clreplaced should only be used in {@code Origin}s to create a labels map.
 * During transformations/destination, it can be used to check if a line is a line but
 * never to find labels. Use {@link TransformWork#getLabel(String)} instead, since it looks
 * in more places for labels.
 *
 * TODO(malcon): Rename to MaybeLabel
 */
public clreplaced LabelFinder {

    private static final String VALID_LABEL_EXPR = "([\\w-]+)";

    public static final Pattern VALID_LABEL = Pattern.compile(VALID_LABEL_EXPR);

    private static final Pattern URL = Pattern.compile(VALID_LABEL + "://.*");

    private static final Pattern LABEL_PATTERN = Pattern.compile("^" + VALID_LABEL_EXPR + "( *[:=] ?)(.*)");

    private final Matcher matcher;

    private final String line;

    public LabelFinder(String line) {
        matcher = LABEL_PATTERN.matcher(line);
        this.line = line;
    }

    public boolean isLabel() {
        // It is a line if it looks like a line but it doesn't look like a url (foo://bar)
        return matcher.matches() && !URL.matcher(line).matches();
    }

    public boolean isLabel(String labelName) {
        return isLabel() && getName().equals(labelName);
    }

    /**
     * Returns the name of the label.
     *
     * <p>Use isLabel() method before calling this method.
     */
    public String getName() {
        checkIsLabel();
        return matcher.group(1);
    }

    /**
     * Returns the separator of the label.
     *
     * <p>Use isLabel() method before calling this method.
     */
    public String getSeparator() {
        checkIsLabel();
        return matcher.group(2);
    }

    /**
     * Returns the value of the label.
     *
     * <p>Use isLabel() method before calling this method.
     */
    public String getValue() {
        checkIsLabel();
        return matcher.group(3);
    }

    private void checkIsLabel() {
        checkState(isLabel(), "Not a label: '" + line + "'. Please call isLabel() first");
    }

    public String getLine() {
        return line;
    }
}

18 Source : HgRepository.java
with Apache License 2.0
from google

/**
 * A clreplaced for manipulating Hg (Mercurial) repositories
 */
public clreplaced HgRepository {

    /**
     * Label to mark the original revision id (Hg SHA-1) for migrated commits.
     */
    static final String HG_ORIGIN_REV_ID = "HgOrigin-RevId";

    private static final Pattern INVALID_HG_REPOSITORY = Pattern.compile("abort: repository .+ not found");

    private static final Pattern UNKNOWN_REVISION = Pattern.compile("abort: unknown revision '.+'");

    private static final Pattern INVALID_REF_EXPRESSION = Pattern.compile("syntax error in revset '.+'");

    /**
     * The location of the {@code .hg} directory.
     */
    private final Path hgDir;

    private final boolean verbose;

    private final Duration fetchTimeout;

    public HgRepository(Path hgDir, boolean verbose, Duration fetchTimeout) {
        this.hgDir = checkNotNull(hgDir);
        this.verbose = verbose;
        this.fetchTimeout = checkNotNull(fetchTimeout);
    }

    /**
     * Initializes a new hg repository
     * @return the new HgRepository
     * @throws RepoException if the directory cannot be created
     */
    public HgRepository init() throws RepoException {
        try {
            Files.createDirectories(hgDir);
        } catch (IOException e) {
            throw new RepoException("Cannot create directory: " + e.getMessage(), e);
        }
        hg(hgDir, ImmutableList.of("init"), DEFAULT_TIMEOUT);
        return this;
    }

    /**
     * Finds all changes from a repository at {@code url} and adds to the current repository.
     * Defaults to forced pull.
     *
     * <p>This does not update the copy of the project in the working directory.
     *
     * @param url remote hg repository url
     */
    void pullAll(String url) throws RepoException, ValidationException {
        pull(url, /*force*/
        true, /*ref*/
        null);
    }

    /**
     * Finds a single reference from a repository at {@code url} and adds to the current repository.
     * Defaults to forced pull.
     *
     * <p>This does not update the copy of the project in the working directory.
     *
     * @param url remote hg repository url
     * @param ref the remote revision to add
     */
    void pullFromRef(String url, String ref) throws RepoException, ValidationException {
        pull(url, /*force*/
        true, /*ref*/
        ref);
    }

    public void pull(String url, boolean force, @Nullable String ref) throws RepoException, ValidationException {
        ImmutableList.Builder<String> builder = ImmutableList.builder();
        builder.add("pull", validateNotHttp(url));
        if (force) {
            builder.add("--force");
        }
        if (!Strings.isNullOrEmpty(ref)) {
            builder.add("--rev", ref);
        }
        try {
            hg(hgDir, builder.build(), fetchTimeout);
        } catch (RepoException e) {
            if (INVALID_HG_REPOSITORY.matcher(e.getMessage()).find()) {
                throw new ValidationException("Repository not found: " + e.getMessage());
            }
            if (UNKNOWN_REVISION.matcher(e.getMessage()).find()) {
                throw new ValidationException("Unknown revision: " + e.getMessage());
            }
            throw e;
        }
    }

    /**
     * Updates the working directory to the revision given at {@code ref} in the repository
     * and discards local changes.
     */
    CommandOutput cleanUpdate(String ref) throws RepoException {
        return hg(hgDir, "update", ref, "--clean");
    }

    /**
     * Returns a revision object given a reference.
     *
     * <p> A reference can be any of the following:
     * <ul>
     *   <li> A global identifier for a revision. Example: f4e0e692208520203de05557244e573e981f6c72
     *   <li> A local identifier for a revision in the repository. Example: 1
     *   <li> A bookmark in the repository.
     *   <li> A branch in the repository, which returns the tip of that branch. Example: default
     *   <li> A tag in the repository. Example: tip
     * </ul>
     */
    public HgRevision identify(String reference) throws RepoException, CannotResolveRevisionException {
        try {
            CommandOutput commandOutput = hg(hgDir, "identify", "--template", "{node}\n", "--id", "--rev", reference);
            String globalId = commandOutput.getStdout().trim();
            return new HgRevision(globalId, reference);
        } catch (RepoException e) {
            if (UNKNOWN_REVISION.matcher(e.getMessage()).find()) {
                throw new CannotResolveRevisionException(String.format("Unknown revision: %s", e.getMessage()));
            }
            throw e;
        }
    }

    /**
     * Creates an unversioned archive of the current working directory and subrepositories
     * in the location {@code archivePath}.
     */
    void archive(String archivePath) throws RepoException {
        hg(hgDir, "archive", archivePath, "--type", "files", "--subrepos");
    }

    /**
     * Creates a log command.
     */
    public LogCmd log() {
        return LogCmd.create(this);
    }

    /**
     * Invokes {@code hg} in the directory given by {@code cwd} against this repository and returns
     * the {@link CommandOutput} if the command execution was successful.
     *
     * <p>Only to be used externally for testing.
     *
     * @param cwd the directory in which to execute the command
     * @param params the argv to preplaced to Hg, excluding the initial {@code hg}
     */
    @VisibleForTesting
    public CommandOutput hg(Path cwd, String... params) throws RepoException {
        return hg(cwd, Arrays.asList(params), DEFAULT_TIMEOUT);
    }

    private CommandOutput hg(Path cwd, Iterable<String> params, Duration timeout) throws RepoException {
        try {
            return executeHg(cwd, params, -1, timeout);
        } catch (BadExitStatusWithOutputException e) {
            throw new RepoException(String.format("Error executing hg: %s", e.getOutput().getStderr()));
        } catch (CommandException e) {
            throw new RepoException(String.format("Error executing hg: %s", e.getMessage()));
        }
    }

    public Path getHgDir() {
        return hgDir;
    }

    private CommandOutputWithStatus executeHg(Path cwd, Iterable<String> params, int maxLogLines, Duration timeout) throws CommandException {
        List<String> allParams = new ArrayList<>(Iterables.size(params) + 1);
        // TODO(jlliu): resolve Hg binary here
        allParams.add("hg");
        Iterables.addAll(allParams, params);
        Command cmd = new Command(Iterables.toArray(allParams, String.clreplaced), null, cwd.toFile());
        // TODO(jlliu): have environment vars
        CommandRunner runner = new CommandRunner(cmd, timeout).withVerbose(verbose);
        return maxLogLines >= 0 ? runner.withMaxStdOutLogLines(maxLogLines).execute() : runner.execute();
    }

    /**
     * An object that can perform a "hg log" operation on a repository and returns a list of
     * {@link HgLogEntry}.
     */
    public static clreplaced LogCmd {

        private final HgRepository repo;

        private final int limit;

        /**
         * Branch to limit the query from. Defaults to all branches if null.
         */
        @Nullable
        private final String branch;

        @Nullable
        private final String referenceExpression;

        @Nullable
        private final String keyword;

        private LogCmd(HgRepository repo, int limit, @Nullable String branch, @Nullable String referenceExpression, @Nullable String keyword) {
            this.repo = repo;
            this.limit = limit;
            this.branch = branch;
            this.referenceExpression = referenceExpression;
            this.keyword = keyword;
        }

        static LogCmd create(HgRepository repo) {
            return new LogCmd(checkNotNull(repo), /*limit*/
            0, /*branch*/
            null, /*referenceExpression*/
            null, /*keyword*/
            null);
        }

        /**
         * Limit the query to references that fit the {@code referenceExpression}.
         *
         * <p>The expression must be in a format that is supported by Mercurial. Mercurial supports a
         * number of operators: for example, x::y represents all revisions that are descendants of x and
         * ancestors of y, including x and y.
         */
        LogCmd withReferenceExpression(String referenceExpression) throws RepoException {
            if (Strings.isNullOrEmpty(referenceExpression.trim())) {
                throw new RepoException("Cannot log null or empty reference");
            }
            return new LogCmd(repo, limit, branch, referenceExpression.trim(), keyword);
        }

        /**
         * Limit the query to {@code limit} results. Should be > 0.
         */
        public LogCmd withLimit(int limit) {
            Preconditions.checkArgument(limit > 0);
            return new LogCmd(repo, limit, branch, referenceExpression, keyword);
        }

        /**
         * Only query for revisions from the branch {@code branch}.
         */
        LogCmd withBranch(String branch) {
            return new LogCmd(repo, limit, branch, referenceExpression, keyword);
        }

        /**
         * Only query for revisions with the keyword {@code keyword}.
         */
        LogCmd withKeyword(String keyword) {
            return new LogCmd(repo, limit, branch, referenceExpression, keyword);
        }

        /**
         * Run "hg log' and return zero or more {@link HgLogEntry}.
         *
         * <p> The order of the log entries is by default in reverse chronological order, where the
         * first element of the list is the most recent commit. However, if a reference expression is
         * provided, log entries will be ordered with respect to the expression. For example, if the
         * expression is "0:tip", entries will be ordered in chronological order, where the first
         * element of the list is the first commit.
         */
        public ImmutableList<HgLogEntry> run() throws RepoException, ValidationException {
            ImmutableList.Builder<String> builder = ImmutableList.builder();
            // verbose flag shows files in output
            builder.add("log", "--verbose");
            if (branch != null) {
                builder.add("--branch", branch);
            }
            /* hg requires limit to be a positive integer */
            if (limit > 0) {
                builder.add("--limit", String.valueOf(limit));
            }
            if (referenceExpression != null) {
                builder.add("--rev", referenceExpression);
            }
            if (keyword != null) {
                builder.add("--keyword", keyword);
            }
            builder.add("-Tjson");
            try {
                CommandOutput output = repo.hg(repo.getHgDir(), builder.build(), DEFAULT_TIMEOUT);
                return parseLog(output.getStdout());
            } catch (RepoException e) {
                if (UNKNOWN_REVISION.matcher(e.getMessage()).find()) {
                    throw new ValidationException("Unknown revision: " + e.getMessage());
                }
                if (INVALID_REF_EXPRESSION.matcher(e.getMessage()).find()) {
                    throw new RepoException("Syntax error in reference expression: " + e.getMessage());
                }
                throw e;
            }
        }

        private ImmutableList<HgLogEntry> parseLog(String log) throws RepoException {
            if (log.isEmpty()) {
                return ImmutableList.of();
            }
            Gson gson = new Gson();
            Type logListType = new TypeToken<List<HgLogEntry>>() {
            }.getType();
            try {
                List<HgLogEntry> logEntries = gson.fromJson(log.trim(), logListType);
                return ImmutableList.copyOf(logEntries);
            } catch (JsonParseException e) {
                throw new RepoException(String.format("Cannot parse log output: %s", e.getMessage()));
            }
        }
    }

    /**
     * An object that represents a commit as returned by 'hg log'.
     */
    public static clreplaced HgLogEntry {

        @SerializedName("node")
        private final String globalId;

        private final List<String> parents;

        private final String user;

        @SerializedName("date")
        private final List<String> commitDate;

        private final String branch;

        @SerializedName("desc")
        private final String description;

        private final List<String> files;

        HgLogEntry(String node, List<String> parents, String user, List<String> commitDate, String branch, String desc, List<String> files) {
            this.globalId = checkNotNull(node);
            this.parents = ImmutableList.copyOf(parents);
            this.user = user;
            this.commitDate = commitDate;
            this.branch = branch;
            this.description = desc;
            this.files = ImmutableList.copyOf(files);
        }

        public List<String> getParents() {
            return parents;
        }

        public String getUser() {
            return user;
        }

        String getGlobalId() {
            return globalId;
        }

        ZonedDateTime getZonedDate() {
            Instant date = Instant.ofEpochSecond(Long.parseLong(commitDate.get(0)));
            ZoneOffset offset = ZoneOffset.ofTotalSeconds(-1 * Integer.parseInt(commitDate.get(1)));
            ZoneId zone = ZoneId.ofOffset("", offset);
            return ZonedDateTime.ofInstant(date, zone);
        }

        public String getBranch() {
            return branch;
        }

        public String getDescription() {
            return description;
        }

        public List<String> getFiles() {
            return files;
        }
    }
}

18 Source : GitHubPRIntegrateLabel.java
with Apache License 2.0
from google

/**
 * Integrate label for GitHub PR
 *
 * <p>Format like: "https://github.com/google/copybara/pull/12345 from mikelalcon:master SHA-1"
 *
 * <p>Where SHA-1 is optional: If present it means to integrate the specific SHA-1. Otherwise the
 * head of the PR is used.
 */
clreplaced GitHubPRIntegrateLabel implements IntegrateLabel {

    private static final Pattern LABEL_PATTERN = Pattern.compile("https://github.com/([a-zA-Z0-9_/-]+)/pull/([0-9]+) from ([^\\s\\r\\n]*)(?: ([0-9a-f]{7,40}))?");

    private final GitRepository repository;

    private final GeneralOptions generalOptions;

    private final String projectId;

    private final long prNumber;

    private final String originBranch;

    @Nullable
    private final String sha1;

    GitHubPRIntegrateLabel(GitRepository repository, GeneralOptions generalOptions, String projectId, long prNumber, String originBranch, @Nullable String sha1) {
        this.repository = Preconditions.checkNotNull(repository);
        this.generalOptions = Preconditions.checkNotNull(generalOptions);
        this.projectId = Preconditions.checkNotNull(projectId);
        this.prNumber = prNumber;
        this.originBranch = Preconditions.checkNotNull(originBranch);
        this.sha1 = sha1;
    }

    @Nullable
    static GitHubPRIntegrateLabel parse(String str, GitRepository repository, GeneralOptions generalOptions) {
        Matcher matcher = LABEL_PATTERN.matcher(str);
        return matcher.matches() ? new GitHubPRIntegrateLabel(repository, generalOptions, matcher.group(1), Long.parseLong(matcher.group(2)), matcher.group(3), matcher.group(4)) : null;
    }

    @Override
    public String toString() {
        return String.format("https://github.com/%s/pull/%d from %s%s", projectId, prNumber, originBranch, sha1 != null ? " " + sha1 : "");
    }

    @Override
    public String mergeMessage(ImmutableList<LabelFinder> labelsToAdd) {
        return IntegrateLabel.withLabels(String.format("Merge pull request #%d from %s", prNumber, originBranch), labelsToAdd);
    }

    @Override
    public GitRevision getRevision() throws RepoException, ValidationException {
        String pr = "https://github.com/" + projectId + "/pull/" + prNumber;
        String repoUrl = "https://github.com/" + projectId;
        GitRevision gitRevision = GitRepoType.GITHUB.resolveRef(repository, repoUrl, pr, generalOptions, /*describeVersion=*/
        false, /*partialFetch*/
        false);
        if (sha1 == null) {
            return gitRevision;
        }
        if (sha1.equals(gitRevision.getSha1())) {
            return gitRevision;
        }
        generalOptions.console().warnFmt("Pull Request %s has more changes after %s (PR HEAD is %s)." + " Not all changes might be migrated", pr, sha1, gitRevision.getSha1());
        return repository.resolveReferenceWithContext(sha1, gitRevision.contextReference(), repoUrl);
    }

    public String getProjectId() {
        return projectId;
    }

    public long getPrNumber() {
        return prNumber;
    }

    public String getOriginBranch() {
        return originBranch;
    }
}

18 Source : GitHubEndPoint.java
with Apache License 2.0
from google

/**
 * GitHub specific clreplaced used in feedback mechanism and migration event hooks to access GitHub
 */
@SuppressWarnings("unused")
@StarlarkBuiltin(name = "github_api_obj", doc = "GitHub API endpoint implementation for feedback migrations and after migration hooks.")
public clreplaced GitHubEndPoint implements Endpoint, StarlarkValue {

    private final LazyResourceLoader<GitHubApi> apiSupplier;

    private final String url;

    private final Console console;

    private GitHubHost ghHost;

    // This might not be complete but it is only used for filtering get_pull_requests. We can
    // add more chars on demand.
    private static final Pattern SAFE_BRANCH_NAME_PREFIX = Pattern.compile("[\\w_.-][\\w/_.-]*");

    GitHubEndPoint(LazyResourceLoader<GitHubApi> apiSupplier, String url, Console console, GitHubHost ghHost) {
        this.apiSupplier = Preconditions.checkNotNull(apiSupplier);
        this.url = Preconditions.checkNotNull(url);
        this.console = Preconditions.checkNotNull(console);
        this.ghHost = ghHost;
    }

    @StarlarkMethod(name = "create_status", doc = "Create or update a status for a commit. Returns the status created.", parameters = { @Param(name = "sha", named = true, doc = "The SHA-1 for which we want to create or update the status"), @Param(name = "state", named = true, doc = "The state of the commit status: 'success', 'error', 'pending' or 'failure'"), @Param(name = "context", doc = "The context for the commit status. Use a value like 'copybara/import_successful'" + " or similar", named = true), @Param(name = "description", named = true, doc = "Description about what happened"), @Param(name = "target_url", allowedTypes = { @ParamType(type = String.clreplaced), @ParamType(type = NoneType.clreplaced) }, named = true, doc = "Url with expanded information about the event", defaultValue = "None") })
    public Status createStatus(String sha, String state, String context, String description, Object targetUrl) throws EvalException, RepoException, ValidationException {
        try {
            checkCondition(State.VALID_VALUES.contains(state), "Invalid value for state. Valid values: %s", State.VALID_VALUES);
            checkCondition(GitRevision.COMPLETE_SHA1_PATTERN.matcher(sha).matches(), "Not a valid complete SHA-1: %s", sha);
            checkCondition(!Strings.isNullOrEmpty(description), "description cannot be empty");
            checkCondition(!Strings.isNullOrEmpty(context), "context cannot be empty");
            String project = ghHost.getProjectNameFromUrl(url);
            return apiSupplier.load(console).createStatus(project, sha, new CreateStatusRequest(State.valueOf(state.toUpperCase()), convertFromNoneable(targetUrl, null), description, context));
        } catch (GitHubApiException gae) {
            if (gae.getResponseCode() == ResponseCode.UNPROCESSABLE_ENreplacedY) {
                throw new ValidationException("GitHub was unable to process the request " + gae.getError(), gae);
            }
            throw gae;
        } catch (ValidationException | RuntimeException e) {
            throw Starlark.errorf("Error calling create_status: %s", e.getMessage());
        }
    }

    @StarlarkMethod(name = "get_check_runs", doc = "Get the list of check runs for a sha. " + "https://developer.github.com/v3/checks/runs/#check-runs", parameters = { @Param(name = "sha", named = true, doc = "The SHA-1 for which we want to get the check runs") })
    public CheckRuns getCheckRuns(String sha) throws EvalException, RepoException {
        try {
            checkCondition(GitRevision.COMPLETE_SHA1_PATTERN.matcher(sha).matches(), "Not a valid complete SHA-1: %s", sha);
            String project = ghHost.getProjectNameFromUrl(url);
            return apiSupplier.load(console).getCheckRuns(project, sha);
        } catch (ValidationException | RuntimeException e) {
            throw Starlark.errorf("Error calling get_check_runs: %s", e.getMessage());
        }
    }

    @StarlarkMethod(name = "get_combined_status", doc = "Get the combined status for a commit. Returns None if not found.", parameters = { @Param(name = "ref", named = true, doc = "The SHA-1 or ref for which we want to get the combined status") }, allowReturnNones = true)
    @Nullable
    public CombinedStatus getCombinedStatus(String ref) throws EvalException, RepoException {
        try {
            checkCondition(!Strings.isNullOrEmpty(ref), "Empty reference not allowed");
            String project = ghHost.getProjectNameFromUrl(url);
            return apiSupplier.load(console).getCombinedStatus(project, ref);
        } catch (GitHubApiException e) {
            return returnNullOnNotFound(e);
        } catch (ValidationException | RuntimeException e) {
            throw Starlark.errorf("Error calling get_combined_status: %s", e);
        }
    }

    @StarlarkMethod(name = "get_commit", doc = "Get information for a commit in GitHub. Returns None if not found.", parameters = { @Param(name = "ref", named = true, // Works for refs too but we don't want to publicize since GH API docs refers to sha
    doc = "The SHA-1 for which we want to get the combined status") }, allowReturnNones = true)
    @Nullable
    public GitHubCommit getCommit(String ref) throws EvalException, RepoException {
        try {
            checkCondition(!Strings.isNullOrEmpty(ref), "Empty reference not allowed");
            String project = ghHost.getProjectNameFromUrl(url);
            return apiSupplier.load(console).getCommit(project, ref);
        } catch (GitHubApiException e) {
            return returnNullOnNotFound(e);
        } catch (ValidationException | RuntimeException e) {
            throw Starlark.errorf("Error calling get_commit: %s", e);
        }
    }

    @StarlarkMethod(name = "update_reference", doc = "Update a reference to point to a new commit. Returns the info of the reference.", parameters = { @Param(name = "ref", named = true, doc = "The name of the reference."), @Param(name = "sha", doc = "The id for the commit" + " status.", named = true), @Param(name = "force", named = true, doc = "Indicates whether to force the update or to make sure the update is a" + " fast-forward update. Leaving this out or setting it to false will make" + " sure you're not overwriting work. Default: false") })
    public Ref updateReference(String sha, String ref, boolean force) throws EvalException, RepoException {
        try {
            checkCondition(GitRevision.COMPLETE_SHA1_PATTERN.matcher(sha).matches(), "Not a valid complete SHA-1: %s", sha);
            checkCondition(!Strings.isNullOrEmpty(ref), "ref cannot be empty");
            if (!ref.startsWith("refs/")) {
                // TODO(malcon): Remove this functionality and use a check once library migrated.
                console.warnFmt("Non-complete ref preplaceded to update_reference '%s'. replaceduming refs/heads/%s", ref, ref);
                ref = "refs/heads/" + ref;
            }
            String project = ghHost.getProjectNameFromUrl(url);
            return apiSupplier.load(console).updateReference(project, ref, new UpdateReferenceRequest(sha, force));
        } catch (ValidationException | RuntimeException e) {
            throw Starlark.errorf("Error calling update_reference: %s", e.getMessage());
        }
    }

    @StarlarkMethod(name = "delete_reference", doc = "Delete a reference.", parameters = { @Param(name = "ref", named = true, doc = "The name of the reference.") })
    public void deleteReference(String ref) throws EvalException, RepoException {
        try {
            checkCondition(!Strings.isNullOrEmpty(ref), "ref cannot be empty");
            checkCondition(ref.startsWith("refs/"), "ref needs to be a complete reference." + " Example: refs/heads/foo");
            String project = ghHost.getProjectNameFromUrl(url);
            apiSupplier.load(console).deleteReference(project, ref);
        } catch (ValidationException | RuntimeException e) {
            throw Starlark.errorf("Error calling delete_reference: %s", e.getMessage());
        }
    }

    @StarlarkMethod(name = "get_reference", doc = "Get a reference SHA-1 from GitHub. Returns None if not found.", parameters = { @Param(name = "ref", named = true, doc = "The name of the reference. For example: \"refs/heads/branchName\".") }, allowReturnNones = true)
    @Nullable
    public Ref getReference(String ref) throws EvalException, RepoException {
        try {
            checkCondition(!Strings.isNullOrEmpty(ref), "Ref cannot be empty");
            String project = ghHost.getProjectNameFromUrl(url);
            return apiSupplier.load(console).getReference(project, ref);
        } catch (GitHubApiException e) {
            return returnNullOnNotFound(e);
        } catch (ValidationException | RuntimeException e) {
            throw Starlark.errorf("Error calling get_reference: %s", e.getMessage());
        }
    }

    @StarlarkMethod(name = "get_pull_requests", doc = "Get Pull Requests for a repo", parameters = { @Param(name = "head_prefix", allowedTypes = { @ParamType(type = String.clreplaced), @ParamType(type = NoneType.clreplaced) }, named = true, doc = "Only return PRs wher the branch name has head_prefix", defaultValue = "None"), @Param(name = "base_prefix", allowedTypes = { @ParamType(type = String.clreplaced), @ParamType(type = NoneType.clreplaced) }, named = true, doc = "Only return PRs where the destination branch name has base_prefix", defaultValue = "None"), @Param(name = "state", doc = "State of the Pull Request. Can be `\"OPEN\"`, `\"CLOSED\"` or `\"ALL\"`", defaultValue = "\"OPEN\"", named = true), @Param(name = "sort", doc = "Sort filter for retrieving the Pull Requests. Can be `\"CREATED\"`," + " `\"UPDATED\"` or `\"POPULARITY\"`", named = true, defaultValue = "\"CREATED\""), @Param(name = "direction", doc = "Direction of the filter. Can be `\"ASC\"` or `\"DESC\"`", defaultValue = "\"ASC\"", named = true) }, allowReturnNones = true)
    @Nullable
    public ImmutableList<PullRequest> getPullRequests(Object headPrefixParam, Object basePrefixParam, String state, String sort, String direction) throws EvalException, RepoException {
        try {
            String project = ghHost.getProjectNameFromUrl(url);
            PullRequestListParams request = PullRequestListParams.DEFAULT;
            String headPrefix = convertFromNoneable(headPrefixParam, null);
            String basePrefix = convertFromNoneable(basePrefixParam, null);
            if (!Strings.isNullOrEmpty(headPrefix)) {
                checkCondition(SAFE_BRANCH_NAME_PREFIX.matches(headPrefix), "'%s' is not a valid head_prefix (%s is used for validation)", headPrefix, SAFE_BRANCH_NAME_PREFIX.pattern());
                request = request.withHead(headPrefix);
            }
            if (!Strings.isNullOrEmpty(basePrefix)) {
                checkCondition(SAFE_BRANCH_NAME_PREFIX.matches(basePrefix), "'%s' is not a valid base_prefix (%s is used for validation)", basePrefix, SAFE_BRANCH_NAME_PREFIX.pattern());
                request = request.withHead(basePrefix);
            }
            return apiSupplier.load(console).getPullRequests(project, request.withState(stringToEnum("state", state, StateFilter.clreplaced)).withDirection(stringToEnum("direction", direction, DirectionFilter.clreplaced)).withSort(stringToEnum("sort", sort, SortFilter.clreplaced)));
        } catch (GitHubApiException e) {
            return returnNullOnNotFound(e);
        } catch (ValidationException | RuntimeException e) {
            throw Starlark.errorf("Error calling get_pull_requests: %s", e.getMessage());
        }
    }

    @StarlarkMethod(name = "update_pull_request", doc = "Update Pull Requests for a repo. Returns None if not found", parameters = { @Param(name = "number", named = true, doc = "Pull Request number"), @Param(name = "replacedle", allowedTypes = { @ParamType(type = String.clreplaced), @ParamType(type = NoneType.clreplaced) }, named = true, doc = "New Pull Request replacedle", defaultValue = "None"), @Param(name = "body", allowedTypes = { @ParamType(type = String.clreplaced), @ParamType(type = NoneType.clreplaced) }, named = true, doc = "New Pull Request body", defaultValue = "None"), @Param(name = "state", allowedTypes = { @ParamType(type = String.clreplaced), @ParamType(type = NoneType.clreplaced) }, doc = "State of the Pull Request. Can be `\"OPEN\"`, `\"CLOSED\"`", named = true, defaultValue = "None") }, allowReturnNones = true)
    @Nullable
    public PullRequest updatePullRequest(StarlarkInt number, Object replacedle, Object body, Object state) throws EvalException, RepoException {
        try {
            String project = ghHost.getProjectNameFromUrl(url);
            return apiSupplier.load(console).updatePullRequest(project, number.toInt("number"), new UpdatePullRequest(convertFromNoneable(replacedle, null), convertFromNoneable(body, null), stringToEnum("state", convertFromNoneable(state, null), UpdatePullRequest.State.clreplaced)));
        } catch (GitHubApiException e) {
            return returnNullOnNotFound(e);
        } catch (ValidationException | RuntimeException e) {
            throw Starlark.errorf("Error calling update_pull_request: %s", e.getMessage());
        }
    }

    @StarlarkMethod(name = "get_authenticated_user", doc = "Get autenticated user info, return null if not found", allowReturnNones = true)
    @Nullable
    public User getAuthenticatedUser() throws EvalException, RepoException {
        try {
            return apiSupplier.load(console).getAuthenticatedUser();
        } catch (GitHubApiException e) {
            return returnNullOnNotFound(e);
        } catch (ValidationException | RuntimeException e) {
            throw Starlark.errorf("Error calling get_authenticated_user: %s", e.getMessage());
        }
    }

    @Nullable
    private <T> T returnNullOnNotFound(GitHubApiException e) throws EvalException {
        SkylarkUtil.check(e.getResponseCode() == ResponseCode.NOT_FOUND, "%s", e.getMessage());
        return null;
    }

    @StarlarkMethod(name = "get_references", doc = "Get all the reference SHA-1s from GitHub. Note that Copybara only returns a maximum " + "number of 500.")
    public Sequence<Ref> getReferences() throws EvalException, RepoException {
        try {
            String project = ghHost.getProjectNameFromUrl(url);
            return StarlarkList.immutableCopyOf(apiSupplier.load(console).getReferences(project));
        } catch (ValidationException | RuntimeException e) {
            throw Starlark.errorf("Error calling get_references: %s", e.getMessage());
        }
    }

    @StarlarkMethod(name = "get_pull_request_comment", doc = "Get a pull request comment", parameters = { @Param(name = "comment_id", named = true, doc = "Comment identifier") })
    public PullRequestComment getPullRequestComment(String commentId) throws EvalException, RepoException {
        try {
            long commentIdLong;
            try {
                commentIdLong = Long.parseLong(commentId);
            } catch (NumberFormatException e) {
                throw Starlark.errorf("Invalid comment id %s: %s", commentId, e.getMessage());
            }
            String project = ghHost.getProjectNameFromUrl(url);
            return apiSupplier.load(console).getPullRequestComment(project, commentIdLong);
        } catch (ValidationException | RuntimeException e) {
            throw Starlark.errorf("Error calling get_pull_request_comment: %s", e.getMessage());
        }
    }

    @StarlarkMethod(name = "get_pull_request_comments", doc = "Get all pull request comments", parameters = { @Param(name = "number", named = true, doc = "Pull Request number") })
    public Sequence<PullRequestComment> getPullRequestComments(StarlarkInt prNumber) throws EvalException, RepoException {
        try {
            String project = ghHost.getProjectNameFromUrl(url);
            return StarlarkList.immutableCopyOf(apiSupplier.load(console).getPullRequestComments(project, prNumber.toInt("number")));
        } catch (ValidationException | RuntimeException e) {
            throw Starlark.errorf("Error calling get_pull_request_comments: %s", e.getMessage());
        }
    }

    @StarlarkMethod(name = "url", doc = "Return the URL of this endpoint.", structField = true)
    @Override
    public String getUrl() {
        return url;
    }

    @StarlarkMethod(name = "add_label", doc = "Add labels to a PR/issue", parameters = { @Param(name = "number", named = true, doc = "Pull Request number"), @Param(name = "labels", allowedTypes = { @ParamType(type = Sequence.clreplaced, generic1 = String.clreplaced) }, named = true, doc = "List of labels to add.") })
    public void addLabels(StarlarkInt prNumber, Sequence<?> labels) throws EvalException, RepoException {
        try {
            String project = ghHost.getProjectNameFromUrl(url);
            // Swallow response, until a use-case for returning it surfaces.
            apiSupplier.load(console).addLabels(project, prNumber.toInt("number"), SkylarkUtil.convertStringList(labels, "Expected list of GitHub label names."));
        } catch (ValidationException | RuntimeException e) {
            throw Starlark.errorf("Error calling add_label: %s", e.getMessage());
        }
    }

    @StarlarkMethod(name = "post_issue_comment", doc = "Post a comment on a issue.", parameters = { @Param(name = "number", named = true, doc = "Issue or Pull Request number"), @Param(name = "comment", named = true, doc = "Comment body to post.") })
    public void postIssueComment(StarlarkInt prNumber, String comment) throws EvalException, RepoException {
        try {
            String project = ghHost.getProjectNameFromUrl(url);
            apiSupplier.load(console).postComment(project, prNumber.toInt("number"), comment);
        } catch (ValidationException | RuntimeException e) {
            throw Starlark.errorf("Error calling post_issue_comment: %s", e.getMessage());
        }
    }

    @Override
    public GitHubEndPoint withConsole(Console console) {
        return new GitHubEndPoint(this.apiSupplier, this.url, console, ghHost);
    }

    @Override
    public ImmutableSetMultimap<String, String> describe() {
        return ImmutableSetMultimap.of("type", "github_api", "url", url);
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this).add("url", url).toString();
    }
}

18 Source : PaginatedList.java
with Apache License 2.0
from google

/**
 * A List that contains additional information on how to fetch the next/prev page.
 */
public clreplaced PaginatedList<T> extends ArrayList<T> {

    private static final Pattern LINK_HEADER_PATTERN = Pattern.compile("<([^>]+)>; rel=\"([a-z]+)\"");

    @Nullable
    private final String nextUrl;

    @Nullable
    private final String prevUrl;

    @Nullable
    private final String lastUrl;

    @Nullable
    private final String firstUrl;

    @SuppressWarnings("unused")
    public PaginatedList() {
        this(ImmutableList.of(), null, null, null, null);
    }

    private PaginatedList(Collection<T> elements, @Nullable String firstUrl, @Nullable String prevUrl, @Nullable String nextUrl, @Nullable String lastUrl) {
        super(elements);
        this.firstUrl = firstUrl;
        this.prevUrl = prevUrl;
        this.nextUrl = nextUrl;
        this.lastUrl = lastUrl;
    }

    @Nullable
    public String getNextUrl() {
        return nextUrl;
    }

    @Nullable
    public String getPrevUrl() {
        return prevUrl;
    }

    @Nullable
    public String getLastUrl() {
        return lastUrl;
    }

    @Nullable
    public String getFirstUrl() {
        return firstUrl;
    }

    /**
     * Return a PaginatedList with the next/last/etc. fields populated if linkHeader is not null.
     */
    public PaginatedList<T> withPaginationInfo(String apiPrefix, @Nullable String linkHeader) {
        if (linkHeader == null) {
            return this;
        }
        String next = null;
        String prev = null;
        String last = null;
        String first = null;
        for (String e : Splitter.on(',').trimResults().split(linkHeader)) {
            Matcher matcher = LINK_HEADER_PATTERN.matcher(e);
            Verify.verify(matcher.matches(), "'%s' doesn't match Link regex. Header: %s", e, linkHeader);
            String url = matcher.group(1);
            String rel = matcher.group(2);
            Verify.verify(url.startsWith(apiPrefix));
            url = url.substring(apiPrefix.length());
            switch(rel) {
                case "first":
                    first = url;
                    break;
                case "prev":
                    prev = url;
                    break;
                case "next":
                    next = url;
                    break;
                case "last":
                    last = url;
                    break;
                // fall out
                default:
            }
        }
        return new PaginatedList<>(this, first, prev, next, last);
    }
}

18 Source : GerritIntegrateLabel.java
with Apache License 2.0
from google

/**
 * Integrate label for Gerrit changes
 *
 * <p>Returns a string like:
 *
 * <ul>
 *   <li>"Gerrit https://example.com/project 1271"</li>
 *   </li>"Gerrit https://example.com/project 1271 5"</li>
 *   </li>"Gerrit https://example.com/project 1271 ChangeId"</li>
 *   </li>"Gerrit https://example.com/project 1271 5 ChangeId"</li>
 *   </ul>
 * Where both the PatchSet and ChangeId are optional.
 */
clreplaced GerritIntegrateLabel implements IntegrateLabel {

    private static final Pattern LABEL_PATTERN = Pattern.compile("gerrit ([^ ]+)" + " ([0-9]+)(?: Patch Set ([0-9]+))?(?: (I[a-f0-9]+))?");

    private final GitRepository repository;

    private final GeneralOptions generalOptions;

    private final String url;

    private final int changeNumber;

    @Nullable
    private Integer patchSet;

    @Nullable
    private final String changeId;

    GerritIntegrateLabel(GitRepository repository, GeneralOptions generalOptions, String url, int changeNumber, @Nullable Integer patchSet, @Nullable String changeId) {
        this.repository = Preconditions.checkNotNull(repository);
        this.generalOptions = Preconditions.checkNotNull(generalOptions);
        this.url = Preconditions.checkNotNull(url);
        this.changeNumber = changeNumber;
        this.patchSet = patchSet;
        this.changeId = changeId;
    }

    @Nullable
    static GerritIntegrateLabel parse(String str, GitRepository repository, GeneralOptions generalOptions) {
        Matcher matcher = LABEL_PATTERN.matcher(str);
        return matcher.matches() ? new GerritIntegrateLabel(repository, generalOptions, matcher.group(1), Integer.parseInt(matcher.group(2)), (matcher.group(3) == null ? null : Integer.parseInt(matcher.group(3))), matcher.group(4)) : null;
    }

    @Override
    public String toString() {
        return String.format("gerrit %s %d%s%s", url, changeNumber, patchSet != null ? " Patch Set " + patchSet : "", changeId != null ? " " + changeId : "");
    }

    @Override
    public String mergeMessage(ImmutableList<LabelFinder> labelsToAdd) {
        if (changeId != null) {
            labelsToAdd = ImmutableList.<LabelFinder>builder().addAll(labelsToAdd).add(new LabelFinder("Change-Id: " + changeId)).build();
        }
        return IntegrateLabel.withLabels("Merge Gerrit change " + changeNumber + (patchSet == null ? "" : " Patch Set " + patchSet), labelsToAdd);
    }

    @Override
    public GitRevision getRevision() throws RepoException, ValidationException {
        int latestPatchSet = GerritChange.getGerritPatchSets(repository, url, changeNumber).lastEntry().getKey();
        if (patchSet == null) {
            patchSet = latestPatchSet;
        } else if (latestPatchSet > patchSet) {
            generalOptions.console().warnFmt("Change %s has more patch sets after Patch Set %s. Latest is Patch Set %s." + " Not all changes might be migrated", changeNumber, patchSet, latestPatchSet);
        }
        return GitRepoType.GERRIT.resolveRef(repository, url, String.format("refs/changes/%02d/%d", changeNumber % 100, changeNumber) + "/" + patchSet, generalOptions, /*describeVersion=*/
        false, /*partialFetch=*/
        false);
    }
}

18 Source : GlobalMigrations.java
with Apache License 2.0
from google

@StarlarkBuiltin(name = GlobalMigrations.GLOBAL_MIGRATIONS, doc = "Global variable that holds the registered migrations in the config files", doreplacedented = false)
public clreplaced GlobalMigrations implements StarlarkValue {

    private static final Pattern MIGRATION_NAME_FORMAT = Pattern.compile("[a-zA-Z0-9_\\-\\./]+");

    static final String GLOBAL_MIGRATIONS = "global_migrations";

    private final Map<String, Migration> migrations = new HashMap<>();

    public static GlobalMigrations getGlobalMigrations(Module module) {
        return (GlobalMigrations) Objects.requireNonNull(module.getPredeclaredBindings().get(GLOBAL_MIGRATIONS));
    }

    public Map<String, Migration> getMigrations() {
        return migrations;
    }

    public void addMigration(String name, Migration migration) throws EvalException {
        checkMigrationName(name);
        check(migrations.put(name, migration) == null, "A migration with the name '%s' is already defined", name);
    }

    /**
     * Checks if a migration name conforms to the expected format.
     *
     * @param name Migration name
     * @throws EvalException If the name does not conform to the expected format
     */
    public static void checkMigrationName(String name) throws EvalException {
        check(MIGRATION_NAME_FORMAT.matches(name), "Migration name '%s' doesn't conform to expected pattern: %s", name, MIGRATION_NAME_FORMAT.pattern());
    }
}

18 Source : ProjectClassToRuleResolverTest.java
with Apache License 2.0
from bazelbuild

/**
 * Tests for {@link ProjectClreplacedToRuleResolver}.
 */
@RunWith(JUnit4.clreplaced)
public clreplaced ProjectClreplacedToRuleResolverTest {

    private Path workspace;

    private static final Pattern WHITELIST_DEFAULT = Pattern.compile(".*");

    @Before
    public void setUp() throws IOException {
        FileSystem fileSystem = createDefaultFileSystem();
        workspace = fileSystem.getPath("/src");
    }

    /**
     * Tests behavior of resolver on a graph where each clreplaced is uniquely mapped to a target.
     * Resulting map should have each clreplaced uniquely mapped to a build rule.
     */
    @Test
    public void simpleDAGWithUniqueTargets() throws IOException {
        MutableGraph<String> clreplacedgraph = newGraph(String.clreplaced);
        clreplacedgraph.putEdge("com.A", "com.B");
        clreplacedgraph.putEdge("com.B", "com.C");
        ProjectClreplacedToRuleResolver resolver = newResolver(clreplacedgraph, WHITELIST_DEFAULT, ImmutableMap.of("com.A", workspace.resolve("java/com/A.java"), "com.B", workspace.resolve("java/com/B.java"), "com.C", workspace.resolve("java/com/C.java")));
        ImmutableMap<String, BuildRule> actual = resolver.resolve(clreplacedgraph.nodes());
        String packagePath = "/src/java/com/";
        BuildRule ruleA = buildRule(packagePath, "/src/java/com/A.java");
        BuildRule ruleB = buildRule(packagePath, "/src/java/com/B.java");
        BuildRule ruleC = buildRule(packagePath, "/src/java/com/C.java");
        replacedertThat(actual).containsExactly("com.A", ruleA, "com.B", ruleB, "com.C", ruleC);
    }

    /**
     * Tests behavior of resolver on a graph where each clreplaced is uniquely mapped to a target, AND only
     * a subset of clreplacedes have been asked to be resolved. Resulting map should have each clreplaced, in
     * the provided list, uniquely mapped to a build rule.
     */
    @Test
    public void filteredDAGWithUniqueTargets() throws IOException {
        MutableGraph<String> clreplacedgraph = newGraph(String.clreplaced);
        clreplacedgraph.putEdge("com.A", "com.B");
        ProjectClreplacedToRuleResolver resolver = newResolver(clreplacedgraph, WHITELIST_DEFAULT, ImmutableMap.of("com.A", workspace.resolve("java/com/A.java"), "com.B", workspace.resolve("java/com/B.java")));
        ImmutableMap<String, BuildRule> actual = resolver.resolve(ImmutableSet.of("com.A"));
        String packagePath = "/src/java/com/";
        replacedertThat(actual).containsExactly("com.A", buildRule(packagePath, "/src/java/com/A.java"));
        replacedertThat(actual).doesNotContainKey("com.B");
    }

    /**
     * Tests behavior of resolver on a graph where all clreplacedes map to the same target. Resulting map
     * should have each clreplaced map to the same build rule.
     */
    @Test
    public void graphWithOnlyOneTarget() throws IOException {
        MutableGraph<String> clreplacedgraph = newGraph(String.clreplaced);
        clreplacedgraph.putEdge("com.A", "com.B");
        clreplacedgraph.putEdge("com.B", "com.C");
        clreplacedgraph.putEdge("com.C", "com.A");
        ProjectClreplacedToRuleResolver resolver = newResolver(clreplacedgraph, WHITELIST_DEFAULT, ImmutableMap.of("com.A", workspace.resolve("java/com/A.java"), "com.B", workspace.resolve("java/com/B.java"), "com.C", workspace.resolve("java/com/C.java")));
        ImmutableMap<String, BuildRule> actual = resolver.resolve(clreplacedgraph.nodes());
        replacedertThat(actual).containsKey("com.A");
        replacedertThat(actual.get("com.A")).isEqualTo(actual.get("com.B"));
        replacedertThat(actual.get("com.B")).isEqualTo(actual.get("com.C"));
    }

    /**
     * Tests behavior of resolver on a graph when an inner clreplaced is provided. Resolver should throw an
     * Illegal State Exception in this event.
     */
    @Test
    public void graphWithInnerClreplaced() throws IOException {
        MutableGraph<String> clreplacedgraph = newGraph(String.clreplaced);
        clreplacedgraph.putEdge("com.A$hello", "com.B");
        try {
            newResolver(clreplacedgraph, WHITELIST_DEFAULT, ImmutableMap.of());
            fail("Expected an exception, but nothing was thrown.");
        } catch (IllegalStateException e) {
            replacedertThat(e).hasMessageThat().isEqualTo("Argument contained inner clreplaced name com.A$hello but expected no inner clreplacedes");
        }
    }

    /**
     * Tests behavior of resolver when there are no clreplacedes that match white list pattern. The
     * resulting map should contain no entries.
     */
    @Test
    public void noMatchingClreplacedNames() throws IOException {
        MutableGraph<String> clreplacedgraph = newGraph(String.clreplaced);
        clreplacedgraph.putEdge("com.A", "com.B");
        clreplacedgraph.putEdge("com.B", "com.C");
        ProjectClreplacedToRuleResolver resolver = newResolver(clreplacedgraph, Pattern.compile("com.hello.*"), ImmutableMap.of("com.A", workspace.resolve("java/com/A.java"), "com.B", workspace.resolve("java/com/B.java"), "com.C", workspace.resolve("java/com/C.java")));
        ImmutableMap<String, BuildRule> actual = resolver.resolve(clreplacedgraph.nodes());
        replacedertThat(actual).isEmpty();
    }

    /**
     * If a clreplaced's source file cannot be found, then that clreplaced name should not be in the ensuing
     * clreplaced. Since less than the default threshold are unresolved, BFG should NOT error out.
     */
    @Test
    public void ignoresUnresolvedClreplaced_smallerThanThreshold() throws IOException {
        MutableGraph<String> clreplacedgraph = newGraph(String.clreplaced);
        clreplacedgraph.putEdge("com.A", "com.DoesNotExist");
        ProjectClreplacedToRuleResolver resolver = newResolver(clreplacedgraph, WHITELIST_DEFAULT, ImmutableMap.of("com.A", workspace.resolve("java/com/A.java")));
        ImmutableMap<String, BuildRule> actual = resolver.resolve(clreplacedgraph.nodes());
        replacedertThat(actual).containsExactly("com.A", buildRule("/src/java/com/", "/src/java/com/A.java"));
        replacedertThat(actual).doesNotContainKey("com.DoesNotExist");
    }

    /**
     * If a clreplaced's source file cannot be found, then that clreplaced name should not be in the ensuing
     * clreplaced. If more than the default threshold are unresolved, then it BFG should error out.
     */
    @Test
    public void ignoresUnresolvedClreplaced_exceedsThreshold() throws IOException {
        MutableGraph<String> clreplacedgraph = newGraph(String.clreplaced);
        clreplacedgraph.putEdge("com.A", "com.DoesNotExist");
        clreplacedgraph.putEdge("com.A", "com.DoesNotExistTwo");
        clreplacedgraph.putEdge("com.A", "com.DoesNotExistThree");
        ProjectClreplacedToRuleResolver resolver = newResolver(clreplacedgraph, WHITELIST_DEFAULT, ImmutableMap.of("com.A", workspace.resolve("java/com/A.java")));
        try {
            resolver.resolve(clreplacedgraph.nodes());
            fail("Expected an exception, but nothing was thrown.");
        } catch (IllegalStateException e) {
            replacedertThat(e).hasMessageThat().isEqualTo(String.format("BUILD File Generator failed to map over %.0f percent of clreplaced names. " + "Check your white list and content roots", UNRESOLVED_THRESHOLD * 100));
        }
    }

    /**
     * Constructs a ProjectClreplacedToRuleResolver using default workspace path and arguments
     */
    private ProjectClreplacedToRuleResolver newResolver(Graph<String> clreplacedGraph, Pattern whiteList, ImmutableMap<String, Path> clreplacedToFile) {
        return new ProjectClreplacedToRuleResolver(ImmutableGraph.copyOf(clreplacedGraph), whiteList, clreplacedToFile, workspace);
    }

    /**
     * Constructs a ProjectBuildRule using default workspace path
     */
    private ProjectBuildRule buildRule(String packagePathString, String... srcFilePaths) {
        Path packagePath = workspace.resolve(packagePathString);
        ImmutableSet<Path> srcFiles = Arrays.stream(srcFilePaths).map(srcFile -> packagePath.resolve(srcFile)).collect(toImmutableSet());
        return new ProjectBuildRule(srcFiles, packagePath, workspace);
    }

    private static FileSystem createDefaultFileSystem() {
        return Jimfs.newFileSystem(Configuration.forCurrentPlatform().toBuilder().build());
    }

    private <T> MutableGraph<T> newGraph(Clreplaced<T> unusedClazz) {
        return GraphBuilder.directed().allowsSelfLoops(false).build();
    }
}

18 Source : ClassGraphPreprocessorTest.java
with Apache License 2.0
from bazelbuild

/**
 * Tests for {@link ClreplacedGraphPreprocessor}.
 */
@RunWith(JUnit4.clreplaced)
public clreplaced ClreplacedGraphPreprocessorTest {

    /**
     * Regular expression to match everything
     */
    private static final Pattern EVERYTHING = Pattern.compile(".*");

    /**
     * Regular expression to match nothing.
     */
    private static final Pattern NOTHING = Pattern.compile("a^");

    /**
     * Tests whether the black listed clreplaced names are removed from the clreplaced graph.
     */
    @Test
    public void trimRemovesBlackListedClreplacedes() {
        MutableGraph<String> graph = newGraph();
        graph.putEdge("com.BlackList", "com.WhiteList");
        graph.putEdge("com.WhiteList", "com.BlackList");
        Pattern blackList = Pattern.compile("BlackList");
        Graph<String> actual = preProcessClreplacedGraph(ImmutableGraph.copyOf(graph), EVERYTHING, blackList);
        MutableGraph<String> expected = newGraph();
        expected.addNode("com.WhiteList");
        replacedertEquivalent(actual, expected);
        replacedertThat(actual.nodes()).doesNotContain("com.BlackList");
    }

    /**
     * Tests whether after trimming the clreplaced graph, the direction of each edge is maintained. If in
     * the original graph a->b, then the resulting graph should only contain a->b and not b->a.
     */
    @Test
    public void trimMaintainsEdgeDirection() {
        MutableGraph<String> graph = newGraph();
        graph.putEdge("com.src", "com.dst");
        Graph<String> actual = preProcessClreplacedGraph(ImmutableGraph.copyOf(graph), EVERYTHING, NOTHING);
        MutableGraph<String> expected = newGraph();
        expected.putEdge("com.src", "com.dst");
        replacedertEquivalent(actual, expected);
        replacedertThat(actual.edges()).doesNotContain(EndpointPair.ordered("com.dst", "com.src"));
    }

    /**
     * replacederts that the only nodes in the trimmed graph are white listed clreplacedes and clreplacedes that the
     * white listed clreplacedes are directly dependent on.
     */
    @Test
    public void trimRemovesTransitiveDependenciesToNonWhiteListedClreplacedes() {
        MutableGraph<String> graph = newGraph();
        graph.putEdge("com.WhiteList", "com.OtherList");
        graph.putEdge("com.OtherList", "com.TransitiveDependency");
        Pattern whiteList = Pattern.compile("WhiteList");
        Graph<String> actual = preProcessClreplacedGraph(ImmutableGraph.copyOf(graph), whiteList, NOTHING);
        MutableGraph<String> expected = newGraph();
        expected.putEdge("com.WhiteList", "com.OtherList");
        replacedertEquivalent(actual, expected);
        replacedertThat(actual.nodes()).doesNotContain("com.TransitiveDependency");
    }

    /**
     * Tests whether black listed clreplacedes names as well as their non-whitelisted dependencies are
     * removed from the clreplaced graph. In addition to not containing the black listed clreplaced, the
     * resulting graph should also not contain nonwhite listed clreplacedes only black listed clreplacedes are
     * dependent on. For example, say we were to have the following clreplaced graph
     *
     * <p>com.Whitelist --> com.Blacklist --> com.NonWhitelist
     *
     * <p>Then the resulting clreplaced graph should only contain com.Whitelist
     */
    @Test
    public void trimRemovesTransitiveDepsOfBlackListedClreplacedes() {
        MutableGraph<String> graph = newGraph();
        graph.putEdge("com.BlackList", "com.OtherList");
        graph.putEdge("com.WhiteList", "com.BlackList");
        Pattern blackList = Pattern.compile("BlackList");
        Pattern whiteList = Pattern.compile("WhiteList");
        Graph<String> actual = preProcessClreplacedGraph(ImmutableGraph.copyOf(graph), whiteList, blackList);
        MutableGraph<String> expected = newGraph();
        expected.addNode("com.WhiteList");
        replacedertEquivalent(actual, expected);
        replacedertThat(actual.nodes()).doesNotContain("com.BlackList");
        replacedertThat(actual.nodes()).doesNotContain("com.OtherList");
    }

    /**
     * Ensures there are no inner clreplacedes outputted in preprocessed clreplaced graph
     */
    @Test
    public void collapseRemovesInnerClreplacedes() {
        MutableGraph<String> graph = newGraph();
        graph.putEdge("Clreplaced", "Clreplaced$Inner");
        graph.putEdge("Clreplaced$Inner", "Clreplaced");
        Graph<String> actual = preProcessClreplacedGraph(ImmutableGraph.copyOf(graph), EVERYTHING, NOTHING);
        replacedertThat(actual.nodes()).containsExactly("Clreplaced");
        replacedertThat(actual.edges()).isEmpty();
    }

    /**
     * Ensures the inner clreplacedes' dependencies on other clreplacedes are retained. For example, if an inner
     * clreplaced depends on a clreplaced A. Then, in the resulting graph, the outer clreplaced must depend on the
     * outer clreplaced of A.
     */
    @Test
    public void collapseMaintainsDepsOfInnerClreplacedToOtherClreplacedes() {
        MutableGraph<String> graph = newGraph();
        graph.putEdge("Clreplaced", "Clreplaced$Inner");
        graph.putEdge("Clreplaced$Inner", "OtherClreplaced");
        Graph<String> actual = preProcessClreplacedGraph(ImmutableGraph.copyOf(graph), EVERYTHING, NOTHING);
        replacedertThat(actual.nodes()).containsExactly("Clreplaced", "OtherClreplaced");
        replacedertThat(actual.edges()).containsExactly(EndpointPair.ordered("Clreplaced", "OtherClreplaced"));
    }

    /**
     * Ensures the dependencies of other clreplacedes on inner clreplacedes are retained. For example, if a
     * clreplaced A depends on an inner clreplaced B$Inner. Then, in the resulting graph, clreplaced A must depend on
     * clreplaced B.
     */
    @Test
    public void collapseMaintainsDepsOfOtherClreplacedesOnInnerClreplacedes() {
        MutableGraph<String> graph = newGraph();
        graph.putEdge("Clreplaced", "Clreplaced$Inner");
        graph.putEdge("OtherClreplaced", "Clreplaced$Inner");
        Graph<String> actual = preProcessClreplacedGraph(ImmutableGraph.copyOf(graph), EVERYTHING, NOTHING);
        replacedertThat(actual.nodes()).containsExactly("OtherClreplaced", "Clreplaced");
        replacedertThat(actual.edges()).containsExactly(EndpointPair.ordered("OtherClreplaced", "Clreplaced"));
    }

    /**
     * Returns a Directed Mutable Graph
     */
    private MutableGraph<String> newGraph() {
        return GraphBuilder.directed().build();
    }

    /**
     * The test graph is considered equal to control graph iff the two graphs have matching edge lists
     * and node lists.
     */
    private void replacedertEquivalent(Graph<String> actual, Graph<String> expected) {
        replacedertThat(actual.edges()).containsExactlyElementsIn(expected.edges());
        replacedertThat(actual.nodes()).containsExactlyElementsIn(expected.nodes());
    }
}

18 Source : ClassGraphPreprocessor.java
with Apache License 2.0
from bazelbuild

/**
 * Removes all outgoing edges from clreplacedes that are not white listed.
 */
private static ImmutableGraph<String> trimClreplacedGraph(ImmutableGraph<String> clreplacedGraph, Pattern whiteList, Pattern blackList) {
    MutableGraph<String> graph = GraphBuilder.directed().allowsSelfLoops(false).build();
    for (String src : clreplacedGraph.nodes()) {
        if (!whiteList.matcher(src).find() || blackList.matcher(src).find()) {
            continue;
        }
        graph.addNode(src);
        for (String dst : clreplacedGraph.successors(src)) {
            if (blackList.matcher(dst).find()) {
                continue;
            }
            graph.putEdge(src, dst);
        }
    }
    return ImmutableGraph.copyOf(graph);
}

17 Source : BufrTables.java
with BSD 3-Clause "New" or "Revised" License
from Unidata

/**
 * Reads BUFR tables of various forms. Interacts with TableLookup.
 *
 * <pre>
 * Table B:
 *
 * csv----------
 * Clreplaced,FXY,enElementName,BUFR_Unit,BUFR_Scale,BUFR_ReferenceValue,BUFR_DataWidth_Bits,CREX_Unit,CREX_Scale,CREX_DataWidth,Status
 * 00,000001,Table A: entry,CCITT IA5,0,0,24,Character,0,3,Operational
 *
 * ecmwf---------
 * 000001 TABLE A:  ENTRY                                                  CCITTIA5                   0            0  24 CHARACTER                 0          3
 * 000001 TABLE A:  ENTRY                                                  CCITTIA5                   0            0  24 CHARACTER                 0         3
 *
 * mel-bufr-----------
 * 0; 7; 190; 1; -1024; 12; M; HEIGHT INCREMENT
 *
 * mel-tabs (tab delimited) ---------------
 * F  X  Y  Scale  RefVal  Width  Units  Element Name
 * 0  0  1  0  0  24  CCITT_IA5  Table A: entry
 * 0  0  2  0  0  256  CCITT_IA5  Table A: data category description, line 1
 *
 * ncep-----------
 * #====================================================================================================
 * # F-XX-YYY |SCALE| REFERENCE   | BIT |      UNIT      | MNEMONIC ;DESC ;  ELEMENT NAME
 * #          |     |   VALUE     |WIDTH|                |          ;CODE ;
 * #====================================================================================================
 * 0-00-001 |   0 |           0 |  24 | CCITT IA5      | TABLAE   ;     ; Table A: entry
 *
 * opera ----------------------------
 * 0;02;181;Supplementary present weather sensor;Flag-Table;0;0;21
 * 0;07;192;Pixel size in Z-direction;Meters;-1;0;16
 * 0;21;036;Radar rainfall intensity;mm*h-1;2;0;16
 *
 * ===============================================================
 * Table D:
 * csv----------
 * SNo,Category,FXY1,enElementName1,FXY2,enElementName2,Status
 * 1,00,300002,,000002,"Table A category, line 1",Operational
 *
 * ecmwf-------------
 * 300002  2 000002
 * 000003
 * 300003  3 000010
 * 000011
 * 000012
 *
 * mel-bufr------------
 * 3   1 192  optional_name
 * 0   1   7
 * 0  25  60
 * 0   1  33
 * 1   1   2
 * 3  61 169
 * 0   5  40
 * -1
 *
 * ncep ------------------
 * #====================================================================================================
 * # F-XX-YYY | MNEMONIC   ;DCOD ; NAME           <-- sequence definition
 * #          | F-XX-YYY > | NAME                 <-- element definition (first thru next-to-last)
 * #          | F-XX-YYY   | NAME                 <-- element definition (last)
 * #====================================================================================================
 *
 * 3-00-002 | TABLACAT   ;     ; Table A category definition
 * | 0-00-002 > | Table A category, line 1
 * | 0-00-003   | Table A category, line 2
 *
 * opera ---------------
 * # Heights of side view
 * 3;13;192;  1;01;000
 * ;  ;   ;  0;31;001
 * ;  ;   ;  0;10;007
 * # 4 bit per pixel radar images (top view)
 * 3;21;192;  1;10;000
 * ...
 *
 * </pre>
 */
public clreplaced BufrTables {

    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(BufrTables.clreplaced);

    public enum Mode {

        // wmo entries only found from wmo table
        wmoOnly,
        // if wmo entries not found in wmo table, look in local table
        wmoLocal,
        // look in local first, then wmo
        localOverride
    }

    public enum Format {

        ecmwf,
        mel_bufr,
        mel_tabs,
        ncep,
        ncep_nm,
        opera,
        ukmet,
        wmo_csv,
        wmo_xml,
        cypher,
        embed
    }

    static final String RESOURCE_PATH = "/resources/bufrTables/";

    private static final String canonicalLookup = "resource:" + RESOURCE_PATH + "local/tablelookup.csv";

    private static final int latestVersion = 19;

    private static boolean canonicalTableRead = false;

    private static final boolean showTables = false;

    private static final boolean showReadErrs = true;

    private static final List<TableConfig> tables = new ArrayList<>();

    private static final Map<String, TableB> tablesB = new ConcurrentHashMap<>();

    private static final Map<String, TableD> tablesD = new ConcurrentHashMap<>();

    // Called with reflection from RuntimeConfigParser
    public static synchronized void addLookupFile(String filename) throws IOException {
        if (!filename.startsWith("resource:")) {
            File f = new File(filename);
            if (!f.exists()) {
                throw new FileNotFoundException(filename + " not found");
            }
        }
        try {
            readLookupTable(filename);
        } catch (Throwable t) {
            throw new IOException(filename + " got error", t);
        }
    }

    private static synchronized void readCanonicalLookup() {
        if (canonicalTableRead) {
            return;
        }
        readLookupTable(canonicalLookup);
        canonicalTableRead = true;
    }

    // center,subcenter,master,local,cat,tableB,tableBformat,tableD,tableDformat, mode
    private static void readLookupTable(String filename) {
        try (InputStream ios = openStream(filename)) {
            BufferedReader dataIS = new BufferedReader(new InputStreamReader(ios, StandardCharsets.UTF_8));
            int count = 0;
            while (true) {
                String line = dataIS.readLine();
                if (line == null)
                    break;
                if (line.startsWith("#"))
                    continue;
                count++;
                String[] flds = line.split(",");
                if (flds.length < 8) {
                    log.warn(String.format("%d BAD line == '%s' filename=%s%n", count, line, filename));
                    continue;
                }
                int fldidx = 0;
                try {
                    TableConfig table = new TableConfig();
                    table.name = flds[fldidx++].trim();
                    table.center = Integer.parseInt(flds[fldidx++].trim());
                    table.subcenter = Integer.parseInt(flds[fldidx++].trim());
                    table.master = Integer.parseInt(flds[fldidx++].trim());
                    table.local = Integer.parseInt(flds[fldidx++].trim());
                    table.cat = Integer.parseInt(flds[fldidx++].trim());
                    table.tableBname = flds[fldidx++].trim();
                    table.tableBformat = getFormat(flds[fldidx++].trim(), line);
                    if (table.tableBformat == null)
                        continue;
                    table.tableDname = flds[fldidx++].trim();
                    table.tableDformat = getFormat(flds[fldidx++].trim(), line);
                    if (fldidx < flds.length) {
                        String modes = flds[fldidx++].trim();
                        if (modes.equalsIgnoreCase("wmoLocal"))
                            table.mode = Mode.wmoLocal;
                        else if (modes.equalsIgnoreCase("localWmo"))
                            table.mode = Mode.localOverride;
                    }
                    tables.add(table);
                    if (showTables)
                        System.out.printf("Added table == %s%n", table);
                } catch (Exception e) {
                    log.warn(String.format("%d %d BAD line == '%s' filename=%s (%s)%n", count, fldidx, line, filename, e.getMessage()));
                }
            }
        } catch (IOException ioe) {
            String mess = "BUFR readLookupTable failed on " + filename;
            throw new RuntimeException(mess, ioe);
        }
    }

    private static Format getFormat(String formatS, String line) {
        if (formatS.isEmpty())
            return null;
        try {
            return Format.valueOf(formatS);
        } catch (Exception e) {
            if (showReadErrs)
                log.warn("BAD format = {} line == {}%n", formatS, line);
            return null;
        }
    }

    // //////////////////////////////////////////////////////////////////////////////////
    public static clreplaced TableConfig {

        String name;

        int center, subcenter, master, local, cat;

        String tableBname, tableDname;

        Format tableBformat, tableDformat;

        Mode mode = Mode.localOverride;

        boolean matches(int center, int subcenter, int master, int local, int cat) {
            if ((this.center >= 0) && (center >= 0) && (center != this.center))
                return false;
            if ((this.subcenter >= 0) && (subcenter >= 0) && (subcenter != this.subcenter))
                return false;
            if ((this.master >= 0) && (master >= 0) && (master != this.master))
                return false;
            if ((this.local >= 0) && (local >= 0) && (local != this.local))
                return false;
            return (this.cat < 0) || (cat < 0) || (cat == this.cat);
        }

        public String getName() {
            return name;
        }

        public Format getTableBformat() {
            return tableBformat;
        }

        public Format getTableDformat() {
            return tableDformat;
        }

        public Mode getMode() {
            return mode;
        }

        public String getTableDname() {
            return tableDname;
        }

        public String getTableBname() {
            return tableBname;
        }

        @Override
        public String toString() {
            return name;
        }
    }

    public static List<TableConfig> getTables() {
        readCanonicalLookup();
        return tables;
    }

    public static TableConfig[] getTableConfigsAsArray() {
        readCanonicalLookup();
        TableConfig[] result = new TableConfig[tables.size()];
        return tables.toArray(result);
    }

    private static TableConfig matchTableConfig(int center, int subcenter, int master, int local, int cat) {
        readCanonicalLookup();
        for (TableConfig tc : tables) {
            if (tc.matches(center, subcenter, master, local, cat))
                return tc;
        }
        return null;
    }

    /*
   * private static TableConfig matchTableConfig(BufrIdentificationSection ids) {
   * if (tables == null) readLookupTable();
   * 
   * int center = ids.getCenterId();
   * int subcenter = ids.getSubCenterId();
   * int master = ids.getMasterTableVersion();
   * int local = ids.getLocalTableVersion();
   * int cat = ids.getCategory();
   * 
   * return matchTableConfig(center, subcenter, master, local, cat);
   * }
   */
    // /////////////////////
    /*
   * Note Robb has this cleanup in DescriptorTableB
   * desc = desc.replaceFirst( "\\w+\\s+TABLE B ENTRY( - )?", "" );
   * desc = desc.trim();
   * this.description = desc;
   * desc = desc.replaceAll( "\\s*\\(.*\\)", "" );
   * desc = desc.replaceAll( "\\*", "" );
   * desc = desc.replaceAll( "\\s+", "_" );
   * desc = desc.replaceAll( "\\/", "_or_" );
   * desc = desc.replaceFirst( "1-", "One-" );
   * desc = desc.replaceFirst( "2-", "Two-" );
   */
    public static clreplaced Tables {

        public TableB b;

        public TableD d;

        public Mode mode;

        Tables() {
        }

        Tables(TableB b, TableD d, Mode mode) {
            this.b = b;
            this.d = d;
            this.mode = (mode == null) ? Mode.wmoOnly : mode;
        }
    }

    public static Tables getLocalTables(int center, int subcenter, int master, int local, int cat) throws IOException {
        TableConfig tc = matchTableConfig(center, subcenter, master, local, cat);
        if (tc == null)
            return null;
        if (tc.tableBformat == Format.ncep_nm) {
            // LOOK ??
            // see if we already have it
            TableB b = tablesB.get(tc.tableBname);
            TableD d = tablesD.get(tc.tableBname);
            if ((b != null) && (d != null))
                return new Tables(b, d, tc.mode);
            // read it
            b = new TableB(tc.tableBname, tc.tableBname);
            d = new TableD(tc.tableBname, tc.tableBname);
            Tables t = new Tables(b, d, tc.mode);
            try (InputStream ios = openStream(tc.tableBname)) {
                NcepMnemonic.read(ios, t);
            }
            // cache
            // replacedume we would get the same table in any thread, so race condition is ok
            tablesB.put(tc.tableBname, t.b);
            // replacedume we would get the same table in any thread, so race condition is ok
            tablesD.put(tc.tableBname, t.d);
            return t;
        }
        Tables tables = new Tables();
        tables.b = readTableB(tc.tableBname, tc.tableBformat, false);
        tables.d = readTableD(tc.tableDname, tc.tableDformat, false);
        tables.mode = tc.mode;
        return tables;
    }

    // //////////////////////////////////////////////////
    private static TableB latestWmoB;

    public static synchronized TableB getWmoTableBlatest() {
        if (latestWmoB == null) {
            try {
                latestWmoB = getWmoTableB(latestVersion);
            } catch (IOException ioe) {
                log.error("Cant open latest WMO ", ioe);
                throw new RuntimeException(ioe);
            }
        }
        return latestWmoB;
    }

    /*
   * // private static final String version14 = "wmo.v14";
   * static public TableB getWmoTableBold(int version) throws IOException {
   * String version13 = "wmo.v13.composite";
   * String tableName = (version == 14) ? version14 : version13;
   * TableB tb = tablesB.get(tableName);
   * if (tb != null) return tb;
   * 
   * // always read 14 in
   * TableConfig tc14 = matchTableConfig(0, 0, 14, 0, -1);
   * TableB result = readTableB(tc14.tableBname, tc14.tableBformat, false);
   * tablesB.put(version14, result); // hash by standard name
   * 
   * // everyone else uses 13 : cant override - do it in local if needed
   * if (version < 14) {
   * TableConfig tc = matchTableConfig(0, 0, 13, 0, -1);
   * TableB b13 = readTableB(tc.tableBname, tc.tableBformat, false);
   * TableB.Composite bb = new TableB.Composite(version13, version13);
   * bb.addTable(b13); // check in 13 first, so it overrides
   * bb.addTable(result); // then in 14
   * result = bb;
   * tablesB.put(version13, result); // hash by standard name
   * }
   * 
   * return result;
   * }
   */
    public static TableB getWmoTableB(int masterTableVersion) throws IOException {
        TableConfig tc = matchTableConfig(0, 0, masterTableVersion, 0, -1);
        if (tc != null)
            return readTableB(tc.tableBname, tc.tableBformat, false);
        return null;
    }

    public static TableB readTableB(String location, Format format, boolean force) throws IOException {
        if (!force) {
            TableB tb = tablesB.get(location);
            if (tb != null)
                return tb;
        }
        if (showTables)
            System.out.printf("Read BufrTable B %s format=%s%n", location, format);
        TableB b = new TableB(location, location);
        try (InputStream ios = openStream(location)) {
            switch(format) {
                case cypher:
                    readCypherTableB(ios, b);
                    break;
                case ecmwf:
                    readEcmwfTableB(ios, b);
                    break;
                case embed:
                    b = readEmbeddedTableB(location);
                    break;
                case ncep:
                    readNcepTableB(ios, b);
                    break;
                case ncep_nm:
                    Tables t = new Tables(b, null, null);
                    NcepMnemonic.read(ios, t);
                    break;
                case mel_bufr:
                    readMelbufrTableB(ios, b);
                    break;
                case mel_tabs:
                    readMeltabTableB(ios, b);
                    break;
                case opera:
                    readOperaTableB(ios, b);
                    break;
                case ukmet:
                    readBmetTableB(ios, b);
                    break;
                case wmo_csv:
                    readWmoCsvTableB(ios, b);
                    break;
                case wmo_xml:
                    WmoXmlReader.readWmoXmlTableB(ios, b);
                    break;
            }
        }
        if (b != null)
            // replacedume we would get the same table in any thread, so race condition is ok
            tablesB.put(location, b);
        return b;
    }

    private static void readWmoCsvTableB(InputStream ios, TableB b) throws IOException {
        BufferedReader dataIS = new BufferedReader(new InputStreamReader(ios, StandardCharsets.UTF_8));
        int count = 0;
        while (true) {
            String line = dataIS.readLine();
            if (line == null)
                break;
            if (line.startsWith("#"))
                continue;
            count++;
            if (count == 1) {
                // skip first line - its the header
                continue;
            }
            // any commas that are embedded in quotes - replace with blanks for now so split works
            int pos1 = line.indexOf('"');
            if (pos1 >= 0) {
                int pos2 = line.indexOf('"', pos1 + 1);
                StringBuilder sb = new StringBuilder(line);
                for (int i = pos1; i < pos2; i++) if (sb.charAt(i) == ',')
                    sb.setCharAt(i, ' ');
                line = sb.toString();
            }
            String[] flds = line.split(",");
            if (flds.length < 7) {
                if (showReadErrs)
                    System.out.printf("%d BAD split == %s%n", count, line);
                continue;
            }
            // Start at 1 to skip clreplacedId
            int fldidx = 1;
            try {
                int xy = Integer.parseInt(flds[fldidx++].trim());
                String name = StringUtil2.remove(flds[fldidx++], '"');
                // alphanumeric plus these chars
                String units = StringUtil2.filter(flds[fldidx++], " %+-_/()*");
                int scale = Integer.parseInt(clean(flds[fldidx++].trim()));
                int refVal = Integer.parseInt(clean(flds[fldidx++].trim()));
                int width = Integer.parseInt(clean(flds[fldidx++].trim()));
                int x = xy / 1000;
                int y = xy % 1000;
                b.addDescriptor((short) x, (short) y, scale, refVal, width, name, units, null);
            } catch (Exception e) {
                if (showReadErrs)
                    System.out.printf("%d %d BAD line == %s%n", count, fldidx, line);
            }
        }
    }

    private static String clean(String s) {
        return StringUtil2.remove(s, ' ');
    }

    private static TableB readEmbeddedTableB(String location) throws IOException {
        try (RandomAccessFile raf = new RandomAccessFile(location, "r")) {
            MessageScanner scan = new MessageScanner(raf);
            TableLookup lookup = scan.getTableLookup();
            if (lookup != null) {
                return lookup.getLocalTableB();
            }
            return null;
        }
    }

    private static TableD readEmbeddedTableD(String location) throws IOException {
        try (RandomAccessFile raf = new RandomAccessFile(location, "r")) {
            MessageScanner scan = new MessageScanner(raf);
            TableLookup lookup = scan.getTableLookup();
            if (lookup != null) {
                return lookup.getLocalTableD();
            }
            return null;
        }
    }

    // tables are in mel-bufr format
    private static TableB readMelbufrTableB(InputStream ios, TableB b) throws IOException {
        BufferedReader dataIS = new BufferedReader(new InputStreamReader(ios, StandardCharsets.UTF_8));
        // read table B looking for descriptors
        while (true) {
            String line = dataIS.readLine();
            if (line == null)
                break;
            if (line.startsWith("#") || line.isEmpty())
                continue;
            try {
                String[] split = line.split(";");
                short x = Short.parseShort(split[1].trim());
                short y = Short.parseShort(split[2].trim());
                int scale = Integer.parseInt(split[3].trim());
                int refVal = Integer.parseInt(split[4].trim());
                int width = Integer.parseInt(split[5].trim());
                b.addDescriptor(x, y, scale, refVal, width, split[7], split[6], null);
            } catch (Exception e) {
                log.error("Bad table B entry: table=" + b.getName() + " entry=<" + line + ">", e.getMessage());
            }
        }
        return b;
    }

    /*
   * tables are in cypher format : http://www.northern-lighthouse.com/tables/B_d00v13.htm
   * #
   * 002063 Aircraft roll angle
   * Degree
   * 2 -18000 16
   */
    private static TableB readCypherTableB(InputStream ios, TableB b) throws IOException {
        boolean startMode = false;
        BufferedReader dataIS = new BufferedReader(new InputStreamReader(ios, StandardCharsets.UTF_8));
        while (true) {
            String line = dataIS.readLine();
            if (line == null)
                break;
            if (line.isEmpty())
                continue;
            if (line.startsWith("<"))
                continue;
            if (line.startsWith("#")) {
                startMode = true;
                continue;
            }
            if (startMode) {
                try {
                    String xys = line.substring(0, 8).trim();
                    int xy = Integer.parseInt(xys);
                    short x = (short) (xy / 1000);
                    short y = (short) (xy % 1000);
                    String name = Util.cleanName(line.substring(8));
                    String units = "";
                    line = dataIS.readLine();
                    if (line != null)
                        units = WmoXmlReader.cleanUnit(line);
                    int scale = 0, refVal = 0, width = 0;
                    line = dataIS.readLine();
                    if (line != null) {
                        line = StringUtil2.remove(line, '*');
                        ImmutableList<String> split = StringUtil2.splitList(line);
                        scale = Integer.parseInt(split.get(0));
                        refVal = Integer.parseInt(split.get(1));
                        width = Integer.parseInt(split.get(2));
                    }
                    b.addDescriptor(x, y, scale, refVal, width, name, units, null);
                    startMode = false;
                } catch (Exception e) {
                    log.error("Bad table B entry: table=" + b.getName() + " entry=<" + line + ">", e.getMessage());
                }
            }
        }
        return b;
    }

    // tables are in mel-bufr format
    // #F X Y Scale RefVal Width Units Element Name
    // 0 0 1 0 0 24 CCITT_IA5 Table A: entry
    private static TableB readMeltabTableB(InputStream ios, TableB b) throws IOException {
        BufferedReader dataIS = new BufferedReader(new InputStreamReader(ios, StandardCharsets.UTF_8));
        // read table B looking for descriptors
        while (true) {
            String line = dataIS.readLine();
            if (line == null)
                break;
            if (line.startsWith("#") || line.isEmpty())
                continue;
            try {
                String[] split = line.split("\t");
                short x = Short.parseShort(split[1].trim());
                short y = Short.parseShort(split[2].trim());
                int scale = Integer.parseInt(split[3].trim());
                int refVal = Integer.parseInt(split[4].trim());
                int width = Integer.parseInt(split[5].trim());
                b.addDescriptor(x, y, scale, refVal, width, split[7], split[6], null);
            } catch (Exception e) {
                log.error("Bad table " + b.getName() + " entry=<" + line + ">", e);
            }
        }
        return b;
    }

    // F-XX-YYY |SCALE| REFERENCE | BIT | UNIT | MNEMONIC ;DESC ; ELEMENT NAME
    private static TableB readNcepTableB(InputStream ios, TableB b) throws IOException {
        BufferedReader dataIS = new BufferedReader(new InputStreamReader(ios, StandardCharsets.UTF_8));
        // throw first line away
        dataIS.readLine();
        while (true) {
            String line = dataIS.readLine();
            if (line == null)
                break;
            if (line.startsWith("#") || line.isEmpty())
                continue;
            try {
                String[] flds = line.split("[\\|;]");
                if (flds[0].equals("END"))
                    break;
                if (flds.length < 8) {
                    log.error("Bad line in table " + b.getName() + " entry=<" + line + ">");
                    continue;
                }
                String fxys = flds[0];
                int scale = Integer.parseInt(clean(flds[1]));
                int refVal = Integer.parseInt(clean(flds[2]));
                int width = Integer.parseInt(clean(flds[3]));
                String units = StringUtil2.remove(flds[4], '"');
                String name = StringUtil2.remove(flds[5], '"');
                String desc = StringUtil2.remove(flds[7], '"');
                String[] xyflds = fxys.split("-");
                short x = Short.parseShort(clean(xyflds[1]));
                short y = Short.parseShort(clean(xyflds[2]));
                b.addDescriptor(x, y, scale, refVal, width, name, units, desc);
            } catch (Exception e) {
                log.error("Bad table " + b.getName() + " entry=<" + line + ">", e);
            }
        }
        return b;
    }

    /*
   * opera ----------------------------
   * 0;02;181;Supplementary present weather sensor;Flag-Table;0;0;21
   * 0;07;192;Pixel size in Z-direction;Meters;-1;0;16
   * 0;21;036;Radar rainfall intensity;mm*h-1;2;0;16
   */
    private static void readOperaTableB(InputStream ios, TableB b) throws IOException {
        BufferedReader dataIS = new BufferedReader(new InputStreamReader(ios, StandardCharsets.UTF_8));
        int count = 0;
        while (true) {
            String line = dataIS.readLine();
            if (line == null)
                break;
            if (line.startsWith("#"))
                continue;
            count++;
            String[] flds = line.split(";");
            if (flds.length < 8) {
                if (showReadErrs)
                    System.out.printf("%d BAD split == %s%n", count, line);
                continue;
            }
            // skip clreplacedId
            int fldidx = 1;
            try {
                int x = Integer.parseInt(flds[fldidx++].trim());
                int y = Integer.parseInt(flds[fldidx++].trim());
                String name = StringUtil2.remove(flds[fldidx++], '"');
                // alphanumeric plus these chars
                String units = StringUtil2.filter(flds[fldidx++], " %+-_/()*");
                int scale = Integer.parseInt(clean(flds[fldidx++].trim()));
                int refVal = Integer.parseInt(clean(flds[fldidx++].trim()));
                int width = Integer.parseInt(clean(flds[fldidx++].trim()));
                b.addDescriptor((short) x, (short) y, scale, refVal, width, name, units, null);
            } catch (Exception e) {
                if (showReadErrs)
                    System.out.printf("%d %d BAD line == %s%n", count, fldidx, line);
            }
        }
    }

    /*
   * fxy name units scale ref w units
   * 01234567 72 97 102 119
   * 001015 STATION OR SITE NAME CCITTIA5 0 0 160 CHARACTER 0 20
   * 001041 ABSOLUTE PLATFORM VELOCITY - FIRST COMPONENT (SEE NOTE 6) M/S 5 -1073741824 31 M/S 5 10
   */
    private static TableB readEcmwfTableB(InputStream ios, TableB b) throws IOException {
        List<TableParser.Record> recs = TableParser.readTable(ios, "4i,7i,72,97,102i,114i,119i", 50000);
        for (TableParser.Record record : recs) {
            if (record.nfields() < 7) {
                continue;
            }
            int x = (Integer) record.get(0);
            int y = (Integer) record.get(1);
            String name = (String) record.get(2);
            String units = (String) record.get(3);
            int scale = (Integer) record.get(4);
            int ref = (Integer) record.get(5);
            int width = (Integer) record.get(6);
            b.addDescriptor((short) x, (short) y, scale, ref, width, name, units, null);
        }
        return b;
    }

    private static void readBmetTableB(InputStream ios, TableB b) throws IOException {
        org.jdom2.Doreplacedent doc;
        try {
            SAXBuilder builder = new SAXBuilder();
            doc = builder.build(ios);
        } catch (JDOMException e) {
            throw new IOException(e.getMessage());
        }
        Element root = doc.getRootElement();
        List<Element> featList = root.getChildren("featureCatalogue");
        for (Element featureCat : featList) {
            List<Element> features = featureCat.getChildren("feature");
            for (Element feature : features) {
                String name = feature.getChild("annotation").getChildTextNormalize("doreplacedentation");
                int f = Integer.parseInt(feature.getChildText("F"));
                int x = Integer.parseInt(feature.getChildText("X"));
                int y = Integer.parseInt(feature.getChildText("Y"));
                int fxy = (f << 16) + (x << 8) + y;
                Element bufrElem = feature.getChild("BUFR");
                String units = bufrElem.getChildTextNormalize("BUFR_units");
                int scale = 0, reference = 0, width = 0;
                String s = null;
                try {
                    s = bufrElem.getChildTextNormalize("BUFR_scale");
                    scale = Integer.parseInt(clean(s));
                } catch (NumberFormatException e) {
                    log.warn(" key {} name '{}' has bad scale='{}'%n", fxy, name, s);
                }
                try {
                    s = bufrElem.getChildTextNormalize("BUFR_reference");
                    reference = Integer.parseInt(clean(s));
                } catch (NumberFormatException e) {
                    log.warn(" key {} name '{}' has bad reference='{}' %n", fxy, name, s);
                }
                try {
                    s = bufrElem.getChildTextNormalize("BUFR_width");
                    width = Integer.parseInt(clean(s));
                } catch (NumberFormatException e) {
                    log.warn(" key {} name '{}' has bad width='{}' %n", fxy, name, s);
                }
                b.addDescriptor((short) x, (short) y, scale, reference, width, name, units, null);
            }
        }
    }

    // /////////////////////////////////////////////////////
    private static TableD latestWmoD;

    public static synchronized TableD getWmoTableDlatest() {
        if (latestWmoD == null) {
            try {
                latestWmoD = getWmoTableD(latestVersion);
            } catch (IOException ioe) {
                log.error("Cant open latest WMO ", ioe);
                throw new RuntimeException(ioe);
            }
        }
        return latestWmoD;
    }

    public static TableD getWmoTableD(int masterTableVersion) throws IOException {
        TableConfig tc = matchTableConfig(0, 0, masterTableVersion, 0, -1);
        if (tc != null) {
            return readTableD(tc.tableDname, tc.tableDformat, false);
        }
        return null;
    }

    public static TableD readTableD(String location, Format format, boolean force) throws IOException {
        if (location == null)
            return null;
        if (location.trim().isEmpty())
            return null;
        if (!force) {
            TableD tb = tablesD.get(location);
            if (tb != null)
                return tb;
        }
        if (showTables)
            System.out.printf("Read BufrTable D %s format=%s%n", location, format);
        TableD d = new TableD(location, location);
        try (InputStream ios = openStream(location)) {
            switch(format) {
                case cypher:
                    readCypherTableD(ios, d);
                    break;
                case embed:
                    d = readEmbeddedTableD(location);
                    break;
                case ncep:
                    readNcepTableD(ios, d);
                    break;
                case ncep_nm:
                    Tables t = new Tables(null, d, null);
                    NcepMnemonic.read(ios, t);
                    break;
                case ecmwf:
                    readEcmwfTableD(ios, d);
                    break;
                case mel_bufr:
                    readMelbufrTableD(ios, d);
                    break;
                case opera:
                    readOperaTableD(ios, d);
                    break;
                case wmo_csv:
                    readWmoCsvTableD(ios, d);
                    break;
                case wmo_xml:
                    WmoXmlReader.readWmoXmlTableD(ios, d);
                    break;
                default:
                    log.warn("Unknown format= {}", format);
                    return null;
            }
        }
        if (d != null)
            // replacedume we would get the same table in any thread, so race condition is ok
            tablesD.put(location, d);
        return d;
    }

    /*
   * #
   * <A HREF="#C40"> 40</A>
   * </TT></P>
   * <PRE></PRE>
   * <HR>
   * <A NAME="C00"></A>
   * <H4 ALIGN="CENTER">Clreplaced 00</H4>
   * <PRE>
   * #
   * 300002
   * 000002 Table A category, line 1
   * 000003 Table A category, line 2
   * #
   * 300003
   * 000010 F, part descriptor
   * 000011 X, part descriptor
   * 000012 Y, part descriptor
   * #
   * 300004
   * 300003
   * 000013 Element name, line 1
   * 000014 Element name, line 2
   * 000015 Units name
   * 000016 Units scale sign
   * 000017 Units scale
   * 000018 Units reference sign
   * 000019 Units reference value
   * 000020 Element data width
   */
    private static void readCypherTableD(InputStream ios, TableD t) throws IOException {
        TableD.Descriptor currDesc = null;
        boolean startMode = false;
        BufferedReader dataIS = new BufferedReader(new InputStreamReader(ios, StandardCharsets.UTF_8));
        while (true) {
            String line = dataIS.readLine();
            if (line == null)
                break;
            if (line.isEmpty())
                continue;
            if (line.startsWith("<"))
                continue;
            if (line.startsWith("#")) {
                startMode = true;
                continue;
            }
            if (startMode) {
                try {
                    ImmutableList<String> flds = StringUtil2.splitList(line);
                    int fxy = Integer.parseInt(flds.get(0));
                    int y = fxy % 1000;
                    fxy /= 1000;
                    int x = fxy % 100;
                    int f1 = fxy / 100;
                    if (f1 != 3)
                        log.error("Bad table " + t.getName() + " entry=<" + line + ">");
                    else
                        currDesc = t.addDescriptor((short) x, (short) y, "", new ArrayList<>());
                    startMode = false;
                    continue;
                } catch (Exception e) {
                    log.warn("Bad table " + t.getName() + " line=<" + line + ">", e.getMessage());
                }
            }
            if (currDesc != null) {
                try {
                    ImmutableList<String> flds = StringUtil2.splitList(line);
                    String fxys = cleanNumber(flds.get(0));
                    int fxy = Integer.parseInt(fxys);
                    int y1 = fxy % 1000;
                    fxy /= 1000;
                    int x1 = fxy % 100;
                    int f1 = fxy / 100;
                    int fxy1 = (f1 << 14) + (x1 << 8) + y1;
                    currDesc.addFeature((short) fxy1);
                } catch (Exception e) {
                    log.warn("Bad table " + t.getName() + " line=<" + line + ">", e.getMessage());
                }
            } else {
                log.warn("Bad table " + t.getName() + " line=<" + line + ">" + " trying to add feature without descriptor.");
            }
        }
    }

    static String cleanNumber(String s) {
        int pos = s.indexOf("(");
        if (pos > 0)
            return s.substring(0, pos);
        return s;
    }

    /*
   * opera:
   * # Heights of side view
   * 3;13;192; 1;01;000
   * ; ; ; 0;31;001
   * ; ; ; 0;10;007
   * # 4 bit per pixel radar images (top view)
   * 3;21;192; 1;10;000
   * ...
   */
    private static void readOperaTableD(InputStream ios, TableD t) throws IOException {
        BufferedReader dataIS = new BufferedReader(new InputStreamReader(ios, StandardCharsets.UTF_8));
        TableD.Descriptor currDesc = null;
        String name = null;
        while (true) {
            String line = dataIS.readLine();
            if (line == null)
                break;
            line = line.trim();
            if (line.isEmpty())
                continue;
            if (line.startsWith("#")) {
                name = line.substring(2).trim();
                continue;
            }
            try {
                String[] flds = line.split(";");
                if (!flds[0].trim().isEmpty()) {
                    int x = Integer.parseInt(flds[1].trim());
                    int y = Integer.parseInt(flds[2].trim());
                    currDesc = t.addDescriptor((short) x, (short) y, name, new ArrayList<>());
                }
                if (currDesc != null) {
                    int f1 = Integer.parseInt(flds[3].trim());
                    int x1 = Integer.parseInt(flds[4].trim());
                    int y1 = Integer.parseInt(flds[5].trim());
                    int fxy = (f1 << 14) + (x1 << 8) + y1;
                    currDesc.addFeature((short) fxy);
                } else {
                    throw new Exception("Trying to add feature to null descriptor");
                }
            } catch (Exception e) {
                log.error("Bad table " + t.getName() + " entry=<" + line + ">", e);
            }
        }
    }

    private static void readWmoCsvTableD(InputStream ios, TableD tableD) throws IOException {
        BufferedReader dataIS = new BufferedReader(new InputStreamReader(ios, StandardCharsets.UTF_8));
        int count = 0;
        int currSeqno = -1;
        TableD.Descriptor currDesc = null;
        while (true) {
            String line = dataIS.readLine();
            if (line == null)
                break;
            if (line.startsWith("#"))
                continue;
            count++;
            if (count == 1) {
                continue;
            }
            // commas embedded in quotes - replace with blanks for now
            int pos1 = line.indexOf('"');
            if (pos1 >= 0) {
                int pos2 = line.indexOf('"', pos1 + 1);
                StringBuilder sb = new StringBuilder(line);
                for (int i = pos1; i < pos2; i++) if (sb.charAt(i) == ',')
                    sb.setCharAt(i, ' ');
                line = sb.toString();
            }
            String[] flds = line.split(",");
            if (flds.length < 5) {
                if (showReadErrs)
                    System.out.printf("%d INCOMPLETE line == %s%n", count, line);
                continue;
            }
            // skip sno and cat
            int fldidx = 2;
            try {
                int seq = Integer.parseInt(flds[fldidx++]);
                String seqName = flds[fldidx++];
                String featno = flds[fldidx++].trim();
                if (featno.isEmpty()) {
                    if (showReadErrs)
                        System.out.printf("%d no FXY2 specified; line == %s%n", count, line);
                    continue;
                }
                if (currSeqno != seq) {
                    int y = seq % 1000;
                    int w = seq / 1000;
                    int x = w % 100;
                    seqName = StringUtil2.remove(seqName, '"');
                    currDesc = tableD.addDescriptor((short) x, (short) y, seqName, new ArrayList<>());
                    currSeqno = seq;
                }
                int fno = Integer.parseInt(featno);
                int y = fno % 1000;
                int w = fno / 1000;
                int x = w % 100;
                int f = w / 100;
                int fxy = (f << 14) + (x << 8) + y;
                if (currDesc != null) {
                    currDesc.addFeature((short) fxy);
                } else {
                    log.error("Trying to add feature to null desc!");
                }
            } catch (Exception e) {
                if (showReadErrs)
                    System.out.printf("%d %d BAD line == %s : %s%n", count, fldidx, line, e.getMessage());
            }
        }
    }

    // get 3 integers from
    private static final Pattern threeInts = Pattern.compile("^\\s*(\\d+)\\s+(\\d+)\\s+(\\d+)");

    // beginning of line
    // check for -1 sequence terminator
    private static final Pattern negOne = Pattern.compile("^\\s*-1");

    private static void readMelbufrTableD(InputStream ios, TableD t) throws IOException {
        BufferedReader dataIS = new BufferedReader(new InputStreamReader(ios, StandardCharsets.UTF_8));
        int count = 0;
        // read table D to store sequences and their descriptors
        while (true) {
            String line = dataIS.readLine();
            if (line == null)
                break;
            count++;
            // check for comment lines
            if (line.startsWith("#") || line.isEmpty())
                continue;
            line = line.trim();
            // 1 or more whitespace
            String[] split = line.split("[ \t]+");
            if (split.length < 3)
                continue;
            if (split[0].equals("END"))
                break;
            try {
                short seqF = Short.parseShort(split[0]);
                short seqX = Short.parseShort(split[1]);
                short seqY = Short.parseShort(split[2]);
                replacedert seqF == 3;
                String seqName = "";
                if (split.length > 3) {
                    StringBuilder sb = new StringBuilder(40);
                    for (int i = 3; i < split.length; i++) sb.append(split[i]).append(" ");
                    seqName = sb.toString();
                    seqName = StringUtil2.remove(seqName, "()");
                }
                List<Short> seq = new ArrayList<>();
                // look for descriptors within sequence terminated by -1
                while (true) {
                    line = dataIS.readLine();
                    if (line == null)
                        break;
                    count++;
                    // check for comment lines
                    if (line.startsWith("#") || line.isEmpty())
                        continue;
                    Matcher m = threeInts.matcher(line);
                    // descriptor found
                    if (m.find()) {
                        short f = Short.parseShort(m.group(1));
                        short x = Short.parseShort(m.group(2));
                        short y = Short.parseShort(m.group(3));
                        seq.add(Descriptor.getFxy(f, x, y));
                    } else {
                        m = negOne.matcher(line);
                        if (m.find()) {
                            // store this sequence
                            t.addDescriptor(seqX, seqY, seqName, seq);
                            break;
                        }
                    }
                }
            } catch (Exception e) {
                log.warn("TableD " + t.getName() + " Failed on line " + count + " = " + line + "\n " + e);
            // e.printStackTrace();
            }
        }
    }

    /*
   * 3-00-010 | DELAYREP ; ; Table D sequence definition
   * | 3-00-003 > | Table D descriptor to be defined
   * | 1-01-000 > | Delayed replication of 1 descriptor
   * | 0-31-001 > | Delayed descriptor replication factor
   * | 0-00-030 | Descriptor defining sequence
   * 
   * 3-01-001 | WMOBLKST ; ;
   * | 0-01-001 > | WMO block number
   * | 0-01-002 | WMO station number
   * 
   */
    private static void readNcepTableD(InputStream ios, TableD t) throws IOException {
        BufferedReader dataIS = new BufferedReader(new InputStreamReader(ios, StandardCharsets.UTF_8));
        // throw first line away
        dataIS.readLine();
        TableD.Descriptor currDesc = null;
        while (true) {
            String line = dataIS.readLine();
            if (line == null)
                break;
            if (line.startsWith("#") || line.trim().isEmpty())
                continue;
            try {
                String[] flds = line.split("[\\|;]");
                if (flds[0].equals("END"))
                    break;
                String fxys = flds[0].trim();
                if (!fxys.isEmpty()) {
                    String[] xyflds = fxys.split("-");
                    short x = Short.parseShort(clean(xyflds[1]));
                    short y = Short.parseShort(clean(xyflds[2]));
                    String seqName = (flds.length > 3) ? flds[3].trim() : "";
                    currDesc = t.addDescriptor(x, y, seqName, new ArrayList<>());
                } else if (currDesc != null) {
                    fxys = StringUtil2.remove(flds[1], '>');
                    String[] xyflds = fxys.split("-");
                    short f = Short.parseShort(clean(xyflds[0]));
                    short x = Short.parseShort(clean(xyflds[1]));
                    short y = Short.parseShort(clean(xyflds[2]));
                    int fxy = (f << 14) + (x << 8) + y;
                    currDesc.addFeature((short) fxy);
                }
            } catch (Exception e) {
                log.error("Bad table " + t.getName() + " entry=<" + line + ">", e);
            }
        }
    }

    /*
   * 300002 2 000002
   * 000003
   * 300003 3 000010
   * 000011
   * 000012
   */
    private static void readEcmwfTableD(InputStream ios, TableD t) throws IOException {
        BufferedReader dataIS = new BufferedReader(new InputStreamReader(ios, StandardCharsets.UTF_8));
        TableD.Descriptor currDesc = null;
        int n = 0;
        while (true) {
            String line = dataIS.readLine();
            if (line == null)
                break;
            line = line.trim();
            if (line.startsWith("#") || line.isEmpty())
                continue;
            try {
                String fxys;
                // Need to do fixed-width parsing since, spaces can disappear (if
                // middle number is triple digits, it can end up right against the
                // first number
                if (line.length() > 6) {
                    String[] flds = { line.substring(0, 6), line.substring(6, 9), line.substring(9) };
                    fxys = flds[0].trim();
                    int fxy = Integer.parseInt(fxys);
                    int y = fxy % 1000;
                    fxy /= 1000;
                    int x = fxy % 100;
                    currDesc = t.addDescriptor((short) x, (short) y, "", new ArrayList<>());
                    n = Integer.parseInt(flds[1].trim());
                    fxys = flds[2].trim();
                } else {
                    fxys = line;
                }
                int fxy = Integer.parseInt(fxys);
                int y = fxy % 1000;
                fxy /= 1000;
                int x = fxy % 100;
                fxy /= 100;
                fxy = (fxy << 14) + (x << 8) + y;
                if (currDesc != null) {
                    currDesc.addFeature((short) fxy);
                }
                n--;
            } catch (Exception e) {
                log.error("Bad table " + t.getName() + " entry=<" + line + ">", e);
            }
        }
    }

    // ///////////////////////////////////////////////////////////////////////////////////////
    static InputStream openStream(String location) throws IOException {
        InputStream ios;
        if (location.startsWith("resource:")) {
            location = location.substring(9);
            ios = BufrTables.clreplaced.getResourcereplacedtream(location);
            if (ios == null)
                throw new RuntimeException("resource not found=<" + location + ">");
            return ios;
        }
        if (location.startsWith("http:")) {
            URL url = new URL(location);
            ios = url.openStream();
        } else {
            ios = new FileInputStream(location);
        }
        return ios;
    }
}

17 Source : Re2jRegexDeserializerTest.java
with Apache License 2.0
from Nextdoor

@Test(expected = DeserializationException.clreplaced)
public void testNoMatches() {
    List<ReFieldConfig> fields = new ArrayList<>();
    fields.add(new ReFieldConfig("foo", ReFieldType.STRING));
    Pattern p = Pattern.compile("(test i am)");
    Re2jRegexDeserializer deser = new Re2jRegexDeserializer(p, fields);
    DeserializedEvent event = deser.deserialize("i am a test");
}

17 Source : SolidUtilities.java
with Apache License 2.0
from google

public final clreplaced SolidUtilities {

    private static final Logger logger = LoggerFactory.getLogger(SolidContactsExport.clreplaced);

    private static final Pattern PROBLEMATIC_TURTLE = Pattern.compile("(\\s\\d+\\.)\n");

    private static final HttpTransport TRANSPORT = new NetHttpTransport();

    private final String authCookie;

    private final HttpRequestFactory factory;

    public SolidUtilities(String authCookie) {
        this.authCookie = authCookie;
        this.factory = TRANSPORT.createRequestFactory();
    }

    /**
     * Does a depth first traversal of a RDF graph, preplaceding each {@link Resource} into the
     * provided {@link Consumer}
     */
    public void explore(String url, Consumer<Resource> resourceConsumer) throws IOException {
        logger.debug("Exploring: %s", url);
        Model model = getModel(url);
        Resource selfResource = getResource(url, model);
        if (selfResource == null) {
            resourceConsumer.accept(model.createResource(url));
            return;
        }
        if (isType(selfResource, "http://www.w3.org/ns/ldp#Container")) {
            for (Resource r : getContainedResource(model, url)) {
                explore(r.getURI(), resourceConsumer);
            }
        }
        resourceConsumer.accept(selfResource);
    }

    /**
     * Parses the contents of a URL to produce an RDF model.
     */
    public Model getModel(String url) throws IOException {
        HttpRequestFactory factory = TRANSPORT.createRequestFactory();
        HttpRequest rootGetRequest = factory.buildGetRequest(new GenericUrl(url));
        HttpHeaders headers = new HttpHeaders();
        headers.setCookie(authCookie);
        headers.setAccept("text/turtle");
        rootGetRequest.setHeaders(headers);
        HttpResponse response = rootGetRequest.execute();
        if (response.getStatusCode() != 200) {
            throw new IOException("Unexpected return code: " + response.getStatusCode() + "\nMessage:\n" + response.getStatusMessage());
        }
        StringWriter writer = new StringWriter();
        IOUtils.copy(response.getContent(), writer, "UTF-8");
        String fixedString = fixProblematicPeriods(writer.toString());
        Model defaultModel = ModelFactory.createDefaultModel();
        return defaultModel.read(new StringReader(fixedString), url, "TURTLE");
    }

    /**
     * Recursively deletes all sub resources starting at the given url. *
     */
    public void recursiveDelete(String url) throws IOException {
        explore(url, r -> delete(r.getURI()));
    }

    /**
     * Posts an RDF model to a Solid server. *
     */
    public String postContent(String url, String slug, String type, Model model) throws IOException {
        StringWriter stringWriter = new StringWriter();
        model.write(stringWriter, "TURTLE");
        HttpContent content = new ByteArrayContent("text/turtle", stringWriter.toString().getBytes());
        HttpRequest postRequest = factory.buildPostRequest(new GenericUrl(url), content);
        HttpHeaders headers = new HttpHeaders();
        headers.setCookie(authCookie);
        headers.set("Link", "<" + type + ">; rel=\"type\"");
        headers.set("Slug", slug);
        postRequest.setHeaders(headers);
        HttpResponse response = postRequest.execute();
        validateResponse(response, 201);
        return response.getHeaders().getLocation();
    }

    /**
     * Checks if a {@link Resource} is a given type. *
     */
    public static boolean isType(Resource resource, String type) {
        return resource.listProperties(RDF.type).toList().stream().anyMatch(s -> s.getResource().getURI().equalsIgnoreCase(type));
    }

    /**
     * Gets a given resource (including the #this reference) from a model. *
     */
    public static Resource getResource(String url, Model model) {
        List<Resource> matchingSubjects = model.listSubjects().filterKeep(s -> s.getURI() != null).filterKeep(s -> s.getURI().equalsIgnoreCase(url) || s.getURI().equalsIgnoreCase(url + "#this")).toList();
        if (matchingSubjects.isEmpty()) {
            return null;
        }
        checkState(matchingSubjects.size() == 1, "Model %s didn't contain %s", model, url);
        return matchingSubjects.get(0);
    }

    private void delete(String url) {
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept("text/turtle");
        headers.setCookie(authCookie);
        try {
            HttpRequest deleteRequest = factory.buildDeleteRequest(new GenericUrl(url)).setThrowExceptionOnExecuteError(false);
            deleteRequest.setHeaders(headers);
            validateResponse(deleteRequest.execute(), 200);
            logger.debug("Deleted: %s", url);
        } catch (IOException e) {
            throw new IllegalStateException("Couldn't delete: " + url, e);
        }
    }

    private static void validateResponse(HttpResponse response, int expectedCode) throws IOException {
        if (response.getStatusCode() != expectedCode) {
            throw new IOException("Unexpected return code: " + response.getStatusCode() + "\nMessage:\n" + response.getStatusMessage() + "\nHeaders:\n" + response.getHeaders());
        }
    }

    private static List<Resource> getContainedResource(Model model, String url) {
        ImmutableList.Builder<Resource> results = new ImmutableList.Builder<>();
        Resource self = model.getResource(url);
        self.listProperties(model.createProperty("http://www.w3.org/ns/ldp#contains")).forEachRemaining(s -> results.add(s.getResource()));
        /*List<Statement> containedStatements = getProperties(
        self,
        "http://www.w3.org/ns/ldp#contains");

    for (Statement s : containedStatements) {
      results.add(s.getResource());
    }*/
        return results.build();
    }

    private static String fixProblematicPeriods(String source) {
        // SOLID outputs lines like:
        // st:size 4096.
        // And jena thinks the trailing '.' belongs to the number, and not the end of
        // the stanza.  So this turn cases like that into:
        // st:size 4096.0.
        // which everyone likes
        return PROBLEMATIC_TURTLE.matcher(source).replaceAll("$10.\n");
    }

    /**
     * Utility method for debugging model problems. *
     */
    @SuppressWarnings("unused")
    public static void describeModel(Model model) {
        model.listSubjects().forEachRemaining(r -> {
            logger.info(r.toString());
            Stmreplacederator props = r.listProperties();
            props.forEachRemaining(p -> logger.info("\t" + p.getPredicate() + " " + p.getObject()));
        });
    }
}

17 Source : Scrubber.java
with Apache License 2.0
from google

/**
 * A transformer that removes matching substrings from the change description.
 */
public clreplaced Scrubber implements Transformation {

    private final Pattern pattern;

    private final String replacement;

    private final Location location;

    @Nullable
    private final String defaultPublicMsg;

    private final boolean failIfNotMacth;

    Scrubber(Pattern pattern, @Nullable String defaultPublicMsg, boolean failIfNotMacth, String replacement, Location location) {
        this.pattern = Preconditions.checkNotNull(pattern);
        this.defaultPublicMsg = defaultPublicMsg;
        this.failIfNotMacth = failIfNotMacth;
        this.replacement = Preconditions.checkNotNull(replacement);
        this.location = Preconditions.checkNotNull(location);
    }

    @Override
    public void transform(TransformWork work) throws IOException, ValidationException {
        try {
            String scrubbedMessage = pattern.matcher(work.getMessage()).replaceAll(replacement);
            if (!work.getMessage().equals(scrubbedMessage)) {
                work.getConsole().verboseFmt("Scrubbed change description '%s' by '%s'", work.getMessage(), scrubbedMessage);
                work.setMessage(scrubbedMessage);
                return;
            }
            ValidationException.checkCondition(!failIfNotMacth, "Scrubber regex: '%s' didn't match for description: '%s'", pattern.pattern(), work.getMessage());
            if (defaultPublicMsg != null) {
                work.setMessage(defaultPublicMsg);
            }
        } catch (IndexOutOfBoundsException e) {
            throw new ValidationException(String.format("Could not find matching group. Are you missing a group in your regex '%s'?", pattern), e);
        }
    }

    @Override
    public Transformation reverse() throws NonReversibleValidationException {
        return new ExplicitReversal(IntentionalNoop.INSTANCE, this);
    }

    @Override
    public String describe() {
        return "Description scrubber";
    }

    @Override
    public Location location() {
        return location;
    }
}

17 Source : ProjectClassToRuleResolver.java
with Apache License 2.0
from bazelbuild

/**
 * Maps top level clreplaced names to rules.
 */
clreplaced ProjectClreplacedToRuleResolver implements ClreplacedToRuleResolver {

    private final Logger logger = Logger.getLogger(ProjectBuildRule.clreplaced.getName());

    private final ImmutableGraph<String> clreplacedGraph;

    private final Pattern whiteList;

    private final Path workspace;

    /**
     * Maps each clreplaced name to the file it's defined in.
     */
    private final ImmutableMap<String, Path> clreplacedToFile;

    /**
     * The maximum percentage of clreplacedes that can be unresolved before BFG errors out.
     */
    static final double UNRESOLVED_THRESHOLD = 0.7;

    ProjectClreplacedToRuleResolver(ImmutableGraph<String> clreplacedGraph, Pattern whiteList, ImmutableMap<String, Path> clreplacedToFile, Path workspace) {
        checkNoInnerClreplacedesPresent(clreplacedGraph.nodes());
        this.clreplacedToFile = clreplacedToFile;
        this.workspace = workspace;
        this.clreplacedGraph = clreplacedGraph;
        this.whiteList = whiteList;
    }

    @Override
    public ImmutableMap<String, BuildRule> resolve(Set<String> clreplacedes) {
        ImmutableSet<String> projectClreplacedes = clreplacedes.stream().filter(name -> whiteList.matcher(name).find()).collect(toImmutableSet());
        Map<String, Path> clreplacedToSrcFileMap = Maps.filterKeys(clreplacedToFile, k -> projectClreplacedes.contains(k));
        handleUnresolvedClreplacedes(projectClreplacedes, clreplacedToSrcFileMap.keySet());
        ImmutableGraph<String> resolvedClreplacedGraph = ImmutableGraph.copyOf(Graphs.inducedSubgraph(clreplacedGraph, clreplacedToSrcFileMap.keySet()));
        ImmutableGraph<Path> srcFileGraph = ClreplacedToSourceGraphConsolidator.map(resolvedClreplacedGraph, clreplacedToSrcFileMap);
        ImmutableGraph<ImmutableSet<Path>> componentDAG = new StronglyConnectedComponents<>(srcFileGraph).computeComponentDAG();
        return ImmutableMap.copyOf(mapClreplacedToBuildRule(componentDAG, clreplacedToSrcFileMap, resolvedClreplacedGraph));
    }

    /**
     * Maps each top level clreplaced name to a build rule.
     */
    private Map<String, BuildRule> mapClreplacedToBuildRule(ImmutableGraph<ImmutableSet<Path>> componentDAG, Map<String, Path> clreplacedToSrcFileMap, Graph<String> clreplacedGraph) {
        ImmutableMap<Path, Path> dirToBuildFileMap = mapDirectoriesToPackages(componentDAG.nodes());
        Map<Path, BuildRule> srcToTargetMap = new HashMap<>();
        for (ImmutableSet<Path> component : componentDAG.nodes()) {
            Path buildFilePath = dirToBuildFileMap.get(component.iterator().next().getParent());
            BuildRule rule = new ProjectBuildRule(component, buildFilePath, workspace);
            component.stream().forEach(src -> srcToTargetMap.put(src, rule));
        }
        ImmutableMap.Builder<String, BuildRule> clreplacedToBuildRuleMap = ImmutableMap.builder();
        for (String clreplacedName : clreplacedGraph.nodes()) {
            Path srcFile = clreplacedToSrcFileMap.get(clreplacedName);
            clreplacedToBuildRuleMap.put(clreplacedName, srcToTargetMap.get(srcFile));
        }
        return clreplacedToBuildRuleMap.build();
    }

    /**
     * Identifies and logs all unresolved clreplaced names. Then computes the percentage of unresolved
     * clreplacedes. If more than a certain threshold are unresolved, then it will throw an
     * IllegalStateException.
     */
    private void handleUnresolvedClreplacedes(ImmutableSet<String> projectClreplacedes, Set<String> resolvedClreplacedes) {
        Set<String> unresolvedClreplacedes = Sets.difference(projectClreplacedes, resolvedClreplacedes);
        if (unresolvedClreplacedes.isEmpty() || projectClreplacedes.isEmpty()) {
            return;
        }
        logger.severe(String.format("Unresolved clreplaced names = {\n\t%s\n}", Joiner.on("\n\t").join(unresolvedClreplacedes)));
        if (UNRESOLVED_THRESHOLD * projectClreplacedes.size() < unresolvedClreplacedes.size()) {
            throw new IllegalStateException(String.format("BUILD File Generator failed to map over %.0f percent of clreplaced names. " + "Check your white list and content roots", UNRESOLVED_THRESHOLD * 100));
        }
    }
}

16 Source : VerifyMatch.java
with Apache License 2.0
from google

/**
 * A source code pseudo-transformation which verifies that all specified files satisfy a RegEx.
 * Does not actually transform any code, but will throw errors on failure. Not applied in reversals.
 */
public final clreplaced VerifyMatch implements Transformation {

    private final Pattern pattern;

    private final boolean verifyNoMatch;

    private final boolean alsoOnReversal;

    private final Glob fileMatcherBuilder;

    private final LocalParallelizer parallelizer;

    private final Location location;

    private VerifyMatch(Pattern pattern, boolean verifyNoMatch, boolean alsoOnReversal, Glob fileMatcherBuilder, LocalParallelizer parallelizer, Location location) {
        this.pattern = checkNotNull(pattern);
        this.verifyNoMatch = verifyNoMatch;
        this.alsoOnReversal = alsoOnReversal;
        this.fileMatcherBuilder = checkNotNull(fileMatcherBuilder);
        this.parallelizer = parallelizer;
        this.location = checkNotNull(location);
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this).add("Pattern", pattern).add("verifyNoMatch", verifyNoMatch).add("also_on_reversal", alsoOnReversal).add("path", fileMatcherBuilder).toString();
    }

    @Override
    public void transform(TransformWork work) throws IOException, ValidationException {
        Path checkoutDir = work.getCheckoutDir();
        Iterable<FileState> files = work.getTreeState().find(fileMatcherBuilder.relativeTo(checkoutDir));
        Iterable<String> errors = Iterables.concat(parallelizer.run(files, new BatchRun(work.getCheckoutDir())));
        int size = 0;
        for (String error : errors) {
            size++;
            work.getConsole().error(String.format("File '%s' failed validation '%s'.", error, describe()));
        }
        work.getTreeState().notifyNoChange();
        ValidationException.checkCondition(size == 0, "%d file(s) failed the validation of %s, located at %s.", size, describe(), location);
    }

    private clreplaced BatchRun implements TransformFunc<FileState, List<String>> {

        private final Path checkoutDir;

        private BatchRun(Path checkoutDir) {
            this.checkoutDir = checkNotNull(checkoutDir);
        }

        @Override
        public List<String> run(Iterable<FileState> files) throws IOException, ValidationException {
            List<String> errors = new ArrayList<>();
            // TODO(malcon): Remove reconstructing pattern once RE2J doesn't synchronize on matching.
            Pattern batchPattern = Pattern.compile(pattern.pattern(), pattern.flags());
            for (FileState file : files) {
                String originalFileContent = new String(Files.readAllBytes(file.getPath()), UTF_8);
                if (verifyNoMatch == batchPattern.matcher(originalFileContent).find()) {
                    errors.add(checkoutDir.relativize(file.getPath()).toString());
                }
            }
            return errors;
        }
    }

    @Override
    public String describe() {
        return String.format("Verify match '%s'", pattern);
    }

    @Override
    public Location location() {
        return location;
    }

    @Override
    public Transformation reverse() {
        if (alsoOnReversal) {
            return new ExplicitReversal(this, this);
        }
        return new ExplicitReversal(IntentionalNoop.INSTANCE, this);
    }

    public static VerifyMatch create(Location location, String regEx, Glob paths, boolean verifyNoMatch, boolean alsoOnReversal, LocalParallelizer parallelizer) throws EvalException {
        Pattern parsed;
        try {
            parsed = Pattern.compile(regEx, Pattern.MULTILINE);
        } catch (PatternSyntaxException ex) {
            throw Starlark.errorf("Regex '%s' is invalid: %s", regEx, ex.getMessage());
        }
        return new VerifyMatch(parsed, verifyNoMatch, alsoOnReversal, paths, parallelizer, location);
    }
}

16 Source : RegexTemplateTokens.java
with Apache License 2.0
from google

/**
 * A string which is interpolated with named variables. The string is composed of interpolated and
 * non-interpolated (literal) pieces called tokens.
 */
public final clreplaced RegexTemplateTokens {

    private final String template;

    private final Pattern before;

    private final ArrayListMultimap<String, Integer> groupIndexes = ArrayListMultimap.create();

    private final ImmutableList<Token> tokens;

    private final Set<String> unusedGroups;

    private final Location location;

    public RegexTemplateTokens(String template, Map<String, Pattern> regexGroups, boolean repeatedGroups, Location location) throws EvalException {
        this.template = Preconditions.checkNotNull(template);
        this.tokens = ImmutableList.copyOf(new Parser().parse(template));
        this.location = Preconditions.checkNotNull(location);
        this.before = buildBefore(regexGroups, repeatedGroups);
        this.unusedGroups = Sets.difference(regexGroups.keySet(), groupIndexes.keySet());
    }

    /**
     * How this template can be used when it is the "before" value of core.replace - as a regex to
     * search for.
     */
    public Pattern getBefore() {
        return before;
    }

    public ImmutableListMultimap<String, Integer> getGroupIndexes() {
        return ImmutableListMultimap.copyOf(groupIndexes);
    }

    public Replacer replacer(RegexTemplateTokens after, boolean firstOnly, boolean multiline, List<Pattern> patternsToIgnore) {
        // TODO(malcon): Remove reconstructing pattern once RE2J doesn't synchronize on matching.
        return new Replacer(Pattern.compile(before.pattern(), before.flags()), after, null, firstOnly, multiline, patternsToIgnore, location);
    }

    public Replacer callbackReplacer(RegexTemplateTokens after, AlterAfterTemplate callback, boolean firstOnly, boolean multiline, @Nullable List<Pattern> patternsToIgnore) {
        return new Replacer(Pattern.compile(before.pattern()), after, callback, firstOnly, multiline, patternsToIgnore, location);
    }

    public clreplaced Replacer {

        private final Pattern before;

        private final RegexTemplateTokens after;

        private final boolean firstOnly;

        private final boolean multiline;

        private final String afterReplaceTemplate;

        private final Multimap<String, Integer> repeatedGroups = ArrayListMultimap.create();

        private final Location location;

        @Nullable
        private final List<Pattern> patternsToIgnore;

        @Nullable
        private final AlterAfterTemplate callback;

        private Replacer(Pattern before, RegexTemplateTokens after, @Nullable AlterAfterTemplate callback, boolean firstOnly, boolean multiline, @Nullable List<Pattern> patternsToIgnore, Location location) {
            this.before = before;
            this.after = after;
            afterReplaceTemplate = this.after.after(RegexTemplateTokens.this);
            // Precomputed the repeated groups as this should be used only on rare occasions and we
            // don't want to iterate over the map for every line.
            for (Entry<String, Collection<Integer>> e : groupIndexes.asMap().entrySet()) {
                if (e.getValue().size() > 1) {
                    repeatedGroups.putAll(e.getKey(), e.getValue());
                }
            }
            this.firstOnly = firstOnly;
            this.multiline = multiline;
            this.callback = callback;
            this.patternsToIgnore = patternsToIgnore;
            this.location = location;
        }

        public String replace(String content) {
            List<String> originalRanges = multiline ? ImmutableList.of(content) : Splitter.on('\n').splitToList(content);
            List<String> newRanges = new ArrayList<>(originalRanges.size());
            for (String line : originalRanges) {
                newRanges.add(replaceLine(line));
            }
            return Joiner.on('\n').join(newRanges);
        }

        private String replaceLine(String line) {
            if (patternsToIgnore != null) {
                for (Pattern patternToIgnore : patternsToIgnore) {
                    if (patternToIgnore.matches(line)) {
                        return line;
                    }
                }
            }
            Matcher matcher = before.matcher(line);
            StringBuilder sb = new StringBuilder(line.length());
            while (matcher.find()) {
                for (Collection<Integer> groupIndexes : repeatedGroups.asMap().values()) {
                    // Check that all the references of the repeated group match the same string
                    Iterator<Integer> iterator = groupIndexes.iterator();
                    String value = matcher.group(iterator.next());
                    while (iterator.hasNext()) {
                        if (!value.equals(matcher.group(iterator.next()))) {
                            return line;
                        }
                    }
                }
                String replaceTemplate;
                if (callback != null) {
                    ImmutableMap.Builder<Integer, String> groupValues = ImmutableMap.builder();
                    for (int i = 0; i <= matcher.groupCount(); i++) {
                        groupValues.put(i, matcher.group(i));
                    }
                    replaceTemplate = callback.alter(groupValues.build(), afterReplaceTemplate);
                } else {
                    replaceTemplate = afterReplaceTemplate;
                }
                matcher.appendReplacement(sb, replaceTemplate);
                if (firstOnly) {
                    break;
                }
            }
            matcher.appendTail(sb);
            return sb.toString();
        }

        @Override
        public String toString() {
            return String.format("s/%s/%s/%s", RegexTemplateTokens.this, after, firstOnly ? "" : "g");
        }

        public Location getLocation() {
            return location;
        }

        public boolean isFirstOnly() {
            return firstOnly;
        }
    }

    /**
     * How this template can be used when it is the "after" value of core.replace - as a string to
     * insert in place of the regex, possibly including $N, referring to captured groups.
     *
     * <p>Returns a template in which the literals are escaped (if they are a $ or {) and the
     * interpolations appear as $N, where N is the group's index as given by {@code groupIndexes}.
     */
    private String after(RegexTemplateTokens before) {
        StringBuilder template = new StringBuilder();
        for (Token token : tokens) {
            switch(token.getType()) {
                case INTERPOLATION:
                    template.append("$").append(before.groupIndexes.get(token.getValue()).iterator().next());
                    break;
                case LITERAL:
                    for (int c = 0; c < token.getValue().length(); c++) {
                        char thisChar = token.getValue().charAt(c);
                        if (thisChar == '$' || thisChar == '\\') {
                            template.append('\\');
                        }
                        template.append(thisChar);
                    }
                    break;
            }
        }
        return template.toString();
    }

    @Override
    public String toString() {
        return template.replace("\n", "\\n").replace("\r", "\\r").replace("\t", "\\t");
    }

    @Override
    public boolean equals(Object other) {
        if (other instanceof RegexTemplateTokens) {
            RegexTemplateTokens comp = (RegexTemplateTokens) other;
            return before.equals(comp.before) && tokens.equals(comp.tokens);
        } else {
            return false;
        }
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(before, tokens);
    }

    /**
     * Converts this sequence of tokens into a regex which can be used to search a string. It
     * automatically quotes literals and represents interpolations as named groups.
     *
     * <p>It also fills groupIndexes with all the interpolation locations.
     *
     * @param regexesByInterpolationName map from group name to the regex to interpolate when the
     * group is mentioned
     * @param repeatedGroups true if a regex group is allowed to be used multiple times
     */
    private Pattern buildBefore(Map<String, Pattern> regexesByInterpolationName, boolean repeatedGroups) throws EvalException {
        int groupCount = 1;
        StringBuilder fullPattern = new StringBuilder();
        for (Token token : tokens) {
            switch(token.getType()) {
                case INTERPOLATION:
                    Pattern subPattern = regexesByInterpolationName.get(token.getValue());
                    check(subPattern != null, "Interpolation is used but not defined: %s", token.getValue());
                    fullPattern.append(String.format("(%s)", subPattern.pattern()));
                    check(groupIndexes.get(token.getValue()).isEmpty() || repeatedGroups, "Regex group is used in template multiple times: %s", token.getValue());
                    groupIndexes.put(token.getValue(), groupCount);
                    groupCount += subPattern.groupCount() + 1;
                    break;
                case LITERAL:
                    fullPattern.append(Pattern.quote(token.getValue()));
                    break;
            }
        }
        return Pattern.compile(fullPattern.toString(), Pattern.MULTILINE);
    }

    /**
     * Checks that the set of interpolated tokens matches {@code definedInterpolations}.
     *
     * @throws EvalException if not all interpolations are used in this template
     */
    public void validateUnused() throws EvalException {
        check(unusedGroups.isEmpty(), "Following interpolations are defined but not used: %s", unusedGroups);
    }

    /**
     * Callback for {@see callbackReplacer}.
     */
    public interface AlterAfterTemplate {

        /**
         *  Upon encountering a match, the replacer will call the callback with the matched groups and
         *  the template to be used in the replace. The return value of this function will be used
         *  in place of {@code template}, i.e. group tokens like '$1' in the return value will be
         *  replaced with the group values. Note that the groupValues are immutable.
         * @param groupValues The values of the groups in the before pattern. 0 holds the entire match.
         * @param template The replacement template the replacer would normally use
         * @return The template to be used instead
         */
        String alter(Map<Integer, String> groupValues, String template);
    }
}

15 Source : Re2jRegexDeserializerTest.java
with Apache License 2.0
from Nextdoor

@Test
public void testSingleMatch() throws FieldNotFoundException {
    List<ReFieldConfig> fields = new ArrayList<>();
    fields.add(new ReFieldConfig("foo", ReFieldType.STRING));
    Pattern p = Pattern.compile("(.*)");
    Re2jRegexDeserializer deser = new Re2jRegexDeserializer(p, fields);
    DeserializedEvent event = deser.deserialize("test i am");
    replacedertEquals("test i am", event.getField("foo"));
}

15 Source : VerifyMatch.java
with Apache License 2.0
from google

public static VerifyMatch create(Location location, String regEx, Glob paths, boolean verifyNoMatch, boolean alsoOnReversal, LocalParallelizer parallelizer) throws EvalException {
    Pattern parsed;
    try {
        parsed = Pattern.compile(regEx, Pattern.MULTILINE);
    } catch (PatternSyntaxException ex) {
        throw Starlark.errorf("Regex '%s' is invalid: %s", regEx, ex.getMessage());
    }
    return new VerifyMatch(parsed, verifyNoMatch, alsoOnReversal, paths, parallelizer, location);
}

15 Source : ProxyHttpTransparent.java
with Apache License 2.0
from DeNA

private HostPort parseHostName(byte[] buffer) throws Exception {
    int start = 0;
    if ((start = StringUtils.binaryFind(buffer, "Host:".getBytes())) > 0) {
        start += 5;
    } else if ((start = StringUtils.binaryFind(buffer, "host:".getBytes())) > 0) {
        start += 5;
    } else {
        throw new Exception("Host: header field is not found in beginning of 4096 bytes of packets.");
    }
    int end = StringUtils.binaryFind(buffer, "\n".getBytes(), start);
    String serverCand = new String(ArrayUtils.subarray(buffer, start, end));
    String server = "";
    int port = 80;
    Pattern pattern = Pattern.compile("^ *([^:\n\r]+)(?::([0-9]+))?");
    Matcher matcher = pattern.matcher(serverCand);
    if (matcher.find()) {
        if (matcher.group(1) != null)
            server = matcher.group(1);
        if (matcher.group(2) != null)
            port = Integer.parseInt(matcher.group(2));
    } else {
        throw new Exception("Host: header field format is not recognized.");
    }
    return new HostPort(server, port);
}

15 Source : Modification.java
with Apache License 2.0
from DeNA

private byte[] replaceRegex(byte[] data, Packet packet) {
    Pattern pattern = Pattern.compile(this.pattern, Pattern.MULTILINE);
    Matcher matcher = pattern.matcher(new String(data));
    String result = new String(data);
    while (matcher.find()) {
        result = matcher.replaceAll(this.replaced);
        packet.setModified();
    }
    return result.getBytes();
}

15 Source : InterceptOption.java
with Apache License 2.0
from DeNA

private boolean matchRegex(byte[] data) {
    Pattern pattern = Pattern.compile(this.pattern, Pattern.MULTILINE);
    Matcher matcher = pattern.matcher(new String(data));
    return matcher.find();
}

15 Source : Http.java
with Apache License 2.0
from DeNA

private void replacedyzeResponseStatusLine(String status_line) throws Exception {
    Pattern pattern = Pattern.compile("[^ ]+ +([^ ]+) +([a-z0-9A-Z ]+)$");
    Matcher matcher = pattern.matcher(status_line);
    if (matcher.find()) {
        this.statusCode = matcher.group(1).trim();
    // if (matcher.group(2).trim().equals("Connection established")) {
    // //flag_httpsは使ってない
    // flag_https = true;
    // }
    }
}

15 Source : CamelCase.java
with Apache License 2.0
from DeNA

/*
	static public void main(String[] args) {
		PacketProxyUtility util = PacketProxyUtility.getInstance();
		try {
			util.packetProxyLog(CamelCase.toCamelCase("1453"));
			util.packetProxyLog(CamelCase.toCamelCase("a"));
			util.packetProxyLog(CamelCase.toCamelCase("test"));
			util.packetProxyLog(CamelCase.toCamelCase("abc_def_ghf"));
			util.packetProxyLog(CamelCase.toCamelCase("abc-def-ghf"));
			util.packetProxyLog(CamelCase.toCamelCase("abc_def-ghf"));
			util.packetProxyLog(CamelCase.toCamelCase("abc%def$-ghf"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	*/
static public String toCamelCase(String s) {
    Pattern pattern = Pattern.compile("([a-zA-Z][a-zA-Z0-9]*)");
    Matcher matcher = pattern.matcher(s);
    StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
        matcher.appendReplacement(sb, toProperCase(matcher.group()));
    }
    matcher.appendTail(sb);
    return sb.toString();
}

15 Source : Regex.java
with Apache License 2.0
from datasonnet

public static Value regexScan(String expr, String str) throws RegexException {
    Pattern pattern = Pattern.compile(expr);
    Matcher matcher = pattern.matcher(str);
    ArrayNode regexMatch = scan(matcher);
    return regexMatch != null ? ujsonUtils.parse(regexMatch.toString()) : Null$.MODULE$;
}

15 Source : ClassGraphPreprocessorTest.java
with Apache License 2.0
from bazelbuild

/**
 * Tests whether black listed clreplacedes names as well as their non-whitelisted dependencies are
 * removed from the clreplaced graph. In addition to not containing the black listed clreplaced, the
 * resulting graph should also not contain nonwhite listed clreplacedes only black listed clreplacedes are
 * dependent on. For example, say we were to have the following clreplaced graph
 *
 * <p>com.Whitelist --> com.Blacklist --> com.NonWhitelist
 *
 * <p>Then the resulting clreplaced graph should only contain com.Whitelist
 */
@Test
public void trimRemovesTransitiveDepsOfBlackListedClreplacedes() {
    MutableGraph<String> graph = newGraph();
    graph.putEdge("com.BlackList", "com.OtherList");
    graph.putEdge("com.WhiteList", "com.BlackList");
    Pattern blackList = Pattern.compile("BlackList");
    Pattern whiteList = Pattern.compile("WhiteList");
    Graph<String> actual = preProcessClreplacedGraph(ImmutableGraph.copyOf(graph), whiteList, blackList);
    MutableGraph<String> expected = newGraph();
    expected.addNode("com.WhiteList");
    replacedertEquivalent(actual, expected);
    replacedertThat(actual.nodes()).doesNotContain("com.BlackList");
    replacedertThat(actual.nodes()).doesNotContain("com.OtherList");
}

15 Source : ClassGraphPreprocessorTest.java
with Apache License 2.0
from bazelbuild

/**
 * replacederts that the only nodes in the trimmed graph are white listed clreplacedes and clreplacedes that the
 * white listed clreplacedes are directly dependent on.
 */
@Test
public void trimRemovesTransitiveDependenciesToNonWhiteListedClreplacedes() {
    MutableGraph<String> graph = newGraph();
    graph.putEdge("com.WhiteList", "com.OtherList");
    graph.putEdge("com.OtherList", "com.TransitiveDependency");
    Pattern whiteList = Pattern.compile("WhiteList");
    Graph<String> actual = preProcessClreplacedGraph(ImmutableGraph.copyOf(graph), whiteList, NOTHING);
    MutableGraph<String> expected = newGraph();
    expected.putEdge("com.WhiteList", "com.OtherList");
    replacedertEquivalent(actual, expected);
    replacedertThat(actual.nodes()).doesNotContain("com.TransitiveDependency");
}

15 Source : ClassGraphPreprocessorTest.java
with Apache License 2.0
from bazelbuild

/**
 * Tests whether the black listed clreplaced names are removed from the clreplaced graph.
 */
@Test
public void trimRemovesBlackListedClreplacedes() {
    MutableGraph<String> graph = newGraph();
    graph.putEdge("com.BlackList", "com.WhiteList");
    graph.putEdge("com.WhiteList", "com.BlackList");
    Pattern blackList = Pattern.compile("BlackList");
    Graph<String> actual = preProcessClreplacedGraph(ImmutableGraph.copyOf(graph), EVERYTHING, blackList);
    MutableGraph<String> expected = newGraph();
    expected.addNode("com.WhiteList");
    replacedertEquivalent(actual, expected);
    replacedertThat(actual.nodes()).doesNotContain("com.BlackList");
}

14 Source : ReferenceMigrator.java
with Apache License 2.0
from google

/**
 * Adjusts textual references in  change messages to match the destination.
 */
public clreplaced ReferenceMigrator implements Transformation {

    static final int MAX_CHANGES_TO_VISIT = 5000;

    private final RegexTemplateTokens before;

    private final RegexTemplateTokens after;

    private final ImmutableList<String> additionalLabels;

    @Nullable
    private final Pattern reversePattern;

    private final Location location;

    private final Map<String, String> knownChanges = new HashMap<>();

    ReferenceMigrator(RegexTemplateTokens before, RegexTemplateTokens after, @Nullable Pattern reversePattern, ImmutableList<String> additionalLabels, Location location) {
        this.before = checkNotNull(before, "before");
        this.after = checkNotNull(after, "after");
        this.additionalLabels = checkNotNull(additionalLabels, "additionalLabels");
        this.reversePattern = reversePattern;
        this.location = checkNotNull(location);
    }

    public static ReferenceMigrator create(String before, String after, Pattern forward, @Nullable Pattern backward, ImmutableList<String> additionalLabels, Location location) throws EvalException {
        Map<String, Pattern> patterns = ImmutableMap.of("reference", forward);
        RegexTemplateTokens beforeTokens = new RegexTemplateTokens(before, patterns, /* repeatedGroups= */
        false, location);
        beforeTokens.validateUnused();
        RegexTemplateTokens afterTokens = new RegexTemplateTokens(after, patterns, /* repeatedGroups= */
        false, location);
        afterTokens.validateUnused();
        check(after.lastIndexOf("$1") == -1, "Destination format '%s' uses the reserved token '$1'.", after);
        return new ReferenceMigrator(beforeTokens, afterTokens, backward, additionalLabels, location);
    }

    @Override
    public void transform(TransformWork work) throws ValidationException {
        AtomicReference<ValidationException> thrown = new AtomicReference<>();
        Replacer replacer = before.callbackReplacer(after, (groupValues, template) -> {
            if (groupValues.get(0) != null) {
                try {
                    String destinationRef = findChange(groupValues.get(1), work.getMigrationInfo().getOriginLabel(), work.getMigrationInfo().destinationVisitable());
                    if (destinationRef != null) {
                        // This will not work for the case where the template was "foo\\$1", if this is an
                        // issue, a non-naive implementation might be required.
                        return Pattern.compile("[$]1").matcher(template).replaceAll(destinationRef);
                    } else {
                        return groupValues.get(0);
                    }
                } catch (ValidationException exception) {
                    thrown.compareAndSet(null, exception);
                    return groupValues.get(0);
                }
            }
            return template;
        }, false, false, null);
        String replaced = replacer.replace(work.getMessage());
        if (thrown.get() != null) {
            throw thrown.get();
        }
        if (!replaced.equals(work.getMessage())) {
            work.setMessage(replaced);
        }
    }

    @Override
    public Transformation reverse() {
        return new ExplicitReversal(IntentionalNoop.INSTANCE, this);
    }

    @Override
    public String describe() {
        return "map_references: " + before + " to " + after;
    }

    @Nullable
    private String findChange(String refBeingMigrated, String originLabel, ChangeVisitable<?> destinationReader) throws ValidationException {
        AtomicInteger changesVisited = new AtomicInteger(0);
        ImmutableList<String> originLabels = ImmutableList.<String>builder().add(originLabel).addAll(additionalLabels).build();
        checkCondition(destinationReader != null, "Destination does not support reading change history.");
        if (knownChanges.containsKey(refBeingMigrated)) {
            return knownChanges.get(refBeingMigrated);
        }
        try {
            destinationReader.visitChangesWithAnyLabel(null, originLabels, (input, labels) -> {
                for (String labelValue : labels.values()) {
                    knownChanges.putIfAbsent(labelValue, input.getRef());
                    if (labelValue.equals(refBeingMigrated)) {
                        return TERMINATE;
                    }
                }
                return changesVisited.incrementAndGet() > MAX_CHANGES_TO_VISIT ? TERMINATE : CONTINUE;
            });
            String retVal = knownChanges.get(refBeingMigrated);
            if (reversePattern != null && retVal != null && !reversePattern.matches(retVal)) {
                throw new ValidationException(String.format("Reference %s does not match regex '%s'", retVal, reversePattern));
            }
            return retVal;
        } catch (RepoException exception) {
            throw new ValidationException("Exception finding reference.", exception);
        }
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this).add("before", before).add("after", after).toString();
    }

    @Override
    public boolean equals(Object other) {
        return other instanceof ReferenceMigrator && Objects.equal(this.before, ((ReferenceMigrator) other).before) && Objects.equal(this.after, ((ReferenceMigrator) other).after);
    }

    @Override
    public Location location() {
        return location;
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(before, after);
    }
}

14 Source : ReferenceMigrator.java
with Apache License 2.0
from google

public static ReferenceMigrator create(String before, String after, Pattern forward, @Nullable Pattern backward, ImmutableList<String> additionalLabels, Location location) throws EvalException {
    Map<String, Pattern> patterns = ImmutableMap.of("reference", forward);
    RegexTemplateTokens beforeTokens = new RegexTemplateTokens(before, patterns, /* repeatedGroups= */
    false, location);
    beforeTokens.validateUnused();
    RegexTemplateTokens afterTokens = new RegexTemplateTokens(after, patterns, /* repeatedGroups= */
    false, location);
    afterTokens.validateUnused();
    check(after.lastIndexOf("$1") == -1, "Destination format '%s' uses the reserved token '$1'.", after);
    return new ReferenceMigrator(beforeTokens, afterTokens, backward, additionalLabels, location);
}

14 Source : MetadataModule.java
with Apache License 2.0
from google

@SuppressWarnings("unused")
@StarlarkMethod(name = "verify_match", doc = "Verifies that a RegEx matches (or not matches) the change message. Does not" + " transform anything, but will stop the workflow if it fails.", parameters = { @Param(name = "regex", named = true, doc = "The regex pattern to verify. The re2j pattern will be applied in multiline" + " mode, i.e. '^' refers to the beginning of a file and '$' to its end."), @Param(name = "verify_no_match", named = true, doc = "If true, the transformation will verify that the RegEx does not match.", defaultValue = "False") }, useStarlarkThread = true)
@Example(replacedle = "Check that a text is present in the change description", before = "Check that the change message contains a text enclosed in <public></public>:", code = "metadata.verify_match(\"<public>(.|\\n)*</public>\")")
public Transformation verifyMatch(String regex, Boolean verifyNoMatch, StarlarkThread thread) throws EvalException {
    Pattern pattern;
    try {
        pattern = Pattern.compile(regex, Pattern.MULTILINE);
    } catch (PatternSyntaxException e) {
        throw Starlark.errorf("Invalid regex expression: %s", e.getMessage());
    }
    return new MetadataVerifyMatch(pattern, verifyNoMatch, thread.getCallerLocation());
}

14 Source : FilterReplace.java
with Apache License 2.0
from google

// Module needed because both Transformation and ReversibleFunction are Starlark objects but
// neither of them extend each other
@StarlarkBuiltin(name = "filter_replace", doc = "A core.filter_replace transformation")
public clreplaced FilterReplace implements Transformation, ReversibleFunction<String, String> {

    private static final FluentLogger logger = FluentLogger.forEnclosingClreplaced();

    private final WorkflowOptions workflowOptions;

    private final Pattern before;

    @Nullable
    private final Pattern after;

    private final int group;

    private final int reverseGroup;

    private final ReversibleFunction<String, String> mapping;

    private final Glob glob;

    private final Location location;

    public FilterReplace(WorkflowOptions workflowOptions, Pattern before, @Nullable Pattern after, int group, int reverseGroup, ReversibleFunction<String, String> mapping, Glob glob, Location location) {
        this.workflowOptions = workflowOptions;
        this.before = before;
        this.after = after;
        this.group = group;
        this.reverseGroup = reverseGroup;
        this.mapping = mapping;
        this.glob = glob;
        this.location = location;
    }

    @Override
    public void transform(TransformWork work) throws IOException, ValidationException {
        Path checkoutDir = work.getCheckoutDir();
        Iterable<FileState> files = work.getTreeState().find(glob.relativeTo(checkoutDir));
        BatchReplace batchReplace = new BatchReplace();
        workflowOptions.parallelizer().run(files, batchReplace);
        List<FileState> changed = batchReplace.getChanged();
        boolean matchedFile = batchReplace.matchedFile;
        logger.atInfo().log("Applied %s to %d files. %d changed.", this, Iterables.size(files), changed.size());
        work.getTreeState().notifyModify(changed);
        if (changed.isEmpty()) {
            workflowOptions.reportNoop(work.getConsole(), "Transformation '" + toString() + "' was a no-op because it didn't " + (matchedFile ? "change any of the matching files" : "match any file"), work.getIgnoreNoop());
        }
    }

    @Override
    public Transformation reverse() throws NonReversibleValidationException {
        return internalReverse();
    }

    private FilterReplace internalReverse() throws NonReversibleValidationException {
        if (after == null) {
            throw new NonReversibleValidationException("No 'after' defined");
        }
        return new FilterReplace(workflowOptions, after, before, reverseGroup, group, mapping.reverseMapping(), glob, location);
    }

    @Override
    public String describe() {
        return "Nested replaceString";
    }

    @Override
    public Location location() {
        return location;
    }

    @Override
    public String apply(String s) {
        return replaceString(s);
    }

    @Override
    public ReversibleFunction<String, String> reverseMapping() throws NonReversibleValidationException {
        return internalReverse();
    }

    private clreplaced BatchReplace implements TransformFunc<FileState, Boolean> {

        private final List<FileState> changed = new ArrayList<>();

        private boolean matchedFile = false;

        public List<FileState> getChanged() {
            return changed;
        }

        @Override
        public Boolean run(Iterable<FileState> elements) throws IOException {
            List<FileState> changed = new ArrayList<>();
            boolean matchedFile = false;
            for (FileState file : elements) {
                if (Files.isSymbolicLink(file.getPath())) {
                    continue;
                }
                matchedFile = true;
                String originalContent = new String(Files.readAllBytes(file.getPath()), UTF_8);
                String transformed = replaceString(originalContent);
                // replaceString returns the same instance if not replacement happens. This avoid comparing
                // the whole file content.
                // noinspection StringEquality
                if (transformed == originalContent) {
                    continue;
                }
                changed.add(file);
                Files.write(file.getPath(), transformed.getBytes(UTF_8));
            }
            synchronized (this) {
                this.matchedFile |= matchedFile;
                this.changed.addAll(changed);
            }
            // We cannot return null here.
            return true;
        }
    }

    private String replaceString(String originalContent) {
        Pattern pattern = Pattern.compile(before.pattern());
        Matcher matcher = pattern.matcher(originalContent);
        boolean anyReplace = false;
        StringBuilder result = new StringBuilder(originalContent.length());
        while (matcher.find()) {
            String val = matcher.group(FilterReplace.this.group);
            if (val == null) {
                matcher.appendReplacement(result, Matcher.quoteReplacement(matcher.group(0)));
                continue;
            }
            String res = mapping.apply(val);
            anyReplace |= !val.equals(res);
            if (group == 0) {
                matcher.appendReplacement(result, Matcher.quoteReplacement(res));
            } else {
                String prefix = originalContent.substring(matcher.start(), matcher.start(group));
                String suffix = originalContent.substring(matcher.end(group), matcher.end());
                matcher.appendReplacement(result, Matcher.quoteReplacement((prefix + res + suffix)));
            }
        }
        if (!anyReplace) {
            return originalContent;
        }
        matcher.appendTail(result);
        return result.toString();
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this).add("before", before).toString();
    }
}

14 Source : Http.java
with Apache License 2.0
from DeNA

@SuppressWarnings("unused")
private String replaceStatusLineToNonProxyStyte(String status_line) throws Exception {
    String result = status_line;
    Pattern pattern = Pattern.compile("http://[^/]+");
    Matcher matcher = pattern.matcher(status_line);
    if (matcher.find()) {
        result = matcher.replaceAll("");
    }
    return result;
}

See More Examples