001: /*
002: * $Header: /cvsroot/webman-cms/source/webman/com/teamkonzept/publishing/markups/TKMarkupParamClass.java,v 1.7 2001/06/11 09:14:10 alex Exp $
003: *
004: */
005: package com.teamkonzept.publishing.markups;
006:
007: import java.sql.*;
008: import java.util.*;
009:
010: import com.teamkonzept.db.*;
011: import com.teamkonzept.lib.*;
012: import com.teamkonzept.publishing.markups.db.queries.*;
013: import org.apache.log4j.Category;
014:
015: public class TKMarkupParamClass {
016: private static final Category cat = Category
017: .getInstance(TKMarkupParamClass.class);
018:
019: public String name;
020: public String className;
021: public int id;
022:
023: public static int typeId = 1;
024:
025: protected static final String identifierFirstCodes = "_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
026: protected static final String digitCodes = "0123456789";
027: protected static final String identifierCodes = identifierFirstCodes
028: + digitCodes;
029:
030: private TKMarkupStatics statics = null;
031:
032: TKMarkupParamClass(TKMarkupStatics statics) {
033:
034: this .name = null;
035: this .className = null;
036: }
037:
038: TKMarkupParamClass() {
039:
040: this (TKMarkupStatics.setup());
041: }
042:
043: public String toString() {
044:
045: return name + "/" + className;
046: }
047:
048: public String checkValue(String value) {
049:
050: return value;
051: }
052:
053: public String typeDefault() {
054:
055: return null;
056:
057: }
058:
059: public int parsePattern(String text, int pos, StringBuffer str,
060: String pattern) {
061:
062: int i = 0;
063:
064: while ((pos + i < text.length()) && (i < pattern.length())) {
065:
066: char chr = text.charAt(pos + i);
067:
068: if (chr != pattern.charAt(i++))
069: return pos;
070: else if (str != null)
071: str.append(chr);
072: }
073:
074: if (i == pattern.length())
075: return pos + i;
076: else
077: return pos;
078: }
079:
080: public int parseStringValue(String text, int pos, StringBuffer value)
081: throws TKMarkupParserException {
082:
083: int oldPos = pos;
084: pos = parsePattern(text, pos, null, "\"");
085: if (pos <= oldPos)
086: return oldPos;
087:
088: while (pos < text.length()) {
089:
090: char chr = text.charAt(pos);
091:
092: if (chr == '"')
093: break;
094:
095: else if ((chr == '\\') && (pos + 1 < text.length())) {
096:
097: if (value == null)
098: pos++;
099: else
100: value.append(text.charAt(++pos));
101:
102: } else if (value != null)
103: value.append(chr);
104:
105: pos++;
106: }
107:
108: int nextOldPos = pos;
109: pos = parsePattern(text, pos, null, "\"");
110:
111: if (pos <= nextOldPos)
112: return oldPos;
113: else
114: return pos;
115: }
116:
117: public int parseUnquotedValue(String text, int pos,
118: StringBuffer value) throws TKMarkupParserException {
119:
120: if (text == null)
121: return pos;
122: if (value != null)
123: value.setLength(0);
124:
125: boolean isId = true;
126: boolean isInt = true;
127: boolean isFloat = true;
128:
129: while (pos < text.length()) {
130:
131: char chr = text.charAt(pos);
132:
133: if ((pos == 0) && (identifierFirstCodes.indexOf(chr) == -1))
134: isId = false;
135: else if ((pos != 0) && (identifierCodes.indexOf(chr) == -1))
136: isId = false;
137:
138: if (chr == '.')
139: if (!isInt)
140: isFloat = false;
141: else
142: isInt = false;
143:
144: else if (digitCodes.indexOf(chr) == -1)
145: isInt = isFloat = false;
146:
147: if (!isId && !isInt && !isFloat)
148: break;
149: if (value != null)
150: value.append(chr);
151:
152: pos++;
153: }
154:
155: return pos;
156: }
157:
158: public boolean parseQuotedValue(StringBuffer value)
159: throws TKMarkupParserException {
160:
161: if (value == null)
162: return false;
163:
164: int pos = parseUnquotedValue(value.toString().trim(), 0, value);
165: return pos > 0;
166: }
167:
168: public int parseValue(String text, int pos, StringBuffer value)
169: throws TKMarkupParserException {
170:
171: if (text == null)
172: return pos;
173:
174: int oldPos = pos;
175: pos = parseStringValue(text, pos, value);
176:
177: if (pos > oldPos) {
178:
179: if (parseQuotedValue(value))
180: return pos;
181: else
182: return oldPos;
183: }
184:
185: return parseUnquotedValue(text, pos, value);
186: }
187:
188: public String wrapValue(String value) {
189:
190: if (value == null)
191: return null;
192: else if (quotingRequired(value))
193: return par2quotes(value);
194: else
195: return value;
196: }
197:
198: public boolean quotingRequired(String text) {
199:
200: boolean isId = true;
201: boolean isInt = true;
202: boolean isFloat = true;
203:
204: if (text == null)
205: return false;
206:
207: int pos = 0;
208: while (pos < text.length()) {
209:
210: char chr = text.charAt(pos++);
211:
212: if ((pos == 0) && (identifierFirstCodes.indexOf(chr) == -1))
213: isId = false;
214: else if ((pos != 0) && (identifierCodes.indexOf(chr) == -1))
215: isId = false;
216:
217: if (chr == '.')
218: if (!isInt)
219: isFloat = false;
220: else
221: isInt = false;
222:
223: else if (digitCodes.indexOf(chr) == -1)
224: isInt = isFloat = false;
225:
226: if (!isId && !isInt && !isFloat)
227: return true;
228: }
229:
230: return false;
231: }
232:
233: public static String par2quotes(String text) {
234:
235: if (text == null)
236: return null;
237:
238: StringBuffer buf = new StringBuffer();
239: buf.append('"');
240:
241: int pos = 0;
242: while (pos < text.length()) {
243:
244: char chr = text.charAt(pos++);
245:
246: if ((chr == '\\') || (chr == '"'))
247: buf.append('\\');
248:
249: buf.append(chr);
250: }
251:
252: buf.append('"');
253: return buf.toString();
254: }
255:
256: public boolean isFlag() {
257:
258: return false;
259: }
260:
261: public TKVector getOptions(String data) {
262:
263: if (data == null)
264: return null;
265:
266: TKVector options = null;
267: StringBuffer value = null;
268:
269: int pos = 0;
270: while (pos < data.length()) {
271:
272: char chr = data.charAt(pos++);
273:
274: if (chr == ';') {
275:
276: if (value == null)
277: continue;
278:
279: if (options == null)
280: options = new TKVector();
281: options.addElement(value.toString());
282: value = null;
283:
284: continue;
285: }
286:
287: if ((chr == '\\') && (pos < data.length()))
288: chr = data.charAt(pos++);
289:
290: (value == null ? (value = new StringBuffer()) : value)
291: .append(chr);
292: }
293:
294: if (value != null) {
295: if (options == null)
296: options = new TKVector();
297: options.addElement(value.toString());
298: }
299:
300: return options;
301: }
302:
303: public String setOptions(TKVector optionList) {
304:
305: if (optionList == null)
306: return null;
307:
308: StringBuffer dataBuffer = new StringBuffer();
309:
310: Enumeration e = optionList.elements();
311: while (e.hasMoreElements()) {
312:
313: String option = (String) e.nextElement();
314: if (option == null)
315: continue;
316:
317: if (dataBuffer.length() != 0)
318: dataBuffer.append(';');
319:
320: for (int i = 0; i < option.length(); i++) {
321:
322: char chr = option.charAt(i);
323:
324: if (chr == ';')
325: dataBuffer.append("\\;");
326: else if (chr == '\\')
327: dataBuffer.append("\\\\");
328: else
329: dataBuffer.append(chr);
330: }
331: }
332:
333: return dataBuffer.toString();
334: }
335:
336: public static void getClasses() throws SQLException {
337:
338: TKMarkupStatics statics = TKMarkupStatics.setup();
339:
340: statics.paramClassNames = new TKHashtable();
341: statics.paramClassIds = new TKHashtable();
342: statics.defaultTypeClass = null;
343:
344: TKQuery q = TKDBManager.newQuery(TKMarkupParamGetClasses.class);
345: q.execute();
346: ResultSet rs = q.fetchResultSet();
347:
348: while (rs.next()) {
349:
350: int classId = rs.getInt("CLASS_ID");
351: String name = rs.getString("NAME");
352: String className = rs.getString("CLASS_NAME");
353: int type = rs.getInt("TYPE");
354:
355: if (type != typeId)
356: continue;
357:
358: TKMarkupParamClass paramClass = register(statics, classId,
359: name, className);
360: if (paramClass != null)
361: statics.paramClassIds.put(new Integer(classId),
362: paramClass);
363: }
364: }
365:
366: public static TKMarkupParamClass register(TKMarkupStatics statics,
367: int id, String name, String className) {
368:
369: try {
370: Class objectClass = Class.forName(className);
371:
372: TKMarkupParamClass paramClass = (TKMarkupParamClass) objectClass
373: .newInstance();
374:
375: paramClass.id = id;
376: paramClass.name = name;
377: paramClass.className = className;
378:
379: statics.paramClassNames.put(name, paramClass);
380: return paramClass;
381:
382: } catch (Exception ex) {
383: cat.error("TKWMAttrSetup.setup() failed: ", ex);
384: return null;
385: }
386: }
387:
388: public static synchronized void setup() {
389:
390: try {
391: getClasses();
392: } catch (Exception ex) {
393:
394: cat.error("TKWMAttrSetup.setup() failed: ", ex);
395: } catch (TKSQLError er) {
396: cat.error("TKWMAttrSetup.setup() failed: ", er);
397: }
398: }
399:
400: public static synchronized TKMarkupParamClass lookup(
401: TKMarkupStatics statics, String name) {
402:
403: return (TKMarkupParamClass) statics.paramClassNames.get(name);
404: }
405:
406: public static synchronized TKMarkupParamClass lookup(
407: TKMarkupStatics statics, int id) {
408:
409: return (TKMarkupParamClass) statics.paramClassIds
410: .get(new Integer(id));
411: }
412:
413: public static synchronized Enumeration allNames(
414: TKMarkupStatics statics) {
415:
416: return statics.paramClassNames.keys();
417: }
418:
419: public static TKMarkupParamClass lookup(String name) {
420:
421: return lookup(TKMarkupStatics.setup(), name);
422: }
423:
424: public static TKMarkupParamClass lookup(int id) {
425:
426: return lookup(TKMarkupStatics.setup(), id);
427: }
428:
429: public static Enumeration allNames() {
430:
431: return allNames(TKMarkupStatics.setup());
432: }
433: }
|