/** {@inheritDoc} */ @Override protected void processPaths(PathData parent, PathData ... items) throws IOException { if(parent == null) { // processing a command line argument so clear the links followed linksFollowed.clear(); } Expression expr = getRootExpression(); for (PathData item : items) { try { if(getOptions().isDepth()) { recursePath(item); expr.apply(item); } else if (expr.apply(item).isDescend()) { recursePath(item); } } catch (IOException e) { displayError(e); } } }
/** * Parse a list of arguments to extract the {@link PathData} elements. * The input deque will be modified to remove the used elements. * @param args arguments to be parsed * @return list of {@link PathData} elements applicable to this command * @throws IOException if list can not be parsed */ private LinkedList<PathData> parsePathData(Deque<String> args) throws IOException { LinkedList<PathData> pathArgs = new LinkedList<PathData>(); while(!args.isEmpty()) { String arg = args.pop(); if(isExpression(arg) || "(".equals(arg) || (arg == null) || arg.startsWith("-")) { args.push(arg); return pathArgs; } pathArgs.addAll(expandArgument(arg)); } return pathArgs; }
/** {@inheritDoc} */ @Override protected void processPathArgument(PathData item) throws IOException { if(item.stat.isSymlink() && (getOptions().isFollowArgLink() || getOptions().isFollowLink())) { item = new PathData(item.stat.getSymlink().toString(), getConf()); } super.processPathArgument(item); } /** Gets a named expression from the factory. */
/** {@inheritDoc} */ @Override protected LinkedList<PathData> expandArguments(LinkedList<String> argList) throws IOException { Deque<String> args = new LinkedList<String>(argList); LinkedList<PathData> pathArgs = parsePathData(args); if(pathArgs.size() == 0) { throw new IOException("No path specified"); } Expression expression = parseExpression(args); if(!expression.isAction()) { Expression and = getExpression(And.class); Deque<Expression> children = new LinkedList<Expression>(); children.add(getExpression(Print.class)); children.add(expression); and.addChildren(children); expression = and; } setRootExpression(expression); return pathArgs; }
Deque<Expression> primaries = new LinkedList<Expression>(); Deque<Expression> operators = new LinkedList<Expression>(); Expression prevExpr = getExpression(And.class); while(!args.isEmpty()) { String arg = args.pop(); if("(".equals(arg)) { Expression expr = parseExpression(args); primaries.add(expr); prevExpr = new BaseExpression(){}; // stub the previous expression to be a non-op break; else if(isExpression(arg)) { Expression expr = getExpression(arg); expr.addArguments(args); if(expr.isOperator()) { Expression and = getExpression(And.class); while(!operators.isEmpty()) { if(operators.peek().getPrecedence() >= and.getPrecedence()) { return primaries.isEmpty() ? getExpression(Print.class) : primaries.pop();
/** {@inheritDoc} */ @Override protected void processArguments(LinkedList<PathData> args) throws IOException { Expression expr = getRootExpression(); expr.initialise(getOptions()); super.processArguments(args); expr.finish(); }
/** Gets an instance of an expression from the factory. */ private Expression getExpression(Class<? extends Expression> expressionClass) { return ExpressionFactory.getExpressionFactory().createExpression(expressionClass, getConf()); }
/** Returns the current find options, creating them if necessary. */ @InterfaceAudience.Private FindOptions getOptions() { if(options == null) { options = new FindOptions(); options.setOut(out); options.setErr(err); options.setIn(System.in); options.setCommandFactory(getCommandFactory()); } return options; }
/** Gets a named expression from the factory. */ private Expression getExpression(String expressionName) { return ExpressionFactory.getExpressionFactory().getExpression(expressionName, getConf()); }
@Override protected void recursePath(PathData item) throws IOException { if(item.stat.isSymlink() && getOptions().isFollowLink()) { PathData linkedItem = new PathData(item.stat.getSymlink().toString(), getConf()); if(linksFollowed.contains(item)) { getOptions().getErr().println("Infinite loop ignored: " + item.toString() + " -> " + linkedItem.toString()); return; } linksFollowed.add(item); item = linkedItem; } if(item.stat.isDirectory()) { super.recursePath(item); } }