Coverage Report - org.jtheque.primary.utils.web.analyzers.generic.field.SimpleFieldGetterFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
SimpleFieldGetterFactory
0 %
0/10
N/A
1.2
SimpleFieldGetterFactory$SimpleFieldGetter
0 %
0/28
0 %
0/6
1.2
 
 1  
 package org.jtheque.primary.utils.web.analyzers.generic.field;
 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.condition.Condition;
 23  
 import org.jtheque.primary.utils.web.analyzers.generic.condition.ConditionUtils;
 24  
 import org.jtheque.primary.utils.web.analyzers.generic.operation.Operation;
 25  
 import org.jtheque.primary.utils.web.analyzers.generic.operation.ScannerPossessor;
 26  
 import org.jtheque.primary.utils.web.analyzers.generic.transform.Transformer;
 27  
 import org.jtheque.primary.utils.web.analyzers.generic.value.ValueGetter;
 28  
 import org.jtheque.primary.utils.web.analyzers.generic.value.ValueGetterFactory;
 29  
 
 30  
 import java.util.ArrayList;
 31  
 import java.util.Collection;
 32  
 
 33  
 /**
 34  
  * @author Baptiste Wicht
 35  
  */
 36  0
 final class SimpleFieldGetterFactory extends AbstractFieldGetterFactory {
 37  
         @Override
 38  
         public boolean canFactor(Element element, XMLReader reader) throws XMLException{
 39  0
                 return "getter".equals(element.getName());
 40  
         }
 41  
 
 42  
         @Override
 43  
         public FieldGetter factor(Element element, XMLReader reader) throws XMLException{
 44  0
                 SimpleFieldGetter getter = new SimpleFieldGetter();
 45  
 
 46  0
                 getter.setFieldName(reader.readString("@field", element));
 47  0
                 getter.setLineCondition(ConditionUtils.getCondition(element, "condition", reader));
 48  0
                 getter.setValueGetter(ValueGetterFactory.getValueGetter(element, reader));
 49  0
                 initTransformers(getter, element, reader);
 50  0
                 initOperations(getter, element, reader);
 51  
 
 52  0
                 return getter;
 53  
         }
 54  
 
 55  
         /**
 56  
          * A Field Getter. It's an object who's responsible to get the value of a field of a film in
 57  
          * the site.
 58  
          *
 59  
          * @author Baptiste Wicht
 60  
          */
 61  0
         static class SimpleFieldGetter implements FieldGetter {
 62  
                 private String fieldName;
 63  
                 private Condition lineCondition;
 64  
                 private ValueGetter valueGetter;
 65  
                 private final Collection<Transformer> transformers;
 66  
                 private final Collection<Operation> operations;
 67  
 
 68  
                 /**
 69  
                  * Construct a new FieldGetter.
 70  
                  */
 71  
                 SimpleFieldGetter(){
 72  0
                         super();
 73  
 
 74  0
                         transformers = new ArrayList<Transformer>(5);
 75  0
                         operations = new ArrayList<Operation>(5);
 76  0
                 }
 77  
 
 78  
                 /**
 79  
                  * Set the name of the field for which the getter is made.
 80  
                  *
 81  
                  * @param fieldName The name of the field.
 82  
                  */
 83  
                 public final void setFieldName(String fieldName){
 84  0
                         this.fieldName = fieldName;
 85  0
                 }
 86  
 
 87  
                 @Override
 88  
                 public final String getFieldName(){
 89  0
                         return fieldName;
 90  
                 }
 91  
 
 92  
                 /**
 93  
                  * Set the condition to test if the getter must analyze the line or not.
 94  
                  *
 95  
                  * @param lineCondition The condition.
 96  
                  */
 97  
                 public final void setLineCondition(Condition lineCondition){
 98  0
                         this.lineCondition = lineCondition;
 99  0
                 }
 100  
 
 101  
                 /**
 102  
                  * Set the value getter of the field getter. This object is responsible to get the
 103  
                  * value of the field if the condition match the line.
 104  
                  *
 105  
                  * @param valueGetter The value getter.
 106  
                  */
 107  
                 public final void setValueGetter(ValueGetter valueGetter){
 108  0
                         this.valueGetter = valueGetter;
 109  0
                 }
 110  
 
 111  
                 /**
 112  
                  * Return the value getter of the field getter.
 113  
                  *
 114  
                  * @return The value getter.
 115  
                  */
 116  
                 final ValueGetter getValueGetter(){
 117  0
                         return valueGetter;
 118  
                 }
 119  
 
 120  
                 @Override
 121  
                 public boolean mustGet(String line){
 122  0
                         return lineCondition.match(line);
 123  
                 }
 124  
 
 125  
                 @Override
 126  
                 public String getValue(String line){
 127  0
                         String value = valueGetter.getValue(line);
 128  
 
 129  0
                         if (!transformers.isEmpty()){
 130  0
                                 for (Transformer transformer : transformers){
 131  0
                                         value = transformer.transform(value);
 132  
                                 }
 133  
                         }
 134  
 
 135  0
                         return value;
 136  
                 }
 137  
 
 138  
                 /**
 139  
                  * Add a transformer to the getter.
 140  
                  *
 141  
                  * @param transformer The transformer to add.
 142  
                  */
 143  
                 public final void addTransformer(Transformer transformer){
 144  0
                         transformers.add(transformer);
 145  0
                 }
 146  
 
 147  
                 /**
 148  
                  * Add an operation to the getter.
 149  
                  *
 150  
                  * @param operation The operation to add.
 151  
                  */
 152  
                 public final void addOperation(Operation operation){
 153  0
                         operations.add(operation);
 154  0
                 }
 155  
 
 156  
                 @Override
 157  
                 public String performOperations(String line, ScannerPossessor analyzer){
 158  0
                         String performed = line;
 159  
 
 160  0
                         for (Operation operation : operations){
 161  0
                                 performed = operation.perform(performed, analyzer);
 162  
                         }
 163  
 
 164  0
                         return performed;
 165  
                 }
 166  
 
 167  
                 @Override
 168  
                 public String toString(){
 169  0
                         return "SimpleFieldGetter{" +
 170  
                                         "fieldName='" + fieldName + '\'' +
 171  
                                         ", lineCondition=" + lineCondition +
 172  
                                         ", valueGetter=" + valueGetter +
 173  
                                         ", transformers=" + transformers +
 174  
                                         ", operations=" + operations +
 175  
                                         '}';
 176  
                 }
 177  
 
 178  
                 /**
 179  
                  * Return all the transformers of the field getter.
 180  
                  *
 181  
                  * @return A collection of all the transformers of the getter.
 182  
                  */
 183  
                 public final Collection<Transformer> getTransformers(){
 184  0
                         return transformers;
 185  
                 }
 186  
         }
 187  
 }