1 22 package org.jboss.xb.binding; 23 24 import java.util.Map ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import org.jboss.xb.binding.parser.JBossXBParser; 28 29 33 public abstract class UnmarshallerFactory 34 { 35 protected Map features; 36 protected Boolean validation = Boolean.TRUE; 37 protected Boolean namespaces = Boolean.TRUE; 38 39 public static UnmarshallerFactory newInstance() 40 { 41 return new UnmarshallerFactoryImpl(); 42 } 43 44 public abstract Unmarshaller newUnmarshaller(); 45 46 public void setFeature(String name, Object value) 47 { 48 Boolean bValue; 49 if(value == null) 50 { 51 bValue = null; 52 } 53 else if(value instanceof String ) 54 { 55 bValue = Boolean.valueOf((String )value); 56 } 57 else if(value instanceof Boolean ) 58 { 59 bValue = (Boolean )value; 60 } 61 else 62 { 63 throw new JBossXBRuntimeException( 64 "Allowed feature values are null, 'true, 'false', Boolean.TRUE, Boolean.FALSE. Passed in value: " + value 65 ); 66 } 67 68 if(Unmarshaller.VALIDATION.equals(name)) 69 { 70 validation = bValue; 71 } 72 else if(Unmarshaller.NAMESPACES.equals(name)) 73 { 74 namespaces = bValue; 75 } 76 else 77 { 78 if(features == null) 79 { 80 features = new HashMap (); 81 } 82 features.put(name, value); 83 } 84 } 85 86 88 static class UnmarshallerFactoryImpl 89 extends UnmarshallerFactory 90 { 91 public Unmarshaller newUnmarshaller() 92 { 93 UnmarshallerImpl unmarshaller; 94 try 95 { 96 unmarshaller = new UnmarshallerImpl(); 97 } 98 catch(JBossXBException e) 99 { 100 throw new JBossXBRuntimeException(e.getMessage(), e); 101 } 102 103 JBossXBParser parser = unmarshaller.getParser(); 104 if(validation != null) 105 { 106 parser.setFeature(Unmarshaller.VALIDATION, validation.booleanValue()); 107 } 108 109 if(namespaces != null) 110 { 111 parser.setFeature(Unmarshaller.NAMESPACES, namespaces.booleanValue()); 112 } 113 114 if(features != null) 115 { 116 for(Iterator i = features.entrySet().iterator(); i.hasNext();) 117 { 118 Map.Entry entry = (Map.Entry )i.next(); 119 if(entry.getValue() != null) 120 { 121 Boolean value = (Boolean )entry.getValue(); 122 parser.setFeature((String )entry.getKey(), value.booleanValue()); 123 } 124 } 125 } 126 127 130 try 131 { 132 parser.setFeature(Unmarshaller.DYNAMIC_VALIDATION, true); 133 } 134 catch(JBossXBRuntimeException e) 135 { 136 } 138 139 return unmarshaller; 140 } 141 } 142 } 143 | Popular Tags |