KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * NodeNameFilter.java
3  *
4  * Created on 27. listopad 2003, 17:41
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 /**
17  * Filter of node names.
18  * <p>
19  * Filter is implemented as list of patterns.
20  * If name of node passes any of the patterns, it passes whole filter
21  *
22  * @author Ladislav Sobr
23  */

24 public class NodeNameFilter implements Cloneable JavaDoc, Serializable JavaDoc
25 {
26   private List patterns;
27   
28   /** Creates a new instance of NodeNameFilter */
29   public NodeNameFilter()
30   {
31     patterns = new LinkedList();
32   }
33   
34   public Object JavaDoc clone()
35   {
36     NodeNameFilter clone = null;
37     try
38     {
39       clone = (NodeNameFilter)super.clone();
40     }
41     catch (CloneNotSupportedException JavaDoc e)
42     {
43       throw new InternalError JavaDoc();
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 JavaDoc 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 JavaDoc pattern = (String JavaDoc)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 JavaDoc 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 JavaDoc pattern = (String JavaDoc)it.next();
103           patterns.add(pattern);
104         }
105       }
106     }
107   }
108   
109   /**
110    * Loads the NodeNameFiler from the XML DOM tree.
111    * <p>
112    * XML format:
113    * <p>
114    * <pre>
115    * *&lt;filter pattern="string"/&gt;
116    * </pre>
117    */

118   public void loadFromXML(Element element) throws SAXException JavaDoc
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 JavaDoc pattern = e.getAttribute("pattern");
128         if (pattern.length() == 0) throw new SAXException JavaDoc("Missing 'pattern' attribute in 'filter' tag");
129         patterns.add(pattern);
130       }
131     }
132   }
133   
134   /**
135    * Saves the NodeNameFilter to XML DOM tree.
136    */

137   public void saveToXML(Element element)
138   {
139     Iterator it = patterns.iterator();
140     while (it.hasNext())
141     {
142       String JavaDoc pattern = (String JavaDoc)it.next();
143       XML.newOneAttributeElement(element, "filter", "pattern", pattern);
144     }
145   }
146 }
147
Popular Tags