KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > xb > binding > sunday > xop > XOPIncludeHandler


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.xb.binding.sunday.xop;
23
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.ObjectInputStream JavaDoc;
27 import javax.xml.namespace.NamespaceContext JavaDoc;
28 import javax.xml.namespace.QName JavaDoc;
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 JavaDoc;
36
37 /**
38  * Handler impl for xop:Include type.
39  *
40  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
41  * @version <tt>$Revision: 2148 $</tt>
42  */

43 public class XOPIncludeHandler
44    implements ParticleHandler
45 {
46    // type that can be XOP-optimized (should actually be the element)
47
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 JavaDoc startParticle(Object JavaDoc parent,
62                                QName JavaDoc elementName,
63                                ParticleBinding particle,
64                                Attributes JavaDoc attrs,
65                                NamespaceContext JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc)
97       {
98          try
99          {
100             ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc((InputStream JavaDoc)content);
101             content = ois.readObject();
102          }
103          catch(IOException JavaDoc e)
104          {
105             throw new JBossXBRuntimeException("Failed to deserialize object: " + e.getMessage());
106          }
107          catch(ClassNotFoundException JavaDoc 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 JavaDoc endParticle(Object JavaDoc o, QName JavaDoc elementName, ParticleBinding particle)
116    {
117       return o;
118    }
119
120    public void setParent(Object JavaDoc parent,
121                          Object JavaDoc o,
122                          QName JavaDoc 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