1 57 58 package org.enhydra.apache.xerces.validators.datatype; 59 60 import java.util.Enumeration ; 61 import java.util.Hashtable ; 62 63 import org.enhydra.apache.xerces.utils.XMLMessages; 64 65 74 public class IDREFDatatypeValidator extends StringDatatypeValidator { 75 private static Object fNullValue = new Object (); 76 77 public static final int IDREF_VALIDATE = 0; 78 public static final int IDREF_CHECKID = 1; 79 80 public IDREFDatatypeValidator () throws InvalidDatatypeFacetException { 81 this( null, null, false ); } 83 84 public IDREFDatatypeValidator ( DatatypeValidator base, Hashtable facets, 85 boolean derivedByList ) throws InvalidDatatypeFacetException { 86 87 super (base, facets, derivedByList); 89 90 if ( derivedByList ) 92 return; 93 94 if (base != null) 96 setTokenType(((IDREFDatatypeValidator)base).fTokenType); 97 else 98 setTokenType(SPECIAL_TOKEN_IDREFNAME); 99 } 100 101 104 public short getWSFacet() { 105 return COLLAPSE; 106 } 107 108 124 public Object validate(String content, Object state ) throws InvalidDatatypeValueException{ 125 StateMessageDatatype message = (StateMessageDatatype) state; 126 if (message != null && message.getDatatypeState() == IDREF_CHECKID) { 127 Object [] params = (Object [])message.getDatatypeObject(); 128 checkIdRefs((Hashtable )params[0], (Hashtable )params[1]); 129 } 130 else { 131 super.validate(content, state); 133 134 if ( message != null && message.getDatatypeState() == IDREF_VALIDATE ) 135 addIdRef( content, (Hashtable )message.getDatatypeObject()); 136 } 137 138 return null; 139 } 140 141 142 145 public Object clone() throws CloneNotSupportedException { 146 throw new CloneNotSupportedException ("clone() is not supported in "+this.getClass().getName()); 147 } 148 149 150 private void addIdRef(String content, Hashtable IDREFList) { 151 if ( IDREFList.containsKey( content ) ) 152 return; 153 154 try { 155 IDREFList.put( content, fNullValue ); 156 } 157 catch ( OutOfMemoryError ex ) { 158 System.out.println( "Out of Memory: Hashtable of ID's has " + IDREFList.size() + " Elements" ); 159 ex.printStackTrace(); 160 } 161 } 163 164 private void checkIdRefs(Hashtable IDList, Hashtable IDREFList) throws InvalidDatatypeValueException { 165 Enumeration en = IDREFList.keys(); 166 167 while ( en.hasMoreElements() ) { 168 String key = (String )en.nextElement(); 169 if ( !IDList.containsKey(key) ) { 170 InvalidDatatypeValueException error = new InvalidDatatypeValueException( key ); 171 error.setMinorCode(XMLMessages.MSG_ELEMENT_WITH_ID_REQUIRED); 172 error.setMajorCode(XMLMessages.VC_IDREF); 173 throw error; 174 } 175 } 176 } } 178 | Popular Tags |