KickJava   Java API By Example, From Geeks To Geeks.

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


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
32 import org.jibx.binding.classes.*;
33 import org.jibx.runtime.JiBXException;
34
35 /**
36  * Component decorator for optional structure with associated property. This
37  * just handles necessary glue code generation for the marshalling operations,
38  * where the presence of the structure needs to be tested before actually
39  * handling tag generation.
40  *
41  * @author Dennis M. Sosnoski
42  * @version 1.0
43  */

44
45 public class OptionalStructureWrapper extends PassThroughComponent
46 {
47     private static final String JavaDoc MARSHALCONTEXT_CLASS =
48         "org.jibx.runtime.impl.MarshallingContext";
49         
50     /** Property definition. */
51     private final PropertyDefinition m_property;
52
53     /** Load object for marshalling code generation flag. */
54     private final boolean m_loadMarshal;
55     
56     /**
57      * Constructor.
58      *
59      * @param wrap wrapped binding component
60      * @param load flag for need to load object for marshalling code
61      */

62
63     public OptionalStructureWrapper(IComponent wrap, PropertyDefinition prop,
64         boolean load) {
65         super(wrap);
66         m_property = prop;
67         m_loadMarshal = load;
68     }
69
70     //
71
// IComponent interface method definitions
72

73     public void genAttributeMarshal(ContextMethodBuilder mb)
74         throws JiBXException {
75         mb.loadObject();
76         BranchWrapper ifmiss = m_property.genTest(mb);
77         super.genAttributeMarshal(mb);
78         mb.targetNext(ifmiss);
79     }
80
81     public void genContentMarshal(ContextMethodBuilder mb)
82         throws JiBXException {
83         mb.loadObject();
84         BranchWrapper ifmiss = m_property.genTest(mb);
85         if (m_loadMarshal) {
86             mb.loadObject();
87             m_property.genLoad(mb);
88         }
89         super.genContentMarshal(mb);
90         mb.targetNext(ifmiss);
91     }
92     
93     // DEBUG
94
public void print(int depth) {
95         BindingDefinition.indent(depth);
96         System.out.print("optional structure wrapper " + m_property.toString());
97         if (m_loadMarshal) {
98             System.out.print(" (load marshal)");
99         }
100         System.out.println();
101         m_component.print(depth+1);
102     }
103 }
Popular Tags