private void checkConstr(String str, String delim, String res[], Collection seps) { MyTokenizer tokenizer = new MyTokenizer(str, delim, seps); String tok; int i; int idx = 0; for (i = 0; i < res.length; i++) { assertTrue( "MyTokenizer(\"" + str + "\", \"" + delim + "\") lacks tokens", tokenizer.hasMoreTokens()); tok = tokenizer.nextToken(); assertTrue("tokenIndex broken", idx == tokenizer.getTokenIndex()); idx += tok.length(); assertTrue( "MyTokenizer(\"" + str + "\", \"" + delim + "\") has wrong token \"" + tok + "\" != \"" + res[i] + "\"", res[i].equals(tok)); } assertTrue( "MyTokenizer(\"" + str + "\", \"" + delim + "\") has too many tokens", !tokenizer.hasMoreTokens()); }
/** Test putToken(). */ public void testPutToken() { MyTokenizer st = new MyTokenizer("Hello old friend", " "); int oidx; assertTrue("Token 1", "Hello".equals(st.nextToken())); oidx = st.getTokenIndex(); st.putToken(","); assertTrue("PutToken 1", ",".equals(st.nextToken())); assertTrue("PutTokenIndex 1", oidx == st.getTokenIndex()); assertTrue("Token 2", " ".equals(st.nextToken())); assertTrue("Token 3", "old".equals(st.nextToken())); oidx = st.getTokenIndex(); st.putToken(","); st.putToken(";"); assertTrue("PutToken 2", ";".equals(st.nextToken())); assertTrue("PutTokenIndex 2", oidx == st.getTokenIndex()); st.putToken("?"); assertTrue("PutToken 3", "?".equals(st.nextToken())); assertTrue("PutTokenIndex 3", oidx == st.getTokenIndex()); assertTrue("Token 4", " ".equals(st.nextToken())); assertTrue("Token 5", "friend".equals(st.nextToken())); assertTrue("MoreTokens 1", !st.hasMoreTokens()); st.putToken("?"); assertTrue("MoreTokens 2", st.hasMoreTokens()); assertTrue("PutToken 4", "?".equals(st.nextToken())); assertTrue("MoreTokens 3", !st.hasMoreTokens()); try { st.putToken(null); assertTrue("Allows nulls", false); } catch (NullPointerException npe) { } }
protected void parseEnumerationLiteral(String text, Object literal) throws ParseException { text = text.trim(); if (text.length() == 0) { return; } // strip any trailing semi-colons if (text.charAt(text.length() - 1) == ';') { text = text.substring(0, text.length() - 2); } MyTokenizer st; String name = null; StringBuilder stereotype = null; String token; try { st = new MyTokenizer(text, "<<,\u00AB,\u00BB,>>"); while (st.hasMoreTokens()) { token = st.nextToken(); if ("<<".equals(token) || "\u00AB".equals(token)) { if (stereotype != null) { String msg = "parsing.error.model-element-name.twin-stereotypes"; throw new ParseException(Translator.localize(msg), st.getTokenIndex()); } stereotype = new StringBuilder(); while (true) { token = st.nextToken(); if (">>".equals(token) || "\u00BB".equals(token)) { break; } stereotype.append(token); } } else { if (name != null) { String msg = "parsing.error.model-element-name.twin-names"; throw new ParseException(Translator.localize(msg), st.getTokenIndex()); } name = token; } } } catch (NoSuchElementException nsee) { String msg = "parsing.error.model-element-name.unexpected-name-element"; throw new ParseException(Translator.localize(msg), text.length()); } catch (ParseException pre) { throw pre; } if (name != null) { name = name.trim(); } if (name != null) { Model.getCoreHelper().setName(literal, name); } StereotypeUtility.dealWithStereotypes(literal, stereotype, false); return; }