| 1 | |
package org.jtheque.primary.utils.web.analyzers.generic.field; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 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 | |
|
| 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 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 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 | |
|
| 85 | |
|
| 86 | |
|
| 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 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
private void testStartCondition(String line){ |
| 101 | 0 | if (startCondition.match(line)){ |
| 102 | 0 | parse = true; |
| 103 | |
} |
| 104 | 0 | } |
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 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 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
public void setStartCondition(Condition startCondition){ |
| 155 | 0 | this.startCondition = startCondition; |
| 156 | 0 | } |
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
public void setStopCondition(Condition stopCondition){ |
| 164 | 0 | this.stopCondition = stopCondition; |
| 165 | 0 | } |
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 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 | |
} |