/*
 * #%L
 * Tutorials for SCIFIO API
 * %%
 * Copyright (C) 2011 - 2013 Open Microscopy Environment:
 *   - Board of Regents of the University of Wisconsin-Madison
 *   - Glencoe Software, Inc.
 *   - University of Dundee
 * %%
 * To the extent possible under law, the SCIFIO developers have waived
 * all copyright and related or neighboring rights to this tutorial code.
 * 
 * See the CC0 1.0 Universal license for details:
 * http://creativecommons.org/publicdomain/zero/1.0/
 * #L%
 */

package io.scif.tutorials;

import io.scif.Format;
import io.scif.FormatException;
import io.scif.Metadata;
import io.scif.Parser;
import io.scif.SCIFIO;
import io.scif.xml.XMLTools;

import java.io.IOException;

import ome.xml.meta.OMEMetadata;

/**
 * Tutorial on obtaining OME-XML using SCIFIO.
 * 
 * @author Mark Hiner
 */
public class T1dOpeningOMEXML {

	public static void main(final String... args) throws FormatException,
		IOException
	{
		// Creation of OME-XML metadata in SCIFIO is accomplished via translation.
		// The OME-XML component is essentially a collection of translators, from
		// specific formats to OME-XML, which define how to extract the OME-XML
		// schema.
		// So, we will need to work with a sample image that has a defined
		// translator
		// to OME-XML. Luckily we already have a tutorial which creates a PNG image
		// for us:
		T1cSavingImagePlanes.main(new String[] {});

		// We'll need a context for discovering formats and translators
		final SCIFIO scifio = new SCIFIO();

		// Now we can reference that image via the path:
		final String outPath = "SCIFIOTutorial.png";

		// This is the Metadata object we will translate to OMEXML Metadata;
		Metadata meta = null;

		// NB: we could use the following line here:
		// Reader reader =
		// context.getService(SCIFIO.class).initializer().initializeReader(sampleImage);
		// which would provide us with an initialized reader. We could then obtain
		// its Metadata
		// for Translation:
		// meta = reader.getMetadata();
		// If we were going to continue to use that Reader for more operations this
		// would
		// be quite reasonable. But if we ONLY want the Metadata, it's really doing
		// more than
		// we need. We just need a Parser:

		final Format format = scifio.format().getFormat(outPath);
		final Parser parser = format.createParser();

		// You can see a more in-depth treatment of individual components in T2a and
		// T2b.
		// For now it's enough to know that Parsers parse metadata from a dataset:
		meta = parser.parse(outPath);

		// Now that we have our source Metadata, we will need OME-XML Metadata to
		// translate
		// to:

		final OMEMetadata omexml = new OMEMetadata();
		omexml.setContext(scifio.getContext());

		scifio.translator().translate(meta, omexml, true);

		// Now that we have our OME-XML we can print it:
		final String xml = omexml.getRoot().dumpXML();
		System.out.println(XMLTools.indentXML(xml, 3, true));
	}
}

@@ -1174,21 +1174,16 @@
  schema.
-%0A%09%09//
  So, we 
@@ -1254,21 +1254,16 @@
 anslator
-%0A%09%09//
  to OME-
@@ -1317,16 +1317,21 @@
 reates a
+%0A%09%09//
  PNG ima
@@ -1332,21 +1332,16 @@
 NG image
-%0A%09%09//
  for us:
@@ -1373,23 +1373,8 @@
 ain(
-new String%5B%5D %7B%7D
 );%0A%0A
@@ -1720,16 +1720,21 @@
 e here:%0A
+%09%09//%0A
 %09%09// Rea
@@ -1749,46 +1749,15 @@
 er =
-%0A%09%09// context.getService(SCIFIO.class)
+ scifio
 .ini
@@ -1798,16 +1798,21 @@
 Image);%0A
+%09%09//%0A
 %09%09// whi
@@ -1897,21 +1897,16 @@
 Metadata
-%0A%09%09//
  for Tra
@@ -1915,16 +1915,21 @@
 lation:%0A
+%09%09//%0A
 %09%09// met
@@ -1954,16 +1954,21 @@
 data();%0A
+%09%09//%0A
 %09%09// If 
@@ -2047,21 +2047,16 @@
 // would
-%0A%09%09//
  be quit
@@ -2118,19 +2118,19 @@
 ally
+%0A%09%09//
  doing
-%0A%09%09//
  mor
@@ -2135,21 +2135,16 @@
 ore than
-%0A%09%09//
  we need
@@ -2552,16 +2552,16 @@
 data to%0A
+
 %09%09// tra
@@ -2566,21 +2566,16 @@
 ranslate
-%0A%09%09//
  to:%0A%0A%09%09

clean up style and comments