1 6 7 package SOFA.SOFAnet.Repository; 8 9 import java.util.*; 10 import org.w3c.dom.*; 11 import org.xml.sax.SAXException ; 12 import SOFA.Util.XML; 13 import java.io.Serializable ; 14 15 23 public class BundleNameFilter implements Cloneable , Serializable 24 { 25 private List patterns; 26 27 28 public BundleNameFilter() 29 { 30 patterns = Collections.synchronizedList(new LinkedList()); 31 } 32 33 public Object clone() 34 { 35 BundleNameFilter clone = null; 36 try 37 { 38 clone = (BundleNameFilter)super.clone(); 39 } 40 catch (CloneNotSupportedException e) 41 { 42 throw new InternalError (); 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 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 pattern = (String )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 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 pattern = (String )it.next(); 105 patterns.add(pattern); 106 } 107 } 108 } 109 } 110 111 120 public void loadFromXML(Element element) throws SAXException 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 pattern = e.getAttribute("pattern"); 132 if (pattern.length() == 0) throw new SAXException ("Missing 'pattern' attribute in 'filter' tag"); 133 patterns.add(pattern); 134 } 135 } 136 } 137 } 138 139 142 public void saveToXML(Element element) 143 { 144 synchronized (patterns) 145 { 146 Iterator it = patterns.iterator(); 147 while (it.hasNext()) 148 { 149 String pattern = (String )it.next(); 150 XML.newOneAttributeElement(element, "filter", "pattern", pattern); 151 } 152 } 153 } 154 155 } 156 | Popular Tags |