JiBX 1.2, part 2nd: From XML Schema to Java code (II)

Source: Internet
Author: User
Tags definition final xmlns

Code generated for timecard

The ant build.xml file in the Hrxml directory defines an ant target that attempts to generate the codebase for timecard mode, including default builds and several custom examples (discussed later). The sample directory also contains a test program org.jibx.hrxml.Test. It will use the generated data model classes to group the sample documents, then regroup the document and compare the results with the original document. And the sample directory has a set of test documents from the Hr-xml release. The CodeGen target will run with the default value Codegen,compile will compile the generated code and test code, BIND will compile the JiBX bindings, and roundtrip will run the test program on the sample document. You can also use the full task to run all of these steps sequentially.

Most ways to generate code in a pattern will generate a separate class for each ComplexType definition and enumeration simpleType. CodeGen typically reduces the number of generated classes by examining references and inline definitions where possible, and ignoring definitions that are not used in schema definitions including and imports. In timecard mode, there are a total of 10 global (named) ComplexType and additional 23 local (anonymous) ComplexType, and 8 enumerated simpleType. The generated default data model will contain 15 top-level classes and 23 internal classes, less than those calculated based on the schema component. You will see later how you can use customizations to further simplify the data model if you do not need to use all the schema components.

<xs:choice> Treatment

Listing 8 shows how CodeGen handles the selection between two elements in the Timecardtype complexType definition. By default, CodeGen uses a selection variable to track the currently active selection. The set method for the values included in the selection will allow you to write to the new value currently selected, but you cannot change the selection directly (throw illegalstateexception if you try to do so). To change the current selection after setting, you first need to invoke a purge method (here is Clearreportedresourceselect ()), which resets the selection state.

Listing 8. Hr-xml timecard generated code sample

/** * Schema fragment (s) for this class: * <pre> * <xs:complextype xmlns:ns= "http://ns.hr-xml.org/2007-04-15" * xmlns:ns1= "http://www.w3.org/XML/1998/namespace" * xmlns:xs= "Http://www.w3.org/2001/XMLSchema" name= "Timecardtyp"  E "> * <xs:sequence> * <xs:element type=" Ns:entityidtype "name=" Id "minoccurs=" 0 "/> * <xs:element Name= "Reportedresource" > * <xs:complexType> * <xs:choice> * <xs:element type= "Ns:timecar Dpersontype "Name="/> * <xs:element name= "Resource" > * <!--Reference to inner class Resou    RCE--> * </xs:element> * </xs:choice> * </xs:complexType> * </xs:element> *
  ... * * public class Timecardtype {private Entityidtype ID;
  private int reportedresourceselect =-1;
  Private final int reported_resource_person_choice = 0;
  Private final int resource_choice = 1;
  Private Timecardpersontype Reportedresourceperson; Private Resource Resource; ... private void setreportedresourceselect (int choice) {if (Reportedresourceselect = = 1) {Reportedresourcesel
   ECT = choice; else if (reportedresourceselect!= choice) {throw new IllegalStateException ("Need to call Clearreportedreso
    Urceselect () before changing existing choice ");
   }/** * Clear the choice selection.
  */public void Clearreportedresourceselect () {reportedresourceselect =-1;
   }/** * Check If Reportedresourceperson is current selection for choice. * * @return <code>true</code> If selection, <code>false</code> if not/public boolean if
  Reportedresourceperson () {return reportedresourceselect = = Reported_resource_person_choice;
   }/** * Get the ' person ' element value.
  * * @return Value * * * Timecardpersontype Getreportedresourceperson () {return reportedresourceperson;
   }/** * Set the ' person ' element value. * * @pAram Reportedresourceperson */public void Setreportedresourceperson (Timecardpersontype Reportedresourceperson
    ) {Setreportedresourceselect (reported_resource_person_choice);
  This.reportedresourceperson = Reportedresourceperson;
   }/** * Check If Resource is current selection for choice. * * @return <code>true</code> If selection, <code>false</code> if not/public boolean if
  Resource () {return reportedresourceselect = = Resource_choice;
   }/** * Get the ' Resource ' element value.
  * * @return Value * * * Resource getresource () {return Resource;
   }/** * Set the ' Resource ' element value. * * @param resource */public void Setresource (resource Resource) {Setreportedresourceselect (Resource_choice)
    ;
  This.resource = resource; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.