1 package xdoclet.sdk.xgg.binding;
2
3 import java.util.HashMap;
4 import java.util.Iterator;
5
6 import org.relaxng.datatype.Datatype;
7
8 import com.sun.msv.datatype.xsd.AnyURIType;
9 import com.sun.msv.datatype.xsd.Base64BinaryType;
10 import com.sun.msv.datatype.xsd.BooleanType;
11 import com.sun.msv.datatype.xsd.ByteType;
12 import com.sun.msv.datatype.xsd.DateTimeType;
13 import com.sun.msv.datatype.xsd.DateType;
14 import com.sun.msv.datatype.xsd.DoubleType;
15 import com.sun.msv.datatype.xsd.DurationType;
16 import com.sun.msv.datatype.xsd.EntityType;
17 import com.sun.msv.datatype.xsd.EnumerationFacet;
18 import com.sun.msv.datatype.xsd.FloatType;
19 import com.sun.msv.datatype.xsd.FractionDigitsFacet;
20 import com.sun.msv.datatype.xsd.GDayType;
21 import com.sun.msv.datatype.xsd.GMonthDayType;
22 import com.sun.msv.datatype.xsd.GMonthType;
23 import com.sun.msv.datatype.xsd.GYearMonthType;
24 import com.sun.msv.datatype.xsd.GYearType;
25 import com.sun.msv.datatype.xsd.HexBinaryType;
26 import com.sun.msv.datatype.xsd.IDREFType;
27 import com.sun.msv.datatype.xsd.IDType;
28 import com.sun.msv.datatype.xsd.IntType;
29 import com.sun.msv.datatype.xsd.IntegerType;
30 import com.sun.msv.datatype.xsd.LengthFacet;
31 import com.sun.msv.datatype.xsd.LongType;
32 import com.sun.msv.datatype.xsd.MaxExclusiveFacet;
33 import com.sun.msv.datatype.xsd.MaxInclusiveFacet;
34 import com.sun.msv.datatype.xsd.MaxLengthFacet;
35 import com.sun.msv.datatype.xsd.MinExclusiveFacet;
36 import com.sun.msv.datatype.xsd.MinLengthFacet;
37 import com.sun.msv.datatype.xsd.NameType;
38 import com.sun.msv.datatype.xsd.NcnameType;
39 import com.sun.msv.datatype.xsd.NegativeIntegerType;
40 import com.sun.msv.datatype.xsd.NmtokenType;
41 import com.sun.msv.datatype.xsd.NonNegativeIntegerType;
42 import com.sun.msv.datatype.xsd.NonPositiveIntegerType;
43 import com.sun.msv.datatype.xsd.NormalizedStringType;
44 import com.sun.msv.datatype.xsd.NumberType;
45 import com.sun.msv.datatype.xsd.PatternFacet;
46 import com.sun.msv.datatype.xsd.PositiveIntegerType;
47 import com.sun.msv.datatype.xsd.QnameType;
48 import com.sun.msv.datatype.xsd.ShortType;
49 import com.sun.msv.datatype.xsd.SimpleURType;
50 import com.sun.msv.datatype.xsd.StringType;
51 import com.sun.msv.datatype.xsd.TimeType;
52 import com.sun.msv.datatype.xsd.TokenType;
53 import com.sun.msv.datatype.xsd.TotalDigitsFacet;
54 import com.sun.msv.datatype.xsd.UnsignedByteType;
55 import com.sun.msv.datatype.xsd.UnsignedIntType;
56 import com.sun.msv.datatype.xsd.UnsignedLongType;
57 import com.sun.msv.datatype.xsd.UnsignedShortType;
58 import com.sun.msv.datatype.xsd.WhiteSpaceFacet;
59 import com.sun.msv.grammar.relax.EmptyStringType;
60 import com.sun.msv.grammar.relax.NoneType;
61
62 /***
63 * Utility Class to process Data Type and compute regular expression
64 * pattern from Data Type.
65 *
66 * @author <a href="mailto:letiemble at users.sourceforge.net">Laurent Etiemble</a>
67 * @created 7 mai 2003
68 * @version $Revision: 1.2 $
69 */
70 public class DataTypeUtils {
71 /*** A map of all Data Types */
72 private static HashMap types = new HashMap();
73
74
75 /*** Avoid instantiation */
76 private DataTypeUtils() { }
77
78
79 /***
80 * Gets the regular expression pattern of the Data Type
81 *
82 * @param dt The Data Type
83 * @return The regular expression pattern or "*" if
84 * the Data Type is unknown
85 */
86 public static String getRegexp(Datatype dt) {
87 Object o = types.get(dt.getClass());
88 if (o == null) {
89 return ".*";
90 }
91 // It is a simple pattern
92 if (o instanceof String) {
93 return (String) o;
94 }
95 // We need an extractor to compute the pattern
96 return ((Extractor) o).extract(dt);
97 }
98
99
100 /***
101 * Implementation of this interface are used to extract a regular
102 * expression pattern from a Data Type
103 *
104 * @author <a href="mailto:letiemble at users.sourceforge.net">Laurent Etiemble</a>
105 * @created 7 mai 2003
106 * @version $Revision: 1.2 $
107 */
108 private interface Extractor {
109 /***
110 * Extract a regular expression pattern from a Data Type
111 *
112 * @param dt The Data Type
113 * @return The regular expression pattern
114 */
115 public String extract(Datatype dt);
116 }
117
118 /*** Initialize the datatypes and their mapping */
119 static {
120 // Concrete Types
121 // ==============================
122
123 types.put(AnyURIType.class, ".*");
124 types.put(Base64BinaryType.class, "^[A-Za-z0-9+/]+$");
125 types.put(HexBinaryType.class, "^[0-9A-F]+$");
126 types.put(BooleanType.class, "^(true|false|0|1)$");
127
128 types.put(DateTimeType.class, "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}$");
129 types.put(DateType.class, "^[0-9]{4}-[0-9]{2}-[0-9]{2}$");
130 types.put(GDayType.class, "^[0-9]{2}$");
131 types.put(GMonthDayType.class, "^[0-9]{2}-[0-9]{2}$");
132 types.put(GMonthType.class, "^[0-9]{2}$");
133 types.put(GYearMonthType.class, "^[0-9]{4}-[0-9]{2}$");
134 types.put(GYearType.class, "^[0-9]{4}$");
135 types.put(TimeType.class, "^[0-9]{2}:[0-9]{2}:[0-9]{2}$");
136
137 types.put(DurationType.class, ".*");
138 types.put(EmptyStringType.class, "^$");
139 types.put(EntityType.class, ".*");
140
141 types.put(DoubleType.class, "^[-+]?[0-9]+(////.)?[0-9]{0,18}$");
142 types.put(FloatType.class, "^[-+]?[0-9]+(////.)?[0-9]{0,18}+$");
143
144 types.put(ByteType.class, "^[-+]?[0-9]+$");
145 types.put(IntegerType.class, "^[-+]?[0-9]+$");
146 types.put(NegativeIntegerType.class, "^-[0-9]+$");
147 types.put(NonNegativeIntegerType.class, "^(+)?[0-9]+$");
148 types.put(NonPositiveIntegerType.class, "^-?[0-9]+$");
149 types.put(PositiveIntegerType.class, "^(+)?[0-9]+$");
150 types.put(UnsignedLongType.class, "^[0-9]+$");
151 types.put(IntType.class, "^[-+]?[0-9]+$");
152 types.put(UnsignedShortType.class, "^[0-9]+$");
153 types.put(LongType.class, "^[-+]?[0-9]+$");
154 types.put(UnsignedIntType.class, "^[0-9]+$");
155 types.put(ShortType.class, "^[-+]?[0-9]+$");
156 types.put(UnsignedByteType.class, "^[0-9]+$");
157
158 types.put(NoneType.class, "");
159 types.put(NumberType.class, ".*");
160 types.put(QnameType.class, ".*");
161 types.put(SimpleURType.class, ".*");
162
163 types.put(StringType.class, ".*");
164 types.put(NormalizedStringType.class, ".*");
165 types.put(TokenType.class, ".*");
166 types.put(NameType.class, ".*");
167 types.put(NcnameType.class, ".*");
168 types.put(IDREFType.class, ".*");
169 types.put(IDType.class, ".*");
170 types.put(NmtokenType.class, ".*");
171
172 // Types with facets
173 // ==============================
174 types.put(FractionDigitsFacet.class,
175 new Extractor() {
176 public String extract(Datatype dt) {
177 return ".*";
178 }
179 }
180 );
181 types.put(PatternFacet.class,
182 new Extractor() {
183 public String extract(Datatype dt) {
184 PatternFacet facet = (PatternFacet) dt;
185 StringBuffer pattern = new StringBuffer();
186 pattern.append("(");
187 for (int i = 0; i < facet.patterns.length; i++) {
188 if (i > 0) {
189 pattern.append("|");
190 }
191 pattern.append(facet.patterns[i]);
192 }
193 pattern.append(")");
194 return pattern.toString();
195 }
196 }
197 );
198 types.put(TotalDigitsFacet.class,
199 new Extractor() {
200 public String extract(Datatype dt) {
201 return ".*";
202 }
203 }
204 );
205 types.put(EnumerationFacet.class,
206 new Extractor() {
207 public String extract(Datatype dt) {
208 EnumerationFacet facet = (EnumerationFacet) dt;
209 StringBuffer pattern = new StringBuffer();
210 int i = 0;
211 pattern.append("(");
212 for (Iterator it = facet.values.iterator(); it.hasNext(); i++) {
213 if (i > 0) {
214 pattern.append("|");
215 }
216 pattern.append("" + it.next());
217 }
218 pattern.append(")");
219 return pattern.toString();
220 }
221 }
222 );
223 types.put(LengthFacet.class,
224 new Extractor() {
225 public String extract(Datatype dt) {
226 LengthFacet facet = (LengthFacet) dt;
227 StringBuffer pattern = new StringBuffer();
228 pattern.append("^(.){0,");
229 pattern.append(facet.length);
230 pattern.append("}$");
231 return pattern.toString();
232 }
233 }
234 );
235 types.put(MaxLengthFacet.class,
236 new Extractor() {
237 public String extract(Datatype dt) {
238 MaxLengthFacet facet = (MaxLengthFacet) dt;
239 StringBuffer pattern = new StringBuffer();
240 pattern.append("^(.){0,");
241 pattern.append(facet.maxLength);
242 pattern.append("}$");
243 return pattern.toString();
244 }
245 }
246 );
247 types.put(MinLengthFacet.class,
248 new Extractor() {
249 public String extract(Datatype dt) {
250 MinLengthFacet facet = (MinLengthFacet) dt;
251 StringBuffer pattern = new StringBuffer();
252 pattern.append("^(.){");
253 pattern.append(facet.minLength);
254 pattern.append(",}$");
255 return pattern.toString();
256 }
257 }
258 );
259 types.put(MaxExclusiveFacet.class,
260 new Extractor() {
261 public String extract(Datatype dt) {
262 return ".*";
263 }
264 }
265 );
266 types.put(MaxInclusiveFacet.class,
267 new Extractor() {
268 public String extract(Datatype dt) {
269 return ".*";
270 }
271 }
272 );
273 types.put(MinExclusiveFacet.class,
274 new Extractor() {
275 public String extract(Datatype dt) {
276 return ".*";
277 }
278 }
279 );
280 types.put(MinExclusiveFacet.class,
281 new Extractor() {
282 public String extract(Datatype dt) {
283 return ".*";
284 }
285 }
286 );
287 types.put(WhiteSpaceFacet.class,
288 new Extractor() {
289 public String extract(Datatype dt) {
290 return ".*";
291 }
292 }
293 );
294 }
295 }
This page was automatically generated by Maven