KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > util > MacroString


1 /*
2  * Copyright 2002 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: MacroString.java,v 1.2 2002/10/17 21:01:00 pierreg0 Exp $
9  */

10
11 package com.triactive.jdo.util;
12
13 import javax.jdo.JDOUserException;
14
15
16 public class MacroString
17 {
18     private final Class JavaDoc thisClass;
19     private final Imports imports;
20     private final String JavaDoc macroString;
21
22
23     public MacroString(Class JavaDoc thisClass, String JavaDoc importsString, String JavaDoc macroString)
24     {
25         this.thisClass = thisClass;
26         this.macroString = macroString;
27
28         imports = new Imports();
29         imports.importPackage(thisClass);
30
31         if (importsString != null)
32             imports.parseImports(importsString);
33     }
34
35
36     public String JavaDoc substituteMacros(MacroHandler mh)
37     {
38         StringBuffer JavaDoc outBuf = new StringBuffer JavaDoc();
39         int left, right;
40
41         /* Pass 1: Substitute SQL identifier macros "{...}". */
42         for (int curr = 0; curr < macroString.length(); curr = right + 1)
43         {
44             if ((left = macroString.indexOf('{', curr)) < 0)
45             {
46                 outBuf.append(macroString.substring(curr));
47                 break;
48             }
49
50             outBuf.append(macroString.substring(curr, left));
51
52             if ((right = macroString.indexOf('}', left + 1)) < 0)
53                 throw new JDOUserException("Unmatched braces for identifier macro: " + macroString);
54
55             IdentifierMacro im = parseIdentifierMacro(macroString.substring(left + 1, right));
56             mh.onIdentifierMacro(im);
57
58             outBuf.append(im.value);
59         }
60
61         String JavaDoc tmpString = outBuf.toString();
62         outBuf = new StringBuffer JavaDoc();
63
64         /* Pass 2: Substitute parameter macros "?...?". */
65         for (int curr = 0; curr < tmpString.length(); curr = right + 1)
66         {
67             if ((left = tmpString.indexOf('?', curr)) < 0)
68             {
69                 outBuf.append(tmpString.substring(curr));
70                 break;
71             }
72
73             outBuf.append(tmpString.substring(curr, left));
74
75             if ((right = tmpString.indexOf('?', left + 1)) < 0)
76                 throw new JDOUserException("Unmatched question marks for parameter macro: " + tmpString);
77
78             ParameterMacro pm = new ParameterMacro(tmpString.substring(left + 1, right));
79             mh.onParameterMacro(pm);
80
81             outBuf.append('?');
82         }
83
84         return outBuf.toString();
85     }
86
87
88     private Class JavaDoc resolveClassDeclaration(String JavaDoc className)
89     {
90         try
91         {
92             return className.equals("this") ? thisClass : imports.resolveClassDeclaration(className);
93         }
94         catch (ClassNotFoundException JavaDoc e)
95         {
96             return null;
97         }
98     }
99
100
101     private IdentifierMacro parseIdentifierMacro(String JavaDoc unparsed)
102     {
103         String JavaDoc className = null;
104         String JavaDoc fieldName = null;
105         String JavaDoc subfieldName = null;
106
107         Class JavaDoc c = resolveClassDeclaration(unparsed);
108
109         if (c == null)
110         {
111             /* The last '.' in the string. */
112             int lastDot = unparsed.lastIndexOf('.');
113
114             if (lastDot < 0)
115                 throw new JDOUserException("Cannot parse identifier macro: " + unparsed);
116
117             fieldName = unparsed.substring(lastDot + 1);
118             className = unparsed.substring(0, lastDot);
119             c = resolveClassDeclaration(className);
120
121             if (c == null)
122             {
123                 /* The second to the last dot in the string. */
124                 int lastDot2 = unparsed.lastIndexOf('.', lastDot - 1);
125
126                 if (lastDot2 < 0)
127                     throw new JDOUserException("Cannot parse identifier macro: " + unparsed);
128
129                 subfieldName = fieldName;
130                 fieldName = unparsed.substring(lastDot2 + 1, lastDot);
131                 className = unparsed.substring(0, lastDot2);
132                 c = resolveClassDeclaration(className);
133
134                 if (c == null)
135                     throw new JDOUserException("Cannot parse identifier macro: " + unparsed);
136             }
137         }
138
139         return new IdentifierMacro(unparsed, c, fieldName, subfieldName);
140     }
141
142
143     public static class IdentifierMacro
144     {
145         public final String JavaDoc unparsed;
146         public final Class JavaDoc clazz;
147         public final String JavaDoc fieldName;
148         public final String JavaDoc subfieldName;
149         public String JavaDoc value;
150
151         IdentifierMacro(String JavaDoc unparsed, Class JavaDoc clazz, String JavaDoc fieldName, String JavaDoc subfieldName)
152         {
153             this.unparsed = unparsed;
154             this.clazz = clazz;
155             this.fieldName = fieldName;
156             this.subfieldName = subfieldName;
157             this.value = null;
158         }
159
160         public String JavaDoc toString()
161         {
162             return "{" + unparsed + "}";
163         }
164     }
165
166
167     public static class ParameterMacro
168     {
169         public final String JavaDoc parameterName;
170
171         ParameterMacro(String JavaDoc parameterName)
172         {
173             this.parameterName = parameterName;
174         }
175
176         public String JavaDoc toString()
177         {
178             return "?" + parameterName + "?";
179         }
180     }
181
182
183     public static interface MacroHandler
184     {
185         void onIdentifierMacro(IdentifierMacro im);
186         void onParameterMacro(ParameterMacro pm);
187     }
188 }
189
Popular Tags