1 package com.sun.tools.xjc.runtime; 2 3 import java.util.HashMap ; 4 import java.util.HashSet ; 5 import java.util.Iterator ; 6 import java.util.Map ; 7 8 import javax.xml.bind.ValidationEvent; 9 import javax.xml.bind.ValidationEventHandler; 10 import javax.xml.bind.helpers.NotIdentifiableEventImpl; 11 import javax.xml.bind.helpers.ValidationEventImpl; 12 import javax.xml.bind.helpers.ValidationEventLocatorImpl; 13 14 import org.xml.sax.SAXException ; 15 16 import com.sun.xml.bind.ProxyGroup; 17 import com.sun.xml.bind.serializer.AbortSerializationException; 18 import com.sun.xml.bind.validator.Messages; 19 20 37 class ValidationContext 38 { 39 final DefaultJAXBContextImpl jaxbContext; 40 44 ValidationContext( DefaultJAXBContextImpl _context, ValidationEventHandler _eventHandler, boolean validateID ) { 45 this.jaxbContext = _context; 46 this.eventHandler = _eventHandler; 47 this.validateID = validateID; 48 } 49 50 51 52 58 59 private final IdentityHashSet validatedObjects = new IdentityHashSet(); 60 61 65 public void validate( ValidatableObject vo ) throws SAXException { 66 if( validatedObjects.add(ProxyGroup.unwrap(vo)) ) { 67 MSVValidator.validate(jaxbContext,this,vo); 69 } else { 70 reportEvent( vo, Messages.format( Messages.CYCLE_DETECTED ) ); 72 } 73 } 74 75 76 82 83 private final NamespaceContextImpl nsContext = new NamespaceContextImpl(null); 84 85 public NamespaceContextImpl getNamespaceContext() { return nsContext; } 86 87 88 94 private final boolean validateID; 95 96 private final HashSet IDs = new HashSet (); 97 private final HashMap IDREFs = new HashMap (); 98 99 public String onID( XMLSerializable owner, String value ) throws SAXException { 100 101 if(!validateID) return value; 102 103 if(!IDs.add(value)) { 104 reportEvent(jaxbContext.getGrammarInfo().castToValidatableObject(owner), 108 Messages.format(Messages.DUPLICATE_ID,value)); 109 } 110 111 return value; 112 } 113 public String onIDREF( XMLSerializable referer, String value ) throws SAXException { 114 if(!validateID) return value; 115 116 if(IDs.contains(value)) 117 return value; 119 IDREFs.put(value,referer); 121 122 return value; 123 } 124 125 protected void reconcileIDs() throws SAXException { 126 if(!validateID) return; 127 128 for (Iterator itr = IDREFs.entrySet().iterator(); itr.hasNext();) { 129 Map.Entry e = (Map.Entry ) itr.next(); 130 131 if(IDs.contains(e.getKey())) 132 continue; 134 ValidatableObject source = (ValidatableObject)e.getValue(); 136 reportEvent( 137 source, 138 new NotIdentifiableEventImpl( 139 ValidationEvent.ERROR, 140 Messages.format( Messages.ID_NOT_FOUND, e.getKey() ), 141 new ValidationEventLocatorImpl( source ) ) ); 142 } 143 144 IDREFs.clear(); 145 } 146 147 148 private final ValidationEventHandler eventHandler; 154 155 158 public void reportEvent(ValidatableObject source, String formattedMessage) throws AbortSerializationException { 159 reportEvent( 160 source, 161 new ValidationEventImpl( ValidationEvent.ERROR, 162 formattedMessage, 163 new ValidationEventLocatorImpl(source) ) ); 164 } 165 166 170 public void reportEvent(ValidatableObject source, Exception nestedException ) throws AbortSerializationException { 171 reportEvent( 172 source, 173 new ValidationEventImpl( ValidationEvent.ERROR, 174 nestedException.toString(), 175 new ValidationEventLocatorImpl(source), 176 nestedException ) ); 177 } 178 179 public void reportEvent( ValidatableObject source, ValidationEvent event ) throws AbortSerializationException { 180 boolean r; 181 182 try { 183 r = eventHandler.handleEvent( event ); 184 } catch( RuntimeException re ) { 185 r = false; 188 } 189 190 if(!r) { 191 throw new AbortSerializationException( event.getMessage() ); 193 } 194 } 195 196 197 198 } 199 | Popular Tags |