[MOBY-guts] biomoby commit
Eddie Kawas
kawas at pub.open-bio.org
Wed Feb 23 15:41:03 UTC 2005
kawas
Wed Feb 23 10:41:03 EST 2005
Update of /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/gui/util
In directory pub.open-bio.org:/tmp/cvs-serv28029/org/biomoby/client/gui/util
Modified Files:
MobyObjectPrimitiveExtractor.java
Log Message:
Corrected the use of Dom from JDOM to w3c DOM to make class compatible
with other jMoby classes.
Eddie
moby-live/Java/src/main/org/biomoby/client/gui/util MobyObjectPrimitiveExtractor.java,1.1,1.2
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/gui/util/MobyObjectPrimitiveExtractor.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/gui/util/MobyObjectPrimitiveExtractor.java 2005/02/16 15:38:28 1.1
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/gui/util/MobyObjectPrimitiveExtractor.java 2005/02/23 15:41:03 1.2
@@ -5,60 +5,91 @@
package org.biomoby.client.gui.util;
import java.io.BufferedReader;
-import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
-import java.util.List;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
-import org.jdom.Attribute;
-import org.jdom.Document;
-import org.jdom.Element;
-import org.jdom.JDOMException;
-import org.jdom.Namespace;
-import org.jdom.input.SAXBuilder;
+import org.biomoby.shared.MobyDataFloat;
+import org.biomoby.shared.MobyDataInt;
+import org.biomoby.shared.MobyDataSimpleInstance;
+import org.biomoby.shared.MobyDataString;
+import org.biomoby.shared.MobyDataType;
+import org.jdom.input.DOMBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
-import com.hp.hpl.jena.vocabulary.RDF;
-import com.hp.hpl.jena.vocabulary.RDFS;
-
/**
* @author Eddie Kawas
- * <p>Created for
* <p>This class was created to ...
* <p>For questions, comments, or bugs
* <p>email me at edward.kawas at gmail.com
*/
public class MobyObjectPrimitiveExtractor {
final private static String URL_OBJECT = new String(
- "http://mobycentral.cbr.nrc.ca:8090/authority/metadata?lsid=");
- final static Namespace HAS = Namespace.getNamespace("has","http://biomoby.org/RESOURCES/MOBY-S/Predicates#");
- final static Namespace HASA = Namespace.getNamespace("hasa","http://biomoby.org/RESOURCES/MOBY-S/Predicates#");
- final static Namespace ARTICLE = Namespace.getNamespace("articleName","http://biomoby.org/RESOURCES/MOBY-S/Predicates#");
- final static String[] PRIMITIVES = {"String", "Float", "Integer", "Object", "DateTime"};
+ "http://mobycentral.cbr.nrc.ca:8090/authority/metadata?lsid=");
+
+ final static String[] PRIMITIVES = {
+ "String",
+ "Float",
+ "Integer",
+ "Object",
+ "DateTime" };
+
+ final static String MOBY = "http://www.biomoby.org/moby";
+
+ /**
+ *
+ * <b>PRE:</b><p>
+ * <b>POST:</b><p>
+ * @param MobyElement - The Element containing the MOBY Object to find primitives for
+ * @return an array of MobyDataSimpleInstances that are the primitives for MobyElement.
+ */
+ public MobyDataSimpleInstance[] getPrimitives(Element MobyElement) {
+ return getPrimitivesHelper(MobyElement.getTagName());
+ }
+
/**
*
* <b>PRE:</b><p>
* <b>POST:</b><p>
- * @param name - name of the Moby Object to decompose
- * @return Triples[] - an array of Triples or null if an error occured
- * @throws ParserConfigurationException
- * @throws IOException
- * @throws SAXException
+ * @param Object - The object to find primitives for
+ * @return an array of MobyDataSimpleInstances that are the primitives for Object.
+ */
+ public MobyDataSimpleInstance[] getPrimitives(String object) {
+ return getPrimitivesHelper(object);
+ }
+
+ /*
+ * getPrimitives helper.
*/
- public ArrayList getPrimitives(String name) {
+ private MobyDataSimpleInstance[] getPrimitivesHelper(String name) {
ArrayList list = new ArrayList();
if (isPrimitive(name)) {
- list.add(new Triples(name, name, Triples.HASA));
- return list;
+ MobyDataSimpleInstance mdsi = new MobyDataSimpleInstance("");
+ mdsi.setDataType(new MobyDataType(name));
+ return new MobyDataSimpleInstance[] { mdsi };
}
String lsid = "urn:lsid:biomoby.org:objectclass:" + name;
String rdf = getObjectRDF(lsid);
@@ -69,121 +100,130 @@
// create a DOM document
Document doc = getDOMDocument(rdf);
// start parsing
- List rdfsClassList = doc.getRootElement().getContent();
- Iterator it = rdfsClassList.iterator();
- while (it.hasNext()) {
- Object obj = it.next();
- Element hasa = null, has = null;
- if (obj instanceof Element) {
- Element classElement = (Element) obj;
- // set up appropriate iterators
- Iterator hasIterator = classElement.getChildren("has", HAS).iterator();
- Iterator hasaIterator = classElement.getChildren("hasa",HASA).iterator();
- Iterator subClassIterator = classElement.getChildren("subClassOf", Namespace.getNamespace(RDFS.getURI())).iterator();
- // iterate through the lists
- while (hasIterator.hasNext()) {
- Element hasElement = (Element) hasIterator.next();
- List hasInnerElementList = hasElement.getChildren();
- if (!hasInnerElementList.isEmpty()) {
- Element mobyObject = (Element) hasInnerElementList.get(0);
- String primitive = mobyObject.getName();
- int type = Triples.HAS;
- String articleName = mobyObject.getChildText("articleName", ARTICLE);
- // check if primitive is a primitive
- if (isPrimitive(primitive)) {
- Triples t = new Triples(articleName, primitive, type);
- list.add(t);
- } else {
- // recurse
- ArrayList recurse = getPrimitives(primitive);
- if (recurse == null) {
- return null;
- }
- processRecursionList(list, recurse);
+ Element root = doc.getDocumentElement();
+ NodeList rdfsClassList = root.getElementsByTagName("rdfs:Class");
+ for (int x = 0; x < rdfsClassList.getLength(); x++) {
+ // process each class:
+ Element _class = ((Element) rdfsClassList.item(x));
+ NodeList has = _class.getElementsByTagName("mobyPred:has");
+ NodeList hasa = _class.getElementsByTagName("mobyPred:hasa");
+ // subClassOf can contain attributes or just an element
+ NodeList subClassOf = _class
+ .getElementsByTagName("rdfs:subClassOf");
+ for (int i = 0; i < has.getLength(); i++) {
+ NodeList children = ((Element) has.item(i)).getChildNodes();
+ for (int j = 0; j < children.getLength(); j++) {
+ if (!(children.item(j) instanceof Element))
+ continue;
+ Element child = (Element) children.item(j);
+ Node articleNode = child.getElementsByTagName(
+ "mobyPred:articleName").item(0);
+ String localName = child.getLocalName();
+ String articleName = (articleNode == null) ? ""
+ : articleNode.getTextContent();
+ // TODO - recurse if object is not a primitive
+ if (isPrimitive(localName)) {
+ MobyDataSimpleInstance mdsi = createPrimitiveType(localName, articleName);
+ list.add(mdsi);
+ } else {
+ MobyDataSimpleInstance[] recurseArray = getPrimitives(localName);
+ for (int k = 0; k < recurseArray.length; k++) {
+ list.add(recurseArray[k]);
}
}
}
- hasIterator = null;
- while (hasaIterator.hasNext()) {
- Element hasaElement = (Element) hasaIterator.next();
- List hasaInnerElementList = hasaElement.getChildren();
- if (!hasaInnerElementList.isEmpty()) {
- Element mobyObject = (Element) hasaInnerElementList.get(0);
- String primitive = mobyObject.getName();
- int type = Triples.HASA;
- String articleName = mobyObject.getChildText("articleName", ARTICLE);
- // check if primitive is a primitive
- if (isPrimitive(primitive)) {
- Triples t = new Triples(articleName, primitive, type);
- list.add(t);
+ }
+ for (int i = 0; i < hasa.getLength(); i++) {
+ NodeList children = ((Element) hasa.item(i)).getChildNodes();
+ for (int j = 0; j < children.getLength(); j++) {
+ if (children.item(j) instanceof Element) {
+ Element child = (Element) children.item(j);
+ Node articleNode = child.getElementsByTagName(
+ "mobyPred:articleName").item(0);
+ String localName = child.getLocalName();
+ String articleName = (articleNode == null) ? ""
+ : articleNode.getTextContent();
+ // TODO - recurse if object is not a primitive
+ if (isPrimitive(localName)) {
+ MobyDataSimpleInstance mdsi = createPrimitiveType(localName, articleName);
+ list.add(mdsi);
} else {
- // recurse
- ArrayList recurse = getPrimitives(primitive);
- if (recurse == null) {
- return null;
+ MobyDataSimpleInstance[] recurseArray = getPrimitives(localName);
+ for (int k = 0; k < recurseArray.length; k++) {
+ list.add(recurseArray[k]);
}
- processRecursionList(list, recurse);
}
}
}
- hasaIterator = null;
- /*
- * iterate through subClassOf
- * checking to see if the object is
- * a subclass of a primitive
- */
- while (subClassIterator.hasNext()) {
- Element sub = (Element)subClassIterator.next();
-
- // check if we have an element called Class
- Element rdfsClass = sub.getChild("Class", Namespace.getNamespace(RDFS.getURI()));
- if (rdfsClass != null) {
- // get the rdf:about attribute
- String attr = rdfsClass.getAttributeValue("about", Namespace.getNamespace(RDF.getURI()));
- if (attr != null) {
- // i.e. http://biomoby.org/RESOURCES/MOBY-S/Objects#text-plain
- if (attr.indexOf("#") > 0) {
- attr = attr.substring(attr.indexOf("#")+1);
- }
- if (isPrimitive(attr)&& !attr.equals("Object")) {
- // assume if you are something, it means you have it (singular)
- Triples t = new Triples(name, attr, Triples.HASA);
- list.add(t);
- }
- }
+ }
+
+ for (int i = 0; i < subClassOf.getLength(); i++) {
+ Element subClass = (Element) subClassOf.item(i);
+ // check for attributes
+ if (subClass.hasAttributes()) {
+ String object = subClass.getAttribute("rdf:resource");
+ if (object.indexOf("#") > 0) {
+ object = object.substring(object.indexOf("#") + 1);
}
- /* process <rdfs:subClassOf rdf:resource="..."/> */
- String attr = sub.getAttributeValue("resource", Namespace.getNamespace(RDF.getURI()));
- if (attr != null) {
- if (attr.indexOf("#") > 0) {
- attr = attr.substring(attr.indexOf("#") + 1);
- }
- if (isPrimitive(attr) && !attr.equals("Object")) {
- // assume if you are something, it means you have it (singular)
- Triples t = new Triples(name, attr, Triples.HASA);
- list.add(t);
- }
+ if (isPrimitive(object) && !object.equals("Object")) {
+ MobyDataSimpleInstance mdsi = new MobyDataSimpleInstance(
+ object);
+ mdsi.setDataType(new MobyDataType(object));
+ list.add(mdsi);
+ }
+ } else {
+ String object = ((Element) ((Element) subClass)
+ .getElementsByTagName("rdfs:Class").item(0))
+ .getAttribute("rdf:about");
+ if (object.indexOf("#") > 0) {
+ object = object.substring(object.indexOf("#") + 1);
+ }
+ if (isPrimitive(object) && !object.equals("Object")) {
+ MobyDataSimpleInstance mdsi = new MobyDataSimpleInstance(
+ object);
+ mdsi.setDataType(new MobyDataType(object));
+ list.add(mdsi);
}
}
+
}
}
-
- return list;
+
+ return convertArrayList(list);
}
-
- private void processRecursionList(ArrayList list, ArrayList recurse) {
- for (Iterator x = recurse.iterator(); x.hasNext();) {
- Triples t = (Triples) x.next();
- list.add(t);
+
+
+ /*
+ * method to create default DataTypes.
+ */
+ private MobyDataSimpleInstance createPrimitiveType(String localName, String articleName) {
+ if (localName.equalsIgnoreCase("Float")) {
+ return new MobyDataFloat(articleName, 0.0);
+ } else if (localName.equalsIgnoreCase("String")) {
+ return new MobyDataString(articleName, "");
+ } else if (localName.equalsIgnoreCase("Integer")) {
+ return new MobyDataInt(articleName, 0);
+ } else if(localName.equalsIgnoreCase("DateTime")) {
+ MobyDataSimpleInstance mdsi = new MobyDataSimpleInstance(articleName);
+ mdsi.setDataType(new MobyDataType(localName));
+ return mdsi;
}
+ // error
+ return null;
}
- /**
- *
- * <b>PRE:</b><p>
- * <b>POST:</b><p>
- * @param test - the test Moby object name to test with
- * @return true if test is a primitive, false otherwise
+ private MobyDataSimpleInstance[] convertArrayList(ArrayList list) {
+ int size = list.size();
+ MobyDataSimpleInstance[] mdsi = new MobyDataSimpleInstance[size];
+ size = 0;
+ for (Iterator x = list.iterator(); x.hasNext(); size++) {
+ mdsi[size] = (MobyDataSimpleInstance) x.next();
+ }
+ return mdsi;
+ }
+
+ /*
+ * simply tests whether a string is a primitive
*/
private boolean isPrimitive(String test) {
for (int x = 0; x < PRIMITIVES.length; x++) {
@@ -192,94 +232,106 @@
}
return false;
}
+
/*
* given an xml string, create a DOM document
*/
private Document getDOMDocument(String rdfXML) {
- // start creating the triples
- SAXBuilder builder = new SAXBuilder();
- Document doc = null;
try {
- // Create the document
- doc = builder.build(new ByteArrayInputStream(rdfXML.getBytes()));
- } catch (JDOMException e) {
- System.err.println("Error in getPrimitives:1 - JDOMException.\n" + e.getMessage());
- return null;
- } catch (IOException e) {
- System.err.println("Error in getPrimitives:2 - IOException.\n"+ e.getMessage());
- return null;
- }
- return doc;
+ // Create a builder factory
+ DocumentBuilderFactory factory = DocumentBuilderFactory
+ .newInstance();
+ factory.setNamespaceAware(true);
+ // Create the builder and parse the file
+ Document doc = factory.newDocumentBuilder().parse(
+ new InputSource(new StringReader(rdfXML)));
+ return doc;
+ } catch (SAXException e) {
+ // A parsing error occurred; the xml input is not valid
+ } catch (ParserConfigurationException e) {} catch (IOException e) {}
+ return null;
}
-
+
/*
* given an lsdi, get the moby object rdf document
*/
private String getObjectRDF(String lsid) {
URL url;
try {
- url = new URL(URL_OBJECT+lsid);
+ url = new URL(URL_OBJECT + lsid);
} catch (MalformedURLException e) {
- System.err.println("Error in getObjectRDF:1 - MalformedURLException.");
+ System.err
+ .println("Error in getObjectRDF:1 - MalformedURLException.");
return null;
}
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(url.openStream()));
} catch (IOException e1) {
- System.err.println("Error in getObjectRDF:2 - IOException. \n- Can't read from URL");
+ System.err
+ .println("Error in getObjectRDF:2 - IOException. \n- Can't read from URL");
return null;
}
String input;
StringBuffer sb = new StringBuffer();
try {
while ((input = in.readLine()) != null) {
- sb.append(input+System.getProperty("line.separator"));
+ sb.append(input + System.getProperty("line.separator"));
}
} catch (IOException e2) {
- System.err.println("Error in getObjectRDF:3 - IOException.\n-Problems reading from URL");
+ System.err
+ .println("Error in getObjectRDF:3 - IOException.\n-Problems reading from URL");
return null;
}
return sb.toString();
}
-
+
/**
*
* <b>PRE:</b> Object is a valid Moby Object<p>
* <b>POST:</b> An element representing the moby class structure is created.</br>>
* @link http://www.biomoby.org/twiki/bin//view/Moby/MobySAPI#ObjectStructure
* @param object - the Moby object to create a structure for
- * @param triples - the primitives in Triples format to add to this structure
- * @return an Element representing the Moby Class Structure
+ * @param mdsi - an array of MobyDataSimpleInstances to add to the element
+ * @return an Element representing the Moby Class Structure containing the MobyDataSimpleInstances
*/
- public Element getElementContainingTriples(String object, ArrayList triples) {
+ public Element getElementContainingPrimitives(String object,
+ MobyDataSimpleInstance[] mdsi) {
/*
-<moby:GenericSequence namespace='AGI_LocusCode' id='At2g42410'>
- <CrossReference>
- <Object namespace='Global_Keyword' id='superman'/>
- </CrossReference>
- <moby:Integer namespace='' id='' articleName='Length'>214</moby:Integer>
- <moby:String namespace='' id=''articleName='SequenceString'>MKRTHLASFSNRDKTRLGYL</moby:String>
-</moby:GenericSequence>
+ <moby:GenericSequence namespace='AGI_LocusCode' id='At2g42410'>
+ <CrossReference>
+ <Object namespace='Global_Keyword' id='superman'/>
+ </CrossReference>
+ <moby:Integer namespace='' id='' articleName='Length'>214</moby:Integer>
+ <moby:String namespace='' id=''articleName='SequenceString'>MKRTHLASFSNRDKTRLGYL</moby:String>
+ </moby:GenericSequence>
*/
/*root element*/
Element root = null;
- Namespace ns = Namespace.getNamespace("moby", "http://www.biomoby.org/moby");
- root = new Element(object);
- root.setNamespace(ns);
-
+ Document doc = createDomDocument();
+ root = doc.createElementNS(MOBY, object);
/*iterate through triples, create elements and add to root*/
- Iterator it = triples.iterator();
- while (it.hasNext()) {
- Triples t = (Triples)it.next();
- Element e = new Element(t.getPrimitive(), ns);
- e.setAttribute(new Attribute("id", ""));
- e.setAttribute(new Attribute("articleName", t.getArticleName()));
- root.addContent(e);
+ for (int x = 0; x < mdsi.length; x++) {
+ MobyDataSimpleInstance mds = mdsi[x];
+ Element e = doc.createElementNS(MOBY, mds.getDataType().getName());
+ e.setAttribute("id", "");
+ e.setAttribute("articleName", mds.getName());
+ root.appendChild(e);
}
return root;
}
+
+ private Document createDomDocument() {
+ try {
+ DocumentBuilder builder = DocumentBuilderFactory.newInstance()
+ .newDocumentBuilder();
+ Document doc = builder.newDocument();
+ return doc;
+ } catch (ParserConfigurationException e) {}
+ return null;
+ }
+
/**
*
* <b>PRE:</b>Element e exists and Class, namespace, and id are valid.<p>
@@ -290,30 +342,33 @@
* @param id - the identifier within that namespace that specifies the precise resource represented in the object
* @return Element - the root element containing the CRIB just added.
*/
- public Element addCrossReferenceToElement(Element e, String Class, String namespace, String id) {
+ public Element addCrossReferenceToElement(Element e, String Class,
+ String namespace, String id) {
/* first make sure that the element doesnt already contain
* the CrossReference child
*/
- Iterator it = e.getChildren("CrossReference").iterator();
+ NodeList it = e.getElementsByTagName("CrossReference");
Element cross = null;
- if (it.hasNext()) {
+ if (it.getLength() > 0) {
/*There is a pre-existing tag*/
- cross = (Element)it.next();
- Element object = new Element(Class);
- object.setAttribute(new Attribute("namespace", namespace));
- object.setAttribute(new Attribute("id", id));
- cross.addContent(object);
+ cross = (Element) it.item(0);
+ Document doc = createDomDocument();
+ Element object = doc.createElement(Class);
+ object.setAttribute("namespace", namespace);
+ object.setAttribute("id", id);
+ cross.appendChild(object);
} else {
- cross = new Element ("CrossReference");
- Element object = new Element(Class);
- object.setAttribute(new Attribute("namespace", namespace));
- object.setAttribute(new Attribute("id", id));
- cross.addContent(object);
- e.addContent(cross);
+ Document doc = e.getOwnerDocument();
+ Element object = doc.createElement(Class);
+ cross = doc.createElement("CrossReference");
+ object.setAttribute("namespace", namespace);
+ object.setAttribute("id", id);
+ cross.appendChild(object);
+ e.appendChild(cross);
}
return e;
}
-
+
/**
*
* <b>PRE:</b><p>
@@ -328,49 +383,55 @@
* @param description - this is the textual content of the XML element, and contains a human-readable description of the intended interpretation of this cross-reference.
* @return - the root object Element with the Xref cross reference added to it.
*/
- public Element addXrefCrossReferenceToElement(Element e, String id, String namespace, String authURI, String serviceName, String evidenceCode, String XrefType, String description) {
+
+ public Element addXrefCrossReferenceToElement(Element e, String id,
+ String namespace, String authURI, String serviceName,
+ String evidenceCode, String XrefType, String description) {
/*
* everything is required other than description
*/
- if (namespace == null || id == null || authURI == null || serviceName == null || evidenceCode == null || XrefType == null)
+ if (namespace == null || id == null || authURI == null
+ || serviceName == null || evidenceCode == null
+ || XrefType == null)
return e;
/*
* make sure that the element doesnt already contain
* the CrossReference child
*/
- Iterator it = e.getChildren("CrossReference").iterator();
+ NodeList it = e.getElementsByTagName("CrossReference");
Element cross = null;
- if (it.hasNext()) {
+ if (it.getLength() > 0) {
+ Document doc = e.getOwnerDocument();
/*There is a pre-existing tag*/
- cross = (Element)it.next();
- Element object = new Element("Xref");
- object.setAttribute(new Attribute("namespace", namespace));
- object.setAttribute(new Attribute("id", id));
- object.setAttribute(new Attribute("authURI", authURI));
- object.setAttribute(new Attribute("serviceName", serviceName));
- object.setAttribute(new Attribute("evidenceCode", evidenceCode));
- object.setAttribute(new Attribute("XrefType", XrefType));
+ cross = (Element) it.item(0);
+ Element object = doc.createElement("Xref");
+ object.setAttribute("namespace", namespace);
+ object.setAttribute("id", id);
+ object.setAttribute("authURI", authURI);
+ object.setAttribute("serviceName", serviceName);
+ object.setAttribute("evidenceCode", evidenceCode);
+ object.setAttribute("XrefType", XrefType);
if (description != null)
- object.setText(description);
- cross.addContent(object);
+ object.setNodeValue(description);
+ cross.appendChild(object);
} else {
- cross = new Element ("CrossReference");
- Element object = new Element("Xref");
- object.setAttribute(new Attribute("namespace", namespace));
- object.setAttribute(new Attribute("id", id));
- object.setAttribute(new Attribute("authURI", authURI));
- object.setAttribute(new Attribute("serviceName", serviceName));
- object.setAttribute(new Attribute("evidenceCode", evidenceCode));
- object.setAttribute(new Attribute("XrefType", XrefType));
+ Document doc = e.getOwnerDocument();
+ cross = doc.createElement("CrossReference");
+ Element object = doc.createElement("Xref");
+ object.setAttribute("namespace", namespace);
+ object.setAttribute("id", id);
+ object.setAttribute("authURI", authURI);
+ object.setAttribute("serviceName", serviceName);
+ object.setAttribute("evidenceCode", evidenceCode);
+ object.setAttribute("XrefType", XrefType);
if (description != null)
- object.setText(description);
- cross.addContent(object);
- e.addContent(cross);
+ object.setNodeValue(description);
+ cross.appendChild(object);
+ e.appendChild(cross);
}
return e;
}
-
-
+
/**
*
* <b>PRE:</b><p>
@@ -381,27 +442,30 @@
*/
/*
* <ProvisionInformation>
- ... one or more of the provision elements (below) ...
- </ProvisionInformation>
-
- <serviceSoftware software_name="" software_version="" software_comment=""/>
- <serviceDatabase database_name="" database_version="" database_comment=""/>
- <serviceComment>comment here</serviceComment>
+ ... one or more of the provision elements (below) ...
+ </ProvisionInformation>
+
+ <serviceSoftware software_name="" software_version="" software_comment=""/>
+ <serviceDatabase database_name="" database_version="" database_comment=""/>
+ <serviceComment>comment here</serviceComment>
*/
- public Element addProvisionInformationBlock(Element root, Element[] elements){
+ public Element addProvisionInformationBlock(Element root, Element[] elements) {
if (elements == null || root == null || elements.length < 1)
return root;
/* only add PIB to a root element with no PIB child*/
- if (!root.getChildren("ProvisionInformation").isEmpty()) {
+ if (root.getElementsByTagName("ProvisionInformation").item(0) != null) {
return root;
}
- Element pib = new Element("ProvisionInformation");
- for (int x=0; x < elements.length; x++)
- pib.addContent(elements[x]);
- root.addContent(pib);
+ Document doc = root.getOwnerDocument();
+ Element pib = doc.createElement("ProvisionInformation");
+ for (int x = 0; x < elements.length; x++) {
+ doc.adoptNode(elements[x]);
+ pib.appendChild(elements[x]);
+ }
+ root.appendChild(pib);
return root;
}
-
+
/**
*
* <b>PRE:</b><p>
@@ -411,15 +475,18 @@
* @param comment - any applicable comments
* @return - a serviceSoftware Element with its attributes appropriately set.
*/
- public Element createPIBServiceSoftwareElement(String name, String version, String comment) {
+ public Element createPIBServiceSoftwareElement(String name, String version,
+ String comment) {
if (name == null || version == null || comment == null)
return null;
- Element e = new Element("serviceSoftware");
- e.setAttribute(new Attribute("software_name", name));
- e.setAttribute(new Attribute("software_version", version));
- e.setAttribute(new Attribute("software_comment", comment));
+ Document doc = createDomDocument();
+ Element e = doc.createElement("serviceSoftware");
+ e.setAttribute("software_name", name);
+ e.setAttribute("software_version", version);
+ e.setAttribute("software_comment", comment);
return e;
}
+
/**
*
* <b>PRE:</b><p>
@@ -429,16 +496,18 @@
* @param comment - any applicable comments
* @return - a serviceDatabase Element with its attributes appropriately set.
*/
- public Element createPIBServiceDatabaseElement(String name, String version, String comment) {
+ public Element createPIBServiceDatabaseElement(String name, String version,
+ String comment) {
if (name == null || version == null || comment == null)
return null;
- Element e = new Element("serviceDatabase");
- e.setAttribute(new Attribute("database_name", name));
- e.setAttribute(new Attribute("database_version", version));
- e.setAttribute(new Attribute("database_comment", comment));
+ Document doc = createDomDocument();
+ Element e = doc.createElement("serviceDatabase");
+ e.setAttribute("database_name", name);
+ e.setAttribute("database_version", version);
+ e.setAttribute("database_comment", comment);
return e;
}
-
+
/**
*
* <b>PRE:</b><p>
@@ -449,24 +518,12 @@
public Element createPIBServiceCommentElement(String comment) {
if (comment == null)
return null;
- Element e = new Element("serviceComment");
- e.setText(comment);
+
+ Element e = createDomDocument().createElement("serviceComment");
+ e.setTextContent(comment);
return e;
}
-
- public static void main(String[] args) {
- MobyObjectPrimitiveExtractor g = new MobyObjectPrimitiveExtractor();
- ArrayList l = g.getPrimitives("GenericSequence");
- Element e = g.getElementContainingTriples("GenericSequence", l);
- System.out.println(output(e));
- e = g.addCrossReferenceToElement(e, "Object","NCBI_gi", "bob");
- System.out.println(output(e));
- e = g.addXrefCrossReferenceToElement(e, "myNamespace", "545454", "www.illuminae.com", "myServiceName", "myCode", "myXtype", "bob is your description");
- System.out.println(output(e));
- //g.getPrimitives("DNASequence");
- //g.getPrimitives("CommentedRNASequence");
- }
-
+
/**
*
* <b>PRE:</b><p>
@@ -474,7 +531,7 @@
* @param e - the dom element to stringify.
* @return - a string representation of the element
*/
- public static String output(Element e) {
+ public String createStringFromElement(Element e) {
ByteArrayOutputStream buf = new ByteArrayOutputStream();
// Create the document
XMLOutputter fmt = new XMLOutputter();
@@ -483,10 +540,36 @@
format.setIndent(" ");
fmt.setFormat(format);
try {
- fmt.output(e, buf);
+ fmt.output(convert(e), buf);
} catch (IOException ex) {
System.out.println("Error: " + ex.getLocalizedMessage());
}
- return "Document: \n" + buf.toString();
+ return buf.toString();
}
-}
+
+ private void outputDocumentToStream(Document doc, OutputStream s) {
+ // Use a Transformer for output
+ TransformerFactory tFactory = TransformerFactory.newInstance();
+ Transformer transformer;
+ try {
+ transformer = tFactory.newTransformer();
+ DOMSource source = new DOMSource(doc);
+ StreamResult result = new StreamResult(s);
+ transformer.transform(source, result);
+ } catch (TransformerConfigurationException e1) {
+ System.err.println("Couldnt print out DOM Doc.1 "
+ + e1.getLocalizedMessage());
+ } catch (TransformerException e) {
+ System.err.println("Couldnt print out DOM Doc.2 "
+ + e.getLocalizedMessage());
+ }
+ }
+
+ private static org.jdom.Element convert(Element domDoc) throws IOException {
+ // Create new DOMBuilder, using default parser
+ DOMBuilder builder = new DOMBuilder();
+ org.jdom.Element jdomDoc = builder.build(domDoc);
+ return jdomDoc;
+ }
+
+}
\ No newline at end of file
More information about the MOBY-guts
mailing list