KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
32
33 import org.jibx.binding.classes.*;
34 import org.jibx.runtime.JiBXException;
35
36 /**
37  * Base class for structure and collection binding definitions. This handles one
38  * or more child components, which may be ordered or unordered.
39  *
40  * @author Dennis M. Sosnoski
41  * @version 1.0
42  */

43
44 public abstract class NestedBase extends BindingBuilder.ContainerBase
45 implements IComponent, IContainer
46 {
47     /** Context object for this definition. */
48     private final IContextObj m_contextObject;
49     
50     /** Flag for context defined at level. */
51     private final boolean m_hasContext;
52
53     /** Flag for ordered child content (used by subclasses). */
54     protected final boolean m_isOrdered;
55     
56     /** Definition context for container (may be same as parent). */
57     private final DefinitionContext m_defContext;
58
59     /** Included attribute definitions (lazy create, only if needed). */
60     protected ArrayList JavaDoc m_attributes;
61
62     /** Nested content definitions (initially used for all child components). */
63     protected ArrayList JavaDoc m_contents;
64
65     /**
66      * Constructor.
67      *
68      * @param contain containing binding definition context
69      * @param objc current object context
70      * @param ord ordered content flag
71      * @param ctx define context for structure flag
72      */

73
74     public NestedBase(IContainer contain, IContextObj objc,
75         boolean ord, boolean defc) {
76         
77         // set base class defaults
78
super(contain);
79         m_styleDefault = m_autoLink = m_accessLevel = m_nameStyle = -1;
80
81         // initialize members at this level
82
m_contextObject = objc;
83         m_contents = new ArrayList JavaDoc();
84         m_isOrdered = ord;
85         m_hasContext = defc;
86         if (defc) {
87             m_defContext = new DefinitionContext(contain);
88         } else {
89             m_defContext = contain.getDefinitionContext();
90         }
91     }
92     
93     /**
94      * Get the attribute children of this mapping.
95      *
96      * @return list of attribute children (<code>null</code> if none; should not
97      * be modified)
98      */

99
100     public ArrayList JavaDoc getAttributes() {
101         return m_attributes;
102     }
103     
104     /**
105      * Get the content children of this mapping.
106      *
107      * @return list of content children (should not be modified)
108      */

109
110     public ArrayList JavaDoc getContents() {
111         return m_contents;
112     }
113     
114     /**
115      * Add child component to nested structure. All components are initially
116      * assumed to contain content. When {@link #setLinkages} is called the
117      * components are checked to determine whether they actually supply
118      * attribute(s), content, or both.
119      *
120      * @param comp child component to be added to structure
121      */

122
123     public void addComponent(IComponent comp) {
124         m_contents.add(comp);
125     }
126     
127     //
128
// IContainer interface method definitions
129

130     public boolean isContentOrdered() {
131         return m_isOrdered;
132     }
133
134     public boolean hasNamespaces() {
135         return m_hasContext && m_defContext.hasNamespace();
136     }
137
138     public BindingDefinition getBindingRoot() {
139         return m_container.getBindingRoot();
140     }
141
142     public DefinitionContext getDefinitionContext() {
143         return m_defContext;
144     }
145     
146     //
147
// IComponent interface method definitions
148

149     public boolean isOptional() {
150         
151         // optional if and only if all child components are optional
152
if (m_attributes != null) {
153             for (int i = 0; i < m_attributes.size(); i++) {
154                 if (!((IComponent)m_attributes.get(i)).isOptional()) {
155                     return false;
156                 }
157             }
158         }
159         for (int i = 0; i < m_contents.size(); i++) {
160             if (!((IComponent)m_contents.get(i)).isOptional()) {
161                 return false;
162             }
163         }
164         return true;
165     }
166
167     public boolean hasContent() {
168         return m_contents.size() > 0;
169     }
170
171     public void genContentPresentTest(ContextMethodBuilder mb)
172         throws JiBXException {
173         if (m_contents.size() > 0) {
174             
175             // if single possiblity just test it directly
176
int count = m_contents.size();
177             if (count == 1) {
178                 ((IComponent)m_contents.get(0)).genContentPresentTest(mb);
179             } else {
180                 
181                 // generate code for chained test with branches to found exit
182
BranchWrapper[] tofound = new BranchWrapper[count];
183                 for (int i = 0; i < count; i++) {
184                     IComponent comp = (IComponent)m_contents.get(i);
185                     comp.genContentPresentTest(mb);
186                     tofound[i] = mb.appendIFNE(this);
187                 }
188                 
189                 // fall off end of loop to push "false" on stack and jump to end
190
mb.appendICONST_0();
191                 BranchWrapper toend = mb.appendUnconditionalBranch(this);
192                 
193                 // generate found target to push "true" on stack and continue
194
for (int i = 0; i < count; i++) {
195                     mb.targetNext(tofound[i]);
196                 }
197                 mb.appendICONST_1();
198                 mb.targetNext(toend);
199                 
200             }
201         } else {
202             throw new IllegalStateException JavaDoc
203                 ("Internal error - no content present");
204         }
205     }
206     
207     public void genNewInstance(ContextMethodBuilder mb) {
208         throw new IllegalStateException JavaDoc
209             ("Internal error - no instance creation");
210     }
211
212     public String JavaDoc getType() {
213         return m_contextObject.getBoundClass().getClassName();
214     }
215 }
Popular Tags