KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > util > KeywordParser


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  * $Id: KeywordParser.java,v 1.3 2005/03/24 12:22:51 slobodan Exp $
23  */

24
25
26
27
28
29 package com.lutris.util;
30 import java.text.MessageFormat JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32
33 /**
34  * Class used to parse KeywordsValueTable keywords.
35  *
36  * @version $Revision: 1.3 $
37  * @author Mark Diekhans
38  * @since Jolt1.0
39  */

40 public class KeywordParser {
41     static private String JavaDoc separator = ".";
42
43     /**
44      * Parse and validate the next component of a keyword.
45      *
46      * @param tokens Parsed token object.
47      * @param compIdx Index of the component we are about to parse. Used for
48      * error messages.
49      * @param keyword Entire keyword for use in error messages.
50      * @return The parsed keyword component.
51      */

52     static private String JavaDoc parseComponent (StringTokenizer JavaDoc tokens,
53                                           int compIdx,
54                                           String JavaDoc keyword)
55             throws KeywordValueException {
56         String JavaDoc comp = tokens.nextToken ();
57
58         /*
59          * Special check for keywords starting out with a separator, generates
60          * a more helpful error message.
61          */

62         if (comp.equals (separator) && (compIdx == 0)) {
63             String JavaDoc pattern = "keyword should not start with a {0} separator: \"{1} \"";
64             Object JavaDoc [] args = new String JavaDoc[] {separator,keyword};
65             String JavaDoc msg = MessageFormat.format(pattern, args);
66
67             //String msg = "keyword should not start with a '" +
68
// separator + "' separator: \"" + keyword + "\"";
69
throw new KeywordValueException (msg);
70         }
71
72         /*
73          * Validate the legality of the name.
74          */

75         boolean isOk = (comp.length () > 0);
76         if (isOk) {
77             if (!Character.isJavaIdentifierStart (comp.charAt (0))) {
78                 isOk = false;
79             }
80             for (int j = 1; j < comp.length (); j++) {
81                 if (!Character.isJavaIdentifierPart (comp.charAt (j))) {
82                     isOk = false;
83                     break;
84                 }
85             }
86         }
87         if (!isOk) {
88             String JavaDoc msg = "keyword component must be a legal Java identifier " +
89                 "component \"" + comp + "\": \"" + keyword + "\"";
90             throw new KeywordValueException (msg);
91         }
92
93         /*
94          * Check the separator if its not the last component.
95          */

96         if (tokens.hasMoreTokens ()) {
97             String JavaDoc sep = tokens.nextToken ();
98             if (!sep.equals (separator)) {
99                 String JavaDoc msg = "keyword component separator must be a " +
100                     "single '" + separator + "', got \"" + sep + "\": " +
101                     keyword + "\"";
102                 throw new KeywordValueException (msg);
103             }
104         }
105         return comp;
106     }
107
108     /**
109      * Parse a keyword into its components, validating that the components
110      * are legal names.
111      *
112      * @return An array of the keyword components.
113      * @exception KeywordValueException If the keyword is not syntactically legal.
114      */

115     static public String JavaDoc [] parse (String JavaDoc keyword)
116             throws KeywordValueException {
117
118         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc (keyword,
119                                                       separator,
120                                                       true);
121         /*
122          * Set up the array for the keywords, making sure that an odd
123          * number of tokens are present, either single word or words
124          * separated by `.'.
125          */

126         int numTokens = tokens.countTokens ();
127         if ((numTokens % 2) != 1) {
128             String JavaDoc msg = "keyword component must be single word or words " +
129                 "separated by '" + separator + "': \"" + keyword + "\"";
130             throw new KeywordValueException (msg);
131         }
132         int numComps = (numTokens / 2) + 1;
133         String JavaDoc [] keyParts = new String JavaDoc [numComps];
134
135         for (int compIdx = 0; compIdx < numComps; compIdx++) {
136             keyParts [compIdx] = parseComponent (tokens,
137                                                   compIdx,
138                                                   keyword);
139         }
140
141         return keyParts;
142     }
143
144     /**
145      * Create a keyword from its components.
146      *
147      * @param keywordPath Array of path components.
148      * @return The keyword path string.
149      */

150     static public String JavaDoc join (String JavaDoc [] keywordPath) {
151         StringBuffer JavaDoc keyword = new StringBuffer JavaDoc ();
152
153         for (int idx = 0; idx < keywordPath.length; idx++) {
154             if (idx > 0) {
155                 keyword.append (separator);
156             }
157             keyword.append (keywordPath [idx]);
158         }
159         return keyword.toString ();
160     }
161
162     /**
163      * Concatenate two keyword paths.
164      *
165      * @param keyword1 First keyword.
166      * @param keyword2 Second keyword.
167      * @return The keyword path string.
168      */

169     static public String JavaDoc concat (String JavaDoc keyword1,
170                                  String JavaDoc keyword2) {
171         return keyword1 + separator + keyword2;
172     }
173 }
174
Popular Tags