Coverage Report - org.apache.commons.flatfile.dsl.ParserEntityFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
ParserEntityFactory
72%
46/64
54%
13/24
0
ParserEntityFactory$1
100%
4/4
N/A
0
ParserEntityFactory$EntityHolder
100%
8/8
100%
2/2
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 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  
  * Entity factory; provides access to named entities parsed from one or more description files.
 39  
  * @version $Revision: 760672 $ $Date: 2009-03-31 16:45:13 -0500 (Tue, 31 Mar 2009) $
 40  
  */
 41  71
 public class ParserEntityFactory implements EntityFactory {
 42  
     /** Field option constant */
 43  
     public static final String OPTION_FIELD = "field";
 44  
     /** DynamicField option constant */
 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  
          * Create a new EntityHolder instance.
 56  
          * @param definition base
 57  
          */
 58  273
         EntityHolder(EntityDefinition definition) {
 59  273
             this.definition = definition;
 60  273
         }
 61  
 
 62  
         /**
 63  
          * Get the prototypical entity from this {@link EntityHolder}.
 64  
          * @return Entity
 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  
      * Create a new ParserEntityFactory.
 95  
      */
 96  0
     public ParserEntityFactory() {
 97  0
     }
 98  
 
 99  
     /**
 100  
      * Create a new ParserEntityFactory.
 101  
      * @param source to read
 102  
      */
 103  32
     public ParserEntityFactory(InputStream source) {
 104  32
         setSource(source);
 105  32
     }
 106  
 
 107  
     /**
 108  
      * Create a new ParserEntityFactory.
 109  
      * @param sources to read
 110  
      */
 111  0
     public ParserEntityFactory(InputStream[] sources) {
 112  0
         setSources(sources);
 113  0
     }
 114  
 
 115  
     /**
 116  
      * {@inheritDoc}
 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  
      * Validate all defined entities.
 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  
      * Get the entityNameStrategy.
 140  
      * @return EntityNameStrategy.
 141  
      */
 142  
     public synchronized EntityNameStrategy getEntityNameStrategy() {
 143  88
         return entityNameStrategy == null ? DEFAULT_NAME_STRATEGY : entityNameStrategy;
 144  
     }
 145  
 
 146  
     /**
 147  
      * Set the entityNameStrategy.
 148  
      * @param entityNameStrategy The EntityNameStrategy entityNameStrategy to set.
 149  
      */
 150  
     public synchronized void setEntityNameStrategy(EntityNameStrategy entityNameStrategy) {
 151  2
         this.entityNameStrategy = entityNameStrategy;
 152  2
     }
 153  
 
 154  
     /**
 155  
      * Get the InputStream[] sources.
 156  
      * @return InputStream[]
 157  
      */
 158  
     public InputStream[] getSources() {
 159  32
         return sources;
 160  
     }
 161  
 
 162  
     /**
 163  
      * Set the InputStream[] sources.
 164  
      * @param sources InputStream[]
 165  
      */
 166  
     public void setSources(InputStream[] sources) {
 167  32
         this.sources = sources;
 168  32
     }
 169  
 
 170  
     /**
 171  
      * Convenience setter for a single InputStream source.
 172  
      * @param source to set
 173  
      */
 174  
     public void setSource(InputStream source) {
 175  32
         setSources(new InputStream[] { source });
 176  32
     }
 177  
 
 178  
     /**
 179  
      * Get the parent.
 180  
      * @return EntityFactory
 181  
      */
 182  
     public EntityFactory getParent() {
 183  0
         return parent;
 184  
     }
 185  
 
 186  
     /**
 187  
      * Set the parent.
 188  
      * @param parent EntityFactory
 189  
      */
 190  
     public void setParent(EntityFactory parent) {
 191  0
         this.parent = parent;
 192  0
     }
 193  
 
 194  
     /**
 195  
      * Add an EntityDefinition
 196  
      * @param name to associate
 197  
      * @param def to add
 198  
      */
 199  
     void add(String name, EntityDefinition def) {
 200  273
         entityMap.put(name, new EntityHolder(def));
 201  273
     }
 202  
 
 203  
     /**
 204  
      * Set the default options for a given entity "type".
 205  
      * @param type String key
 206  
      * @param options option Map
 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  
      * Get the default option map for a given entity "type".
 217  
      * @param type String key
 218  
      * @return option Map
 219  
      */
 220  
     Map<String, ? extends Object> getDefaultOptions(String type) {
 221  0
         return defaultOptionMaps.get(type);
 222  
     }
 223  
 
 224  
     /**
 225  
      * Create a Field of a particular length, applying default options.
 226  
      * @param length of field
 227  
      * @return Field
 228  
      */
 229  
     Field createField(int length) {
 230  76
         return applyDefaultOptions(new Field(length), OPTION_FIELD);
 231  
     }
 232  
 
 233  
     /**
 234  
      * Create a dynamic field, applying default options.
 235  
      * @return DynamicField
 236  
      */
 237  
     DynamicField createDynamicField() {
 238  14
         return applyDefaultOptions(new DynamicField(), OPTION_DYNAMIC_FIELD);
 239  
     }
 240  
 
 241  
     /**
 242  
      * Create a field of a particular value, applying default options.
 243  
      * @param value of the field.
 244  
      * @return Field
 245  
      */
 246  
     Field createField(byte[] value) {
 247  38
         return applyDefaultOptions(new Field(value), OPTION_FIELD);
 248  
     }
 249  
 
 250  
     /**
 251  
      * Apply the default options of the specified type to the specified Entity.
 252  
      * @param <E> Entity subtype
 253  
      * @param e E
 254  
      * @param type associated field type
 255  
      * @return e
 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  
      * Get this thread's {@link EntityTreeParser}
 264  
      * @return {@link EntityTreeParser}
 265  
      */
 266  
     private EntityTreeParser getTreeParser() {
 267  103
         return treeParser.get();
 268  
     }
 269  
 
 270  
     /**
 271  
      * Return our entity map, initializing if necessary.
 272  
      * @return Map<String, EntityProxy> - value may be an EntityDefinition or an Entity.
 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  
 }