KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > kawa > xml > AttributeType


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

4 package gnu.kawa.xml;
5 import gnu.bytecode.*;
6 import gnu.lists.*;
7 import gnu.xml.*;
8 import gnu.expr.*;
9 import java.io.*;
10 import gnu.mapping.Symbol;
11
12 /** Matches an attribute name pattern.
13  * FIXME: ElementType and AttributeType should both inherit
14  * from a common NamedNodeType class. */

15
16 public class AttributeType extends NodeType
17 implements TypeValue, Externalizable, AttributePredicate
18 {
19   Symbol qname;
20
21   public static AttributeType make (String JavaDoc namespaceURI, String JavaDoc localName)
22   {
23     Symbol qname;
24     if (namespaceURI != null)
25       qname = Symbol.make(namespaceURI, localName);
26     else if (localName == ElementType.MATCH_ANY_LOCALNAME)
27       qname = ElementType.MATCH_ANY_QNAME;
28     else
29       qname = new Symbol(null, localName);
30     return new AttributeType(qname);
31   }
32
33   public static AttributeType make (Symbol qname)
34   {
35     return new AttributeType(qname);
36   }
37
38   public AttributeType(Symbol qname)
39   {
40     this(null, qname);
41   }
42
43   public AttributeType(String JavaDoc name, Symbol qname)
44   {
45     super(name != null && name.length() > 0 ? name
46         : "ATTRIBUTE "+qname+" (*)");
47     this.qname = qname;
48   }
49
50   public Type getImplementationType()
51   {
52     return ClassType.make("gnu.kawa.xml.KAttr");
53   }
54
55   public final String JavaDoc getNamespaceURI () { return qname.getNamespaceURI(); }
56   public final String JavaDoc getLocalName () { return qname.getLocalName(); }
57
58   public void emitCoerceFromObject (CodeAttr code)
59   {
60     code.emitPushString(qname.getNamespaceURI());
61     code.emitPushString(qname.getLocalName());
62     code.emitInvokeStatic(coerceMethod);
63   }
64
65   public Object JavaDoc coerceFromObject (Object JavaDoc obj)
66   {
67     return coerce(obj, qname.getNamespaceURI(), qname.getLocalName());
68   }
69
70   public boolean isInstancePos (AbstractSequence seq, int ipos)
71   {
72     int kind = seq.getNextKind(ipos);
73     if (kind == Sequence.ATTRIBUTE_VALUE)
74       return isInstance(seq, ipos, seq.getNextTypeObject(ipos));
75     if (kind == Sequence.OBJECT_VALUE)
76       return isInstance(seq.getPosNext(ipos));
77     return false;
78   }
79
80   public boolean isInstance(AbstractSequence seq, int ipos, Object JavaDoc attrType)
81   {
82     String JavaDoc namespaceURI = qname.getNamespaceURI();
83     String JavaDoc localName = qname.getLocalName();
84     String JavaDoc curNamespaceURI;
85     String JavaDoc curLocalName;
86     if (attrType instanceof Symbol)
87       {
88     Symbol qname = (Symbol) attrType;
89     curNamespaceURI = qname.getNamespaceURI();
90     curLocalName = qname.getLocalName();
91       }
92     /* #ifdef JAXP-1.3 */
93     // else if (attrType instanceof javax.xml.namespace.QName)
94
// {
95
// javax.xml.namespace.QName qtype
96
// = (javax.xml.namespace.QName) attrType;
97
// curNamespaceURI = qtype.getNamespaceURI();
98
// curLocalName = qtype.getLocalPart();
99
// }
100
/* #endif */
101     else
102       {
103     curNamespaceURI = "";
104     curLocalName = attrType.toString().intern(); // FIXME
105
}
106     if (localName != null && localName.length() == 0)
107       localName = null;
108     return ((localName == curLocalName || localName == null)
109         && (namespaceURI == curNamespaceURI || namespaceURI == null));
110   }
111
112   public boolean isInstance (Object JavaDoc obj)
113   {
114     return coerceOrNull(obj, qname.getNamespaceURI(),qname.getLocalName())
115       != null;
116   }
117
118   public static SeqPosition coerceOrNull (Object JavaDoc obj,
119                     String JavaDoc namespaceURI, String JavaDoc localName)
120   {
121     SeqPosition pos = NodeType.coerceOrNull(obj, ATTRIBUTE_OK);
122     if (pos == null)
123       return null;
124     if (localName != null && localName.length() == 0)
125       localName = null;
126     Object JavaDoc curName = pos.getNextTypeObject();
127     String JavaDoc curNamespaceURI;
128     String JavaDoc curLocalName;
129     if (curName instanceof Symbol)
130       {
131     Symbol qname = (Symbol) curName;
132     curNamespaceURI = qname.getNamespaceURI();
133     curLocalName = qname.getLocalName();
134       }
135     /* #ifdef JAXP-1.3 */
136     // else if (curName instanceof javax.xml.namespace.QName)
137
// {
138
// javax.xml.namespace.QName qtype
139
// = (javax.xml.namespace.QName) curName;
140
// curNamespaceURI = qtype.getNamespaceURI();
141
// curLocalName = qtype.getLocalPart();
142
// }
143
/* #endif */
144     else
145       {
146     curNamespaceURI = "";
147     curLocalName = curName.toString().intern(); // FIXME
148
}
149     if ((localName == curLocalName || localName == null)
150     && (namespaceURI == curNamespaceURI || namespaceURI == null))
151       return pos;
152     return null;
153   }
154
155   public static SeqPosition coerce (Object JavaDoc obj,
156                     String JavaDoc namespaceURI, String JavaDoc localName)
157   {
158     SeqPosition pos = coerceOrNull(obj, namespaceURI, localName);
159     if (pos == null)
160       throw new ClassCastException JavaDoc();
161     return pos;
162   }
163
164   protected void emitCoerceOrNullMethod(Variable incoming, Compilation comp)
165   {
166     CodeAttr code = comp.getCode();
167     if (incoming != null)
168       code.emitLoad(incoming);
169     code.emitPushString(qname.getNamespaceURI());
170     code.emitPushString(qname.getLocalName());
171     code.emitInvokeStatic(coerceOrNullMethod);
172   }
173
174   public static final ClassType typeAttributeType
175     = ClassType.make("gnu.kawa.xml.AttributeType");
176   static final Method coerceMethod
177     = typeAttributeType.getDeclaredMethod("coerce", 3);
178   static final Method coerceOrNullMethod
179     = typeAttributeType.getDeclaredMethod("coerceOrNull", 3);
180
181   public void writeExternal(ObjectOutput out) throws IOException
182   {
183     String JavaDoc name = getName();
184     out.writeUTF(name == null ? "" : name);
185     out.writeObject(qname);
186   }
187
188   public void readExternal(ObjectInput in)
189     throws IOException, ClassNotFoundException JavaDoc
190   {
191     String JavaDoc name = in.readUTF();
192     if (name.length() > 0)
193       setName(name);
194     qname = (Symbol) in.readObject();
195   }
196
197   public String JavaDoc toString ()
198   {
199     return "AttributeType " + qname;
200   }
201 }
202
Popular Tags