KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > om > Name


1 package net.sf.saxon.om;
2
3 import net.sf.saxon.Err;
4
5 /**
6  * This class exists to contain some static methods
7  * for validating the syntax of names.
8  *
9  * @author Michael H. Kay
10  */

11
12 public abstract class Name {
13
14     /**
15      * This class is never instantiated
16      */

17
18     private Name() { }
19
20     /**
21      * Validate whether a given string constitutes a valid QName, as defined in XML Namespaces.
22      * Note that this does not test whether the prefix is actually declared.
23      *
24      * @param name the name to be tested
25      * @return true if the name is a lexically-valid QName
26      */

27
28     public static final boolean isQName(String JavaDoc name) {
29         int colon = name.indexOf(':');
30         if (colon<0) return XMLChar.isValidNCName(name);
31         if (colon==0 || colon==name.length()-1) return false;
32         if (!XMLChar.isValidNCName(name.substring(0, colon))) return false;
33         if (!XMLChar.isValidNCName(name.substring(colon+1))) return false;
34         return true;
35     }
36
37     /**
38      * Extract the prefix from a QName. Note, the QName is assumed to be valid.
39      *
40      * @param qname The lexical QName whose prefix is required
41      * @return the prefix, that is the part before the colon. Returns an empty
42      * string if there is no prefix
43      */

44
45     public static final String JavaDoc getPrefix(String JavaDoc qname) {
46         int colon = qname.indexOf(':');
47         if (colon<0) {
48             return "";
49         }
50         return qname.substring(0, colon);
51     }
52
53
54     /**
55      * Validate a QName, and return the prefix and local name.
56      *
57      * @exception QNameException if not a valid QName.
58      * @param qname the lexical QName whose parts are required
59      * @return an array of two strings, the prefix and the local name. The first
60      * item is a zero-length string if there is no prefix.
61      */

62
63     public static final String JavaDoc[] getQNameParts(CharSequence JavaDoc qname) throws QNameException {
64         String JavaDoc[] parts = new String JavaDoc[2];
65         int colon = -1;
66         int len = qname.length();
67         for (int i=0; i<len; i++) {
68             if (qname.charAt(i)==':') {
69                 colon = i;
70                 break;
71             }
72         }
73         if (colon<0) {
74             parts[0] = "";
75             parts[1] = qname.toString();
76             if (!XMLChar.isValidNCName(qname)) {
77                 throw new QNameException("Invalid QName " + Err.wrap(qname));
78             }
79         } else {
80             if (colon==0) {
81                 throw new QNameException("QName cannot start with colon: " + Err.wrap(qname));
82             }
83             if (colon==len-1) {
84                 throw new QNameException("QName cannot end with colon: " + Err.wrap(qname));
85             }
86             parts[0] = qname.subSequence(0, colon).toString();
87             parts[1] = qname.subSequence(colon+1, len).toString();
88             // don't validate the prefix. If it isn't valid, then we'll get an error when we try to
89
// find the namespace declaration
90
// if (!XMLChar.isValidNCName(parts[0])) {
91
// throw new QNameException("Invalid QName prefix " + Err.wrap(parts[0]));
92
// }
93
if (!XMLChar.isValidNCName(parts[1])) {
94                 throw new QNameException("Invalid QName local part " + Err.wrap(parts[1]));
95             }
96         }
97         return parts;
98     }
99     
100 }
101
102 //
103
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
104
// you may not use this file except in compliance with the License. You may obtain a copy of the
105
// License at http://www.mozilla.org/MPL/
106
//
107
// Software distributed under the License is distributed on an "AS IS" basis,
108
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
109
// See the License for the specific language governing rights and limitations under the License.
110
//
111
// The Original Code is: all this file.
112
//
113
// The Initial Developer of the Original Code is Michael H. Kay.
114
//
115
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
116
//
117
// Contributor(s): none.
118
//
119
Popular Tags