View Javadoc

1   /*
2    * Copyright 2005 Filipe Tavares
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.devyant.decorutils.xml;
17  
18  import org.apache.commons.beanutils.LazyDynaBean;
19  import org.devyant.decorutils.exceptions.InvalidRuleFormatException;
20  import org.devyant.decorutils.Utils;
21  
22  import java.util.Collection;
23  import java.util.Iterator;
24  import java.lang.reflect.InvocationTargetException;
25  
26  /***
27   * DynaNode is a <b>cool</b> class.
28   *
29   * @author Filipe Tavares
30   * @version $Revision: 1.0$ ($Author: ftavares$)
31   * @since 1/Fev/2005 1:51:13
32   */
33  public class DynaNode extends LazyDynaBean {
34      /***
35       * A Collection of child nodes.
36       */
37      private Collection children = null;
38      /***
39       * The parent <code>DynaNode</code>.
40       */
41      private DynaNode parent = null;
42      /***
43       * The tag's name.
44       */
45      private String name = null;
46      /***
47       * Prefix for class property references.
48       */
49      private static final String THIS_PREFIX = "this.";
50  
51      /***
52       * @return Returns the size of the children <code>Collection</code>.
53       */
54      public final int getChildrenCount() {
55          if (children == null) { // prevent NullPointerException
56              return 0;           // children==null is equivelent to being empty
57          }
58          return children.size();
59      }
60  
61      /***
62       * @return The child nodes
63       */
64      public final Collection getChildren() {
65          return children;
66      }
67  
68      /***
69       * @param children The child nodes
70       */
71      public final void setChildren(Collection children) {
72          this.children = children;
73      }
74  
75      /***
76       * @return The parent node
77       */
78      public final DynaNode getParent() {
79          return parent;
80      }
81  
82      /***
83       * @param parent The parent node
84       */
85      public final void setParent(DynaNode parent) {
86          this.parent = parent;
87      }
88  
89      /***
90       * @return The tag's name
91       */
92      public final String getName() {
93          return name;
94      }
95  
96      /***
97       * @param name The tag's name
98       */
99      public final void setName(String name) {
100         this.name = name;
101     }
102 
103     /***
104      * Validates node against a set of rules.
105      * @param rules Collection of rules
106      * @return <code>true</code> if node verifies rules
107      * @throws InvalidRuleFormatException Thrown when rules are badly formatted
108      * @todo add more operations: ~ (like) and ~* (ilike)
109      * @todo add escape '\' support for operators
110      * @todo generalize for any operator
111      */
112     public final boolean verifiesRules(final Collection rules)
113         throws InvalidRuleFormatException {
114         if (rules == null) { // no rules
115             return true;
116         }
117 
118         boolean verifies = false;
119 
120         for (Iterator i = rules.iterator(); i.hasNext();) {
121             String rule = String.valueOf(i.next());
122 
123             // todo: add more operations: ~ (like) and ~* (ilike)
124             // todo: add escape '\' support for operators
125             // todo: generalize for any operator
126             // lookup which operation is used
127             boolean different = true;
128             int index = rule.indexOf("!=");
129             if (index < 0) {
130                 different = false;
131                 index = rule.indexOf("=");
132             }
133 
134             // invalid rule
135             if (index < 0) {
136                 throw new InvalidRuleFormatException();
137             }
138 
139             // the attribute's name
140             String attrName = rule.substring(0, index);
141             // the attribute's value
142             String attr = String.valueOf(get(attrName));
143             // the attribute's comparison value
144             String value = rule.substring(index + (different ? 2 : 1));
145 
146             // equal operation value
147             verifies = attr.equals(value);
148 
149             // different operation value
150             if (different) {
151                 verifies = !verifies;
152             }
153 
154             if (verifies) {
155                 break;
156             }
157         }
158         return verifies;
159     }
160 
161     /***
162      * Checks for {@link #THIS_PREFIX}.
163      * @see org.apache.commons.beanutils.DynaBean#get(java.lang.String)
164      */
165     public final Object get(final String name) {
166         final Object value;
167         if (name.startsWith(THIS_PREFIX)) {
168             try {
169                 value = Utils.getClassProperty(DynaNode.this,
170                         name.substring(THIS_PREFIX.length()));
171             } catch (IllegalAccessException e) {
172                 return null;
173             } catch (InvocationTargetException e) {
174                 return null;
175             } catch (NoSuchMethodException e) {
176                 return null;
177             }
178         } else {
179             value = super.get(name);
180         }
181         return value;
182     }
183 }