Coverage Report - org.jtheque.primary.utils.web.analyzers.generic.field.MultiLineFieldGetterFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
MultiLineFieldGetterFactory
0 %
0/12
N/A
2
MultiLineFieldGetterFactory$1
N/A
N/A
2
MultiLineFieldGetterFactory$MultiLineFieldGetter
0 %
0/40
0 %
0/20
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.field.SimpleFieldGetterFactory.SimpleFieldGetter;
 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.ValueGetterFactory;
 28  
 
 29  
 /**
 30  
  * @author Baptiste Wicht
 31  
  */
 32  0
 final class MultiLineFieldGetterFactory extends AbstractFieldGetterFactory {
 33  
         @Override
 34  
         public boolean canFactor(Element element, XMLReader reader) throws XMLException{
 35  0
                 return "true".equals(reader.readString("@multiline", element));
 36  
         }
 37  
 
 38  
         @Override
 39  
         public FieldGetter factor(Element element, XMLReader reader) throws XMLException{
 40  0
                 MultiLineFieldGetter getter = new MultiLineFieldGetter();
 41  
 
 42  0
                 getter.setFieldName(reader.readString("@field", element));
 43  0
                 getter.setValueGetter(ValueGetterFactory.getValueGetter(element, reader));
 44  0
                 getter.setStartCondition(ConditionUtils.getCondition(element, "startcondition", reader));
 45  0
                 getter.setStopCondition(ConditionUtils.getCondition(element, "stopcondition", reader));
 46  0
                 getter.setGetCondition(ConditionUtils.getCondition(element, "getcondition", reader));
 47  0
                 initTransformers(getter, element, reader);
 48  0
                 initOperations(getter, element, reader);
 49  
 
 50  0
                 return getter;
 51  
         }
 52  
 
 53  
         /**
 54  
          * A multi line field getter. It seems a field getter who get the value of the field on several
 55  
          * different lines.
 56  
          *
 57  
          * @author Baptiste Wicht
 58  
          */
 59  0
         private static final class MultiLineFieldGetter extends SimpleFieldGetter {
 60  
                 private boolean parse;
 61  0
                 private boolean first = true;
 62  
                 private boolean done;
 63  
 
 64  
                 private Condition startCondition;
 65  
                 private Condition stopCondition;
 66  
                 private Condition getCondition;
 67  
 
 68  0
                 private final StringBuilder builder = new StringBuilder(100);
 69  
 
 70  
                 @Override
 71  
                 public boolean mustGet(String line){
 72  0
                         if (done){
 73  0
                                 return false;
 74  
                         }
 75  
 
 76  0
                         testStartCondition(line);
 77  0
                         testStopCondition(line);
 78  0
                         appendLineIfNecessary(line);
 79  
 
 80  0
                         return done;
 81  
                 }
 82  
 
 83  
                 /**
 84  
                  * Test the stop condition on the line.
 85  
                  *
 86  
                  * @param line The line to test.
 87  
                  */
 88  
                 private void testStopCondition(String line){
 89  0
                         if (parse && stopCondition.match(line)){
 90  0
                                 parse = false;
 91  0
                                 done = true;
 92  
                         }
 93  0
                 }
 94  
 
 95  
                 /**
 96  
                  * Test the start condition on the line.
 97  
                  *
 98  
                  * @param line The line to test.
 99  
                  */
 100  
                 private void testStartCondition(String line){
 101  0
                         if (startCondition.match(line)){
 102  0
                                 parse = true;
 103  
                         }
 104  0
                 }
 105  
 
 106  
                 /**
 107  
                  * Append the line if necessary.
 108  
                  *
 109  
                  * @param line The line to test.
 110  
                  */
 111  
                 private void appendLineIfNecessary(String line){
 112  0
                         if (parse && getCondition.match(line)){
 113  0
                                 if (first){
 114  0
                                         first = false;
 115  
                                 } else {
 116  0
                                         builder.append("%%%");
 117  
                                 }
 118  
 
 119  0
                                 builder.append(getValueGetter().getValue(line));
 120  
                         }
 121  0
                 }
 122  
 
 123  
                 @Override
 124  
                 public String getValue(String line){
 125  0
                         if (done){
 126  0
                                 done = false;
 127  0
                                 parse = false;
 128  0
                                 first = true;
 129  
 
 130  0
                                 String value = builder.toString();
 131  
 
 132  0
                                 if (!getTransformers().isEmpty()){
 133  0
                                         for (Transformer transformer : getTransformers()){
 134  0
                                                 value = transformer.transform(value);
 135  
                                         }
 136  
                                 }
 137  
 
 138  0
                                 return value;
 139  
                         }
 140  
 
 141  0
                         return null;
 142  
                 }
 143  
 
 144  
                 @Override
 145  
                 public String performOperations(String line, ScannerPossessor analyzer){
 146  0
                         return line;
 147  
                 }
 148  
 
 149  
                 /**
 150  
                  * Set the start condition.
 151  
                  *
 152  
                  * @param startCondition The condition who indicate when the parse must be started.
 153  
                  */
 154  
                 public void setStartCondition(Condition startCondition){
 155  0
                         this.startCondition = startCondition;
 156  0
                 }
 157  
 
 158  
                 /**
 159  
                  * Set the stop condition.
 160  
                  *
 161  
                  * @param stopCondition The condition who indicate when the parse must be stopped.
 162  
                  */
 163  
                 public void setStopCondition(Condition stopCondition){
 164  0
                         this.stopCondition = stopCondition;
 165  0
                 }
 166  
 
 167  
                 /**
 168  
                  * Set the get condition. This condition indicate to the getter when it must analyze the line
 169  
                  * to get a value.
 170  
                  *
 171  
                  * @param getCondition The condition who indicate when the parser must get a value.
 172  
                  */
 173  
                 public void setGetCondition(Condition getCondition){
 174  0
                         this.getCondition = getCondition;
 175  0
                 }
 176  
 
 177  
                 @Override
 178  
                 public String toString(){
 179  0
                         return "MultiLineFieldGetter{" +
 180  
                                         "parse=" + parse +
 181  
                                         ", first=" + first +
 182  
                                         ", startCondition=" + startCondition +
 183  
                                         ", stopCondition=" + stopCondition +
 184  
                                         ", getCondition=" + getCondition +
 185  
                                         ", builder=" + builder +
 186  
                                         '}';
 187  
                 }
 188  
         }
 189  
 }