[MOBY-guts] biomoby commit

Paul Gordon gordonp at dev.open-bio.org
Wed Mar 28 18:53:11 UTC 2007


gordonp
Wed Mar 28 14:53:11 EDT 2007
Update of /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data
In directory dev.open-bio.org:/tmp/cvs-serv7972/src/main/org/biomoby/shared/data

Modified Files:
	MobyDataUtils.java 
Log Message:
Added more methods, providing greater I/O flexibility for serialization/deserialization
moby-live/Java/src/main/org/biomoby/shared/data MobyDataUtils.java,1.4,1.5
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataUtils.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataUtils.java	2006/10/26 00:32:20	1.4
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataUtils.java	2007/03/28 18:53:11	1.5
@@ -2,6 +2,7 @@
 
 import org.biomoby.shared.parser.MobyTags;
 import org.biomoby.shared.*;
+import org.xml.sax.InputSource;
 import org.w3c.dom.*;
 import java.io.*;
 
@@ -10,7 +11,21 @@
  * such as serialization and deserialization.
  */
 public class MobyDataUtils{
+
+    /**
+     * Writes the XML version of the contents, with an XML declaration.
+     */
     public static boolean toXMLDocument(OutputStream os, MobyContentInstance mci) throws Exception{
+	return toXMLDocument(os, mci, true);
+    }
+
+    /**
+     * Output the XML representation of the data to an OutputStream 
+     * (i.e. an output that takes byte data, not character-encoded strings).
+     * Note that the character decoding will depend on your runtime environment.
+     */
+    public static boolean toXMLDocument(OutputStream os, MobyContentInstance mci, boolean includeXMLDeclaration) 
+	throws Exception{
 	// May want to check character encoding before doing getBytes(), implement this later
 	os.write("<?xml version=\"1.0\"?>\n".getBytes());
 	os.write(("<moby:MOBY xmlns:moby=\"" + MobyPrefixResolver.MOBY_XML_NAMESPACE + "\">\n").getBytes());
@@ -18,9 +33,40 @@
 	os.write("\n</moby:MOBY>".getBytes());
 	return true;
     }
-    
-    public static MobyContentInstance fromXMLDocument(InputStream is) throws Exception{
+
+    /**
+     * Writes the XML version of the contents, with an XML declaration.
+     */
+    public static boolean toXMLDocument(Writer writer, MobyContentInstance mci) throws Exception{
+	return toXMLDocument(writer, mci, true);
+    }
+
+    /**
+     * Output the XML representation of the data to a Writer (i.e. an output that understands 
+     * character-encoded strings).
+     */
+    public static boolean toXMLDocument(Writer writer, MobyContentInstance mci, boolean includeXMLDeclaration) 
+	throws Exception{
 	
+	writer.write("<?xml version=\"1.0\"?>\n");
+	writer.write(("<moby:MOBY xmlns:moby=\"" + MobyPrefixResolver.MOBY_XML_NAMESPACE + "\">\n"));
+	writer.write(mci.toXML());  
+	writer.write("\n</moby:MOBY>");
+	return true;
+    }
+
+    /**
+     * Create a MOBY Java object representation from a MOBY XML payload stored in a String.
+     */
+    public static MobyContentInstance fromXMLDocument(String xmlData) throws Exception{
+	return fromXMLDocument(new StringReader(xmlData));
+    }
+
+    /**
+     * Create a MOBY Java object representation from a MOBY XML payload coming from an InputStream 
+     * (a byte stream that will becoverted by the parser into character data).
+     */
+    public static MobyContentInstance fromXMLDocument(InputStream is) throws Exception{
 	// Load an XML document
 	javax.xml.parsers.DocumentBuilder docBuilder = null;
 	try{
@@ -48,6 +94,72 @@
 	return fromXMLDocument(doc_root);
     }
 
+    /**
+     * Create a MOBY Java object representation from a MOBY XML payload at the given URL.
+     */
+    public static MobyContentInstance fromXMLDocument(java.net.URL url) throws Exception{
+	// Load an XML document
+	javax.xml.parsers.DocumentBuilder docBuilder = null;
+	try{
+	    javax.xml.parsers.DocumentBuilderFactory dbf = 
+		javax.xml.parsers.DocumentBuilderFactory.newInstance();
+	    dbf.setNamespaceAware(true);	
+	    docBuilder = dbf.newDocumentBuilder();
+	}
+	catch(javax.xml.parsers.ParserConfigurationException pce){
+	    throw new Exception("An XML parser could not be found or configured: " + pce);
+	}
+
+	Document domDoc = null;
+	try{
+	    domDoc = docBuilder.parse(url.toURI().toString());
+	} catch(org.xml.sax.SAXException saxe){
+	    throw new Exception("The XML data could not be parsed (not well-formed): " + saxe);
+	} catch(java.io.IOException ioe){
+	    throw new Exception("The XML data could not be loaded (I/O problem): " + ioe);
+	}
+
+	// Select a node from the document and see if any of the mappings
+	// work out, in which case we can check what services we can run
+	Element doc_root = domDoc.getDocumentElement();
+	return fromXMLDocument(doc_root);
+    }
+
+    /**
+     * Create a MOBY Java object representation from a MOBY XML payload coming from a Reader (character data).
+     */
+    public static MobyContentInstance fromXMLDocument(Reader reader) throws Exception{
+	// Load an XML document
+	javax.xml.parsers.DocumentBuilder docBuilder = null;
+	try{
+	    javax.xml.parsers.DocumentBuilderFactory dbf = 
+		javax.xml.parsers.DocumentBuilderFactory.newInstance();
+	    dbf.setNamespaceAware(true);	
+	    docBuilder = dbf.newDocumentBuilder();
+	}
+	catch(javax.xml.parsers.ParserConfigurationException pce){
+	    throw new Exception("An XML parser could not be found or configured: " + pce);
+	}
+
+	Document domDoc = null;
+	try{
+	    domDoc = docBuilder.parse(new InputSource(reader));
+	} catch(org.xml.sax.SAXException saxe){
+	    throw new Exception("The XML data could not be parsed (not well-formed): " + saxe);
+	} catch(java.io.IOException ioe){
+	    throw new Exception("The XML data could not be loaded (I/O problem): " + ioe);
+	}
+
+	// Select a node from the document and see if any of the mappings
+	// work out, in which case we can check what services we can run
+	Element doc_root = domDoc.getDocumentElement();
+	return fromXMLDocument(doc_root);
+    }
+
+
+    /**
+     * Create a MOBY Java object representation from a MOBY XML payload represented in a DOM.
+     */
     public static MobyContentInstance fromXMLDocument(Element doc_root) throws Exception{
 	if(doc_root == null){
 	    throw new MobyException("The passed in element was null");




More information about the MOBY-guts mailing list