KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > imageio > metadata > XmlNames


1 /*
2  * $Id: XmlNames.java,v 1.3 2001/03/16 04:01:34 edwingo Exp $
3  *
4  * The Apache Software License, Version 1.1
5  *
6  *
7  * Copyright (c) 2000 The Apache Software Foundation. All rights
8  * reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  *
17  * 2. Redistributions in binary form must reproduce the above copyright
18  * notice, this list of conditions and the following disclaimer in
19  * the documentation and/or other materials provided with the
20  * distribution.
21  *
22  * 3. The end-user documentation included with the redistribution,
23  * if any, must include the following acknowledgment:
24  * "This product includes software developed by the
25  * Apache Software Foundation (http://www.apache.org/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Crimson" and "Apache Software Foundation" must
30  * not be used to endorse or promote products derived from this
31  * software without prior written permission. For written
32  * permission, please contact apache@apache.org.
33  *
34  * 5. Products derived from this software may not be called "Apache",
35  * nor may "Apache" appear in their name, without prior written
36  * permission of the Apache Software Foundation.
37  *
38  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
39  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
45  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
46  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49  * SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This software consists of voluntary contributions made by many
53  * individuals on behalf of the Apache Software Foundation and was
54  * originally based on software copyright (c) 1999, Sun Microsystems, Inc.,
55  * http://www.sun.com. For more information on the Apache Software
56  * Foundation, please see <http://www.apache.org/>.
57  */

58
59 package com.sun.imageio.metadata;
60
61 /**
62  * This class contains static methods used to determine whether identifiers
63  * may appear in certain roles in XML documents. Such methods are used
64  * both to parse and to create such documents.
65  *
66  * @version 1.4
67  * @author David Brownell
68  */

69 public class XmlNames
70 {
71     /**
72      * Useful strings from the DOM Level 2 Spec
73      */

74     public static final String JavaDoc
75         SPEC_XML_URI = "http://www.w3.org/XML/1998/namespace";
76     public static final String JavaDoc
77         SPEC_XMLNS_URI = "http://www.w3.org/2000/xmlns/";
78
79     private XmlNames () { }
80
81
82     /**
83      * Returns true if the value is a legal XML name.
84      *
85      * @param value the string being tested
86      */

87     public static boolean isName (String JavaDoc value)
88     {
89     if (value == null || "".equals(value))
90         return false;
91
92     char c = value.charAt (0);
93     if (!XmlChars.isLetter (c) && c != '_' && c != ':')
94         return false;
95     for (int i = 1; i < value.length (); i++)
96         if (!XmlChars.isNameChar (value.charAt (i)))
97         return false;
98     return true;
99     }
100
101     /**
102      * Returns true if the value is a legal "unqualified" XML name, as
103      * defined in the XML Namespaces proposed recommendation.
104      * These are normal XML names, except that they may not contain
105      * a "colon" character.
106      *
107      * @param value the string being tested
108      */

109     public static boolean isUnqualifiedName (String JavaDoc value)
110     {
111     if (value == null || value.length() == 0)
112         return false;
113
114     char c = value.charAt (0);
115     if (!XmlChars.isLetter (c) && c != '_')
116         return false;
117     for (int i = 1; i < value.length (); i++)
118         if (!XmlChars.isNCNameChar (value.charAt (i)))
119         return false;
120     return true;
121     }
122
123     /**
124      * Returns true if the value is a legal "qualified" XML name, as defined
125      * in the XML Namespaces proposed recommendation. Qualified names are
126      * composed of an optional prefix (an unqualified name), followed by a
127      * colon, and a required "local part" (an unqualified name). Prefixes are
128      * declared, and correspond to particular URIs which scope the "local
129      * part" of the name. (This method cannot check whether the prefix of a
130      * name has been declared.)
131      *
132      * @param value the string being tested
133      */

134     public static boolean isQualifiedName (String JavaDoc value)
135     {
136     if (value == null)
137         return false;
138
139         // [6] QName ::= (Prefix ':')? LocalPart
140
// [7] Prefix ::= NCName
141
// [8] LocalPart ::= NCName
142

143     int first = value.indexOf (':');
144
145         // no Prefix, only check LocalPart
146
if (first <= 0)
147             return isUnqualifiedName (value);
148
149         // Prefix exists, check everything
150

151     int last = value.lastIndexOf (':');
152     if (last != first)
153         return false;
154     
155     return isUnqualifiedName (value.substring (0, first))
156         && isUnqualifiedName (value.substring (first + 1));
157     }
158
159     /**
160      * This method returns true if the identifier is a "name token"
161      * as defined in the XML specification. Like names, these
162      * may only contain "name characters"; however, they do not need
163      * to have letters as their initial characters. Attribute values
164      * defined to be of type NMTOKEN(S) must satisfy this predicate.
165      *
166      * @param token the string being tested
167      */

168     public static boolean isNmtoken (String JavaDoc token)
169     {
170     int length = token.length ();
171
172     for (int i = 0; i < length; i++)
173         if (!XmlChars.isNameChar (token.charAt (i)))
174         return false;
175     return true;
176     }
177
178
179     /**
180      * This method returns true if the identifier is a "name token" as
181      * defined by the XML Namespaces proposed recommendation.
182      * These are like XML "name tokens" but they may not contain the
183      * "colon" character.
184      *
185      * @see #isNmtoken
186      *
187      * @param token the string being tested
188      */

189     public static boolean isNCNmtoken (String JavaDoc token)
190     {
191     return isNmtoken (token) && token.indexOf (':') < 0;
192     }
193
194     /**
195      * Return the Prefix of qualifiedName. Does not check that Prefix is a
196      * valid NCName.
197      *
198      * @param qualifiedName name to find the Prefix of
199      * @return prefix or null if it has none
200      */

201     public static String JavaDoc getPrefix(String JavaDoc qualifiedName) {
202         // [6] QName ::= (Prefix ':')? LocalPart
203
// [7] Prefix ::= NCName
204
int index = qualifiedName.indexOf(':');
205         return index <= 0 ? null : qualifiedName.substring(0, index);
206     }
207
208     /**
209      * Return the LocalPart of qualifiedName. Does not check that Prefix is a
210      * valid NCName.
211      *
212      * @param qualifiedName name to find the LocalPart of
213      * @return LocalPart or null if it has none
214      */

215     public static String JavaDoc getLocalPart(String JavaDoc qualifiedName) {
216         // [6] QName ::= (Prefix ':')? LocalPart
217
// [8] LocalPart ::= NCName
218
int index = qualifiedName.indexOf(':');
219     if (index < 0) {
220         return qualifiedName;
221         }
222
223         // ':' at end of qualifiedName
224
if (index == qualifiedName.length() - 1) {
225             return null;
226         }
227
228     return qualifiedName.substring(index + 1);
229     }
230 }
231
Popular Tags