Coverage Report - org.jtheque.primary.utils.web.analyzers.generic.value.ConditionalValueGetterFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
ConditionalValueGetterFactory
0 %
0/19
0 %
0/6
1.286
ConditionalValueGetterFactory$1
N/A
N/A
1.286
ConditionalValueGetterFactory$ConditionalValueGetter
0 %
0/12
0 %
0/4
1.286
ConditionalValueGetterFactory$Else
0 %
0/5
N/A
1.286
ConditionalValueGetterFactory$If
0 %
0/7
N/A
1.286
 
 1  
 package org.jtheque.primary.utils.web.analyzers.generic.value;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *            
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License. 
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import org.jdom.Element;
 20  
 import org.jtheque.core.utils.file.XMLException;
 21  
 import org.jtheque.core.utils.file.XMLReader;
 22  
 import org.jtheque.primary.utils.web.analyzers.generic.Factory;
 23  
 import org.jtheque.primary.utils.web.analyzers.generic.condition.Condition;
 24  
 import org.jtheque.primary.utils.web.analyzers.generic.condition.ConditionUtils;
 25  
 
 26  
 import java.util.ArrayList;
 27  
 import java.util.Collection;
 28  
 
 29  
 /**
 30  
  * @author Baptiste Wicht
 31  
  */
 32  0
 final class ConditionalValueGetterFactory implements Factory<ValueGetter> {
 33  
         @Override
 34  
         public boolean canFactor(Element element, XMLReader reader) throws XMLException{
 35  0
                 return !reader.getNodes("conditional", element).isEmpty();
 36  
         }
 37  
 
 38  
         @Override
 39  
         public ValueGetter factor(Element element, XMLReader reader) throws XMLException{
 40  0
                 Element n = reader.getNode("conditional", element);
 41  
 
 42  0
                 ConditionalValueGetter conditionalGetter = new ConditionalValueGetter();
 43  
 
 44  0
                 conditionalGetter.addValue(getIf(reader.getNode("if", n), reader));
 45  
 
 46  0
                 for (Element elseIfNode : reader.getNodes("elseif", n)){
 47  0
                         conditionalGetter.addValue(getIf(elseIfNode, reader));
 48  
                 }
 49  
 
 50  0
                 Element elseNode = reader.getNode("else", n);
 51  
 
 52  0
                 if (elseNode != null){
 53  0
                         conditionalGetter.addValue(getElse(elseNode, reader));
 54  
                 }
 55  
 
 56  0
                 return conditionalGetter;
 57  
         }
 58  
 
 59  
         /**
 60  
          * Return an Else object in a specific node.
 61  
          *
 62  
          * @param node The nod to search in.
 63  
          * @param reader The XML reader.
 64  
          *
 65  
          * @return The else object.
 66  
          *
 67  
          * @throws XMLException If an errors occurs during the parse of the XML Elements.
 68  
          */
 69  
         private static ConditionalValue getElse(Element node, XMLReader reader) throws XMLException{
 70  0
                 Else elseCondition = new Else();
 71  
 
 72  0
                 elseCondition.setGetter(ValueGetterFactory.getValueGetter(node, reader));
 73  
 
 74  0
                 return elseCondition;
 75  
         }
 76  
 
 77  
         /**
 78  
          * Return an If object in a specific node.
 79  
          *
 80  
          * @param node The nod to search in.
 81  
          * @param reader The XML reader.
 82  
          *
 83  
          * @return The
 84  
          *
 85  
          * @throws XMLException If an errors occurs during the parse of the XML Elements.
 86  
          */
 87  
         private static ConditionalValue getIf(Element node, XMLReader reader) throws XMLException{
 88  0
                 If ifCondition = new If();
 89  
 
 90  0
                 ifCondition.setCondition(ConditionUtils.getCondition(node, "condition", reader));
 91  0
                 ifCondition.setGetter(ValueGetterFactory.getValueGetter(node, reader));
 92  
 
 93  0
                 return ifCondition;
 94  
         }
 95  
 
 96  
         /**
 97  
          * A conditional value getter.
 98  
          *
 99  
          * @author Baptiste Wicht
 100  
          */
 101  0
         private static final class ConditionalValueGetter implements ValueGetter {
 102  
                 private final Collection<ConditionalValue> values;
 103  
 
 104  
                 /**
 105  
                  * Construct a new ConditionalValueGetter.
 106  
                  */
 107  
                 private ConditionalValueGetter(){
 108  0
                         super();
 109  
 
 110  0
                         values = new ArrayList<ConditionalValue>(5);
 111  0
                 }
 112  
 
 113  
                 /**
 114  
                  * Add a conditional value to the getter.
 115  
                  *
 116  
                  * @param value The conditional value to add.
 117  
                  */
 118  
                 public void addValue(ConditionalValue value){
 119  0
                         values.add(value);
 120  0
                 }
 121  
 
 122  
                 @Override
 123  
                 public String getValue(String line){
 124  0
                         String value = null;
 125  
 
 126  0
                         for (ConditionalValue v : values){
 127  0
                                 if (v.match(line)){
 128  0
                                         value = v.getValue(line);
 129  0
                                         break;
 130  
                                 }
 131  
                         }
 132  
 
 133  0
                         return value;
 134  
                 }
 135  
         }
 136  
 
 137  
         /**
 138  
          * A else conditional value.
 139  
          *
 140  
          * @author Baptiste Wicht
 141  
          */
 142  0
         private static final class Else implements ConditionalValue {
 143  
                 private ValueGetter getter;
 144  
 
 145  
                 /**
 146  
                  * Set the value getter.
 147  
                  *
 148  
                  * @param getter The value getter.
 149  
                  */
 150  
                 private void setGetter(ValueGetter getter){
 151  0
                         this.getter = getter;
 152  0
                 }
 153  
 
 154  
                 @Override
 155  
                 public String getValue(String line){
 156  0
                         return getter.getValue(line);
 157  
                 }
 158  
 
 159  
                 @Override
 160  
                 public boolean match(String line){
 161  0
                         return true;
 162  
                 }
 163  
         }
 164  
 
 165  
         /**
 166  
          * A if conditional value.
 167  
          *
 168  
          * @author Baptiste Wicht
 169  
          */
 170  0
         private static final class If implements ConditionalValue {
 171  
                 private Condition condition;
 172  
                 private ValueGetter getter;
 173  
 
 174  
                 @Override
 175  
                 public String getValue(String line){
 176  0
                         return getter.getValue(line);
 177  
                 }
 178  
 
 179  
                 @Override
 180  
                 public boolean match(String line){
 181  0
                         return condition.match(line);
 182  
                 }
 183  
 
 184  
                 /**
 185  
                  * Set the condition of the if.
 186  
                  *
 187  
                  * @param condition The condition.
 188  
                  */
 189  
                 public void setCondition(Condition condition){
 190  0
                         this.condition = condition;
 191  0
                 }
 192  
 
 193  
                 /**
 194  
                  * Set the value getter of the if.
 195  
                  *
 196  
                  * @param getter The value getter.
 197  
                  */
 198  
                 public void setGetter(ValueGetter getter){
 199  0
                         this.getter = getter;
 200  0
                 }
 201  
         }
 202  
 }