1 package jfun.yan.xml.nuts.optional; 2 3 import jfun.yan.Component; 4 import jfun.yan.xml.NutsUtils; 5 import jfun.yan.xml.nut.ComponentNut; 6 7 14 public abstract class BinaryNut extends ComponentNut { 15 private Object val1; 16 private Object val2; 17 private Class type1; 18 private Class type2; 19 private boolean set1 = false; 20 private boolean set2 = false; 21 22 25 public boolean isVal1Set() { 26 return set1; 27 } 28 29 32 public boolean isVal2Set() { 33 return set2; 34 } 35 36 39 public Component getComponent1(){ 40 return NutsUtils.asComponent(getVal1()); 41 } 42 45 public Component getComponent2(){ 46 return NutsUtils.asComponent(getVal2()); 47 } 48 public Object getVal1() { 49 return convert(type1, val1); 50 } 51 52 public void setVal1(Object val1) { 53 this.val1 = val1; 54 this.set1 = true; 55 } 56 private void checkVal(String name, Object v, boolean flag){ 57 if(flag){ 58 raise("attribute " + name+" already set"); 59 } 60 } 61 public void set(Object [] vals){ 62 if(vals.length>2){ 63 raise("at most 2 sub-elements are allowed."); 64 } 65 if(set1){ 66 if(vals.length>1){ 67 raise("attribute val1 already set"); 68 } 69 else{ 70 checkVal("val2", val2, set2); 71 val2 = vals[0]; 72 set2 = true; 73 } 74 } 75 else if(set2){ 76 if(vals.length>1){ 77 raise("attribute val2 already set"); 78 } 79 else{ 80 val1 = vals[0]; 81 set1 = true; 82 } 83 } 84 else{ 85 if(vals.length>0){ 86 val1 = vals[0]; 87 set1 = true; 88 } 89 if(vals.length>1){ 90 val2 = vals[1]; 91 set2 = true; 92 } 93 } 94 } 95 public Object getVal2() { 96 return convert(type2, val2); 97 } 98 99 public void setVal2(Object val2) { 100 this.val2 = val2; 101 this.set2 = true; 102 } 103 104 105 public Class getType1() { 106 return type1; 107 } 108 109 public void setType1(Class type1) { 110 this.type1 = type1; 111 } 112 113 public Class getType2() { 114 return type2; 115 } 116 117 public void setType2(Class type2) { 118 this.type2 = type2; 119 } 120 136 137 } 138 | Popular Tags |