public static XmlObject buildDocument(boolean enableOutput) {
XmlOptions opt = (new XmlOptions()).setSavePrettyPrint();
// Build a new document
RootDocument doc = RootDocument.Factory.newInstance();
RootDocument.Root figures = doc.addNewRoot();
if (enableOutput)
System.out.println("Empty document:\n" + doc.xmlText(opt) + "\n");
// Add abstract figures
Shape s1 = figures.addNewFigure();
s1.setId("001");
Shape s2 = figures.addNewFigure();
s2.setId("002");
// Document contains two shapes now
// Because the shape is abstract, the document will not yet be valid
if (enableOutput)
{
System.out.println("Document containing the abstract types:\n" + doc.xmlText(opt));
System.out.println("Valid = " + doc.validate() + "\n");
}
// Change the abstract figures to concrete ones
Circle circle = (Circle) s1.changeType(Circle.type);
circle.setRadius(10.0);
Square square = (Square) s2.changeType(Square.type);
square.setSide(20.0);
// Document contains two concrete shapes and is valid
if (enableOutput)
{
System.out.println("Final document:\n" + doc.xmlText(opt));
System.out.println("Vald = " + doc.validate());
}
return doc;
}
|