1 22 package org.jboss.xb.binding.sunday.xop; 23 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.ObjectInputStream ; 27 import javax.xml.namespace.NamespaceContext ; 28 import javax.xml.namespace.QName ; 29 import org.jboss.xb.binding.Constants; 30 import org.jboss.xb.binding.JBossXBRuntimeException; 31 import org.jboss.xb.binding.sunday.unmarshalling.ElementBinding; 32 import org.jboss.xb.binding.sunday.unmarshalling.ParticleBinding; 33 import org.jboss.xb.binding.sunday.unmarshalling.ParticleHandler; 34 import org.jboss.xb.binding.sunday.unmarshalling.TypeBinding; 35 import org.xml.sax.Attributes ; 36 37 43 public class XOPIncludeHandler 44 implements ParticleHandler 45 { 46 private final TypeBinding type; 48 private XOPUnmarshaller xopUnmarshaller; 49 50 public XOPIncludeHandler(TypeBinding type) 51 { 52 this.type = type; 53 } 54 55 public XOPIncludeHandler(TypeBinding type, XOPUnmarshaller xopUnmarshaller) 56 { 57 this.type = type; 58 this.xopUnmarshaller = xopUnmarshaller; 59 } 60 61 public Object startParticle(Object parent, 62 QName elementName, 63 ParticleBinding particle, 64 Attributes attrs, 65 NamespaceContext nsCtx) 66 { 67 ElementBinding xopInclude = (ElementBinding)particle.getTerm(); 68 if(!Constants.QNAME_XOP_INCLUDE.equals(xopInclude.getQName())) 69 { 70 throw new JBossXBRuntimeException( 71 "Expected " + Constants.QNAME_XOP_INCLUDE + " but got " + xopInclude.getQName() 72 ); 73 } 74 75 XOPUnmarshaller xopUnmarshaller = this.xopUnmarshaller == null ? type.getXopUnmarshaller() : this.xopUnmarshaller; 76 if(xopUnmarshaller == null) 77 { 78 throw new JBossXBRuntimeException( 79 "Failed to process " + Constants.QNAME_XOP_INCLUDE + ": XOPUnmarshaller is not provided." 80 ); 81 } 82 83 String cid = attrs.getValue("href"); 84 if(cid == null) 85 { 86 throw new JBossXBRuntimeException(Constants.QNAME_XOP_INCLUDE + " doesn't contain required href attribute"); 87 } 88 89 XOPObject xopObject = xopUnmarshaller.getAttachmentAsDataHandler(cid); 90 Object content = xopObject.getContent(); 91 if(content == null) 92 { 93 throw new JBossXBRuntimeException("Content is not available for cid '" + cid + "'"); 94 } 95 96 if(content instanceof InputStream ) 97 { 98 try 99 { 100 ObjectInputStream ois = new ObjectInputStream ((InputStream )content); 101 content = ois.readObject(); 102 } 103 catch(IOException e) 104 { 105 throw new JBossXBRuntimeException("Failed to deserialize object: " + e.getMessage()); 106 } 107 catch(ClassNotFoundException e) 108 { 109 throw new JBossXBRuntimeException("Failed to load the class to deserialize object: " + e.getMessage()); 110 } 111 } 112 return content; 113 } 114 115 public Object endParticle(Object o, QName elementName, ParticleBinding particle) 116 { 117 return o; 118 } 119 120 public void setParent(Object parent, 121 Object o, 122 QName elementName, 123 ParticleBinding particle, 124 ParticleBinding parentParticle) 125 { 126 if(parent instanceof XOPElementHandler.XOPElement) 127 { 128 ((XOPElementHandler.XOPElement)parent).value = o; 129 } 130 else 131 { 132 throw new JBossXBRuntimeException("Expected XOPElement as a parent but got " + parent + " for element " + elementName); 133 } 134 } 135 } 136 | Popular Tags |