|
| 1 | +/* |
| 2 | + * Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a> |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.ocpsoft.rewrite.config; |
| 17 | + |
| 18 | +import java.io.File; |
| 19 | +import java.io.FileFilter; |
| 20 | +import java.util.Set; |
| 21 | + |
| 22 | +import org.ocpsoft.rewrite.config.Condition; |
| 23 | +import org.ocpsoft.rewrite.config.ConfigurationRuleParameterBuilder; |
| 24 | +import org.ocpsoft.rewrite.context.EvaluationContext; |
| 25 | +import org.ocpsoft.rewrite.event.Rewrite; |
| 26 | +import org.ocpsoft.rewrite.param.ParameterStore; |
| 27 | +import org.ocpsoft.rewrite.param.Parameterized; |
| 28 | +import org.ocpsoft.rewrite.param.ParameterizedPattern; |
| 29 | +import org.ocpsoft.rewrite.param.ParameterizedPatternParser; |
| 30 | +import org.ocpsoft.rewrite.param.RegexParameterizedPatternParser; |
| 31 | +import org.ocpsoft.rewrite.util.Transforms; |
| 32 | + |
| 33 | +/** |
| 34 | + * A {@link Condition} responsible for determining existence of {@link File} paths on the host file-system. |
| 35 | + * |
| 36 | + * @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a> |
| 37 | + */ |
| 38 | +public class Filesystem implements Condition, Parameterized |
| 39 | +{ |
| 40 | + private final ParameterizedPatternParser resource; |
| 41 | + private final FileFilter filter; |
| 42 | + |
| 43 | + private Filesystem(final File resource, FileFilter filter) |
| 44 | + { |
| 45 | + this.resource = new RegexParameterizedPatternParser(resource.getAbsolutePath()); |
| 46 | + this.filter = filter; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public boolean evaluate(final Rewrite event, final EvaluationContext context) |
| 51 | + { |
| 52 | + if (resource != null) |
| 53 | + { |
| 54 | + String filePath = resource.getBuilder().build(event, context, Transforms.encodePath()); |
| 55 | + File file = new File(filePath); |
| 56 | + return filter.accept(file); |
| 57 | + } |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Create a {@link Condition} that returns <code>true</code> if the given {@link File} exists in the file-system of |
| 63 | + * the host environment, and is a normal file. |
| 64 | + * |
| 65 | + * <p> |
| 66 | + * The given file path may be parameterized: |
| 67 | + * <p> |
| 68 | + * <code> |
| 69 | + * /tmp/file.txt <br> |
| 70 | + * c:\tmp\{param}.txt <br> |
| 71 | + * ... |
| 72 | + * </code> |
| 73 | + * <p> |
| 74 | + * |
| 75 | + * @param location {@link ParameterizedPattern} specifying the location of the {@link File}. |
| 76 | + * |
| 77 | + * @see {@link ConfigurationRuleParameterBuilder#where(String)} |
| 78 | + */ |
| 79 | + public static Filesystem fileExists(final File resource) |
| 80 | + { |
| 81 | + return new Filesystem(resource, new FileFilter() { |
| 82 | + @Override |
| 83 | + public boolean accept(File file) |
| 84 | + { |
| 85 | + return file.exists() && file.isFile(); |
| 86 | + } |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Create a {@link Condition} that returns <code>true</code> if the given {@link File} exists in the file-system of |
| 92 | + * the host environment, and is a directory. |
| 93 | + * |
| 94 | + * <p> |
| 95 | + * The given file path may be parameterized: |
| 96 | + * <p> |
| 97 | + * <code> |
| 98 | + * /tmp/file.txt <br> |
| 99 | + * c:\tmp\{param}.txt <br> |
| 100 | + * ... |
| 101 | + * </code> |
| 102 | + * <p> |
| 103 | + * |
| 104 | + * @param location {@link ParameterizedPattern} specifying the location of the directory. |
| 105 | + * |
| 106 | + * @see {@link ConfigurationRuleParameterBuilder#where(String)} |
| 107 | + */ |
| 108 | + public static Filesystem directoryExists(final File resource) |
| 109 | + { |
| 110 | + return new Filesystem(resource, new FileFilter() { |
| 111 | + @Override |
| 112 | + public boolean accept(File file) |
| 113 | + { |
| 114 | + return file.exists() && file.isDirectory(); |
| 115 | + } |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Get the {@link ParameterizedPattern} of this {@link Filesystem}. |
| 121 | + */ |
| 122 | + public ParameterizedPatternParser getExpression() |
| 123 | + { |
| 124 | + return resource; |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public Set<String> getRequiredParameterNames() |
| 129 | + { |
| 130 | + return resource.getRequiredParameterNames(); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public void setParameterStore(ParameterStore store) |
| 135 | + { |
| 136 | + resource.setParameterStore(store); |
| 137 | + } |
| 138 | + |
| 139 | +} |
0 commit comments