1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46:
47:
52: public class TypeBuilder
53: implements DatatypeBuilder
54: {
55:
56: final SimpleType type;
57:
58: TypeBuilder(SimpleType type)
59: {
60: this.type = type;
61:
62: type.facets = new LinkedHashSet();
63: }
64:
65: public void addParameter(String name, String value, ValidationContext context)
66: throws DatatypeException
67: {
68:
69: if ("length".equals(name))
70: type.facets.add(parseLengthFacet(value));
71: else if ("minLength".equals(name))
72: type.facets.add(parseMinLengthFacet(value));
73: else if ("maxLength".equals(name))
74: type.facets.add(parseMaxLengthFacet(value));
75: else if ("pattern".equals(name))
76: type.facets.add(parsePatternFacet(value));
77: else if ("enumeration".equals(name))
78: type.facets.add(parseEnumerationFacet(value));
79: else if ("whiteSpace".equals(name))
80: type.facets.add(parseWhiteSpaceFacet(value));
81: else if ("maxInclusive".equals(name))
82: type.facets.add(parseMaxInclusiveFacet(value, context));
83: else if ("maxExclusive".equals(name))
84: type.facets.add(parseMaxExclusiveFacet(value, context));
85: else if ("minExclusive".equals(name))
86: type.facets.add(parseMinExclusiveFacet(value, context));
87: else if ("minInclusive".equals(name))
88: type.facets.add(parseMinInclusiveFacet(value, context));
89: else if ("totalDigits".equals(name))
90: type.facets.add(parseTotalDigitsFacet(value));
91: else if ("fractionDigits".equals(name))
92: type.facets.add(parseFractionDigitsFacet(value));
93: }
94:
95: LengthFacet parseLengthFacet(String value)
96: throws DatatypeException
97: {
98: int si = value.indexOf(' ');
99: boolean fixed = false;
100: if (si != -1)
101: {
102: if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
103: throw new DatatypeException("second argument must be FIXED if present");
104: fixed = true;
105: value = value.substring(0, si);
106: }
107: return new LengthFacet(Integer.parseInt(value), fixed, null);
108: }
109:
110: MinLengthFacet parseMinLengthFacet(String value)
111: throws DatatypeException
112: {
113: int si = value.indexOf(' ');
114: boolean fixed = false;
115: if (si != -1)
116: {
117: if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
118: throw new DatatypeException("second argument must be FIXED if present");
119: fixed = true;
120: value = value.substring(0, si);
121: }
122: return new MinLengthFacet(Integer.parseInt(value), fixed, null);
123: }
124:
125: MaxLengthFacet parseMaxLengthFacet(String value)
126: throws DatatypeException
127: {
128: int si = value.indexOf(' ');
129: boolean fixed = false;
130: if (si != -1)
131: {
132: if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
133: throw new DatatypeException("second argument must be FIXED if present");
134: fixed = true;
135: value = value.substring(0, si);
136: }
137: return new MaxLengthFacet(Integer.parseInt(value), fixed, null);
138: }
139:
140: PatternFacet parsePatternFacet(String value)
141: throws DatatypeException
142: {
143: return new PatternFacet(Pattern.compile(value), null);
144: }
145:
146: EnumerationFacet parseEnumerationFacet(String value)
147: throws DatatypeException
148: {
149: return new EnumerationFacet(value, null);
150: }
151:
152: WhiteSpaceFacet parseWhiteSpaceFacet(String value)
153: throws DatatypeException
154: {
155: int si = value.indexOf(' ');
156: boolean fixed = false;
157: if (si != -1)
158: {
159: if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
160: throw new DatatypeException("second argument must be FIXED if present");
161: fixed = true;
162: value = value.substring(0, si);
163: }
164: if ("preserve".equals(value))
165: return new WhiteSpaceFacet(WhiteSpaceFacet.PRESERVE, fixed, null);
166: if ("replace".equals(value))
167: return new WhiteSpaceFacet(WhiteSpaceFacet.REPLACE, fixed, null);
168: if ("collapse".equals(value))
169: return new WhiteSpaceFacet(WhiteSpaceFacet.COLLAPSE, fixed, null);
170: throw new DatatypeException("argument must be preserve, replace, or collapse");
171: }
172:
173: MaxInclusiveFacet parseMaxInclusiveFacet(String value,
174: ValidationContext context)
175: throws DatatypeException
176: {
177: int si = value.indexOf(' ');
178: boolean fixed = false;
179: if (si != -1)
180: {
181: if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
182: throw new DatatypeException("second argument must be FIXED if present");
183: fixed = true;
184: value = value.substring(0, si);
185: }
186: return new MaxInclusiveFacet(type.createValue(value, context), fixed, null);
187: }
188:
189: MaxExclusiveFacet parseMaxExclusiveFacet(String value,
190: ValidationContext context)
191: throws DatatypeException
192: {
193: int si = value.indexOf(' ');
194: boolean fixed = false;
195: if (si != -1)
196: {
197: if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
198: throw new DatatypeException("second argument must be FIXED if present");
199: fixed = true;
200: value = value.substring(0, si);
201: }
202: return new MaxExclusiveFacet(type.createValue(value, context), fixed, null);
203: }
204:
205: MinExclusiveFacet parseMinExclusiveFacet(String value,
206: ValidationContext context)
207: throws DatatypeException
208: {
209: int si = value.indexOf(' ');
210: boolean fixed = false;
211: if (si != -1)
212: {
213: if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
214: throw new DatatypeException("second argument must be FIXED if present");
215: fixed = true;
216: value = value.substring(0, si);
217: }
218: return new MinExclusiveFacet(type.createValue(value, context), fixed, null);
219: }
220:
221: MinInclusiveFacet parseMinInclusiveFacet(String value,
222: ValidationContext context)
223: throws DatatypeException
224: {
225: int si = value.indexOf(' ');
226: boolean fixed = false;
227: if (si != -1)
228: {
229: if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
230: throw new DatatypeException("second argument must be FIXED if present");
231: fixed = true;
232: value = value.substring(0, si);
233: }
234: return new MinInclusiveFacet(type.createValue(value, context), fixed, null);
235: }
236:
237: TotalDigitsFacet parseTotalDigitsFacet(String value)
238: throws DatatypeException
239: {
240: int si = value.indexOf(' ');
241: boolean fixed = false;
242: if (si != -1)
243: {
244: if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
245: throw new DatatypeException("second argument must be FIXED if present");
246: fixed = true;
247: value = value.substring(0, si);
248: }
249: int val = Integer.parseInt(value);
250: if (val < 0)
251: throw new DatatypeException("value must be a positiveInteger");
252: return new TotalDigitsFacet(val, fixed, null);
253: }
254:
255: FractionDigitsFacet parseFractionDigitsFacet(String value)
256: throws DatatypeException
257: {
258: int si = value.indexOf(' ');
259: boolean fixed = false;
260: if (si != -1)
261: {
262: if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
263: throw new DatatypeException("second argument must be FIXED if present");
264: fixed = true;
265: value = value.substring(0, si);
266: }
267: int val = Integer.parseInt(value);
268: if (val < 0)
269: throw new DatatypeException("value must be a positiveInteger");
270: return new FractionDigitsFacet(val, fixed, null);
271: }
272:
273: public Datatype createDatatype()
274: {
275: return type;
276: }
277:
278: }