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 16 24 public class NodeNameFilter implements Cloneable , Serializable 25 { 26 private List patterns; 27 28 29 public NodeNameFilter() 30 { 31 patterns = new LinkedList(); 32 } 33 34 public Object clone() 35 { 36 NodeNameFilter clone = null; 37 try 38 { 39 clone = (NodeNameFilter)super.clone(); 40 } 41 catch (CloneNotSupportedException e) 42 { 43 throw new InternalError (); 44 } 45 clone.patterns = Collections.synchronizedList(new LinkedList()); 46 clone.patterns.addAll(patterns); 47 return clone; 48 } 49 50 public List getPatterns() 51 { 52 return patterns; 53 } 54 55 public boolean pass(String nodeName) 56 { 57 NodeInfo nodeInfo = new NodeInfo(); 58 try 59 { 60 nodeInfo.setNodeName(nodeName); 61 } 62 catch (NodeInfo.InvalidNodeNameException e) 63 { 64 return false; 65 } 66 67 return pass(nodeInfo); 68 } 69 70 public boolean pass(NodeInfo nodeInfo) 71 { 72 Iterator it = patterns.iterator(); 73 while (it.hasNext()) 74 { 75 String pattern = (String )it.next(); 76 try 77 { 78 if (nodeInfo.matchPattern(pattern)) return true; 79 } 80 catch (NodeInfo.InvalidNodeNameException e) 81 { 82 } 83 } 84 return false; 85 } 86 87 public void add(String pattern) 88 { 89 patterns.add(pattern); 90 } 91 92 public void add(NodeNameFilter nodeFilter) 93 { 94 if (nodeFilter != null) 95 { 96 List li = nodeFilter.getPatterns(); 97 synchronized (li) 98 { 99 Iterator it = li.iterator(); 100 while (it.hasNext()) 101 { 102 String pattern = (String )it.next(); 103 patterns.add(pattern); 104 } 105 } 106 } 107 } 108 109 118 public void loadFromXML(Element element) throws SAXException 119 { 120 NodeList nl = element.getChildNodes(); 121 for (int i = 0; i < nl.getLength(); i++) 122 { 123 Node node = nl.item(i); 124 if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().compareTo("filter") == 0) 125 { 126 Element e = (Element)node; 127 String pattern = e.getAttribute("pattern"); 128 if (pattern.length() == 0) throw new SAXException ("Missing 'pattern' attribute in 'filter' tag"); 129 patterns.add(pattern); 130 } 131 } 132 } 133 134 137 public void saveToXML(Element element) 138 { 139 Iterator it = patterns.iterator(); 140 while (it.hasNext()) 141 { 142 String pattern = (String )it.next(); 143 XML.newOneAttributeElement(element, "filter", "pattern", pattern); 144 } 145 } 146 } 147 | Popular Tags |