NameParser { public static CSLName[] parse(String names) { CharStream cs = CharStreams.fromString(names); InternalNameLexer lexer = new InternalNameLexer(cs); lexer.removeErrorListeners(); CommonTokenStream tokens = new CommonTokenStream(lexer); InternalNameParser parser = new InternalNameParser(tokens); parser.removeErrorListeners(); NamesContext ctx = parser.names(); if (ctx.result.isEmpty() || ctx.exception != null || parser.getNumberOfSyntaxErrors() > 0) { return new CSLName[] { new CSLNameBuilder().literal(names).build() }; } return ctx.result.toArray(new CSLName[0]); }  static CSLName[] parse(String names);  }
@Test public void familyOnly() { CSLName[] names = NameParser.parse("Thompson"); assertEquals(1, names.length); assertEquals("Thompson", names[0].getFamily()); }
@Test public void simple() { CSLName[] names = NameParser.parse("Ken Thompson"); assertEquals(1, names.length); assertEquals("Ken", names[0].getGiven()); assertEquals("Thompson", names[0].getFamily()); }
@Test public void middleName() { CSLName[] names = NameParser.parse("Dennis M. Ritchie"); assertEquals(1, names.length); assertEquals("Dennis M.", names[0].getGiven()); assertEquals("Ritchie", names[0].getFamily()); }
@Test public void initials() { CSLName[] names = NameParser.parse("S. C. Johnson"); assertEquals(1, names.length); assertEquals("S. C.", names[0].getGiven()); assertEquals("Johnson", names[0].getFamily()); }
@Test public void nonDroppingParticle() { CSLName[] names = NameParser.parse("Michael van Gerwen"); assertEquals(1, names.length); assertEquals("Michael", names[0].getGiven()); assertEquals("van", names[0].getNonDroppingParticle()); assertEquals("Gerwen", names[0].getFamily()); }
@Test public void nonDroppingParticleFamilyOnly() { CSLName[] names = NameParser.parse("van Gerwen"); assertEquals(1, names.length); assertEquals("van", names[0].getNonDroppingParticle()); assertEquals("Gerwen", names[0].getFamily()); }
@Test public void comma() { CSLName[] names = NameParser.parse("Thompson, Ken"); assertEquals(1, names.length); assertEquals("Ken", names[0].getGiven()); assertEquals("Thompson", names[0].getFamily()); }
@Test public void commaJunior() { CSLName[] names = NameParser.parse("Friedman, Jr., George"); assertEquals(1, names.length); assertEquals("George", names[0].getGiven()); assertEquals("Jr.", names[0].getSuffix()); assertEquals("Friedman", names[0].getFamily()); }
@Test public void commaNoJunior() { CSLName[] names = NameParser.parse("Familya Familyb, Given"); assertEquals(1, names.length); assertEquals("Given", names[0].getGiven()); assertEquals("Familya Familyb", names[0].getFamily()); }
@Test public void commaInitials() { CSLName[] names = NameParser.parse("Ritchie, Dennis M."); assertEquals(1, names.length); assertEquals("Dennis M.", names[0].getGiven()); assertEquals("Ritchie", names[0].getFamily()); }
@Test public void commaNonDroppingParticle() { CSLName[] names = NameParser.parse("van Gerwen, Michael"); assertEquals(1, names.length); assertEquals("Michael", names[0].getGiven()); assertEquals("van", names[0].getNonDroppingParticle()); assertEquals("Gerwen", names[0].getFamily()); }
@Test public void commaNonDroppingParticles() { CSLName[] names = NameParser.parse("Van der Voort, Vincent"); assertEquals(1, names.length); assertEquals("Vincent", names[0].getGiven()); assertEquals("Van der", names[0].getNonDroppingParticle()); assertEquals("Voort", names[0].getFamily()); }
@Test public void and() { CSLName[] names = NameParser.parse("Michael van Gerwen and Vincent van der Voort"); assertEquals(2, names.length); assertEquals("Michael", names[0].getGiven()); assertEquals("van", names[0].getNonDroppingParticle()); assertEquals("Gerwen", names[0].getFamily()); assertEquals("Vincent", names[1].getGiven()); assertEquals("van der", names[1].getNonDroppingParticle()); assertEquals("Voort", names[1].getFamily()); }
@Test public void andComma() { CSLName[] names = NameParser.parse("van Gerwen, Michael and Van der Voort, Vincent"); assertEquals(2, names.length); assertEquals("Michael", names[0].getGiven()); assertEquals("van", names[0].getNonDroppingParticle()); assertEquals("Gerwen", names[0].getFamily()); assertEquals("Vincent", names[1].getGiven()); assertEquals("Van der", names[1].getNonDroppingParticle()); assertEquals("Voort", names[1].getFamily()); }
@Test public void andComma2() { CSLName[] names = NameParser.parse("van Gerwen, Michael and van der Voort, Vincent"); assertEquals(2, names.length); assertEquals("Michael", names[0].getGiven()); assertEquals("van", names[0].getNonDroppingParticle()); assertEquals("Gerwen", names[0].getFamily()); assertEquals("Vincent", names[1].getGiven()); assertEquals("van der", names[1].getNonDroppingParticle()); assertEquals("Voort", names[1].getFamily()); }
@Test public void andCommaMix() { CSLName[] names = NameParser.parse("van Gerwen, Michael and Vincent van der Voort"); assertEquals(2, names.length); assertEquals("Michael", names[0].getGiven()); assertEquals("van", names[0].getNonDroppingParticle()); assertEquals("Gerwen", names[0].getFamily()); assertEquals("Vincent", names[1].getGiven()); assertEquals("van der", names[1].getNonDroppingParticle()); assertEquals("Voort", names[1].getFamily()); }
@Test public void junior() { CSLName[] names = NameParser.parse("George Friedman, Jr."); assertEquals(1, names.length); assertEquals("George", names[0].getGiven()); assertEquals("Jr.", names[0].getSuffix()); assertEquals("Friedman", names[0].getFamily()); }
@Test public void nonParseable() { String str = "Jerry Peek and Tim O'Reilly and Mike Loukides and other authors of the Nutshell handbooks"; CSLName[] names = NameParser.parse(str); assertEquals(1, names.length); assertEquals(str, names[0].getLiteral()); }