You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by jd...@apache.org on 2008/09/30 10:13:25 UTC
svn commit: r700384 - in /geronimo/gshell/trunk/gshell-commands/gshell-vfs:
./ src/main/java/org/apache/geronimo/gshell/commands/vfs/
src/main/resources/META-INF/spring/
src/main/resources/org/apache/geronimo/gshell/commands/vfs/
Author: jdillon
Date: Tue Sep 30 01:13:25 2008
New Revision: 700384
URL: http://svn.apache.org/viewvc?rev=700384&view=rev
Log:
Adding 'find' command
Added:
geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/java/org/apache/geronimo/gshell/commands/vfs/FindAction.java (with props)
geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/FindAction.properties (contents, props changed)
- copied, changed from r700230, geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/CatAction.properties
Modified:
geronimo/gshell/trunk/gshell-commands/gshell-vfs/pom.xml
geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/META-INF/spring/components.xml
Modified: geronimo/gshell/trunk/gshell-commands/gshell-vfs/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-commands/gshell-vfs/pom.xml?rev=700384&r1=700383&r2=700384&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-commands/gshell-vfs/pom.xml (original)
+++ geronimo/gshell/trunk/gshell-commands/gshell-vfs/pom.xml Tue Sep 30 01:13:25 2008
@@ -42,6 +42,12 @@
<groupId>org.apache.geronimo.gshell.support</groupId>
<artifactId>gshell-vfs</artifactId>
</dependency>
+
+ <dependency>
+ <groupId>oro</groupId>
+ <artifactId>oro</artifactId>
+ <version>2.0.8</version>
+ </dependency>
</dependencies>
</project>
\ No newline at end of file
Added: geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/java/org/apache/geronimo/gshell/commands/vfs/FindAction.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/java/org/apache/geronimo/gshell/commands/vfs/FindAction.java?rev=700384&view=auto
==============================================================================
--- geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/java/org/apache/geronimo/gshell/commands/vfs/FindAction.java (added)
+++ geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/java/org/apache/geronimo/gshell/commands/vfs/FindAction.java Tue Sep 30 01:13:25 2008
@@ -0,0 +1,198 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.geronimo.gshell.commands.vfs;
+
+import org.apache.commons.vfs.FileObject;
+import org.apache.commons.vfs.FileSelectInfo;
+import org.apache.commons.vfs.FileSelector;
+import org.apache.commons.vfs.FileSystemException;
+import org.apache.commons.vfs.FileType;
+import org.apache.geronimo.gshell.clp.Argument;
+import org.apache.geronimo.gshell.clp.Option;
+import org.apache.geronimo.gshell.command.CommandContext;
+import org.apache.geronimo.gshell.io.IO;
+import org.apache.geronimo.gshell.vfs.selector.AggregateFileSelector;
+import org.apache.oro.text.GlobCompiler;
+import org.apache.oro.text.regex.MalformedPatternException;
+import org.apache.oro.text.regex.Pattern;
+import org.apache.oro.text.regex.PatternCompiler;
+import org.apache.oro.text.regex.PatternMatcher;
+import org.apache.oro.text.regex.Perl5Matcher;
+
+/**
+ * Find files in a hierarchy.
+ *
+ * @version $Rev$ $Date$
+ */
+public class FindAction
+ extends VfsActionSupport
+{
+ private final AggregateFileSelector selector = new AggregateFileSelector();
+
+ @Option(name="-name")
+ private void addNameFilter(final String name) throws MalformedPatternException {
+ log.debug("Adding -name selector for: {}", name);
+ selector.getSelectors().add(new NameSelector(name));
+ }
+
+ @Option(name="-iname")
+ private void addiNameFilter(final String name) throws MalformedPatternException {
+ log.debug("Adding -iname selector for: {}", name);
+ selector.getSelectors().add(new NameSelector(name, true));
+ }
+
+ @Option(name="-type")
+ private void addTypeFilter(final Type type) {
+ log.debug("Adding -type selector for: {}", type);
+ selector.getSelectors().add(new TypeSelector(type));
+ }
+
+ @Argument(required=true)
+ private String path;
+
+ public Object execute(final CommandContext context) throws Exception {
+ assert context != null;
+ IO io = context.getIo();
+
+ FileObject root = resolveFile(context, path);
+
+ if (!root.exists()) {
+ io.error("File not found: {}", root.getName());
+ return Result.FAILURE;
+ }
+
+ find(context, root, selector);
+
+ return Result.SUCCESS;
+ }
+
+ private void find(final CommandContext context, final FileObject file, final FileSelector selector) throws FileSystemException {
+ assert context != null;
+ assert file != null;
+ assert selector != null;
+
+ FileObject[] files = file.findFiles(selector);
+
+ if (files != null && files.length != 0) {
+ for (FileObject child : files) {
+ display(context, child);
+
+ if (child.getType() == FileType.FOLDER) {
+ find(context, child, selector);
+ }
+ }
+ }
+ }
+
+ private void display(final CommandContext context, final FileObject file) throws FileSystemException {
+ assert context != null;
+ assert file != null;
+
+ IO io = context.getIo();
+ io.info(file.getName().getPath());
+ }
+
+ //
+ // Type & TypeSelector
+ //
+
+ private enum Type
+ {
+ F, // normal file
+ D, // directory
+ }
+
+ private class TypeSelector
+ implements FileSelector
+ {
+ private final Type type;
+
+ public TypeSelector(final Type type) {
+ assert type != null;
+
+ this.type = type;
+
+ log.trace("Type: {}", type);
+ }
+
+ public boolean includeFile(final FileSelectInfo selection) throws Exception {
+ assert selection != null;
+
+ FileType ftype = selection.getFile().getType();
+
+ switch (type) {
+ case D:
+ return ftype == FileType.FOLDER;
+
+ case F:
+ return ftype == FileType.FILE;
+
+ default:
+ return false;
+ }
+ }
+
+ public boolean traverseDescendents(final FileSelectInfo selection) throws Exception {
+ return true;
+ }
+ }
+
+ //
+ // NameSelector
+ //
+
+ private class NameSelector
+ implements FileSelector
+ {
+ private final Pattern pattern;
+
+ private final PatternMatcher matcher;
+
+ public NameSelector(final String name, final boolean ignoreCase) throws MalformedPatternException {
+ assert name != null;
+
+ PatternCompiler compiler = new GlobCompiler();
+ int options;
+ if (ignoreCase) {
+ options = GlobCompiler.CASE_INSENSITIVE_MASK;
+ }
+ else {
+ options = GlobCompiler.DEFAULT_MASK;
+ }
+ this.pattern = compiler.compile(name, options);
+ this.matcher = new Perl5Matcher();
+
+ log.trace("Pattern: {}", pattern.getPattern());
+ }
+
+ public NameSelector(final String name) throws MalformedPatternException {
+ this(name, false);
+ }
+
+ public boolean includeFile(final FileSelectInfo selection) throws Exception {
+ assert selection != null;
+ return matcher.matches(selection.getFile().getName().getBaseName(), pattern);
+ }
+
+ public boolean traverseDescendents(final FileSelectInfo selection) throws Exception {
+ return true;
+ }
+ }
+}
\ No newline at end of file
Propchange: geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/java/org/apache/geronimo/gshell/commands/vfs/FindAction.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/java/org/apache/geronimo/gshell/commands/vfs/FindAction.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/java/org/apache/geronimo/gshell/commands/vfs/FindAction.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/META-INF/spring/components.xml
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/META-INF/spring/components.xml?rev=700384&r1=700383&r2=700384&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/META-INF/spring/components.xml (original)
+++ geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/META-INF/spring/components.xml Tue Sep 30 01:13:25 2008
@@ -103,6 +103,14 @@
<null/>
</gshell:completers>
</gshell:command>
+
+ <gshell:command name="find">
+ <gshell:action class="org.apache.geronimo.gshell.commands.vfs.FindAction"/>
+ <gshell:completers>
+ <ref bean="fileObjectNameCompleter"/>
+ <null/>
+ </gshell:completers>
+ </gshell:command>
</gshell:command-bundle>
<!--
Copied: geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/FindAction.properties (from r700230, geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/CatAction.properties)
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/FindAction.properties?p2=geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/FindAction.properties&p1=geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/CatAction.properties&r1=700230&r2=700384&rev=700384&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/CatAction.properties (original)
+++ geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/FindAction.properties Tue Sep 30 01:13:25 2008
@@ -21,14 +21,20 @@
## $Rev$ $Date$
##
-command.name=cat
+command.name=find
-command.description=Displays the contents of a file.
+command.description=Find files in a hierarchy.
-command.argument.path=Path of file to display
-command.argument.path.token=PATH
+command.option.addNameFilter=Include files with names matching GLOB
+command.option.addNameFilter.token=GLOB
+
+command.option.addInameFilter=Like -name, but the match is case insensitive
+command.option.addInameFilter.token=GLOB
-command.option.displayLineNumbers=Number the output lines, starting at 1
+command.option.addTypeFilter=Include files matching a type
+
+command.argument.path=Path of file to root search under
+command.argument.path.token=PATH
command.manual=\
- TODO: cat manual
\ No newline at end of file
+ TODO: find manual
\ No newline at end of file
Propchange: geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/FindAction.properties
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/FindAction.properties
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/FindAction.properties
------------------------------------------------------------------------------
svn:mergeinfo =
Propchange: geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/FindAction.properties
------------------------------------------------------------------------------
svn:mime-type = text/plain