1 28 29 package org.jibx.extras; 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 52 53 public class BindingSelector 54 { 55 56 private final String m_attributeUri; 57 58 59 private final String m_attributeName; 60 61 62 private final String [] m_versionTexts; 63 64 65 private final String [] m_versionBindings; 66 67 68 private final UnmarshallingContext m_context; 69 70 71 private OutputStream m_outputStream; 72 73 74 private String m_outputEncoding; 75 76 77 private Writer m_outputWriter; 78 79 80 private int m_outputIndent; 81 82 90 91 public BindingSelector(String uri, String name, String [] versions, 92 String [] bindings) { 93 m_attributeUri = uri; 94 m_attributeName = name; 95 m_versionTexts = versions; 96 m_versionBindings = bindings; 97 m_context = new UnmarshallingContext(); 98 m_outputIndent = -1; 99 } 100 101 109 110 public IUnmarshallingContext getContext() { 111 return m_context; 112 } 113 114 120 121 public void setOutput(OutputStream outs, String enc) { 122 m_outputStream = outs; 123 m_outputEncoding = enc; 124 } 125 126 131 132 public void setOutput(Writer outw) { 133 m_outputWriter = outw; 134 } 135 136 142 143 public void setIndent(int indent) { 144 m_outputIndent = indent; 145 } 146 147 154 155 public void marshalVersioned(Object obj, String version) 156 throws JiBXException { 157 158 String match = (version == null) ? m_versionTexts[0] : version; 160 for (int i = 0; i < m_versionTexts.length; i++) { 161 if (match.equals(m_versionTexts[i])) { 162 163 IBindingFactory fact = BindingDirectory. 165 getFactory(m_versionBindings[i], obj.getClass()); 166 MarshallingContext context = 167 (MarshallingContext)fact.createMarshallingContext(); 168 169 context.setIndent(m_outputIndent); 171 if (m_outputWriter == null) { 172 if (m_outputStream == null) { 173 throw new JiBXException("Output not configured"); 174 } else { 175 context.setOutput(m_outputStream, m_outputEncoding); 176 } 177 } else { 178 context.setOutput(m_outputWriter); 179 } 180 181 context.startDocument(m_outputEncoding, null); 183 ((IMarshallable)obj).marshal(context); 184 context.endDocument(); 185 return; 186 187 } 188 } 189 190 throw new JiBXException("Unrecognized document version " + version); 192 } 193 194 202 203 public Object unmarshalVersioned(Class clas) throws JiBXException { 204 205 m_context.toStart(); 207 String version = m_context.attributeText(m_attributeUri, 208 m_attributeName, m_versionTexts[0]); 209 210 for (int i = 0; i < m_versionTexts.length; i++) { 212 if (version.equals(m_versionTexts[i])) { 213 214 IBindingFactory fact = BindingDirectory. 216 getFactory(m_versionBindings[i], clas); 217 UnmarshallingContext context = 218 (UnmarshallingContext)fact.createUnmarshallingContext(); 219 220 context.setFromContext(m_context); 222 return context.unmarshalElement(); 223 224 } 225 } 226 227 throw new JiBXException("Unrecognized document version " + version); 229 } 230 } | Popular Tags |