KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnet > Repository > BundleNameFilter


1 /*
2  * BundleNameFilter.java
3  *
4  * Created on 23. listopad 2003, 22:45
5  */

6
7 package SOFA.SOFAnet.Repository;
8
9 import java.util.*;
10 import org.w3c.dom.*;
11 import org.xml.sax.SAXException JavaDoc;
12 import SOFA.Util.XML;
13 import java.io.Serializable JavaDoc;
14
15 /**
16  * Filter of bundle names.
17  *
18  * Filter is implemnted as list of patterns.
19  * If name of bundle passes any of the patterns, it passes whole filter
20  *
21  * @author Ladislav Sobr
22  */

23 public class BundleNameFilter implements Cloneable JavaDoc, Serializable JavaDoc
24 {
25   private List patterns;
26   
27   /** Creates a new instance of BundleNameFilter */
28   public BundleNameFilter()
29   {
30     patterns = Collections.synchronizedList(new LinkedList());
31   }
32
33   public Object JavaDoc clone()
34   {
35     BundleNameFilter clone = null;
36     try
37     {
38       clone = (BundleNameFilter)super.clone();
39     }
40     catch (CloneNotSupportedException JavaDoc e)
41     {
42       throw new InternalError JavaDoc();
43     }
44     clone.patterns = Collections.synchronizedList(new LinkedList());
45     clone.patterns.addAll(patterns);
46     return clone;
47   }
48   
49   public List getPatterns()
50   {
51     return patterns;
52   }
53   
54   public boolean pass(String JavaDoc bundleName)
55   {
56     BundleInfo bundleInfo = new BundleInfo();
57     try
58     {
59       bundleInfo.fromBundleName(bundleName);
60     }
61     catch (BundleInfo.InvalidBundleNameException e)
62     {
63       return false;
64     }
65     
66     return pass(bundleInfo);
67   }
68   
69   public boolean pass(BundleInfo bundleInfo)
70   {
71     synchronized (patterns)
72     {
73       Iterator it = patterns.iterator();
74       while (it.hasNext())
75       {
76         String JavaDoc pattern = (String JavaDoc)it.next();
77         try
78         {
79           if (bundleInfo.matchPattern(pattern)) return true;
80         }
81         catch (BundleInfo.InvalidBundleNameException e)
82         {
83         }
84       }
85       return false;
86     }
87   }
88
89   public void add(String JavaDoc pattern)
90   {
91     patterns.add(pattern);
92   }
93
94   public void add(BundleNameFilter bundleNameFilter)
95   {
96     if (bundleNameFilter != null)
97     {
98       List li = bundleNameFilter.getPatterns();
99       synchronized (li)
100       {
101         Iterator it = li.iterator();
102         while (it.hasNext())
103         {
104           String JavaDoc pattern = (String JavaDoc)it.next();
105           patterns.add(pattern);
106         }
107       }
108     }
109   }
110   
111   /**
112    * Loads the BundleNameFilter from the XML DOM tree.
113    *
114    * XML format:
115    * <p>
116    * <pre>
117    * *&lt;filter pattern="string"/&gt;
118    * </pre>
119    */

120   public void loadFromXML(Element element) throws SAXException JavaDoc
121   {
122     synchronized (patterns)
123     {
124       NodeList nl = element.getChildNodes();
125       for (int i = 0; i < nl.getLength(); i++)
126       {
127         Node node = nl.item(i);
128         if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().compareTo("filter") == 0)
129         {
130           Element e = (Element)node;
131           String JavaDoc pattern = e.getAttribute("pattern");
132           if (pattern.length() == 0) throw new SAXException JavaDoc("Missing 'pattern' attribute in 'filter' tag");
133           patterns.add(pattern);
134         }
135       }
136     }
137   }
138   
139   /**
140    * Saves the BundleNameFilter to XML DOM tree.
141    */

142   public void saveToXML(Element element)
143   {
144     synchronized (patterns)
145     {
146       Iterator it = patterns.iterator();
147       while (it.hasNext())
148       {
149         String JavaDoc pattern = (String JavaDoc)it.next();
150         XML.newOneAttributeElement(element, "filter", "pattern", pattern);
151       }
152     }
153   }
154   
155 }
156
Popular Tags