KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.xerces.impl.xpath.regex.RegularExpression;
26 import org.xquark.schema.SchemaException;
27 import org.xquark.schema.validation.ValidationContextProvider;
28
29
30 abstract class AbstractQNameType extends MesureableType {
31     private static final String JavaDoc RCSRevision = "$Revision: 1.2 $";
32     private static final String JavaDoc RCSName = "$Name: $";
33
34     private RegularExpression ncNameFormat = null;
35
36     AbstractQNameType(String JavaDoc name, int type) {
37         super(name, type);
38         ncNameFormat = new RegularExpression("[\\i-[:]][\\c-[:]]*", "X");
39     }
40
41     protected Object JavaDoc toValidType(Object JavaDoc data) {
42         return (QName) data;
43     }
44
45     protected abstract QName createQName(String JavaDoc namespaceURI, String JavaDoc localName);
46     
47     protected QName toQName(String JavaDoc value, ValidationContextProvider context) throws SchemaException {
48         String JavaDoc prefix = null;
49         String JavaDoc localName = null;
50         int index = value.indexOf(':');
51         if (index == 0) {
52             super.invalidValue(value);
53         } else if (index == -1) {
54             prefix = "";
55             localName = value;
56         } else {
57             prefix = value.substring(0, index);
58             if (!ncNameFormat.matches(prefix))
59                 super.invalidValue(value);
60             localName = value.substring(index + 1);
61         }
62         if (!ncNameFormat.matches(localName))
63             super.invalidValue(value);
64         // Avoids errors when context is null. This is lax
65
if (context == null) super.invalidValue(value);
66         String JavaDoc uri = context.getNamespaceURI(prefix);
67         if (uri == null && prefix.length() > 0)
68             super.invalidValue(value);
69         return createQName(uri, localName);
70     }
71
72     public void checkFacets(Object JavaDoc valueSpace) throws SchemaException {
73         super.checkFacets(valueSpace);
74         // length-related facets are deprecated for QName and NOTATION datatypes
75
// super.checkLength(((String) valueSpace).length(), valueSpace);
76
}
77
78     protected Object JavaDoc toValueSpace(String JavaDoc value, ValidationContextProvider context) throws SchemaException {
79         return toQName(value, context);
80     }
81
82     public String JavaDoc toXMLString(Object JavaDoc data, ValidationContextProvider context) {
83         if (data == null)
84             return null;
85         QName value = (QName) data;
86         if (context == null)
87             return value.toString();
88         if (value.getNamespaceURI() == null)
89             return value.getLocalName();
90         String JavaDoc prefix = context.getPrefix(value.getNamespaceURI());
91         if (prefix == null)
92             return value.toString();
93         if (prefix.length() == 0)
94             return value.getLocalName();
95         else
96             return prefix + ":" + value.getLocalName();
97     }
98
99 }
100
Popular Tags