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 public class ProcessingInstructionType extends NodeType 13 implements TypeValue, Externalizable 14 { 15 String target; 16 17 public static final ProcessingInstructionType piNodeTest 18 = new ProcessingInstructionType(null); 19 20 public ProcessingInstructionType(String target) 21 { 22 super(target == null ? "processing-instruction()" 23 : "processing-instruction("+target+")"); 24 this.target = target; 25 } 26 27 public static ProcessingInstructionType getInstance (String target) 28 { 29 return target == null ? piNodeTest : new ProcessingInstructionType(target); 30 } 31 32 public Type getImplementationType() 33 { 34 return ClassType.make("gnu.kawa.xml.KProcessingInstruction"); 35 36 } 37 38 public void emitCoerceFromObject (CodeAttr code) 39 { 40 code.emitPushString(target); 41 code.emitInvokeStatic(coerceMethod); 42 } 43 44 public Object coerceFromObject (Object obj) 45 { 46 return coerce(obj, target); 47 } 48 49 public boolean isInstancePos (AbstractSequence seq, int ipos) 50 { 51 int kind = seq.getNextKind(ipos); 52 if (kind == Sequence.PROCESSING_INSTRUCTION_VALUE) 53 return target == null || target.equals(seq.getNextTypeObject(ipos)); 54 if (kind == Sequence.OBJECT_VALUE) 55 return isInstance(seq.getPosNext(ipos)); 56 return false; 57 } 58 59 public boolean isInstance (Object obj) 60 { 61 return coerceOrNull(obj, target) != null; 62 } 63 64 public static KProcessingInstruction coerceOrNull (Object obj, String target) 65 { 66 KProcessingInstruction pos 67 = (KProcessingInstruction) NodeType.coerceOrNull(obj, PI_OK); 68 return pos != null && (target == null || target.equals(pos.getTarget())) 69 ? pos : null; 70 } 71 72 public static KProcessingInstruction coerce (Object obj, String target) 73 { 74 KProcessingInstruction pos = coerceOrNull(obj, target); 75 if (pos == null) 76 throw new ClassCastException (); 77 return pos; 78 } 79 80 protected void emitCoerceOrNullMethod(Variable incoming, Compilation comp) 81 { 82 CodeAttr code = comp.getCode(); 83 if (incoming != null) 84 code.emitLoad(incoming); 85 code.emitPushString(target); 86 code.emitInvokeStatic(coerceOrNullMethod); 87 } 88 89 public static final ClassType typeProcessingInstructionType 90 = ClassType.make("gnu.kawa.xml.ProcessingInstructionType"); 91 static final Method coerceMethod 92 = typeProcessingInstructionType.getDeclaredMethod("coerce", 2); 93 static final Method coerceOrNullMethod 94 = typeProcessingInstructionType.getDeclaredMethod("coerceOrNull", 2); 95 96 public void writeExternal(ObjectOutput out) throws IOException 97 { 98 out.writeObject(target); 99 } 100 101 public void readExternal(ObjectInput in) 102 throws IOException, ClassNotFoundException 103 { 104 target = (String ) in.readObject(); 105 } 106 107 public String toString () 108 { 109 return "ProcessingInstructionType " + (target == null ? "*" : target); 110 } 111 } 112 | Popular Tags |