Switch jgit library to the EDL (3-clause BSD)
[jgit.git] / org.spearce.jgit / src / org / spearce / jgit / treewalk / filter / PathFilter.java
blob3aff145f38a8e21145ab7f2c9a1df1bc919c71a6
1 /*
2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
21 * written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org.spearce.jgit.treewalk.filter;
40 import java.io.UnsupportedEncodingException;
42 import org.spearce.jgit.lib.Constants;
43 import org.spearce.jgit.treewalk.TreeWalk;
45 /**
46 * Includes tree entries only if they match the configured path.
47 * <p>
48 * Applications should use {@link PathFilterGroup} to connect these into a tree
49 * filter graph, as the group supports breaking out of traversal once it is
50 * known the path can never match.
52 public class PathFilter extends TreeFilter {
53 /**
54 * Create a new tree filter for a user supplied path.
55 * <p>
56 * Path strings are relative to the root of the repository. If the user's
57 * input should be assumed relative to a subdirectory of the repository the
58 * caller must prepend the subdirectory's path prior to creating the filter.
59 * <p>
60 * Path strings use '/' to delimit directories on all platforms.
62 * @param path
63 * the path to filter on. Must not be the empty string. All
64 * trailing '/' characters will be trimmed before string's length
65 * is checked or is used as part of the constructed filter.
66 * @return a new filter for the requested path.
67 * @throws IllegalArgumentException
68 * the path supplied was the empty string.
70 public static PathFilter create(String path) {
71 while (path.endsWith("/"))
72 path = path.substring(0, path.length() - 1);
73 if (path.length() == 0)
74 throw new IllegalArgumentException("Empty path not permitted.");
75 return new PathFilter(path);
78 final String pathStr;
80 final byte[] pathRaw;
82 private PathFilter(final String s) {
83 pathStr = s;
84 try {
85 pathRaw = pathStr.getBytes(Constants.CHARACTER_ENCODING);
86 } catch (UnsupportedEncodingException uee) {
87 throw new RuntimeException("JVM doesn't support "
88 + Constants.CHARACTER_ENCODING
89 + " which is required for path filtering.", uee);
93 @Override
94 public boolean include(final TreeWalk walker) {
95 return walker.isPathPrefix(pathRaw, pathRaw.length) == 0;
98 @Override
99 public boolean shouldBeRecursive() {
100 for (final byte b : pathRaw)
101 if (b == '/')
102 return true;
103 return false;
106 @Override
107 public TreeFilter clone() {
108 return this;
111 public String toString() {
112 return "PATH(\"" + pathStr + "\")";