KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > innig > macker > util > IncludeExcludeLogic


1 /*______________________________________________________________________________
2  *
3  * Macker http://innig.net/macker/
4  *
5  * Copyright 2002 Paul Cantrell
6  *
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License version 2, as published by the
9  * Free Software Foundation. See the file LICENSE.html for more information.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the license for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
17  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
18  *______________________________________________________________________________
19  */

20  
21 package net.innig.macker.util;
22
23 import net.innig.macker.rule.RulesException;
24
25 public class IncludeExcludeLogic
26     {
27     public static boolean apply(IncludeExcludeNode node)
28         throws RulesException
29         {
30         return applyNext(
31             node,
32             node.isInclude()
33                 ? false // include starts with all excluded, and
34
: true); // exclude starts with all included
35
}
36
37     private static boolean applyNext(
38             IncludeExcludeNode node,
39             boolean prevMatches)
40         throws RulesException
41         {
42         IncludeExcludeNode child = node.getChild(), next = node.getNext();
43         boolean curMatches = node.matches();
44         boolean matchesSoFar =
45             node.isInclude()
46                 ? prevMatches || ( curMatches && (child == null || apply(child)))
47                 : prevMatches && (!curMatches || (child != null && apply(child)));
48         return
49             (next == null)
50                 ? matchesSoFar
51                 : applyNext(next, matchesSoFar);
52         }
53     }
Popular Tags