📄 nestedcollection.java
字号:
/*Copyright (c) 2003-2005, Dennis M. SosnoskiAll rights reserved.Redistribution and use in source and binary forms, with or without modification,are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of JiBX nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" ANDANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIEDWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AREDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FORANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ONANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THISSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/package org.jibx.binding.def;import org.apache.bcel.generic.*;import org.jibx.binding.classes.*;import org.jibx.runtime.JiBXException;/** * Collection binding definition. This handles one or more child components, * which may be ordered or unordered. * * @author Dennis M. Sosnoski * @version 1.0 */public class NestedCollection extends NestedBase{ // // Method definitions used in code generation private static final String GROWARRAY_METHOD = "org.jibx.runtime.Utility.growArray"; private static final String GROWARRAY_SIGNATURE = "(Ljava/lang/Object;)Ljava/lang/Object;"; private static final String RESIZEARRAY_METHOD = "org.jibx.runtime.Utility.resizeArray"; private static final String RESIZEARRAY_SIGNATURE = "(ILjava/lang/Object;)Ljava/lang/Object;"; private static final String CHECK_ISSTART_NAME = "org.jibx.runtime.impl.UnmarshallingContext.isStart"; private static final String CHECK_ISSTART_SIGNATURE = "()Z"; private static final String SKIP_ELEMENT_NAME = "org.jibx.runtime.impl.UnmarshallingContext.skipElement"; private static final String SKIP_ELEMENT_SIGNATURE = "()V"; // // Actual instance data. /** Fully qualified class name of values from collection. */ private final String m_itemType; /** Strategy for generating code to load item from collection. */ private final CollectionLoad m_loadStrategy; /** Strategy for generating code to store item to collection. */ private final CollectionStore m_storeStrategy; /** Optional component flag. */ private final boolean m_isOptional; /** * Constructor. * * @param parent containing binding definition context * @param objc current object context * @param ord ordered content flag * @param opt optional component flag * @param flex flexible element handling flag * @param type fully qualified class name of values from collection (may be * <code>null</code>, if child content present) * @param load collection load code generation strategy * @param store collection store code generation strategy */ public NestedCollection(IContainer parent, IContextObj objc, boolean ord, boolean opt, boolean flex, String type, CollectionLoad load, CollectionStore store) { super(parent, objc, ord, flex, false); m_itemType = type; m_loadStrategy = load; m_storeStrategy = store; m_isOptional = opt; } /** * Get the collection item type. * * @return item type */ public String getItemType() { return m_itemType; } // // IComponent interface method definitions public boolean hasAttribute() { return false; } public void genAttrPresentTest(ContextMethodBuilder mb) { throw new IllegalStateException ("Internal error - no attributes present"); } public void genAttributeUnmarshal(ContextMethodBuilder mb) { throw new IllegalStateException ("Internal error - no attributes present"); } public void genAttributeMarshal(ContextMethodBuilder mb) { throw new IllegalStateException ("Internal error - no attributes present"); } public boolean hasContent() { return m_contents.size() > 0; } public void genContentUnmarshal(ContextMethodBuilder mb) throws JiBXException { if (m_contents.size() > 0) { // set up common handling and check for ordered or unordered content m_storeStrategy.genStoreInit(mb); BranchWrapper link = null; int count = m_contents.size(); if (m_isOrdered) { // just generate unmarshal code for each component in order for (int i = 0; i < count; i++) { // start with branch target for loop and link from last type if (link != null) { mb.initStackState(link); } BranchTarget start = mb.appendTargetNOP(); if (link != null) { link.setTarget(start, mb); } // generate code to check if an element matching this // component type is present IComponent child = (IComponent)m_contents.get(i); child.genContentPresentTest(mb); link = mb.appendIFEQ(this); // follow with code to unmarshal the component and store to // collection, ending with loop back to start of this // component child.genContentUnmarshal(mb); if (m_itemType != null && !ClassItem.isPrimitive(m_itemType)) { mb.appendCreateCast(m_itemType); } m_storeStrategy.genStoreItem(mb); mb.appendUnconditionalBranch(this).setTarget(start, mb); } } else { // generate unmarshal loop code that checks for each component, // branching to the next component until one is found and // exiting the loop only when no component is matched BranchTarget first = mb.appendTargetNOP(); for (int i = 0; i < count; i++) { if (link != null) { mb.targetNext(link); } IComponent child = (IComponent)m_contents.get(i); child.genContentPresentTest(mb); link = mb.appendIFEQ(this); child.genContentUnmarshal(mb); if (m_itemType != null && !ClassItem.isPrimitive(m_itemType)) { mb.appendCreateCast(m_itemType); } m_storeStrategy.genStoreItem(mb); mb.appendUnconditionalBranch(this).setTarget(first, mb); } // handle fall through condition depending on flexible flag if (m_isFlexible) { // exit loop if not positioned at element start mb.targetNext(link); mb.loadContext(); mb.appendCallVirtual(CHECK_ISSTART_NAME, CHECK_ISSTART_SIGNATURE); link = mb.appendIFEQ(this); // ignore unknown element and loop back to start mb.loadContext(); mb.appendCallVirtual(SKIP_ELEMENT_NAME, SKIP_ELEMENT_SIGNATURE); mb.appendUnconditionalBranch(this).setTarget(first, mb); } } // patch final test failure branch to fall through loop mb.targetNext(link); m_storeStrategy.genStoreDone(mb); } else { throw new IllegalStateException ("Internal error - no content present"); } } public void genContentMarshal(ContextMethodBuilder mb) throws JiBXException { if (m_contents.size() > 0) { // set up common handling of unknown item and collection empty BranchWrapper[] ifempties; BranchWrapper link = null; m_loadStrategy.genLoadInit(mb); // check for ordered or unordered content int count = m_contents.size(); if (m_isOrdered) { // generate marshal code for each component type in order, with // an exception generated if the end of the possible component // list is reached with anything left in the collection ifempties = new BranchWrapper[count]; for (int i = 0; i < count; i++) { // start generated code with loading next value from // collection if (link != null) { mb.initStackState(link, 1); } BranchTarget start = mb.appendTargetNOP(); ifempties[i] = m_loadStrategy.genLoadItem(mb); mb.targetNext(link); // if multiple types are included in content, append code to // check if item type matches this component IComponent child = (IComponent)m_contents.get(i); String type = child.getType(); if (count > 1) { mb.appendDUP(); mb.appendInstanceOf(type); link = mb.appendIFEQ(this); } if ((!"java.lang.Object".equals(type) && !ClassItem.isPrimitive(type))) { mb.appendCreateCast(type); } // finish with code to marshal the component, looping back // to start of block for more of same type child.genContentMarshal(mb); mb.appendUnconditionalBranch(this).setTarget(start, mb); } } else { // generate marshal loop code that loads an item from the // collection and then checks to see if it matches a component // type, branching to the next component until a match is found // (or generating an exception on no match) BranchTarget start = mb.appendTargetNOP(); ifempties = new BranchWrapper[1]; ifempties[0] = m_loadStrategy.genLoadItem(mb); for (int i = 0; i < count; i++) { // start by setting target for branch from last component mb.targetNext(link); // if multiple types are included in content, append code to // check if item type matches this component IComponent child = (IComponent)m_contents.get(i); String type = child.getType(); if (count > 1 || (!"java.lang.Object".equals(type) && !ClassItem.isPrimitive(type))) { mb.appendDUP(); mb.appendInstanceOf(type); link = mb.appendIFEQ(this); mb.appendCreateCast(type); } // finish with code to marshal the component, branching back // to start of loop child.genContentMarshal(mb); mb.appendUnconditionalBranch(this).setTarget(start, mb); } } // patch final test failure branch to generate an exception if (link != null) { // instruction sequence for exception is create new exception
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -