Coverage Report - org.jtheque.primary.utils.web.analyzers.generic.position.StringPositionFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
StringPositionFactory
0 %
0/15
0 %
0/8
2.111
StringPositionFactory$1
N/A
N/A
2.111
StringPositionFactory$StringPosition
0 %
0/19
0 %
0/12
2.111
 
 1  
 package org.jtheque.primary.utils.web.analyzers.generic.position;
 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  
 
 24  
 /**
 25  
  * @author Baptiste Wicht
 26  
  */
 27  0
 final class StringPositionFactory implements Factory<Position> {
 28  
         @Override
 29  
         public boolean canFactor(Element element, XMLReader reader) throws XMLException{
 30  0
                 return "string".equals(element.getName());
 31  
         }
 32  
 
 33  
         @Override
 34  
         public Position factor(Element node, XMLReader reader) throws XMLException{
 35  0
                 StringPosition p = new StringPosition(reader.readString("text", node));
 36  
 
 37  0
                 if ("true".equals(reader.readString("@first", node))){
 38  0
                         p.setFirst();
 39  
                 }
 40  
 
 41  0
                 if ("true".equals(reader.readString("@last", node))){
 42  0
                         p.setLast();
 43  
                 }
 44  
 
 45  0
                 Element fromNode = reader.getNode("from", node);
 46  
 
 47  0
                 if (fromNode != null){
 48  0
                         p.setFrom(fromNode.getText());
 49  
                 }
 50  
 
 51  0
                 Object addNode = reader.getNode("add", node);
 52  
 
 53  0
                 if (addNode != null){
 54  0
                         p.setAdd(Integer.parseInt(((Element) addNode).getText()));
 55  
                 }
 56  
 
 57  0
                 return p;
 58  
         }
 59  
 
 60  
         /**
 61  
          * A position of a String in a line.
 62  
          *
 63  
          * @author Baptiste Wicht
 64  
          */
 65  0
         private static final class StringPosition implements Position {
 66  
                 private final String text;
 67  
                 private String from;
 68  
                 private boolean first;
 69  
                 private boolean last;
 70  
                 private int add;
 71  
 
 72  
                 /**
 73  
                  * Construct a new StringPosition.
 74  
                  *
 75  
                  * @param text The text to search in the line.
 76  
                  */
 77  
                 private StringPosition(String text){
 78  0
                         super();
 79  
 
 80  0
                         this.text = text;
 81  0
                 }
 82  
 
 83  
                 /**
 84  
                  * Set the boolean flag indicate if we must add the length of the String to the position.
 85  
                  */
 86  
                 public void setFirst(){
 87  0
                         first = true;
 88  0
                 }
 89  
 
 90  
                 /**
 91  
                  * Set the boolean flag indicate if we must search the last occurrence.
 92  
                  */
 93  
                 public void setLast(){
 94  0
                         last = true;
 95  0
                 }
 96  
 
 97  
                 /**
 98  
                  * Set the String from which we start to search the occurrence.
 99  
                  *
 100  
                  * @param from The String from which we start to search the occurrence.
 101  
                  */
 102  
                 public void setFrom(String from){
 103  0
                         this.from = from;
 104  0
                 }
 105  
 
 106  
                 /**
 107  
                  * Set a int value to add to the position.
 108  
                  *
 109  
                  * @param add The value to add to the position.
 110  
                  */
 111  
                 public void setAdd(int add){
 112  0
                         this.add = add;
 113  0
                 }
 114  
 
 115  
                 @Override
 116  
                 public int intValue(String line){
 117  
                         int index;
 118  
 
 119  0
                         if (last){
 120  0
                                 index = from != null && !from.isEmpty() ? line.lastIndexOf(text, line.indexOf(from)) : line.lastIndexOf(text);
 121  
                         } else {
 122  0
                                 index = from != null && !from.isEmpty() ? line.indexOf(text, line.indexOf(from)) : line.indexOf(text);
 123  
                         }
 124  
 
 125  0
                         if (first){
 126  0
                                 index = index + text.length() + add;
 127  
                         }
 128  
 
 129  0
                         return index;
 130  
                 }
 131  
 
 132  
                 @Override
 133  
                 public String toString(){
 134  0
                         return "StringPosition{" +
 135  
                                         "text='" + text + '\'' +
 136  
                                         ", from='" + from + '\'' +
 137  
                                         ", first=" + first +
 138  
                                         ", last=" + last +
 139  
                                         ", add=" + add +
 140  
                                         '}';
 141  
                 }
 142  
         }
 143  
 }