Source Code Cross Referenced for SimpleCompletor.java in  » Development » jLine » jline » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Development » jLine » jline 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved.
003:         *
004:         * This software is distributable under the BSD license. See the terms of the
005:         * BSD license in the documentation provided with this software.
006:         */
007:        package jline;
008:
009:        import java.io.*;
010:        import java.util.*;
011:
012:        /**
013:         *  <p>
014:         *  A simple {@link Completor} implementation that handles a pre-defined
015:         *  list of completion words.
016:         *  </p>
017:         *
018:         *  <p>
019:         *  Example usage:
020:         *  </p>
021:         *  <pre>
022:         *  myConsoleReader.addCompletor (new SimpleCompletor (new String [] { "now", "yesterday", "tomorrow" }));
023:         *  </pre>
024:         *
025:         *  @author  <a href="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a>
026:         */
027:        public class SimpleCompletor implements  Completor, Cloneable {
028:            /**
029:             *  The list of candidates that will be completed.
030:             */
031:            SortedSet candidates;
032:
033:            /**
034:             *  A delimiter to use to qualify completions.
035:             */
036:            String delimiter;
037:            final SimpleCompletorFilter filter;
038:
039:            /**
040:             *  Create a new SimpleCompletor with a single possible completion
041:             *  values.
042:             */
043:            public SimpleCompletor(final String candidateString) {
044:                this (new String[] { candidateString });
045:            }
046:
047:            /**
048:             *  Create a new SimpleCompletor with a list of possible completion
049:             *  values.
050:             */
051:            public SimpleCompletor(final String[] candidateStrings) {
052:                this (candidateStrings, null);
053:            }
054:
055:            public SimpleCompletor(final String[] strings,
056:                    final SimpleCompletorFilter filter) {
057:                this .filter = filter;
058:                setCandidateStrings(strings);
059:            }
060:
061:            /**
062:             *  Complete candidates using the contents of the specified Reader.
063:             */
064:            public SimpleCompletor(final Reader reader) throws IOException {
065:                this (getStrings(reader));
066:            }
067:
068:            /**
069:             *  Complete candidates using the whitespearated values in
070:             *  read from the specified Reader.
071:             */
072:            public SimpleCompletor(final InputStream in) throws IOException {
073:                this (getStrings(new InputStreamReader(in)));
074:            }
075:
076:            private static String[] getStrings(final Reader in)
077:                    throws IOException {
078:                final Reader reader = (in instanceof  BufferedReader) ? in
079:                        : new BufferedReader(in);
080:
081:                List words = new LinkedList();
082:                String line;
083:
084:                while ((line = ((BufferedReader) reader).readLine()) != null) {
085:                    for (StringTokenizer tok = new StringTokenizer(line); tok
086:                            .hasMoreTokens(); words.add(tok.nextToken())) {
087:                        ;
088:                    }
089:                }
090:
091:                return (String[]) words.toArray(new String[words.size()]);
092:            }
093:
094:            public int complete(final String buffer, final int cursor,
095:                    final List clist) {
096:                String start = (buffer == null) ? "" : buffer;
097:
098:                SortedSet matches = candidates.tailSet(start);
099:
100:                for (Iterator i = matches.iterator(); i.hasNext();) {
101:                    String can = (String) i.next();
102:
103:                    if (!(can.startsWith(start))) {
104:                        break;
105:                    }
106:
107:                    if (delimiter != null) {
108:                        int index = can.indexOf(delimiter, cursor);
109:
110:                        if (index != -1) {
111:                            can = can.substring(0, index + 1);
112:                        }
113:                    }
114:
115:                    clist.add(can);
116:                }
117:
118:                if (clist.size() == 1) {
119:                    clist.set(0, ((String) clist.get(0)) + " ");
120:                }
121:
122:                // the index of the completion is always from the beginning of
123:                // the buffer.
124:                return (clist.size() == 0) ? (-1) : 0;
125:            }
126:
127:            public void setDelimiter(final String delimiter) {
128:                this .delimiter = delimiter;
129:            }
130:
131:            public String getDelimiter() {
132:                return this .delimiter;
133:            }
134:
135:            public void setCandidates(final SortedSet candidates) {
136:                if (filter != null) {
137:                    TreeSet filtered = new TreeSet();
138:
139:                    for (Iterator i = candidates.iterator(); i.hasNext();) {
140:                        String element = (String) i.next();
141:                        element = filter.filter(element);
142:
143:                        if (element != null) {
144:                            filtered.add(element);
145:                        }
146:                    }
147:
148:                    this .candidates = filtered;
149:                } else {
150:                    this .candidates = candidates;
151:                }
152:            }
153:
154:            public SortedSet getCandidates() {
155:                return Collections.unmodifiableSortedSet(this .candidates);
156:            }
157:
158:            public void setCandidateStrings(final String[] strings) {
159:                setCandidates(new TreeSet(Arrays.asList(strings)));
160:            }
161:
162:            public void addCandidateString(final String candidateString) {
163:                final String string = (filter == null) ? candidateString
164:                        : filter.filter(candidateString);
165:
166:                if (string != null) {
167:                    candidates.add(string);
168:                }
169:            }
170:
171:            public Object clone() throws CloneNotSupportedException {
172:                return super .clone();
173:            }
174:
175:            /**
176:             *  Filter for elements in the completor.
177:             *
178:             *  @author  <a href="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a>
179:             */
180:            public static interface SimpleCompletorFilter {
181:                /**
182:                 *  Filter the specified String. To not filter it, return the
183:                 *  same String as the parameter. To exclude it, return null.
184:                 */
185:                public String filter(String element);
186:            }
187:
188:            public static class NoOpFilter implements  SimpleCompletorFilter {
189:                public String filter(final String element) {
190:                    return element;
191:                }
192:            }
193:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.