1 28 29 package org.jibx.binding.model; 30 31 import java.io.InputStream ; 36 import java.net.URL ; 37 import java.util.ArrayList ; 38 import java.util.HashSet ; 39 import java.util.Iterator ; 40 41 import org.jibx.binding.classes.ClassCache; 42 import org.jibx.binding.util.StringArray; 44 import org.jibx.runtime.BindingDirectory; 46 import org.jibx.runtime.EnumSet; 47 import org.jibx.runtime.IBindingFactory; 48 import org.jibx.runtime.IUnmarshallingContext; 50 import org.jibx.runtime.JiBXException; 51 52 58 59 public class BindingElement extends NestingElementBase 60 { 61 62 public static final StringArray s_allowedAttributes = 63 new StringArray(new String [] { "direction", "forwards", "name", 64 "package", "track-source" }, NestingElementBase.s_allowedAttributes); 65 66 69 public static final int IN_BINDING = 0; 70 public static final int OUT_BINDING = 1; 71 public static final int BOTH_BINDING = 2; 72 73 static final EnumSet s_directionEnum = new EnumSet(IN_BINDING, 74 new String [] { "input", "output", "both" }); 75 76 79 80 private String m_name; 81 82 83 private String m_direction; 84 85 86 private boolean m_isInput; 87 88 89 private boolean m_isOutput; 90 91 92 private boolean m_isForward; 93 94 95 private boolean m_isTrackSource; 96 97 98 private String m_targetPackage; 99 100 101 private URL m_baseUrl; 102 103 104 private HashSet m_includePaths; 105 106 107 private ArrayList m_children; 108 109 110 private HashSet m_idClassSet; 111 112 115 public BindingElement() { 116 super(BINDING_ELEMENT); 117 m_includePaths = new HashSet (); 118 m_children = new ArrayList (); 119 } 120 121 126 public void setName(String name) { 127 m_name = name; 128 } 129 130 135 public String getName() { 136 return m_name; 137 } 138 139 145 public void setForward(boolean forward) { 146 m_isForward = forward; 147 } 148 149 155 public boolean isForward() { 156 return m_isForward; 157 } 158 159 165 public void setTrackSource(boolean track) { 166 m_isTrackSource = track; 167 } 168 169 175 public boolean isTrackSource() { 176 return m_isTrackSource; 177 } 178 179 184 public void setTargetPackage(String pack) { 185 m_targetPackage = pack; 186 } 187 188 193 public String getTargetPackage() { 194 return m_targetPackage; 195 } 196 197 202 public void setBaseUrl(URL base) { 203 m_baseUrl = base; 204 } 205 206 211 public URL getBaseUrl() { 212 return m_baseUrl; 213 } 214 215 221 public void setOutBinding(boolean out) { 222 m_isOutput = out; 223 } 224 225 231 public boolean isOutBinding() { 232 return m_isOutput; 233 } 234 235 241 public void setInBinding(boolean in) { 242 m_isInput = in; 243 } 244 245 251 public boolean isInBinding() { 252 return m_isInput; 253 } 254 255 260 public boolean addIncludePath(String path) { 261 return m_includePaths.add(path); 262 } 263 264 275 public void addIdClass(IClass clas) { 276 277 if (m_idClassSet == null) { 279 m_idClassSet = new HashSet (); 280 } 281 282 if (m_idClassSet.add(clas.getName())) { 284 285 String [] inames = clas.getInterfaces(); 287 for (int i = 0; i < inames.length; i++) { 288 m_idClassSet.add(inames[i]); 289 } 290 while (clas != null && m_idClassSet.add(clas.getName())) { 291 clas = clas.getSuperClass(); 292 } 293 } 294 } 295 296 304 public boolean isIdClass(String name) { 305 if (m_idClassSet == null) { 306 return false; 307 } else { 308 return m_idClassSet.contains(name); 309 } 310 } 311 312 318 public void addTopChild(Object child) { 319 m_children.add(child); 320 } 321 322 327 public ArrayList topChildren() { 328 return m_children; 329 } 330 331 336 public Iterator topChildIterator() { 337 return m_children.iterator(); 338 } 339 340 343 346 public boolean hasAttribute() { 347 throw new IllegalStateException 348 ("Internal error: method should never be called"); 349 } 350 351 354 public boolean hasContent() { 355 throw new IllegalStateException 356 ("Internal error: method should never be called"); 357 } 358 359 362 public boolean isOptional() { 363 throw new IllegalStateException 364 ("Internal error: method should never be called"); 365 } 366 367 373 public int getDefaultStyle() { 374 int style = super.getDefaultStyle(); 375 if (style < 0) { 376 style = NestingAttributes.s_styleEnum.getValue("element"); 377 } 378 return style; 379 } 380 381 384 390 private void preSet(IUnmarshallingContext uctx) throws JiBXException { 391 validateAttributes(uctx, s_allowedAttributes); 392 } 393 394 399 public void prevalidate(ValidationContext vctx) { 400 401 int index = -1; 403 if (m_direction != null) { 404 index = s_directionEnum.getValue(m_direction); 405 if (index < 0) { 406 vctx.addError("Value \"" + m_direction + 407 "\" is not a valid choice for direction"); 408 } 409 } else { 410 index = BOTH_BINDING; 411 } 412 m_isInput = index == IN_BINDING || index == BOTH_BINDING; 413 m_isOutput = index == OUT_BINDING || index == BOTH_BINDING; 414 super.prevalidate(vctx); 415 } 416 417 private static FormatElement buildFormat(String name, String type, 418 boolean use, String sname, String dname, String dflt) { 419 FormatElement format = new FormatElement(); 420 format.setLabel(name); 421 format.setTypeName(type); 422 format.setDefaultFormat(use); 423 format.setSerializerName(sname); 424 format.setDeserializerName(dname); 425 format.setDefaultText(dflt); 426 return format; 427 } 428 429 private void defineBaseFormat(FormatElement format, 430 DefinitionContext dctx, ValidationContext vctx) { 431 format.prevalidate(vctx); 432 format.validate(vctx); 433 dctx.addFormat(format, vctx); 434 } 435 436 437 442 public void runValidation(ValidationContext vctx) { 443 444 m_isInput = true; 446 m_isOutput = true; 447 448 DefinitionContext dctx = new DefinitionContext(null); 450 vctx.setGlobalDefinitions(dctx); 451 defineBaseFormat(buildFormat("byte:default", "byte", true, 452 "org.jibx.runtime.Utility.serializeByte", 453 "org.jibx.runtime.Utility.parseByte", "0"), dctx, vctx); 454 defineBaseFormat(buildFormat("char:default", "char", true, 455 "org.jibx.runtime.Utility.serializeChar", 456 "org.jibx.runtime.Utility.parseChar", "0"), dctx, vctx); 457 defineBaseFormat(buildFormat("double:default", "double", true, 458 "org.jibx.runtime.Utility.serializeDouble", 459 "org.jibx.runtime.Utility.parseDouble", "0.0"), dctx, vctx); 460 defineBaseFormat(buildFormat("float:default", "float", true, 461 "org.jibx.runtime.Utility.serializeFloat", 462 "org.jibx.runtime.Utility.parseFloat", "0.0"), dctx, vctx); 463 defineBaseFormat(buildFormat("int:default", "int", true, 464 "org.jibx.runtime.Utility.serializeInt", 465 "org.jibx.runtime.Utility.parseInt", "0"), dctx, vctx); 466 defineBaseFormat(buildFormat("long:default", "long", true, 467 "org.jibx.runtime.Utility.serializeLong", 468 "org.jibx.runtime.Utility.parseLong", "0"), dctx, vctx); 469 defineBaseFormat(buildFormat("short:default", "short", true, 470 "org.jibx.runtime.Utility.serializeShort", 471 "org.jibx.runtime.Utility.parseShort", "0"), dctx, vctx); 472 defineBaseFormat(buildFormat("boolean:default", "boolean", true, 473 "org.jibx.runtime.Utility.serializeBoolean", 474 "org.jibx.runtime.Utility.parseBoolean", "false"), dctx, vctx); 475 defineBaseFormat(buildFormat("Date:default", "java.util.Date", true, 476 "org.jibx.runtime.Utility.serializeDateTime", 477 "org.jibx.runtime.Utility.deserializeDateTime", null), dctx, vctx); 478 defineBaseFormat(buildFormat("SqlDate:default", "java.sql.Date", 479 true, "org.jibx.runtime.Utility.serializeSqlDate", 480 "org.jibx.runtime.Utility.deserializeSqlDate", null), dctx, vctx); 481 defineBaseFormat(buildFormat("byte[]:default", "byte[]", true, 482 "org.jibx.runtime.Utility.serializeBase64", 483 "org.jibx.runtime.Utility.deserializeBase64", null), dctx, vctx); 484 defineBaseFormat(buildFormat("String:default", "java.lang.String", 485 true, null, null, null), dctx, vctx); 486 defineBaseFormat(buildFormat("Object:default", "java.lang.Object", 487 true, null, null, null), dctx, vctx); 488 FormatElement format = buildFormat("char:string", "char", false, 489 "org.jibx.runtime.Utility.serializeCharString", 490 "org.jibx.runtime.Utility.deserializeCharString", "0"); 491 format.setDefaultFormat(false); 492 format.prevalidate(vctx); 493 format.validate(vctx); 494 dctx.addFormat(format, vctx); 495 NamespaceElement ns = new NamespaceElement(); 496 ns.setDefaultName("all"); 497 ns.prevalidate(vctx); 498 dctx.addNamespace(ns); 499 501 setDefinitions(new DefinitionContext(dctx)); 503 504 vctx.prevalidate(this); 506 RegistrationVisitor rvisitor = new RegistrationVisitor(vctx); 507 rvisitor.visitTree(this); 508 vctx.validate(this); 509 } 510 511 520 public static BindingElement readBinding(InputStream is, String fname, 521 ValidationContext vctx) throws JiBXException { 522 523 IBindingFactory bfact = 525 BindingDirectory.getFactory(BindingElement.class); 526 527 IUnmarshallingContext uctx = bfact.createUnmarshallingContext(); 529 uctx.setDocument(is, fname, null); 530 uctx.pushObject(new UnmarshalWrapper(vctx)); 531 BindingElement binding = (BindingElement)uctx.unmarshalElement(); 532 uctx.popObject(); 533 return binding; 534 } 535 536 546 public static BindingElement validateBinding(String name, URL path, 547 InputStream is, ValidationContext vctx) throws JiBXException { 548 549 BindingElement binding = readBinding(is, name, vctx); 551 binding.setBaseUrl(path); 552 vctx.setBindingRoot(binding); 553 554 binding.runValidation(vctx); 556 557 ArrayList probs = vctx.getProblems(); 559 if (probs.size() > 0) { 560 for (int i = 0; i < probs.size(); i++) { 561 ValidationProblem prob = (ValidationProblem)probs.get(i); 562 System.out.print(prob.getSeverity() >= 563 ValidationProblem.ERROR_LEVEL ? "Error: " : "Warning: "); 564 System.out.println(prob.getDescription()); 565 } 566 } 567 return binding; 568 } 569 570 575 public static ValidationContext newValidationContext() { 576 IClassLocator locate = new IClassLocator() { 577 public IClass getClassInfo(String name) { 578 try { 579 return new ClassWrapper(ClassCache.getClassFile(name)); 580 } catch (JiBXException e) { 581 return null; 582 } 583 } 584 }; 585 return new ValidationContext(locate); 586 } 587 588 638 639 644 public static class UnmarshalWrapper 645 { 646 private final ValidationContext m_validationContext; 647 648 private UnmarshalWrapper(ValidationContext vctx) { 649 m_validationContext = vctx; 650 } 651 652 public ValidationContext getValidation() { 653 return m_validationContext; 654 } 655 } 656 } | Popular Tags |