KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 Copyright (c) 2003-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.binding.def;
30
31 import org.jibx.runtime.JiBXException;
32
33 /**
34  * Reference to a mapping definition. This is used as a placeholder when
35  * building the component structure of a binding definition. It's necessary
36  * because the referenced mapping may not have been parsed yet. During the
37  * linkage phase that follows parsing this looks up the appropriate mapping
38  * definition and sets up the corresponding component structure. Thereafter it
39  * operates as a simple pass-through wrapper for the top child component.
40  *
41  * @author Dennis M. Sosnoski
42  * @version 1.0
43  */

44
45 public class MappingReference extends PassThroughComponent
46 {
47     /** Containing binding definition structure. */
48     private final IContainer m_container;
49     
50     /** Property definition. */
51     private final PropertyDefinition m_property;
52     
53     /** Fully qualified name of mapped type. */
54     private final String JavaDoc m_type;
55     
56     /** Context object. */
57     private final IContextObj m_contextObject;
58     
59     /** Name from reference (only allowed with abstract mappings) */
60     private final NameDefinition m_name;
61
62     /**
63      * Constructor from property and type.
64      *
65      * @param contain containing binding definition structure
66      * @param prop property definition
67      * @param type fully qualified name of mapped type
68      * @param objc current object context
69      * @param name reference name definition (only allowed with abstract
70      * mappings)
71      */

72
73     public MappingReference(IContainer contain, PropertyDefinition prop,
74         String JavaDoc type, IContextObj objc, NameDefinition name) {
75         super();
76         m_container = contain;
77         m_property = prop;
78         m_type = type;
79         m_contextObject = objc;
80         m_name = name;
81     }
82
83     /**
84      * Constructor from type.
85      *
86      * @param parent containing binding definition structure
87      * @param type fully qualified name of mapped type
88      */

89
90     public MappingReference(IContainer parent, String JavaDoc type) {
91         this(parent, null, type, null, null);
92     }
93     
94     //
95
// IComponent interface method definitions (overrides of defaults)
96

97     public String JavaDoc getType() {
98         return m_type;
99     }
100
101     public void setLinkages() throws JiBXException {
102         
103         // find the mapping being used
104
DefinitionContext defc = m_container.getDefinitionContext();
105         IMapping mdef = defc.getClassMapping(m_type);
106         IComponent wrap;
107         if (mdef == null) {
108             
109             // generate generic mapping to unknown type
110
if (m_name != null) {
111                 throw new JiBXException
112                     ("Name not allowed for generic mapping of type " + m_type);
113             }
114             wrap = new DirectGeneric(m_container, m_type, m_property);
115             
116         } else {
117             
118             // generate wrapped component for all calls
119
wrap = mdef.buildRef(m_container, m_contextObject, m_type,
120                 m_property);
121             if (m_name != null) {
122                 if (mdef.getName() == null) {
123                     wrap = new ElementWrapper(defc, m_name, wrap);
124                     if (m_property.isOptional()) {
125                         ((ElementWrapper)wrap).setOptionalNormal(true);
126                         ((ElementWrapper)wrap).setStructureObject(true);
127                         wrap = new OptionalStructureWrapper(wrap, m_property,
128                             true);
129                         m_property.setOptional(false);
130                     }
131                 } else {
132                     throw new JiBXException
133                         ("Name not allowed for reference to mapping of type " +
134                         m_type + ", which already defines a name");
135                 }
136             }
137         }
138         
139         // link actual component into structure
140
setWrappedComponent(wrap);
141         super.setLinkages();
142     }
143     
144     // DEBUG
145
public void print(int depth) {
146         BindingDefinition.indent(depth);
147         System.out.print("mapping reference to " + m_type);
148         if (m_property != null) {
149             System.out.print(" using " + m_property.toString());
150         }
151         System.out.println();
152     }
153 }
Popular Tags