File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\bnf\RuleElement.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.bnf;
8
9 import java.util.HashMap;
10
11 import org.h2.util.StringUtils;
12
13 /**
14 * A single terminal rule in a BNF object.
15 */
16 class RuleElement implements Rule {
17
18 private final boolean keyword;
19 private final String name;
20 private Rule link;
21 private final int type;
22
23 RuleElement(String name, String topic) {
24 this.name = name;
25 this.keyword = name.length() == 1 || name.equals(StringUtils.toUpperEnglish(name));
26 topic = StringUtils.toLowerEnglish(topic);
27 this.type = topic.startsWith("function") ? Sentence.FUNCTION : Sentence.KEYWORD;
28 }
29
30 public void accept(BnfVisitor visitor) {
31 visitor.visitRuleElement(keyword, name, link);
32 }
33
34 public void setLinks(HashMap<String, RuleHead> ruleMap) {
35 if (link != null) {
36 link.setLinks(ruleMap);
37 }
38 if (keyword) {
39 return;
40 }
41 String test = Bnf.getRuleMapKey(name);
42 for (int i = 0; i < test.length(); i++) {
43 String t = test.substring(i);
44 RuleHead r = ruleMap.get(t);
45 if (r != null) {
46 link = r.getRule();
47 return;
48 }
49 }
50 throw new AssertionError("Unknown " + name + "/" + test);
51 }
52
53 public boolean autoComplete(Sentence sentence) {
54 sentence.stopIfRequired();
55 if (keyword) {
56 String query = sentence.getQuery();
57 String q = query.trim();
58 String up = sentence.getQueryUpper().trim();
59 if (up.startsWith(name)) {
60 query = query.substring(name.length());
61 while (!"_".equals(name) && query.length() > 0 && Character.isSpaceChar(query.charAt(0))) {
62 query = query.substring(1);
63 }
64 sentence.setQuery(query);
65 return true;
66 } else if (q.length() == 0 || name.startsWith(up)) {
67 if (q.length() < name.length()) {
68 sentence.add(name, name.substring(q.length()), type);
69 }
70 }
71 return false;
72 }
73 return link.autoComplete(sentence);
74 }
75
76 }
77