KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > schema > datatypes > PrimitiveUnionType


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.schema.datatypes;
24
25 import java.util.ArrayList JavaDoc;
26
27 import org.xquark.schema.SchemaException;
28 import org.xquark.schema.validation.ValidationContextProvider;
29
30
31 class PrimitiveUnionType extends EnumerableType {
32     private static final String JavaDoc RCSRevision = "$Revision: 1.2 $";
33     private static final String JavaDoc RCSName = "$Name: $";
34
35     ArrayList JavaDoc primitives = new ArrayList JavaDoc();
36
37     PrimitiveUnionType(ArrayList JavaDoc primitives, String JavaDoc unionName) {
38         super(unionName, PrimitiveType.UNION);
39         // No direct whitespace handling for union
40
nWhiteSpace = PrimitiveType.PRESERVE;
41         this.primitives = primitives;
42     }
43
44     protected Object JavaDoc toValidType(Object JavaDoc data) {
45         // TODO: think of something better
46
return data;
47     }
48
49     protected void setWhiteSpace(String JavaDoc value) throws SchemaException {
50         // "Facet whiteSpace not supported in " + this;
51
throw new SchemaException("cos-applicable-facets");
52     }
53
54     protected void invalidValue(String JavaDoc value, SchemaException se) throws SchemaException {
55         throw new SchemaException("cvc-datatype-valid.1.2.3", this, se);
56     }
57
58     protected Object JavaDoc toValueSpace(String JavaDoc value, ValidationContextProvider context) throws SchemaException {
59         return checkUnion(value, context);
60     }
61
62     protected Object JavaDoc checkUnion(String JavaDoc value, ValidationContextProvider context) throws SchemaException {
63         Object JavaDoc result = null;
64         ArrayList JavaDoc exceptions = null;
65         for (int i = 0; i < primitives.size(); i++) {
66             try {
67                 PrimitiveType pType = (PrimitiveType) primitives.get(i);
68                 result = pType.convert(value, true, context);
69                 break;
70             } catch (SchemaException ex) {
71                 if (exceptions == null)
72                     exceptions = new ArrayList JavaDoc();
73                 exceptions.add(ex);
74             }
75         }
76         if (result != null) {
77             return result;
78         } else {
79             throw new SchemaException("cvc-datatype-valid.1.2.3", this, exceptions);
80         }
81     }
82 }
83
Popular Tags