KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > metadata > plugins > repository > visitor > FuzzyMetaDataRepositoryVisitor


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.metadata.plugins.repository.visitor;
23
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import org.jboss.metadata.spi.repository.MetaDataRepository;
28 import org.jboss.metadata.spi.repository.visitor.MetaDataRepositoryVisitor;
29 import org.jboss.metadata.spi.scope.Scope;
30 import org.jboss.metadata.spi.scope.ScopeKey;
31 import org.jboss.metadata.spi.scope.ScopeLevel;
32
33 /**
34  * FuzzyMetaDataRepositoryVisitor.
35  *
36  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
37  * @version $Revision: 46146 $
38  */

39 public class FuzzyMetaDataRepositoryVisitor implements MetaDataRepositoryVisitor
40 {
41    /** The key scopes */
42    private Scope[] matchScopes;
43    
44    /**
45     * Create a new FuzzyMetaDataRepositoryVisitor.
46     *
47     * @param matchKey the match key
48     */

49    public FuzzyMetaDataRepositoryVisitor(ScopeKey matchKey)
50    {
51       if (matchKey != null)
52       {
53          Collection JavaDoc<Scope> testScopes = matchKey.getScopes();
54          matchScopes = testScopes.toArray(new Scope[testScopes.size()]);
55       }
56    }
57    
58    public boolean matchScope(MetaDataRepository repository, ScopeKey key)
59    {
60       if (matchScopes == null || matchScopes.length == 0)
61          return true;
62       
63       boolean match = false;
64       int index = 0;
65       Iterator JavaDoc<Scope> i = key.getScopes().iterator();
66       Scope repositoryScope = i.next();
67       Scope matchScope = matchScopes[index];
68       while (true)
69       {
70          ScopeLevel keyLevel = matchScope.getScopeLevel();
71          ScopeLevel repositoryLevel = repositoryScope.getScopeLevel();
72
73          // Same level
74
if (keyLevel.compareTo(repositoryLevel) == 0)
75          {
76             if (matchScope.equals(repositoryScope))
77                match = true;
78             else
79             {
80                // No match, we are done
81
match = false;
82                break;
83             }
84          }
85          
86          // Key is next
87
if (keyLevel.compareTo(repositoryLevel) <= 0)
88          {
89             // Ran out of keys
90
if (++index == matchScopes.length)
91                break;
92             matchScope = matchScopes[index];
93          }
94          else
95          {
96             // Ran out of keys
97
if (i.hasNext() == false)
98                break;
99             repositoryScope = i.next();
100          }
101       }
102       return match;
103    }
104 }
105
Popular Tags