001 package xmlBeans.schema;
002
003 import org.openuri.bea.samples.workshop.xmlBeans.maritalStatus.EmployeeDocument.Employee;
004 import org.openuri.bea.samples.workshop.xmlBeans.maritalStatus.EmployeeDocument;
005 import org.openuri.bea.samples.workshop.xmlBeans.maritalStatus.MaritalStatusDocument.MaritalStatus;
006 import org.openuri.bea.samples.workshop.xmlBeans.maritalStatus.MaritalStatusDocument;
007 import com.bea.xml.XmlOptions;
008 import com.bea.xml.XmlError;
009 import java.io.File;
010 import java.util.ArrayList;
011
012 /**
013 * This web services illustrates how you can construct an XML document
014 * with XMLBeans and types compiled from schema. It also shows how you
015 * can validate the document in cases where your code may create a
016 * document that is invalid. <br/><br/>
017 *
018 * The schema for the document created here, EmployeeMarital.xsd,
019 * defines a snippet of data about an employee that specifies their
020 * marital status (eg, "Married", "Single", or "Domestic partner").
021 * The schema defines a "choice" structure in which only one of a
022 * set of elements is allowed. The set in this case includes
023 * partner-name and spouse-name elements. Only one of these would
024 * be applicable for a given employee, so only one should be allowed.
025 * However, to illustrate validation, the code here tries to add both. <br/><br/>
026 *
027 * Validation is done here with the XmlObject.validate method, which in
028 * this case takes an XmlOptions argument. The option specifies an ArrayList
029 * as a "listener" to use for collecting errors that occur while validating --
030 * but any object implementing the java.util.Collection interface will work
031 * for this purpose. During validation, the XMLBeans runtime components call
032 * the listener's add method as errors are discovered, adding XmlError instances
033 * that this code can use to record and report what happened with the validation.
034 *
035 * @common:target-namespace namespace="http://workshop.bea.com/SchemaChoice"
036 */
037 public class SchemaChoice implements com.bea.jws.WebService
038 {
039
040 /**
041 * This method creates a new employee XML document, inserting first and
042 * last name, marital status, and the name of a partner, if any; it also
043 * writes an error message to the WebLogic Server console window.
044 * The error demonstrates how your code can listen for errors while
045 * validating XML.<br/><br/>
046 *
047 * To test this method, start enter values into the four fields
048 * provided. The "maritalStatus" field must be "Married", "Single",
049 * or "Domestic partner". To "fix" this code, comment the call to
050 * the setPartnerName OR setSpouseName method.
051 *
052 * @common:operation
053 */
054 public EmployeeDocument updateEmployData(String firstName, String lastName,
055 String maritalStatus, String otherName) throws Exception
056 {
057 /**
058 * Create a new employee XML document.
059 */
060 EmployeeDocument empDoc = EmployeeDocument.Factory.newInstance();
061 Employee newEmp = empDoc.addNewEmployee();
062
063 // Set the first and last names.
064 newEmp.setFirstName(firstName);
065 newEmp.setLastName(lastName);
066
067 if (maritalStatus.equals("Married"))
068 {
069 newEmp.setMaritalStatus(MaritalStatus.MARRIED);
070 }
071 else if (maritalStatus.equals("Single"))
072 {
073 newEmp.setMaritalStatus(MaritalStatus.SINGLE);
074 }
075 else if (maritalStatus.equals("Domestic partner"))
076 {
077 newEmp.setMaritalStatus(MaritalStatus.DOMESTIC_PARTNER);
078 }
079 else
080 {
081 throw new Exception("Exception: Invalid marital status given.");
082 }
083
084 /**
085 * Create an XmlOptions instance to use with the validate
086 * method. Add to the instance an ArrayList to use as an error
087 * listener for recording errors as validation occurs. But any
088 * object implementing the Collection interface will do here.
089 */
090 XmlOptions validateOptions = new XmlOptions();
091 ArrayList errorList = new ArrayList();
092 validateOptions.put(XmlOptions.ERROR_LISTENER, errorList);
093
094 /**
095 * INVALID. This code sets both the partner-name and
096 * spouse-name elements, which is illegal. According to the
097 * schema, only one of these is allowed. Note that the
098 * action is allowed in code because it is possible that
099 * the XML may go through multiple invalid states before the
100 * code is finished assembling it. The validate method is called
101 * at the end to ensure that the document is valid.
102 */
103 newEmp.setPartnerName(otherName);
104 newEmp.setSpouseName(otherName);
105
106 /**
107 * Is the XML valid?
108 */
109 boolean isValid = newEmp.validate(validateOptions);
110
111 /**
112 * If the new XML is not valid, output the errors to
113 * the WLS console. Of course, you could also print the
114 * messages to a log. While this code returns the newly
115 * constructed XML whether valid or not, it might instead
116 * return an error if the XML was invalid.
117 */
118 if (!isValid)
119 {
120 for (int i = 0; i < errorList.size(); i++)
121 {
122 /**
123 * Retrieve XmlError instances from the ArrayList.
124 * These contain information about the context of
125 * the validation error.
126 */
127 XmlError error = (XmlError)errorList.get(i);
128
129 /**
130 * Print information contained in the XmlError instance.
131 */
132 System.out.println("\n");
133 System.out.println("Message: " + error.getMessage() + "\n");
134 System.out.println("Location of invalid XML: " +
135 error.getCursorLocation().xmlText() + "\n");
136 }
137 }
138 return empDoc;
139 }
140 }
|