ThresholdEnumsImpl.jcs Sample

This topic inludes the source code for the ThresholdEnumsImpl.jcs Sample.

Sample Location

This sample is located in the following directory in your WebLogic Workshop installation:

BEA_HOME/weblogic81/samples/workshop/SamplesApp/WebServices/xmlBeans/schema/

Sample Source Code


001 package xmlBeans.schema; 
002 
003 import com.bea.control.*;
004 import com.bea.xml.SchemaStringEnumEntry;
005 import com.bea.xml.SchemaType;
006 import com.bea.xml.SchemaTypeSystem;
007 import com.bea.xml.XmlAnySimpleType;
008 import java.util.HashMap;
009 import javax.xml.namespace.QName;
010 import org.openuri.easypo.priceSummary.PriceSummaryDocument;
011 
012 /**
013  * A Java control illustrating how you can use the schema type system to
014  * retrieve information about a compiled schema. This service simply 
015  * retrieves the values of an enumeration defined in schema.
016  <br/><br/>
017  * The schema type system API is a powerful way to get information 
018  * about compiled schema. It is, in a sense, an API about your schema
019  * API. The SchemaTypeSystem object represents compiled schemas within reach
020  * of your code -- for example, schemas in JAR files in your classpath.
021  <br/><br/>
022  * This code is in a Java control, and you can imagine how you might
023  * use such a control to provide a list of enumerations for a drop-down
024  * list in a JSP page. This control's client, however, is a web service.
025  * Test this code by running the ThresholdService.jws.
026  
027  * @jcs:jc-jar label="ThresholdEnums"
028  * @editor-info:code-gen control-interface="true"
029  */
030 public class ThresholdEnumsImpl implements ThresholdEnums, ControlSource
031 
032     /**
033      * Provides a list of the enumerations defined in the threshold
034      * attribute of the price-summary element. The schema in use here
035      * is PriceSummary.xsd, in the Schemas project of the SamplesApp
036      * application.
037      <br/><br/>
038      * This code is overly complex to show some of the things you can 
039      * do to access a schema type system. There's a simpler version in
040      * the getThresholdValuesSimple method.
041      
042      * @common:operation
043      */
044     public String[] getThresholdValues()
045     {
046         // Create a String array to contain the returned values.
047         String[] enumStrings = new String[0];
048 
049         /**
050          * Create a new instance of PriceSummaryDocument; this will be used to
051          * get information about the type system.
052          */ 
053         PriceSummaryDocument summaryDoc = PriceSummaryDocument.Factory.newInstance();
054 
055         /**
056          * Use the PriceSummaryDocument to get a SchemaTypeSystem object. This
057          * object provides access to all compiled schema types available to this 
058          * code.
059          */
060         SchemaTypeSystem typeSystem = summaryDoc.schemaType().getTypeSystem();
061 
062         /**
063          * Get a SchemaType for the price-summary element. This is necessary because
064          * the attribute containing the enumeration isn't available by name because it's
065          * anonymous. So it's necessary to go through the type that contains it.
066          */
067         SchemaType type = typeSystem.findType(new QName("http://openuri.org/easypo/price-summary"
068             "priceType"));
069             
070         /**
071          * Get all the anonymous types in price-summary, just to iterate through them.
072          */
073         SchemaType[] anonTypes = type.getAnonymousTypes();
074 
075         /**
076          * Loop through the anonymous types, examining their enumerations.
077          * Once one is found that belong to a threshold attribute, add the 
078          * String value for each of its entries to a String array.
079          */
080         for (int i = 0; i < anonTypes.length; i++)
081         {
082             /**
083              * Get an array of the enumeration entries in the current type.
084              */
085             SchemaStringEnumEntry[] enumEntries = anonTypes[i].getStringEnumEntries();
086 
087             /**
088              * Reassign the response String array with an array of the correct size,
089              * now that that size is known.
090              */
091             enumStrings = new String[enumEntries.length];
092 
093             /**
094              * Loop through the enumeration entries, adding to the response array those 
095              * whose container field is "threshold".
096              */            
097             for (int j = 0; j < enumEntries.length; j++)
098             {
099                 if (anonTypes[i].getContainerField().getName().getLocalPart().equals("threshold"))
100                 {
101                     enumStrings[j= enumEntries[j].getString();
102                 }
103             }
104         }
105         // Return the response array.
106         return enumStrings;
107     }
108 
109     /**
110      * Simpler code to provide a list of the enumerations defined in the threshold
111      * attribute of the price-summary element. The schema in use here
112      * is PriceSummary.xsd, in the Schemas project of the SamplesApp
113      * application.
114      
115      * @common:operation
116      */
117     public String[] getThresholdValuesSimple()
118     {
119         /**
120          * Create a SchemaType object that represents the threshold attribute.
121          */        
122         SchemaType enumType = org.openuri.easypo.priceSummary.PriceType.Threshold.type;
123         
124         /**
125          * Get the enumeration entries as an array.
126          */
127         SchemaStringEnumEntry[] enumEntries = enumType.getStringEnumEntries();
128 
129         // Create a String array to contain the returned values.
130         String[] enumStrings = new String[enumEntries.length];
131 
132         /**
133          * Loop through the enumeration entries, adding to the response array those 
134          * whose container field is "threshold".
135          */            
136         for (int j = 0; j < enumEntries.length; j++)
137         {
138             enumStrings[j= enumEntries[j].getString();
139         }
140 
141         // Return the response array.
142         return enumStrings;
143     }
144