1 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 15 16 public class AttributeType extends NodeType 17 implements TypeValue, Externalizable, AttributePredicate 18 { 19 Symbol qname; 20 21 public static AttributeType make (String namespaceURI, String 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 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 getNamespaceURI () { return qname.getNamespaceURI(); } 56 public final String 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 coerceFromObject (Object 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 attrType) 81 { 82 String namespaceURI = qname.getNamespaceURI(); 83 String localName = qname.getLocalName(); 84 String curNamespaceURI; 85 String curLocalName; 86 if (attrType instanceof Symbol) 87 { 88 Symbol qname = (Symbol) attrType; 89 curNamespaceURI = qname.getNamespaceURI(); 90 curLocalName = qname.getLocalName(); 91 } 92 93 101 else 102 { 103 curNamespaceURI = ""; 104 curLocalName = attrType.toString().intern(); } 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 obj) 113 { 114 return coerceOrNull(obj, qname.getNamespaceURI(),qname.getLocalName()) 115 != null; 116 } 117 118 public static SeqPosition coerceOrNull (Object obj, 119 String namespaceURI, String 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 curName = pos.getNextTypeObject(); 127 String curNamespaceURI; 128 String curLocalName; 129 if (curName instanceof Symbol) 130 { 131 Symbol qname = (Symbol) curName; 132 curNamespaceURI = qname.getNamespaceURI(); 133 curLocalName = qname.getLocalName(); 134 } 135 136 144 else 145 { 146 curNamespaceURI = ""; 147 curLocalName = curName.toString().intern(); } 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 obj, 156 String namespaceURI, String localName) 157 { 158 SeqPosition pos = coerceOrNull(obj, namespaceURI, localName); 159 if (pos == null) 160 throw new ClassCastException (); 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 name = getName(); 184 out.writeUTF(name == null ? "" : name); 185 out.writeObject(qname); 186 } 187 188 public void readExternal(ObjectInput in) 189 throws IOException, ClassNotFoundException 190 { 191 String name = in.readUTF(); 192 if (name.length() > 0) 193 setName(name); 194 qname = (Symbol) in.readObject(); 195 } 196 197 public String toString () 198 { 199 return "AttributeType " + qname; 200 } 201 } 202 | Popular Tags |