KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > net > jmx > adaptor > AttributeSer


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 // $Id: AttributeSer.java,v 1.6.6.1 2005/03/02 14:19:54 tdiesler Exp $
9

10 package org.jboss.net.jmx.adaptor;
11
12 import org.jboss.axis.Constants;
13 import org.jboss.axis.encoding.SerializationContext;
14 import org.jboss.axis.encoding.Serializer;
15 import org.jboss.axis.encoding.XMLType;
16 import org.jboss.axis.wsdl.fromJava.Types;
17 import org.w3c.dom.Element JavaDoc;
18 import org.xml.sax.Attributes JavaDoc;
19 import org.xml.sax.helpers.AttributesImpl JavaDoc;
20
21 import javax.management.Attribute JavaDoc;
22 import javax.xml.namespace.QName JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 /**
26  * An <code>AttributeSer</code> is be used to serialize
27  * Attribute objects using the <code>SOAP-ENC</code>
28  * encoding style.<p>
29  *
30  * @author <a HREF="mailto:a_taherkordi@users.sourceforge.net">Alireza Taherkordi</a>
31  * @author <a HREF="mailto:Christoph.Jung@infor.de">Christoph G. Jung</a>
32  * @since 26.04.02
33  * @version $Revision: 1.6.6.1 $
34  */

35
36 public class AttributeSer implements Serializer
37 {
38
39    /** this is the fully-qualified type that we serialize into */
40    protected QName JavaDoc xmlType;
41
42    //
43
// Constructors
44
//
45

46    public AttributeSer(Class JavaDoc javaType, QName JavaDoc xmlType)
47    {
48       this.xmlType = xmlType;
49    }
50
51    //
52
// Public API
53
//
54

55    /**
56     * turns a JMX objectname into a string-based xml element
57     * @param name the name of the element that carries our type
58     * @param attributes the attributes of the element that carries our type
59     * @param value the objectname to serialize
60     * @param context the serialization context we live into
61     */

62    public void serialize(QName JavaDoc name,
63                          Attributes JavaDoc attributes,
64                          Object JavaDoc value,
65                          SerializationContext context)
66            throws IOException JavaDoc
67    {
68
69       // do some initialisation of attributes
70
AttributesImpl JavaDoc attrs;
71       if (attributes != null)
72          attrs = new AttributesImpl JavaDoc(attributes);
73       else
74          attrs = new AttributesImpl JavaDoc();
75
76       // next we utter the attribute name as an attribute
77
QName JavaDoc qname = new QName JavaDoc("", "name");
78       attrs.addAttribute(qname.getNamespaceURI(),
79               qname.getLocalPart(),
80               context.qName2String(qname),
81               "CDATA",
82               ((Attribute JavaDoc)value).getName());
83
84       // start the attribute tag
85
context.startElement(name, attrs);
86
87       // next we utter an embedded value object of any-type with the
88
// attributes content
89
qname = new QName JavaDoc("", "value");
90       Object JavaDoc attrValue = ((Attribute JavaDoc)value).getValue();
91       // lets hope that Object is mapped semantically to xsd:any??
92
context.serialize(qname, null, attrValue);
93       // end the attribute tag
94
context.endElement();
95    }
96
97    /** we use sax approach */
98    public String JavaDoc getMechanismType()
99    {
100       return Constants.AXIS_SAX;
101    }
102
103    /**
104     * Return XML schema for the specified type.
105     * The Attribute type has a string-based name attribute and an
106     * case-typed value element. Note that this function has been
107     * adopted to the Axis 1.1 Beta Emitter code.
108     */

109
110    public Element JavaDoc writeSchema(Class JavaDoc forClass, Types types) throws Exception JavaDoc
111    {
112       // ComplexType representation of SimpleType bean class
113
Element JavaDoc complexType = types.createElement("complexType");
114       //types.writeSchemaElement(xmlType, complexType);
115
types.writeSchemaElement(xmlType, complexType);
116       complexType.setAttribute("name", xmlType.getLocalPart());
117
118       Element JavaDoc nameAttribute =
119               types.createAttributeElement("name",
120                       XMLType.XSD_STRING.getClass(),
121                       XMLType.XSD_STRING,
122                       false,
123                       complexType.getOwnerDocument());
124       complexType.appendChild(nameAttribute);
125       Element JavaDoc complexContent = types.createElement("complexContent");
126       complexType.appendChild(complexContent);
127       Element JavaDoc all = types.createElement("sequence");
128       complexContent.appendChild(all);
129       Element JavaDoc valueElement =
130               types.createElement("value",
131                       types.getNamespaces().getCreatePrefix(XMLType.XSD_ANYTYPE.getNamespaceURI())
132               + ":"
133               + XMLType.XSD_ANYTYPE.getLocalPart(),
134                       true,
135                       false,
136                       all.getOwnerDocument());
137       all.appendChild(valueElement);
138       return complexType;
139    }
140
141 }
142
Popular Tags