1 22 23 package org.xquark.schema; 24 25 import java.util.*; 26 27 import org.xquark.schema.loader.SchemaComponentRef; 28 import org.xquark.schema.loader.TypeRef; 29 30 42 public class SchemaException extends Exception { 43 private static final String RCSRevision = "$Revision: 1.1 $"; 44 private static final String RCSName = "$Name: $"; 45 46 static private ResourceBundle errorStrings = 47 ResourceBundle.getBundle("org.xquark.schema.resources.errors", java.util.Locale.getDefault()); 48 51 private String errorCode = null; 52 53 56 private Object invalidObject = null; 57 58 61 private ArrayList exceptions = null; 62 63 public SchemaException(String errorCode) { 64 this(errorCode, (Object ) null); 65 } 66 67 public SchemaException(String errorCode, Object object) { 68 super(errorCode == null ? null : errorStrings.getString(errorCode)); 69 this.errorCode = errorCode; 70 this.invalidObject = object; 71 } 72 73 public SchemaException(String errorCode, Object object, Exception exception) { 74 this(errorCode, object); 75 add(exception); 76 } 77 78 public SchemaException(String errorCode, Object object, List exceptions) { 79 this(errorCode, object); 80 add(exceptions); 81 } 82 83 92 public SchemaException(Exception e) { 93 this(null, null, e); 94 } 95 96 public SchemaException(List exceptions) { 97 this(null, null, exceptions); 98 } 99 100 103 public String getErrorCode() { 104 return this.errorCode; 105 } 106 107 public Object getInvalidObject() { 108 return invalidObject; 109 } 110 111 public void setInvalidObject(Object object) { 112 invalidObject = object; 113 } 114 115 124 public String getMessage() { 125 return getMessage(new SchemaException.PrintVisitor()); 126 } 127 128 private String getMessage(SchemaException.PrintVisitor visitor) { 129 StringBuffer result = new StringBuffer (); 130 if (errorCode != null) 131 result.append(errorCode); 132 String message = super.getMessage(); 133 if (message != null) { 134 if (result.length() > 0) 135 result.append(": "); 136 result.append(message); 137 } 138 if (invalidObject != null && visitor != null) { 139 result.append(" ("); 140 if (invalidObject instanceof SchemaVisitable) { 141 result.append(visitor.toString((SchemaVisitable) invalidObject)); 142 } else { 143 result.append(invalidObject.toString()); 144 } 145 result.append(")"); 146 } 147 if (exceptions != null) { 148 Iterator it = exceptions.iterator(); 149 while (it.hasNext()) { 150 if (result.length() > 0) 151 result.append('\n'); 152 result.append(((SchemaException) it.next()).getMessage(visitor)); 153 } 154 } 155 return result.toString(); 156 } 157 158 163 public List getExceptions() { 164 return exceptions; 165 } 166 167 public void add(Exception exception) { 168 if (exception == null) 169 return; 170 if (exceptions == null) 171 exceptions = new ArrayList(); 172 exceptions.add(exception); 173 } 174 175 public void add(List exceptions) { 176 if (exceptions == null) 177 return; 178 if (this.exceptions == null) 179 this.exceptions = new ArrayList(); 180 this.exceptions.addAll(exceptions); 181 } 182 183 public static SchemaException computeException(Particle p) { 184 SchemaException.ComputeExceptionVisitor visitor = new SchemaException.ComputeExceptionVisitor(); 185 try { 186 p.accept(visitor); 187 } catch (Exception e) { 188 } 190 return visitor.getResult(); 191 } 192 193 public static SchemaException computeMinExtentException(Particle p) { 194 SchemaException.ComputeExceptionVisitor visitor = new SchemaException.ComputeMinExtentExceptionVisitor(); 195 try { 196 p.accept(visitor); 197 } catch (Exception e) { 198 } 200 return visitor.getResult(); 201 } 202 203 static class ComputeExceptionVisitor extends DefaultSchemaVisitor { 204 private static final String RCSRevision = "$Revision: 1.1 $"; 205 private static final String RCSName = "$Name: $"; 206 207 protected SchemaException result = null; 208 209 public SchemaException getResult() { 210 return result; 211 } 212 213 public void visit(Particle p) throws SchemaException { 214 ((SchemaVisitable) p.getTerm()).accept(this); 215 result.setInvalidObject(p); 216 } 217 218 public void visit(ElementDeclaration decl) throws SchemaException { 219 result = new SchemaException("cvc-particle.2.3"); 220 } 221 222 public void visit(Wildcard wildcard) throws SchemaException { 223 result = new SchemaException("cvc-particle.1.3"); 224 } 225 226 public void visit(ModelGroup group) throws SchemaException { 227 result = new SchemaException("cvc-particle.3.3"); 228 } 229 } 230 231 static class ComputeMinExtentExceptionVisitor extends SchemaException.ComputeExceptionVisitor { 232 private static final String RCSRevision = "$Revision: 1.1 $"; 233 private static final String RCSName = "$Name: $"; 234 235 public void visit(Particle p) throws SchemaException { 236 if (p.getMinExtent() > 0) { 237 super.visit(p); 238 } 239 } 240 241 public void visit(SequenceModelGroup group) throws SchemaException { 242 Iterator it = group.iterator(); 243 while (it.hasNext()) { 244 Particle p = (Particle) it.next(); 245 p.accept(this); 246 if (result != null) 247 break; 248 } 249 result = new SchemaException("cvc-particle.3.3", null, result); 250 } 251 252 public void visit(ChoiceModelGroup group) throws SchemaException { 253 ArrayList exceptions = new ArrayList(); 254 Iterator it = group.iterator(); 255 while (it.hasNext()) { 256 Particle p = (Particle) it.next(); 257 result = null; 258 p.accept(this); 259 if (result != null) 260 exceptions.add(result); 261 } 262 result = new SchemaException("cvc-particle.3.3", null, exceptions); 263 } 264 265 public void visit(AllModelGroup group) throws SchemaException { 266 ArrayList exceptions = new ArrayList(); 267 Iterator it = group.iterator(); 268 while (it.hasNext()) { 269 Particle p = (Particle) it.next(); 270 result = null; 271 p.accept(this); 272 if (result != null) 273 exceptions.add(result); 274 } 275 result = new SchemaException("cvc-particle.3.3", null, exceptions); 276 } 277 } 278 279 static class PrintVisitor extends DefaultSchemaVisitor { 280 private static final String RCSRevision = "$Revision: 1.1 $"; 281 private static final String RCSName = "$Name: $"; 282 private String result = null; 283 private int minOccurs = -1; 284 private int maxOccurs = -1; 285 286 String toString(SchemaVisitable invalidObject) { 287 result = null; 288 minOccurs = -1; 289 maxOccurs = -1; 290 try { 291 invalidObject.accept(this); 292 } catch (Exception e) { 293 } 295 return result; 296 } 297 298 private void addOccurrences() { 299 if (minOccurs != -1) { 300 if (minOccurs != 1) 301 result += " minOccurs=\"" + minOccurs + "\""; 302 if (maxOccurs == Integer.MAX_VALUE) 303 result += " maxOccurs=\"unbounded\""; 304 else if (maxOccurs != 1) 305 result += " maxOccurs=\"" + maxOccurs + "\""; 306 } 307 } 308 309 public void visit(SchemaComponentRef ref) throws SchemaException { 310 result = "<reference name=\"" + ref.getName() + "\">"; 311 } 312 313 public void visit(TypeRef ref) throws SchemaException { 314 result = "<reference name=\"" + ref.getName() + "\">"; 315 } 316 317 public void visit(ElementDeclaration decl) throws SchemaException { 318 result = "<element name=\"" + decl.getName() + "\""; 319 addOccurrences(); 320 result += ">"; 321 } 322 323 public void visit(AttributeDeclaration decl) throws SchemaException { 324 result = "<attribute name=\"" + decl.getName() + "\">"; 325 } 326 327 public void visit(NotationDeclaration decl) throws SchemaException { 328 result = "<notation name=\"" + decl.getName() + "\">"; 329 } 330 331 public void visit(Wildcard w) throws SchemaException { 332 String constraint = null; 333 if (w.isAny()) 334 constraint = null; 335 else if (!w.isInclude()) 336 constraint = "##other"; 337 else { 338 Iterator it = w.getNamespaces().iterator(); 339 while (it.hasNext()) { 340 String ns = (String ) it.next(); 341 if (ns == null) 342 ns = "##local"; 343 else if (ns.equals(w.getNamespace())) 344 ns = "##targetNamespace"; 345 if (constraint == null) 346 constraint = ""; 347 else 348 constraint += " "; 349 constraint += ns; 350 } 351 } 352 if (minOccurs == -1) { 353 result = "<anyAttribute"; 354 if (constraint != null) 355 result += " namespace=\"" + constraint + "\""; 356 result += ">"; 357 } else { 358 result = "<any"; 359 if (constraint != null) 360 result += " namespace=\"" + constraint + "\""; 361 addOccurrences(); 362 result += ">"; 363 } 364 } 365 366 public void visit(ComplexType type) throws SchemaException { 367 result = "<complexType"; 368 if (type.getName() != null) 369 result += " name=\"" + type.getName() + "\""; 370 result += ">"; 371 } 372 373 public void visit(SimpleType type) throws SchemaException { 374 result = "<simpleType"; 375 if (type.getName() != null) 376 result += " name=\"" + type.getName() + "\""; 377 result += ">"; 378 } 379 380 public void visit(Particle p) throws SchemaException { 381 minOccurs = p.getMinOccurs(); 382 maxOccurs = p.getMaxOccurs(); 383 ((SchemaVisitable) p.getTerm()).accept(this); 384 minOccurs = maxOccurs = -1; 385 } 386 387 public void visit(SequenceModelGroup group) throws SchemaException { 388 result = "<sequence"; 389 addOccurrences(); 390 result += ">"; 391 } 392 393 public void visit(ChoiceModelGroup group) throws SchemaException { 394 result = "<choice"; 395 addOccurrences(); 396 result += ">"; 397 } 398 399 public void visit(AllModelGroup group) throws SchemaException { 400 result = "<all"; 401 addOccurrences(); 402 result += ">"; 403 } 404 405 public void visit(IdentityConstraint constraint) throws SchemaException { 406 switch (constraint.getCategory()) { 407 case IdentityConstraint.CATEGORY_UNIQUE : 408 result = "<unique name=\"" + constraint.getName() + "\">"; 409 break; 410 case IdentityConstraint.CATEGORY_KEY : 411 result = "<key name=\"" + constraint.getName() + "\">"; 412 break; 413 case IdentityConstraint.CATEGORY_KEYREF : 414 result = 415 "<keyref name=\"" 416 + constraint.getName() 417 + "\" refer=\"" 418 + constraint.getReferencedKey().getName() 419 + "\">"; 420 break; 421 } 422 } 423 } 424 } 425 | Popular Tags |