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 -->
<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>
</ivy-module>

View File

@ -1,12 +1,13 @@
package org.broadinstitute.sting.playground.samples;
import org.exolab.castor.xml.*;
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.IOException;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.File;
/**
* Created by IntelliJ IDEA.
@ -15,11 +16,18 @@ import java.io.FileNotFoundException;
* Time: 2:16:43 PM
* To change this template use File | Settings | File Templates.
*/
@Root // this is a root output object for simpleXML
public class SampleXmlMarshaller {
public enum RMDType { HapMap };
@Element // simpleXML tag to say make it an element, you could also specifiy Attribute
private RMDType type;
@Element
private String fileVersion;
@Element
private String fileName;
public SampleXmlMarshaller() {
@ -59,55 +67,59 @@ public class SampleXmlMarshaller {
return String.format("Type = %s, Name = %s, Version = %s%n", type, fileName, fileVersion );
}
public static void main( String argv[] ) {
if( argv.length == 0 || (!argv[0].equalsIgnoreCase("marshal") && !argv[0].equalsIgnoreCase("unmarshal")) ) {
System.out.println( "USAGE: java org.broadinstitute.sting.playground.samples.SampleXmlMarshaller {marshal | unmarshal}");
System.exit(-1);
public boolean equals(SampleXmlMarshaller other) {
if (other == null) { return false; }
if (other.getType() == this.getType() &&
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();
if( argv[0].equalsIgnoreCase("marshal") )
marshal();
else if( argv[0].equalsIgnoreCase("unmarshal") )
unmarshal();
marshal(startWith, writeTo);
endWith = unmarshal(writeTo);
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() {
SampleXmlMarshaller descriptor = new SampleXmlMarshaller( RMDType.HapMap, "foo.hapmap", "1.0" );
System.out.printf( "Marshalling descriptor = %s%n", descriptor );
public static void marshal(SampleXmlMarshaller sample, String filename) {
Serializer serializer = new Persister();
File out = new File(filename);
try {
FileWriter writer = new FileWriter( "foo.xml" );
Marshaller.marshal( descriptor, writer );
}
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);
serializer.write(sample, out);
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
public static void unmarshal() {
SampleXmlMarshaller descriptor = null;
public static SampleXmlMarshaller unmarshal(String filename) {
Serializer serializer = new Persister();
File source = new File(filename);
try {
FileReader reader = new FileReader( "foo.xml" );
descriptor = (SampleXmlMarshaller)Unmarshaller.unmarshal( SampleXmlMarshaller.class, reader );
SampleXmlMarshaller example = serializer.read(SampleXmlMarshaller.class, source);
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 );
}
}