nestedstructure.java
来自「对xml很好的java处理引擎,编译中绑定xml」· Java 代码 · 共 440 行 · 第 1/2 页
JAVA
440 行
BranchWrapper[] toends; if (m_isChoice) { toends = new BranchWrapper[count+1]; } else { toends = new BranchWrapper[1]; } for (int i = 0; i < count; i++) { // start with basic test code if (link != null) { mb.targetNext(link); } IComponent child = (IComponent)m_contents.get(i); child.genContentPresentTest(mb); link = mb.appendIFEQ(this); // check for duplicate (if enforced) if (!m_allowDuplicates) { genFlagTest(true, i, "Duplicate element ", child.getWrapperName(), mb); } // set flag for element seen if (useflag || !(child.isOptional() && m_allowDuplicates)) { mb.appendLoadLocal(mb.getSlot(this)); mb.appendLoadConstant(i); mb.appendLoadConstant(1); mb.appendASTORE("boolean"); } // generate actual unmarshalling code child.genContentUnmarshal(mb); BranchWrapper next = mb.appendUnconditionalBranch(this); if (m_isChoice) { toends[i+1] = next; } else { next.setTarget(first, mb); } } // handle comparison fall through depending on flexible flag if (m_isFlexible) { if (link != null) { // exit loop if not positioned at element start mb.targetNext(link); mb.loadContext(); mb.appendCallVirtual(CHECK_ISSTART_NAME, CHECK_ISSTART_SIGNATURE); toends[0] = 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); } } else { // set final test failure branch to fall through loop toends[0] = link; } // patch all branches that exit loop mb.targetNext(toends); // handle required element present tests if (nreq > 0) { for (int i = 0; i < count; i++) { IComponent child = (IComponent)m_contents.get(i); if (!child.isOptional()) { genFlagTest(false, i, "Missing required element ", child.getWrapperName(), mb); } } } mb.freeSlot(this); } } else { throw new IllegalStateException ("Internal error - no content present"); } } /** * Helper method to generate test code for value in boolean array. If the * test fails, the generated code throws an exception with the appropriate * error message. * * @param cond flag setting resulting in exception * @param pos position of element in list of child components * @param msg basic error message when test fails * @param name * @param mb */ private void genFlagTest(boolean cond, int pos, String msg, NameDefinition name, ContextMethodBuilder mb) { // generate code to load array item value mb.appendLoadLocal(mb.getSlot(this)); mb.appendLoadConstant(pos); mb.appendALOAD("boolean"); // append branch for case where test is passed BranchWrapper ifgood; if (cond) { ifgood = mb.appendIFEQ(this); } else { ifgood = mb.appendIFNE(this); } // generate exception for test failed mb.loadContext(); mb.appendLoadConstant(msg); if (name == null) { mb.appendACONST_NULL(); mb.appendLoadConstant("(unknown name, position " + pos + " in binding structure)"); } else { name.genPushUriPair(mb); } mb.appendCallVirtual(THROW_EXCEPTION_NAME, THROW_EXCEPTION_SIGNATURE); // set target for success branch on test mb.targetNext(ifgood); } public void genContentMarshal(ContextMethodBuilder mb) throws JiBXException { if (m_contents.size() > 0) { for (int i = 0; i < m_contents.size(); i++) { IComponent content = (IComponent)m_contents.get(i); content.genContentMarshal(mb); } } else { throw new IllegalStateException ("Internal error - no content present"); } } public String getType() { if (m_hasObject) { return super.getType(); } else if (m_attributes != null && m_attributes.size() > 0) { return ((IComponent)m_attributes.get(0)).getType(); } else if (m_contents.size() > 0) { return ((IComponent)m_contents.get(0)).getType(); } else { throw new IllegalStateException("Internal error - " + "no type defined for structure"); } } public boolean hasId() { return m_idChild != null; } public void genLoadId(ContextMethodBuilder mb) throws JiBXException { if (m_idChild == null) { throw new IllegalStateException("No ID child defined"); } else { m_idChild.genLoadId(mb); } } public void setLinkages() throws JiBXException { if (!m_isLinked) { // set flag first in case of recursive reference m_isLinked = true; // process all child components to link and sort by type int i = 0; while (i < m_contents.size()) { IComponent comp = (IComponent)m_contents.get(i); comp.setLinkages(); if (comp.hasAttribute()) { if (m_attributes == null) { m_attributes = new ArrayList(); } m_attributes.add(comp); } if (!comp.hasContent()) { m_contents.remove(i); } else { i++; } } } } // DEBUG public void print(int depth) { BindingDefinition.indent(depth); System.out.print("structure " + (m_isChoice ? "choice" : (m_isOrdered ? "ordered" : "unordered"))); if (m_allowDuplicates) { System.out.print(", duplicates allowed"); } if (isFlexible()) { System.out.print(", flexible"); } if (m_idChild != null) { System.out.print(" (ID)"); } System.out.println(); for (int i = 0; i < m_contents.size(); i++) { IComponent comp = (IComponent)m_contents.get(i); comp.print(depth+1); } if (m_attributes != null) { for (int i = 0; i < m_attributes.size(); i++) { IComponent comp = (IComponent)m_attributes.get(i); comp.print(depth+1); } } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?