Change the sample XML marshalling code over to simple XML, and take out the castor lines in the ivy.xml

git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@633 348d0f76-0448-11de-a6fe-93d51630548a
This commit is contained in:
aaron 2009-05-08 00:08:25 +00:00
parent 5d9536d2b4
commit 21536df308
2 changed files with 55 additions and 45 deletions

View File

@ -12,7 +12,5 @@
<!-- The following are all castor dependencies. Remove when transitive dependencies are working --> <!-- The following are all castor dependencies. Remove when transitive dependencies are working -->
<dependency org="commons-logging" name="commons-logging" rev="latest.integration" /> <dependency org="commons-logging" name="commons-logging" rev="latest.integration" />
<dependency org="org.codehaus.castor" name="castor-core" rev="1.3" />
<dependency org="org.codehaus.castor" name="castor-xml" rev="1.3" />
</dependencies> </dependencies>
</ivy-module> </ivy-module>

View File

@ -1,12 +1,13 @@
package org.broadinstitute.sting.playground.samples; package org.broadinstitute.sting.playground.samples;
import org.exolab.castor.xml.*;
import org.apache.log4j.BasicConfigurator; import org.apache.log4j.BasicConfigurator;
import org.broadinstitute.sting.utils.StingException;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import java.io.FileWriter; import java.io.File;
import java.io.IOException;
import java.io.FileReader;
import java.io.FileNotFoundException;
/** /**
* Created by IntelliJ IDEA. * Created by IntelliJ IDEA.
@ -15,11 +16,18 @@ import java.io.FileNotFoundException;
* Time: 2:16:43 PM * Time: 2:16:43 PM
* To change this template use File | Settings | File Templates. * To change this template use File | Settings | File Templates.
*/ */
@Root // this is a root output object for simpleXML
public class SampleXmlMarshaller { public class SampleXmlMarshaller {
public enum RMDType { HapMap }; public enum RMDType { HapMap };
@Element // simpleXML tag to say make it an element, you could also specifiy Attribute
private RMDType type; private RMDType type;
@Element
private String fileVersion; private String fileVersion;
@Element
private String fileName; private String fileName;
public SampleXmlMarshaller() { public SampleXmlMarshaller() {
@ -59,55 +67,59 @@ public class SampleXmlMarshaller {
return String.format("Type = %s, Name = %s, Version = %s%n", type, fileName, fileVersion ); return String.format("Type = %s, Name = %s, Version = %s%n", type, fileName, fileVersion );
} }
public static void main( String argv[] ) { public boolean equals(SampleXmlMarshaller other) {
if( argv.length == 0 || (!argv[0].equalsIgnoreCase("marshal") && !argv[0].equalsIgnoreCase("unmarshal")) ) { if (other == null) { return false; }
System.out.println( "USAGE: java org.broadinstitute.sting.playground.samples.SampleXmlMarshaller {marshal | unmarshal}"); if (other.getType() == this.getType() &&
System.exit(-1); other.getFileVersion().equals(this.getFileVersion()) &&
other.getFileName().equals(this.getFileName())) {
return true;
} }
return false;
}
public static void main( String argv[] ) {
if (argv.length != 1) {
System.err.println("You must specify a filename for the XML output.");
System.err.println("\nUsage: SampleXmlMarshaller outputfile\n");
}
// our SampleXmlMarshallers
SampleXmlMarshaller startWith = new SampleXmlMarshaller(RMDType.HapMap, "testFile.xml", "0.1");
SampleXmlMarshaller endWith = null;
// where to read and write to
String writeTo = argv[0];
BasicConfigurator.configure(); BasicConfigurator.configure();
if( argv[0].equalsIgnoreCase("marshal") ) marshal(startWith, writeTo);
marshal(); endWith = unmarshal(writeTo);
else if( argv[0].equalsIgnoreCase("unmarshal") )
unmarshal(); if (startWith.equals(endWith)) {
System.out.println("they're equal, check " + writeTo.toString() + " for the output");
} else {
System.out.println("they're NOT equal, check " + writeTo.toString() + " for the output. Something must of gone wrong");
}
} }
public static void marshal() { public static void marshal(SampleXmlMarshaller sample, String filename) {
SampleXmlMarshaller descriptor = new SampleXmlMarshaller( RMDType.HapMap, "foo.hapmap", "1.0" ); Serializer serializer = new Persister();
System.out.printf( "Marshalling descriptor = %s%n", descriptor ); File out = new File(filename);
try { try {
FileWriter writer = new FileWriter( "foo.xml" ); serializer.write(sample, out);
Marshaller.marshal( descriptor, writer ); } catch (Exception e) {
} e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
catch( IOException ex ) {
System.out.println("Unable to open writer:" + ex);
}
catch( MarshalException ex ) {
System.out.println("Unable to marshal data:" + ex);
}
catch( ValidationException ex ) {
System.out.println("Unable to validate data: " + ex);
} }
} }
public static void unmarshal() { public static SampleXmlMarshaller unmarshal(String filename) {
SampleXmlMarshaller descriptor = null; Serializer serializer = new Persister();
File source = new File(filename);
try { try {
FileReader reader = new FileReader( "foo.xml" ); SampleXmlMarshaller example = serializer.read(SampleXmlMarshaller.class, source);
descriptor = (SampleXmlMarshaller)Unmarshaller.unmarshal( SampleXmlMarshaller.class, reader ); return example;
} catch (Exception e) {
throw new StingException("Failed to marshal the data to file " + filename,e);
} }
catch( FileNotFoundException ex ) {
System.out.println("Unable to open reader:" + ex);
}
catch( MarshalException ex ) {
System.out.println("Unable to unmarshal data:" + ex);
}
catch( ValidationException ex ) {
System.out.println("Unable to validate data: " + ex);
}
System.out.printf( "Unmarshalled descriptor = %s%n", descriptor );
} }
} }