Coverage Report - nl.beesting.beangenerator.generator.PhoneticalNameGenerator
 
Classes in this File Line Coverage Branch Coverage Complexity
PhoneticalNameGenerator
79%
55/70
79%
22/28
0
 
 1  
 package nl.beesting.beangenerator.generator;
 2  
 
 3  
 import java.lang.reflect.Constructor;
 4  
 import java.net.URL;
 5  
 
 6  
 import nl.beesting.beangenerator.TypeNotSupportedException;
 7  
 import nl.beesting.beangenerator.util.ParameterDescriptor;
 8  
 import nl.beesting.beangenerator.util.ParameterPair;
 9  
 import nl.beesting.beangenerator.util.ParameterPairUtil;
 10  
 import nl.beesting.beangenerator.util.RandomUtil;
 11  
 
 12  
 import org.apache.commons.logging.Log;
 13  
 import org.apache.commons.logging.LogFactory;
 14  
 import org.apache.xerces.parsers.DOMParser;
 15  
 import org.w3c.dom.Document;
 16  
 import org.w3c.dom.Element;
 17  
 import org.w3c.dom.Node;
 18  
 import org.w3c.dom.NodeList;
 19  
 import org.xml.sax.InputSource;
 20  
 
 21  
 public class PhoneticalNameGenerator extends AbstractInstanceGenerator {
 22  
 
 23  
    /** The <code>logger</code> for this class. */
 24  1
    private static final Log logger = LogFactory.getLog(PhoneticalNameGenerator.class);
 25  
    public static final String DEFAULT_NAME_DATA = "NameData.xml";
 26  
    public static final String NAME_DATA_PARAMETER = "nameDataLocation";
 27  
    public static final String DEFAULT_NAME_TEMPLATE = "Sawyer";
 28  
    public static final String NAME_TEMPLATE_PARAMETER = "nameTemplate";
 29  
    private String nameTemplate;
 30  
    private NodeList phonemes;
 31  
    private NodeList nameTemplates;
 32  
 
 33  2
    public PhoneticalNameGenerator() {
 34  2
       addSupportedTypes(new Class[]{java.lang.String.class, java.lang.StringBuffer.class});
 35  2
       setSupportedParameterDescriptors(new ParameterDescriptor[]{
 36  
          new ParameterDescriptor(NAME_DATA_PARAMETER, String.class, "NameData location. Points to the location of where the NameData.xml should be."),
 37  
          new ParameterDescriptor(NAME_TEMPLATE_PARAMETER, String.class, "Name template specified in the namedate file used to generated the names.")
 38  
       });
 39  2
    }
 40  
 
 41  
    /**
 42  
     * Initializes the instnce generator with the given array of objects.
 43  
     */
 44  
    public void init(ParameterPair[] initParams) {
 45  10
       ClassLoader loader = this.getClass().getClassLoader();
 46  
 
 47  10
       String nameDateLocation = null;
 48  10
       if (initParams != null) {
 49  4
          nameDateLocation = (String) ParameterPairUtil.getFirstParameterValue(initParams, NAME_DATA_PARAMETER, false, String.class);
 50  4
          nameTemplate = (String) ParameterPairUtil.getFirstParameterValue(initParams, NAME_TEMPLATE_PARAMETER, false, String.class);
 51  
 
 52  4
          if (nameDateLocation == null) {
 53  4
             nameDateLocation = DEFAULT_NAME_DATA;
 54  
          }
 55  
 
 56  4
          if (nameTemplate == null) {
 57  1
             nameTemplate = DEFAULT_NAME_TEMPLATE;
 58  
          }
 59  
 
 60  
       } else {
 61  6
          nameDateLocation = DEFAULT_NAME_DATA;
 62  6
          nameTemplate = DEFAULT_NAME_TEMPLATE;
 63  
       }
 64  
 
 65  10
       URL nameData = loader.getResource(nameDateLocation);
 66  
 
 67  10
       if (nameData == null) {
 68  0
          nameData = loader.getResource(DEFAULT_NAME_DATA);
 69  
       }
 70  
 
 71  10
       DOMParser parser = new DOMParser();
 72  
       try {
 73  10
          parser.parse(new InputSource(nameData.openStream()));
 74  0
       } catch (Exception e) {
 75  
          // TODO read default
 76  0
          logger.error("unable to read namedata.xml", e);
 77  10
       }
 78  
 
 79  10
       Document document = parser.getDocument();
 80  
 
 81  10
       phonemes = document.getElementsByTagName("Phoneme");
 82  10
       nameTemplates = document.getElementsByTagName("NameTemplate");
 83  10
    }
 84  
 
 85  
    public Object generateInstance(Class<?> type) throws TypeNotSupportedException {
 86  4000
       if (type == null) {
 87  0
          throw new IllegalArgumentException("Input parameter type cannot be null.");
 88  
       }
 89  4000
       if (!checkSupport(type)) {
 90  0
          throw new TypeNotSupportedException(type.getName(), "generateInstance");
 91  
       }
 92  
 
 93  4000
       StringBuffer name = new StringBuffer();
 94  4000
       Element template = null;
 95  38000
       for (int i = 0; i < nameTemplates.getLength(); i++) {
 96  38000
          Element element = (Element) nameTemplates.item(i);
 97  38000
          if (nameTemplate.equals(element.getAttribute("Name"))) {
 98  4000
             template = element;
 99  4000
             break;
 100  
          }
 101  
       }
 102  
 
 103  4000
       if (template == null) {
 104  0
          throw new IllegalArgumentException("Specified template name: " + nameTemplate + " does not exsist");
 105  
       }
 106  
 
 107  60000
       for (int i = 0; i < template.getChildNodes().getLength(); i++) {
 108  56000
          Node node = template.getChildNodes().item(i);
 109  56000
          String phonemeName = node.getFirstChild() != null ? node.getFirstChild().getNodeValue() : null;
 110  
 
 111  1456000
          for (int j = 0; j < phonemes.getLength(); j++) {
 112  1400000
             Element phonemeElement = (Element) phonemes.item(j);
 113  1400000
             if (phonemeElement.getAttribute("Name").equals(phonemeName)) {
 114  26000
                NodeList nodeList = phonemeElement.getElementsByTagName("Part");
 115  26000
                int random = RandomUtil.initRandomIntBetween(0, nodeList.getLength());
 116  26000
                Node n = nodeList.item(random);
 117  26000
                String namePart = n.getFirstChild().getNodeValue();
 118  26000
                if (node.getAttributes().item(0).getNodeValue().equals("Cap")) {
 119  6000
                   name.append(namePart.substring(0, 1).toUpperCase()).append(namePart.substring(1));
 120  
                } else {
 121  20000
                   name.append(namePart);
 122  
                }
 123  
             }
 124  
          }
 125  
       }
 126  
 
 127  
       try {
 128  4000
          Constructor<?> c = type.getConstructor(String.class);
 129  4000
          return c.newInstance(name.toString());
 130  0
       } catch (Exception e) {
 131  0
          logger.warn("Could not construct Object of type " + type + " . Returning null.", e);
 132  
       }
 133  
 
 134  0
       return null;
 135  
    }
 136  
 
 137  
    public Object generateInstance(Class<?> type, int modus) throws TypeNotSupportedException {
 138  0
       return generateInstance(type);
 139  
    }
 140  
 
 141  
    protected Object getDefaultFromValue() {
 142  
       // TODO Auto-generated method stub
 143  2
       return null;
 144  
    }
 145  
 
 146  
    protected Object getDefaultUntilValue() {
 147  
       // TODO Auto-generated method stub
 148  2
       return null;
 149  
    }
 150  
 
 151  
    protected Object getFromValue() {
 152  
       // TODO Auto-generated method stub
 153  2
       return null;
 154  
    }
 155  
 
 156  
    protected Object getUntilValue() {
 157  
       // TODO Auto-generated method stub
 158  2
       return null;
 159  
    }
 160  
 
 161  
    public String getNameTemplate() {
 162  6
       return nameTemplate;
 163  
    }
 164  
 
 165  
    public static void main(String[] args) throws TypeNotSupportedException {
 166  0
       PhoneticalNameGenerator generator = new PhoneticalNameGenerator();
 167  0
       generator.init(null);
 168  0
       String test = (String) generator.generateInstance(String.class);
 169  0
       System.out.println(test);
 170  0
    }
 171  
 }