1 8 package com.sun.japex.testsuite.impl.runtime; 9 10 import java.util.HashMap ; 11 import java.util.HashSet ; 12 import java.util.Iterator ; 13 import java.util.Map ; 14 15 import javax.xml.bind.ValidationEvent; 16 import javax.xml.bind.ValidationEventHandler; 17 import javax.xml.bind.helpers.NotIdentifiableEventImpl; 18 import javax.xml.bind.helpers.ValidationEventImpl; 19 import javax.xml.bind.helpers.ValidationEventLocatorImpl; 20 21 import org.xml.sax.SAXException ; 22 23 import com.sun.xml.bind.ProxyGroup; 24 import com.sun.xml.bind.serializer.AbortSerializationException; 25 import com.sun.xml.bind.validator.Messages; 26 27 44 class ValidationContext 45 { 46 final DefaultJAXBContextImpl jaxbContext; 47 51 ValidationContext( DefaultJAXBContextImpl _context, ValidationEventHandler _eventHandler, boolean validateID ) { 52 this.jaxbContext = _context; 53 this.eventHandler = _eventHandler; 54 this.validateID = validateID; 55 } 56 57 58 59 65 66 private final HashSet validatedObjects = new HashSet (); 67 68 72 public void validate( ValidatableObject vo ) throws SAXException { 73 if( validatedObjects.add(ProxyGroup.unwrap(vo)) ) { 74 MSVValidator.validate(jaxbContext,this,vo); 76 } else { 77 reportEvent( vo, Messages.format( Messages.CYCLE_DETECTED ) ); 79 } 80 } 81 82 83 89 90 private final NamespaceContextImpl nsContext = new NamespaceContextImpl(null); 91 92 public NamespaceContextImpl getNamespaceContext() { return nsContext; } 93 94 95 101 private final boolean validateID; 102 103 private final HashSet IDs = new HashSet (); 104 private final HashMap IDREFs = new HashMap (); 105 106 public String onID( XMLSerializable owner, String value ) throws SAXException { 107 108 if(!validateID) return value; 109 110 if(!IDs.add(value)) { 111 reportEvent(jaxbContext.getGrammarInfo().castToValidatableObject(owner), 115 Messages.format(Messages.DUPLICATE_ID,value)); 116 } 117 118 return value; 119 } 120 public String onIDREF( XMLSerializable referer, String value ) throws SAXException { 121 if(!validateID) return value; 122 123 if(IDs.contains(value)) 124 return value; 126 IDREFs.put(value,referer); 128 129 return value; 130 } 131 132 protected void reconcileIDs() throws SAXException { 133 if(!validateID) return; 134 135 for (Iterator itr = IDREFs.entrySet().iterator(); itr.hasNext();) { 136 Map.Entry e = (Map.Entry ) itr.next(); 137 138 if(IDs.contains(e.getKey())) 139 continue; 141 ValidatableObject source = (ValidatableObject)e.getValue(); 143 reportEvent( 144 source, 145 new NotIdentifiableEventImpl( 146 ValidationEvent.ERROR, 147 Messages.format( Messages.ID_NOT_FOUND, e.getKey() ), 148 new ValidationEventLocatorImpl( source ) ) ); 149 } 150 151 IDREFs.clear(); 152 } 153 154 155 private final ValidationEventHandler eventHandler; 161 162 165 public void reportEvent(ValidatableObject source, String formattedMessage) throws AbortSerializationException { 166 reportEvent( 167 source, 168 new ValidationEventImpl( ValidationEvent.ERROR, 169 formattedMessage, 170 new ValidationEventLocatorImpl(source) ) ); 171 } 172 173 177 public void reportEvent(ValidatableObject source, Exception nestedException ) throws AbortSerializationException { 178 reportEvent( 179 source, 180 new ValidationEventImpl( ValidationEvent.ERROR, 181 nestedException.toString(), 182 new ValidationEventLocatorImpl(source), 183 nestedException ) ); 184 } 185 186 public void reportEvent( ValidatableObject source, ValidationEvent event ) throws AbortSerializationException { 187 boolean r; 188 189 try { 190 r = eventHandler.handleEvent( event ); 191 } catch( RuntimeException re ) { 192 r = false; 195 } 196 197 if(!r) { 198 throw new AbortSerializationException( event.getMessage() ); 200 } 201 } 202 203 204 205 } 206 | Popular Tags |