| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.apache.commons.flatfile.dsl; |
| 18 | |
|
| 19 | |
import java.io.InputStream; |
| 20 | |
import java.util.ArrayList; |
| 21 | |
import java.util.HashMap; |
| 22 | |
import java.util.Map; |
| 23 | |
|
| 24 | |
import org.apache.commons.lang.ObjectUtils; |
| 25 | |
import org.apache.commons.logging.Log; |
| 26 | |
import org.apache.commons.logging.LogFactory; |
| 27 | |
|
| 28 | |
import antlr.TokenBuffer; |
| 29 | |
import antlr.collections.AST; |
| 30 | |
|
| 31 | |
import org.apache.commons.flatfile.DynamicField; |
| 32 | |
import org.apache.commons.flatfile.Entity; |
| 33 | |
import org.apache.commons.flatfile.EntityFactory; |
| 34 | |
import org.apache.commons.flatfile.Field; |
| 35 | |
import org.apache.commons.flatfile.util.ApplyOptions; |
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | 71 | public class ParserEntityFactory implements EntityFactory { |
| 42 | |
|
| 43 | |
public static final String OPTION_FIELD = "field"; |
| 44 | |
|
| 45 | |
public static final String OPTION_DYNAMIC_FIELD = "dynamicField"; |
| 46 | |
|
| 47 | 1 | private static final Log LOG = LogFactory.getLog(ParserEntityFactory.class); |
| 48 | 1 | private static final EntityNameStrategy DEFAULT_NAME_STRATEGY = new DefaultEntityNameStrategy(); |
| 49 | |
|
| 50 | |
private class EntityHolder { |
| 51 | |
private EntityDefinition definition; |
| 52 | |
private Entity prototype; |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | 273 | EntityHolder(EntityDefinition definition) { |
| 59 | 273 | this.definition = definition; |
| 60 | 273 | } |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
Entity getPrototype() { |
| 67 | 88 | synchronized (this) { |
| 68 | 88 | if (prototype == null) { |
| 69 | 71 | prototype = getTreeParser().createEntity(definition); |
| 70 | |
} |
| 71 | 86 | } |
| 72 | 86 | return prototype; |
| 73 | |
} |
| 74 | |
} |
| 75 | |
|
| 76 | |
private EntityNameStrategy entityNameStrategy; |
| 77 | |
|
| 78 | 32 | private ThreadLocal<EntityTreeParser> treeParser = new ThreadLocal<EntityTreeParser>() { |
| 79 | 64 | protected EntityTreeParser initialValue() { |
| 80 | 32 | EntityTreeParser result = new EntityTreeParser(); |
| 81 | 32 | result.setEntityFactory(ParserEntityFactory.this); |
| 82 | 32 | return result; |
| 83 | |
} |
| 84 | |
}; |
| 85 | |
|
| 86 | |
private Map<String, EntityHolder> entityMap; |
| 87 | 32 | private Map<String, Map<String, ? extends Object>> defaultOptionMaps |
| 88 | |
= new HashMap<String, Map<String, ? extends Object>>(); |
| 89 | |
private boolean checked; |
| 90 | |
private InputStream[] sources; |
| 91 | |
private EntityFactory parent; |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | 0 | public ParserEntityFactory() { |
| 97 | 0 | } |
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | 32 | public ParserEntityFactory(InputStream source) { |
| 104 | 32 | setSource(source); |
| 105 | 32 | } |
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | 0 | public ParserEntityFactory(InputStream[] sources) { |
| 112 | 0 | setSources(sources); |
| 113 | 0 | } |
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
public final Entity getEntity(Object cue) { |
| 119 | 88 | EntityHolder holder = getEntityMap().get(getEntityNameStrategy().getEntityName(cue)); |
| 120 | 88 | if (holder != null) { |
| 121 | 88 | return holder.getPrototype().clone(); |
| 122 | |
} |
| 123 | 0 | return getParent() == null ? null : getParent().getEntity(cue); |
| 124 | |
} |
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
public final synchronized void validate() { |
| 130 | 0 | if (checked) { |
| 131 | 0 | return; |
| 132 | |
} |
| 133 | 0 | for (String name : getEntityMap().keySet()) { |
| 134 | 0 | getEntity(name); |
| 135 | |
} |
| 136 | 0 | } |
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
public synchronized EntityNameStrategy getEntityNameStrategy() { |
| 143 | 88 | return entityNameStrategy == null ? DEFAULT_NAME_STRATEGY : entityNameStrategy; |
| 144 | |
} |
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
public synchronized void setEntityNameStrategy(EntityNameStrategy entityNameStrategy) { |
| 151 | 2 | this.entityNameStrategy = entityNameStrategy; |
| 152 | 2 | } |
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
public InputStream[] getSources() { |
| 159 | 32 | return sources; |
| 160 | |
} |
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
public void setSources(InputStream[] sources) { |
| 167 | 32 | this.sources = sources; |
| 168 | 32 | } |
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
public void setSource(InputStream source) { |
| 175 | 32 | setSources(new InputStream[] { source }); |
| 176 | 32 | } |
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
public EntityFactory getParent() { |
| 183 | 0 | return parent; |
| 184 | |
} |
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
public void setParent(EntityFactory parent) { |
| 191 | 0 | this.parent = parent; |
| 192 | 0 | } |
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
void add(String name, EntityDefinition def) { |
| 200 | 273 | entityMap.put(name, new EntityHolder(def)); |
| 201 | 273 | } |
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
void setDefaultOptions(String type, Map<String, ? extends Object> options) { |
| 209 | 3 | Map<String, ? extends Object> old = defaultOptionMaps.put(type, options); |
| 210 | 3 | if (!ObjectUtils.equals(old, options)) { |
| 211 | 3 | LOG.warn("Overriding " + type + " options"); |
| 212 | |
} |
| 213 | 3 | } |
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
Map<String, ? extends Object> getDefaultOptions(String type) { |
| 221 | 0 | return defaultOptionMaps.get(type); |
| 222 | |
} |
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
Field createField(int length) { |
| 230 | 76 | return applyDefaultOptions(new Field(length), OPTION_FIELD); |
| 231 | |
} |
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
DynamicField createDynamicField() { |
| 238 | 14 | return applyDefaultOptions(new DynamicField(), OPTION_DYNAMIC_FIELD); |
| 239 | |
} |
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
Field createField(byte[] value) { |
| 247 | 38 | return applyDefaultOptions(new Field(value), OPTION_FIELD); |
| 248 | |
} |
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
private <E extends Entity> E applyDefaultOptions(E e, String type) { |
| 258 | 128 | Map<String, ? extends Object> m = defaultOptionMaps.get(type); |
| 259 | 128 | return m == null ? e : ApplyOptions.apply(e, m); |
| 260 | |
} |
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
private EntityTreeParser getTreeParser() { |
| 267 | 103 | return treeParser.get(); |
| 268 | |
} |
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
private synchronized Map<String, EntityHolder> getEntityMap() { |
| 275 | 88 | if (entityMap == null) { |
| 276 | 32 | entityMap = new HashMap<String, EntityHolder>(); |
| 277 | |
try { |
| 278 | 32 | EntityParser p = null; |
| 279 | 32 | ArrayList<AST> trees = new ArrayList<AST>(); |
| 280 | 32 | InputStream[] is = getSources(); |
| 281 | 64 | for (int i = 0; i < is.length; i++) { |
| 282 | 32 | TokenBuffer tb = new TokenBuffer(new EntityLexer(is[i])); |
| 283 | 32 | if (p == null) { |
| 284 | 32 | p = new EntityParser(tb); |
| 285 | |
} else { |
| 286 | 0 | p.setTokenBuffer(tb); |
| 287 | |
} |
| 288 | 32 | p.parse(); |
| 289 | 32 | trees.add(p.getAST()); |
| 290 | |
} |
| 291 | 32 | for (AST ast : trees) { |
| 292 | 32 | getTreeParser().load(ast); |
| 293 | |
} |
| 294 | 0 | } catch (Exception e) { |
| 295 | 0 | throw e instanceof RuntimeException ? (RuntimeException) e |
| 296 | |
: new RuntimeException(e); |
| 297 | 32 | } |
| 298 | |
} |
| 299 | 88 | return entityMap; |
| 300 | |
} |
| 301 | |
|
| 302 | |
} |