// access general JAVA-COM bridge classes
import com.altova.automation.libs.*;

// access XMLSpy Java-COM bridge
import com.altova.automation.StyleVision.*;
import com.altova.automation.StyleVision.Enums.ENUMApplicationStatus;

/**
 * A simple example that starts XMLSpy COM server and performs a few operations on it.
 * Feel free to extend.
 */
public class RunStyleVision 
{
	public static void main(String[] args) 
	{
		// an instance of the application.
		Application stylevision = null;

		// instead of COM error handling use Java exception mechanism.
		try 
		{
			// Start StyleVision as COM server.
			stylevision = new Application();
			
			ENUMApplicationStatus status = ENUMApplicationStatus.eApplicationRunning;
			do{
				// check the application status
				status = stylevision.getStatus();
				System.out.println("status : " + status + "\n");
			} while (status != ENUMApplicationStatus.eApplicationRunning);

			// COM servers start up invisible so we make it visible
			stylevision.setVisible(true);
			
			// Locate samples installed with the product.
			int majorVersionYear = stylevision.getMajorVersion() + 1998;
			String strExamplesFolder = System.getenv("USERPROFILE") + "\\Documents\\Altova\\StyleVision" + Integer.toString(majorVersionYear) + "\\StyleVisionExamples\\";
			
			// We open two files form the product samples.
			stylevision.getDocuments().openDocument(strExamplesFolder + "OrgChart.pxf");
			stylevision.getDocuments().openDocument(strExamplesFolder + "BiggestCitiesPerContinent.sps");
			
			// We iterate through all open documents
			for (Document doc:stylevision.getDocuments())
			{
				System.out.println("Document name : " + doc.getName() + "\n");
				SchemaSources sources = doc.getSchemaSources();
							
				for (int i = 1; i <= sources.getCount(); i++)
				{
					SchemaSource source = sources.getItem(i);
					System.out.println("\tSchema      file name : " + source.getSchemaFileName() + "\n");
					System.out.println("\tWorking XML file name : " + source.getWorkingXMLFileName() + "\n");
					System.out.println("\tIs main schema source : " + source.getIsMainSchemaSource() + "\tType name : " + source.getTypeName() + "\n");
				}
			}
			// The following lines attach to the document events using a default implementation 
			// for the events and override one of its methods.
			// If you want to override all document events it is better to derive your listener class
			// from DocumentEvents and implement all methods of this interface.
			Document doc = stylevision.getActiveDocument();
			doc.addListener(new DocumentEventsDefaultHandler()
			{
				@Override
				public void onDocumentClosed(Document i_ipDoc) throws AutomationException
				{
					System.out.println("Document " + i_ipDoc.getName() + " requested closing.");
				}
			});
			doc.close();
			doc = null;
			
			System.out.println("Watch StyleVision!");
		} 
		catch (AutomationException e) 
		{
			e.printStackTrace();
		}
		finally
		{
			// Make sure that StyleVision can shut down properly.
			if (stylevision != null)
				stylevision.dispose();

			// Since the COM server was made visible and still is visible, it will keep running
			// and needs to be closed manually.
			System.out.println("Now close StyleVision!");
		}
	}
}