KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jibx > binding > def > DirectProperty


1 /*
2 Copyright (c) 2003-2004, 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.binding.def;
30
31 import org.jibx.binding.classes.*;
32 import org.jibx.runtime.JiBXException;
33
34 /**
35  * Property reference with marshaller and unmarshaller. This handles loading
36  * and storing the property value, calling the supplied marshaller and
37  * unmarshaller for all else.
38  *
39  * @author Dennis M. Sosnoski
40  * @version 1.0
41  */

42
43 public class DirectProperty implements IComponent
44 {
45     /** Property definition. */
46     private final PropertyDefinition m_property;
47     
48     /** Property value direct binding. */
49     private final DirectObject m_direct;
50
51     /**
52      * Constructor.
53      *
54      * @param prop property definition
55      * @param direct object direct binding information
56      */

57
58     public DirectProperty(PropertyDefinition prop, DirectObject direct) {
59         m_property = prop;
60         m_direct = direct;
61     }
62     
63     //
64
// IComponent interface method definitions
65

66     public boolean isOptional() {
67         return m_property.isOptional();
68     }
69
70     public boolean hasAttribute() {
71         return false;
72     }
73
74     public void genAttrPresentTest(ContextMethodBuilder mb)
75         throws JiBXException {
76         throw new IllegalStateException JavaDoc
77             ("Internal error - no attributes allowed");
78     }
79
80     public void genAttributeUnmarshal(ContextMethodBuilder mb)
81         throws JiBXException {
82         throw new IllegalStateException JavaDoc
83             ("Internal error - no attributes allowed");
84     }
85
86     public void genAttributeMarshal(ContextMethodBuilder mb)
87         throws JiBXException {
88         throw new IllegalStateException JavaDoc
89             ("Internal error - no attributes allowed");
90     }
91
92     public boolean hasContent() {
93         return true;
94     }
95
96     public void genContentPresentTest(ContextMethodBuilder mb)
97         throws JiBXException {
98         m_direct.genContentPresentTest(mb);
99     }
100
101     public void genContentUnmarshal(ContextMethodBuilder mb)
102         throws JiBXException {
103         
104         // check type of property binding
105
if (m_property.isImplicit()) {
106             
107             // load null to force create of new object for item in collection
108
mb.appendACONST_NULL();
109             m_direct.genContentUnmarshal(mb);
110             
111         } else if (m_property.isThis()) {
112             
113             // load existing object to unmarshal for "this" reference
114
mb.loadObject();
115             m_direct.genContentUnmarshal(mb);
116             
117         } else {
118             
119         
120             // start by generating code to load owning object so can finish by
121
// storing to property
122
mb.loadObject();
123             BranchWrapper ifpres = null;
124             BranchWrapper tosave = null;
125             if (m_property.isOptional()) {
126             
127                 // generate code to check presence for the case of an optional
128
// item, with branch if so; if not present, set a null value
129
// with branch to be targeted at property store.
130
m_direct.genContentPresentTest(mb);
131                 ifpres = mb.appendIFNE(this);
132                 mb.appendACONST_NULL();
133                 tosave = mb.appendUnconditionalBranch(this);
134             }
135             
136             // generate unmarshalling code for not optional, or optional and
137
// present; get existing instance or create a new one and handle
138
// attribute unmarshalling
139
mb.targetNext(ifpres);
140             mb.loadObject();
141             m_property.genLoad(mb);
142             m_direct.genContentUnmarshal(mb);
143             mb.appendCreateCast(m_property.getSetValueType());
144             mb.targetNext(tosave);
145             m_property.genStore(mb);
146         }
147     }
148
149     public void genContentMarshal(ContextMethodBuilder mb)
150         throws JiBXException {
151     
152         // check type of property binding
153
if (m_property.isImplicit()) {
154         
155             // marshal object already loaded on stack
156
m_direct.genContentMarshal(mb);
157             
158         } else if (m_property.isThis()) {
159             
160             // load object to marshal for "this" reference
161
mb.loadObject();
162             m_direct.genContentMarshal(mb);
163             
164         } else {
165         
166             // start by generating code to load the actual object reference
167
mb.loadObject();
168             m_property.genLoad(mb);
169             BranchWrapper missing = null;
170             if (m_property.isOptional()) {
171             
172                 // generate code to check null for the case of an optional item,
173
// with branch if so; if present, just reload object and fall
174
// through.
175
missing = mb.appendIFNULL(this);
176                 mb.loadObject();
177                 m_property.genLoad(mb);
178             }
179         
180             // generate code for actual marshalling if not optional, or optional
181
// and nonnull; then finish by setting target for optional with
182
// null value case
183
m_direct.genContentMarshal(mb);
184             mb.targetNext(missing);
185         }
186     }
187     
188     public void genNewInstance(ContextMethodBuilder mb) {
189         throw new IllegalStateException JavaDoc
190             ("Internal error - no instance creation");
191     }
192     
193     public String JavaDoc getType() {
194         return m_property.getTypeName();
195     }
196
197     public boolean hasId() {
198         return false;
199     }
200
201     public void genLoadId(ContextMethodBuilder mb) throws JiBXException {
202         throw new IllegalStateException JavaDoc("Internal error - no id defined");
203     }
204
205     public boolean checkContentSequence(boolean text) throws JiBXException {
206         return !m_property.isOptional();
207     }
208
209     public void setLinkages() throws JiBXException {
210         m_direct.setLinkages();
211     }
212     
213     // DEBUG
214
public void print(int depth) {
215         BindingDefinition.indent(depth);
216         if (m_property == null) {
217             System.out.println("direct implicit property");
218         } else {
219             System.out.println("direct property using " +
220                 m_property.toString());
221         }
222         m_direct.print(depth+1);
223     }
224 }
225
Popular Tags