KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jibx > extras > IdRefMapperBase


1 /*
2 Copyright (c) 2005, Dennis M. Sosnoski
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8  * Redistributions of source code must retain the above copyright notice, this
9    list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice,
11    this list of conditions and the following disclaimer in the documentation
12    and/or other materials provided with the distribution.
13  * Neither the name of JiBX nor the names of its contributors may be used
14    to endorse or promote products derived from this software without specific
15    prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */

28
29 package org.jibx.extras;
30
31 import org.jibx.runtime.IAliasable;
32 import org.jibx.runtime.IMarshaller;
33 import org.jibx.runtime.IMarshallingContext;
34 import org.jibx.runtime.IUnmarshaller;
35 import org.jibx.runtime.IUnmarshallingContext;
36 import org.jibx.runtime.JiBXException;
37 import org.jibx.runtime.impl.MarshallingContext;
38 import org.jibx.runtime.impl.UnmarshallingContext;
39
40 /**
41  * <p>Abstract base custom marshaller/unmarshaller for an object reference. This
42  * marshals the reference as an empty element with a single IDREF attribute, and
43  * unmarshals an element with the same structure to create a reference to the
44  * object with that ID value. To use this class you need to create a subclass
45  * with a constructor using the same signature as the one provided (calling the
46  * base class constructor from your subclass constructor) and implement the
47  * abstract {@link #getIdValue} method in your subclass. You can also override
48  * the provided {@link #getAttributeName} method to change the name used for the
49  * IDREF attribute. Note that this class can only be used when the definitions
50  * precede the references in the XML document; if a referenced ID is not defined
51  * the unmarshaller throws an exception.</p>
52  *
53  * @author Dennis M. Sosnoski
54  * @version 1.0
55  */

56
57 public abstract class IdRefMapperBase
58     implements IMarshaller, IUnmarshaller, IAliasable {
59     
60     private String JavaDoc m_uri;
61     private int m_index;
62     private String JavaDoc m_name;
63     
64     /**
65      * Aliased constructor taking a name definition for the element. The
66      * subclass version will be used by JiBX to define the element name to be
67      * used with this custom marshaller/unmarshaller.
68      *
69      * @param uri namespace URI for the top-level element
70      * @param index namespace index corresponding to the defined URI within the
71      * marshalling context definitions
72      * @param name local name for the top-level element
73      */

74     public IdRefMapperBase(String JavaDoc uri, int index, String JavaDoc name) {
75         m_uri = uri;
76         m_index = index;
77         m_name = name;
78     }
79     
80     /**
81      * Get the ID value from object being marshalled.
82      *
83      * @return ID value
84      */

85     protected abstract String JavaDoc getIdValue(Object JavaDoc item);
86     
87     /**
88      * Method which can be overridden to supply a different name for the ID
89      * reference attribute. The attribute name used by default is just "ref".
90      */

91     protected String JavaDoc getAttributeName() {
92         return "ref";
93     }
94     
95     /* (non-Javadoc)
96      * @see org.jibx.runtime.IMarshaller#isExtension(int)
97      */

98     public boolean isExtension(int index) {
99         return false;
100     }
101
102     /* (non-Javadoc)
103      * @see org.jibx.runtime.IMarshaller#marshal(java.lang.Object,
104      * org.jibx.runtime.IMarshallingContext)
105      */

106     public void marshal(Object JavaDoc obj, IMarshallingContext ictx)
107         throws JiBXException {
108         
109         // make sure the parameters are as expected
110
if (obj == null) {
111             return;
112         } else if (!(ictx instanceof MarshallingContext)) {
113             throw new JiBXException("Invalid context type for marshaller");
114         } else {
115             
116             // generate the element start tag
117
MarshallingContext ctx = (MarshallingContext)ictx;
118             ctx.startTagAttributes(m_index, m_name);
119             
120             // add attribute reference to object ID
121
ctx.attribute(0, getAttributeName(), getIdValue(obj));
122             
123             // close start tag for empty element
124
ctx.closeStartEmpty();
125         }
126     }
127
128     /* (non-Javadoc)
129      * @see org.jibx.runtime.IUnmarshaller#isPresent(org.jibx.runtime.IUnmarshallingContext)
130      */

131     public boolean isPresent(IUnmarshallingContext ctx) throws JiBXException {
132         return ctx.isAt(m_uri, m_name);
133     }
134
135     /* (non-Javadoc)
136      * @see org.jibx.runtime.IUnmarshaller#unmarshal(java.lang.Object,
137      * org.jibx.runtime.IUnmarshallingContext)
138      */

139     public Object JavaDoc unmarshal(Object JavaDoc obj, IUnmarshallingContext ictx)
140         throws JiBXException {
141         
142         // make sure we're at the appropriate start tag
143
UnmarshallingContext ctx = (UnmarshallingContext)ictx;
144         if (!ctx.isAt(m_uri, m_name)) {
145             return null;
146         }
147         
148         // get object reference for ID
149
obj = ctx.attributeExistingIDREF(null, getAttributeName(), 0);
150         
151         // skip past the element
152
ctx.parsePastEndTag(m_uri, m_name);
153         return obj;
154     }
155 }
Popular Tags