KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > xml > XName


1 // Copyright (c) 2003 Per M.A. Bothner.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.xml;
5 import gnu.mapping.*;
6 import java.io.*;
7
8 /** A QName with namespace nodes [and future optional type annotation]. */
9
10 public class XName extends Symbol implements Externalizable
11 {
12   NamespaceBinding namespaceNodes;
13
14   public XName ()
15   {
16   }
17
18   public XName (Symbol symbol, NamespaceBinding namespaceNodes)
19   {
20     super(symbol.getNamespace(), symbol.getName());
21     this.namespaceNodes = namespaceNodes;
22   }
23
24   /** Namespace nodes associated with an element.
25    * These are in inverse document/parse order.
26    */

27   public final NamespaceBinding getNamespaceNodes () { return namespaceNodes; }
28   public final void setNamespaceNodes (NamespaceBinding nodes)
29   { this.namespaceNodes = nodes; }
30
31   String JavaDoc lookupNamespaceURI (String JavaDoc prefix)
32   {
33     for (NamespaceBinding ns = namespaceNodes; ns != null; ns = ns.next)
34       {
35     if (prefix == ns.prefix)
36       return ns.uri;
37       }
38     return null;
39   }
40
41   public void writeExternal(ObjectOutput out) throws IOException
42   {
43     super.writeExternal(out);
44     out.writeObject(namespaceNodes);
45   }
46
47   public void readExternal(ObjectInput in)
48     throws IOException, ClassNotFoundException JavaDoc
49   {
50     super.readExternal(in);
51     namespaceNodes = (NamespaceBinding) in.readObject();
52   }
53
54   public static boolean isNameStart(int ch)
55   {
56     /* #ifdef JAVA5 */
57     // return Character.isLetter(ch) || ch == '_';
58
/* #else */
59     return ch >= 0x10000 || Character.isLetter((char) ch) || ch == '_';
60     /* #endif */
61   }
62
63   public static boolean isNamePart(int ch)
64   {
65     /* #ifdef JAVA5 */
66     // return Character.isUnicodeIdentifierPart(ch) || ch == '-' || ch == '.';
67
/* #else */
68     return ch >= 0x10000 || Character.isUnicodeIdentifierPart((char) ch)
69       || ch == '-' || ch == '.';
70     /* #endif */
71   }
72
73   public static boolean isName (String JavaDoc value)
74   {
75     return isName(value, false);
76   }
77
78   public static boolean isName (String JavaDoc value, boolean prohibitColon)
79   {
80     int len = value.length();
81     if (len == 0)
82       return false;
83     for (int i = 0; i < len; )
84       {
85         boolean first = i == 0;
86         int ch = value.charAt(i++);
87         if (ch >= 0xD800 && ch < 0xDC00 && i < len)
88           ch = (ch - 0xD800) * 0x400 + (value.charAt(i++) - 0xDC00) + 0x10000;
89         if (! (first ? XName.isNameStart(ch) : XName.isNamePart(ch)))
90           return false;
91       }
92     return true;
93   }
94 }
95
Popular Tags