1 /* Copyright 2004 The Apache Software Foundation 2 * 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 package org.apache.xmlbeans.samples.abstracttypes; 16 17 import abstractFigures; 18 import figures; 19 import org.apache.xmlbeans.XmlObject; 20 import org.apache.xmlbeans.XmlOptions; 21 22 /** 23 * Test class that builds a document using type substitution 24 */ 25 public class AbstractTypes 26 { 27 public static void main(String[] args) 28 { 29 buildDocument(true); 30 } 31 32 public static XmlObject buildDocument(boolean enableOutput) 33 { 34 XmlOptions opt = (new XmlOptions()).setSavePrettyPrint(); 35 36 // Build a new document 37 RootDocument doc = RootDocument.Factory.newInstance(); 38 RootDocument.Root figures = doc.addNewRoot(); 39 if (enableOutput) 40 System.out.println("Empty document:\n" + doc.xmlText(opt) + "\n"); 41 42 // Add abstract figures 43 Shape s1 = figures.addNewFigure(); 44 s1.setId("001"); 45 Shape s2 = figures.addNewFigure(); 46 s2.setId("002"); 47 // Document contains two shapes now 48 // Because the shape is abstract, the document will not yet be valid 49 if (enableOutput) 50 { 51 System.out.println("Document containing the abstract types:\n" + doc.xmlText(opt)); 52 System.out.println("Valid = " + doc.validate() + "\n"); 53 } 54 55 // Change the abstract figures to concrete ones 56 Circle circle = (Circle) s1.changeType(Circle.type); 57 circle.setRadius(10.0); 58 Square square = (Square) s2.changeType(Square.type); 59 square.setSide(20.0); 60 // Document contains two concrete shapes and is valid 61 if (enableOutput) 62 { 63 System.out.println("Final document:\n" + doc.xmlText(opt)); 64 System.out.println("Vald = " + doc.validate()); 65 } 66 67 return doc; 68 } 69 }