Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / xml / validation / datatype / TypeBuilder.java
blob606fd0e62fb0a87b4c6aaca476aee1616d435a4f
1 /* TypeBuilder.java --
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.xml.validation.datatype;
40 import java.util.LinkedHashSet;
41 import java.util.regex.Pattern;
42 import javax.xml.namespace.QName;
43 import org.relaxng.datatype.Datatype;
44 import org.relaxng.datatype.DatatypeBuilder;
45 import org.relaxng.datatype.DatatypeException;
46 import org.relaxng.datatype.ValidationContext;
48 /**
49 * Datatype builder.
51 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
53 public class TypeBuilder
54 implements DatatypeBuilder
57 final SimpleType type;
59 TypeBuilder(SimpleType type)
61 this.type = type;
62 // TODO fundamental facets
63 type.facets = new LinkedHashSet();
66 public void addParameter(String name, String value, ValidationContext context)
67 throws DatatypeException
69 // TODO fundamental facets
70 if ("length".equals(name))
71 type.facets.add(parseLengthFacet(value));
72 else if ("minLength".equals(name))
73 type.facets.add(parseMinLengthFacet(value));
74 else if ("maxLength".equals(name))
75 type.facets.add(parseMaxLengthFacet(value));
76 else if ("pattern".equals(name))
77 type.facets.add(parsePatternFacet(value));
78 else if ("enumeration".equals(name))
79 type.facets.add(parseEnumerationFacet(value));
80 else if ("whiteSpace".equals(name))
81 type.facets.add(parseWhiteSpaceFacet(value));
82 else if ("maxInclusive".equals(name))
83 type.facets.add(parseMaxInclusiveFacet(value, context));
84 else if ("maxExclusive".equals(name))
85 type.facets.add(parseMaxExclusiveFacet(value, context));
86 else if ("minExclusive".equals(name))
87 type.facets.add(parseMinExclusiveFacet(value, context));
88 else if ("minInclusive".equals(name))
89 type.facets.add(parseMinInclusiveFacet(value, context));
90 else if ("totalDigits".equals(name))
91 type.facets.add(parseTotalDigitsFacet(value));
92 else if ("fractionDigits".equals(name))
93 type.facets.add(parseFractionDigitsFacet(value));
96 LengthFacet parseLengthFacet(String value)
97 throws DatatypeException
99 int si = value.indexOf(' ');
100 boolean fixed = false;
101 if (si != -1)
103 if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
104 throw new DatatypeException("second argument must be FIXED if present");
105 fixed = true;
106 value = value.substring(0, si);
108 return new LengthFacet(Integer.parseInt(value), fixed, null);
111 MinLengthFacet parseMinLengthFacet(String value)
112 throws DatatypeException
114 int si = value.indexOf(' ');
115 boolean fixed = false;
116 if (si != -1)
118 if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
119 throw new DatatypeException("second argument must be FIXED if present");
120 fixed = true;
121 value = value.substring(0, si);
123 return new MinLengthFacet(Integer.parseInt(value), fixed, null);
126 MaxLengthFacet parseMaxLengthFacet(String value)
127 throws DatatypeException
129 int si = value.indexOf(' ');
130 boolean fixed = false;
131 if (si != -1)
133 if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
134 throw new DatatypeException("second argument must be FIXED if present");
135 fixed = true;
136 value = value.substring(0, si);
138 return new MaxLengthFacet(Integer.parseInt(value), fixed, null);
141 PatternFacet parsePatternFacet(String value)
142 throws DatatypeException
144 return new PatternFacet(Pattern.compile(value), null);
147 EnumerationFacet parseEnumerationFacet(String value)
148 throws DatatypeException
150 return new EnumerationFacet(value, null);
153 WhiteSpaceFacet parseWhiteSpaceFacet(String value)
154 throws DatatypeException
156 int si = value.indexOf(' ');
157 boolean fixed = false;
158 if (si != -1)
160 if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
161 throw new DatatypeException("second argument must be FIXED if present");
162 fixed = true;
163 value = value.substring(0, si);
165 if ("preserve".equals(value))
166 return new WhiteSpaceFacet(WhiteSpaceFacet.PRESERVE, fixed, null);
167 if ("replace".equals(value))
168 return new WhiteSpaceFacet(WhiteSpaceFacet.REPLACE, fixed, null);
169 if ("collapse".equals(value))
170 return new WhiteSpaceFacet(WhiteSpaceFacet.COLLAPSE, fixed, null);
171 throw new DatatypeException("argument must be preserve, replace, or collapse");
174 MaxInclusiveFacet parseMaxInclusiveFacet(String value,
175 ValidationContext context)
176 throws DatatypeException
178 int si = value.indexOf(' ');
179 boolean fixed = false;
180 if (si != -1)
182 if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
183 throw new DatatypeException("second argument must be FIXED if present");
184 fixed = true;
185 value = value.substring(0, si);
187 return new MaxInclusiveFacet(type.createValue(value, context), fixed, null);
190 MaxExclusiveFacet parseMaxExclusiveFacet(String value,
191 ValidationContext context)
192 throws DatatypeException
194 int si = value.indexOf(' ');
195 boolean fixed = false;
196 if (si != -1)
198 if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
199 throw new DatatypeException("second argument must be FIXED if present");
200 fixed = true;
201 value = value.substring(0, si);
203 return new MaxExclusiveFacet(type.createValue(value, context), fixed, null);
206 MinExclusiveFacet parseMinExclusiveFacet(String value,
207 ValidationContext context)
208 throws DatatypeException
210 int si = value.indexOf(' ');
211 boolean fixed = false;
212 if (si != -1)
214 if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
215 throw new DatatypeException("second argument must be FIXED if present");
216 fixed = true;
217 value = value.substring(0, si);
219 return new MinExclusiveFacet(type.createValue(value, context), fixed, null);
222 MinInclusiveFacet parseMinInclusiveFacet(String value,
223 ValidationContext context)
224 throws DatatypeException
226 int si = value.indexOf(' ');
227 boolean fixed = false;
228 if (si != -1)
230 if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
231 throw new DatatypeException("second argument must be FIXED if present");
232 fixed = true;
233 value = value.substring(0, si);
235 return new MinInclusiveFacet(type.createValue(value, context), fixed, null);
238 TotalDigitsFacet parseTotalDigitsFacet(String value)
239 throws DatatypeException
241 int si = value.indexOf(' ');
242 boolean fixed = false;
243 if (si != -1)
245 if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
246 throw new DatatypeException("second argument must be FIXED if present");
247 fixed = true;
248 value = value.substring(0, si);
250 int val = Integer.parseInt(value);
251 if (val < 0)
252 throw new DatatypeException("value must be a positiveInteger");
253 return new TotalDigitsFacet(val, fixed, null);
256 FractionDigitsFacet parseFractionDigitsFacet(String value)
257 throws DatatypeException
259 int si = value.indexOf(' ');
260 boolean fixed = false;
261 if (si != -1)
263 if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
264 throw new DatatypeException("second argument must be FIXED if present");
265 fixed = true;
266 value = value.substring(0, si);
268 int val = Integer.parseInt(value);
269 if (val < 0)
270 throw new DatatypeException("value must be a positiveInteger");
271 return new FractionDigitsFacet(val, fixed, null);
274 public Datatype createDatatype()
276 return type;