KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > om > Name


1 package com.icl.saxon.om;
2
3
4 /**
5   * This class, a remnant of its former self, exists to contain some static methods
6   * for validating the syntax of names.<br>
7   *
8   * @author <A HREF="mailto:mhkay@iclway.co.uk>Michael H. Kay</A>
9   */

10
11 public abstract class Name {
12
13     /**
14     * Validate whether a given string constitutes a valid NCName, as defined in XML Namespaces
15     */

16
17     public static boolean isNCName(String JavaDoc name) {
18
19         // This isn't 100% accurate, e.g. FF10 to FF19 are classified as digits in Java but not
20
// in XML; 00AA is a letter in Java but not in XML. But it's close enough.
21

22         if (name.length()==0) return false;
23         char first = name.charAt(0);
24         if (!(first=='_' || Character.isLetter(first))) {
25             return false;
26         }
27         for (int i=1; i<name.length(); i++) {
28             char c = name.charAt(i);
29             if (!(c=='_' || c=='.' || c=='-' || Character.isLetter(c) || Character.isDigit(c))) {
30                 return false;
31             }
32         }
33         return true;
34     }
35
36     /**
37     * Validate whether a given string constitutes a valid QName, as defined in XML Namespaces
38     */

39
40     public static boolean isQName(String JavaDoc name) {
41         int colon = name.indexOf(':');
42         if (colon<0) return isNCName(name);
43         if (colon==0 || colon==name.length()-1) return false;
44         if (!isNCName(name.substring(0, colon))) return false;
45         if (!isNCName(name.substring(colon+1))) return false;
46         return true;
47     }
48
49     /**
50     * Extract the prefix from a QName. Note, the QName is assumed to be valid.
51     */

52     
53     public final static String JavaDoc getPrefix(String JavaDoc qname) {
54         int colon = qname.indexOf(':');
55         if (colon<0) {
56             return "";
57         }
58         return qname.substring(0, colon);
59     }
60     
61     /**
62     * Extract the local name from a QName. The QName is assumed to be valid.
63     */

64     
65     public final static String JavaDoc getLocalName(String JavaDoc qname) {
66         int colon = qname.indexOf(':');
67         if (colon<0) {
68             return qname;
69         }
70         return qname.substring(colon+1);
71     }
72
73 }
74
75 //
76
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
77
// you may not use this file except in compliance with the License. You may obtain a copy of the
78
// License at http://www.mozilla.org/MPL/
79
//
80
// Software distributed under the License is distributed on an "AS IS" basis,
81
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
82
// See the License for the specific language governing rights and limitations under the License.
83
//
84
// The Original Code is: all this file.
85
//
86
// The Initial Developer of the Original Code is
87
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
88
//
89
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
90
//
91
// Contributor(s): none.
92
//
93
Popular Tags