1 28 29 package example15; 30 31 import java.io.OutputStream ; 32 import java.io.Writer ; 33 34 import org.jibx.runtime.BindingDirectory; 35 import org.jibx.runtime.IBindingFactory; 36 import org.jibx.runtime.IMarshallable; 37 import org.jibx.runtime.IUnmarshallingContext; 38 import org.jibx.runtime.JiBXException; 39 import org.jibx.runtime.impl.MarshallingContext; 40 import org.jibx.runtime.impl.UnmarshallingContext; 41 42 public class BindingSelector 43 { 44 45 private final String m_attributeUri; 46 47 48 private final String m_attributeName; 49 50 51 private final String [] m_versionTexts; 52 53 54 private final String [] m_versionBindings; 55 56 57 private final UnmarshallingContext m_context; 58 59 60 private OutputStream m_outputStream; 61 62 63 private String m_outputEncoding; 64 65 66 private Writer m_outputWriter; 67 68 69 private int m_outputIndent; 70 71 79 80 public BindingSelector(String uri, String name, String [] versions, 81 String [] bindings) { 82 m_attributeUri = uri; 83 m_attributeName = name; 84 m_versionTexts = versions; 85 m_versionBindings = bindings; 86 m_context = new UnmarshallingContext(); 87 m_outputIndent = -1; 88 } 89 90 98 99 public IUnmarshallingContext getContext() { 100 return m_context; 101 } 102 103 109 110 void setOutput(OutputStream outs, String enc) { 111 m_outputStream = outs; 112 m_outputEncoding = enc; 113 } 114 115 120 121 void setOutput(Writer outw) { 122 m_outputWriter = outw; 123 } 124 125 131 132 void setIndent(int indent) { 133 m_outputIndent = indent; 134 } 135 136 143 144 public void marshalVersioned(Object obj, String version) 145 throws JiBXException { 146 147 String match = (version == null) ? m_versionTexts[0] : version; 149 for (int i = 0; i < m_versionTexts.length; i++) { 150 if (match.equals(m_versionTexts[i])) { 151 152 IBindingFactory fact = BindingDirectory. 154 getFactory(m_versionBindings[i], obj.getClass()); 155 MarshallingContext context = 156 (MarshallingContext)fact.createMarshallingContext(); 157 158 context.setIndent(m_outputIndent); 160 if (m_outputWriter == null) { 161 if (m_outputStream == null) { 162 throw new JiBXException("Output not configured"); 163 } else { 164 context.setOutput(m_outputStream, m_outputEncoding); 165 } 166 } else { 167 context.setOutput(m_outputWriter); 168 } 169 170 context.startDocument(m_outputEncoding, null); 172 ((IMarshallable)obj).marshal(context); 173 context.endDocument(); 174 return; 175 176 } 177 } 178 179 throw new JiBXException("Unrecognized document version " + version); 181 } 182 183 191 192 public Object unmarshalVersioned(Class clas) throws JiBXException { 193 194 m_context.toStart(); 196 String version = m_context.attributeText(m_attributeUri, 197 m_attributeName, m_versionTexts[0]); 198 199 for (int i = 0; i < m_versionTexts.length; i++) { 201 if (version.equals(m_versionTexts[i])) { 202 203 IBindingFactory fact = BindingDirectory. 205 getFactory(m_versionBindings[i], clas); 206 UnmarshallingContext context = 207 (UnmarshallingContext)fact.createUnmarshallingContext(); 208 209 context.setFromContext(m_context); 211 return context.unmarshalElement(); 212 213 } 214 } 215 216 throw new JiBXException("Unrecognized document version " + version); 218 } 219 } | Popular Tags |