[MOBY-guts] biomoby commit

Paul Gordon gordonp at pub.open-bio.org
Fri Jul 22 05:26:43 UTC 2005


gordonp
Fri Jul 22 01:26:43 EDT 2005
Update of /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared
In directory pub.open-bio.org:/tmp/cvs-serv30954

Modified Files:
	MobyObjectDecompositionImpl.java 
Log Message:
Removed Java 1.5 (actually a DOM level 3) dependency by implementing getTextContent(Node)

moby-live/Java/src/main/org/biomoby/shared MobyObjectDecompositionImpl.java,1.4,1.5
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/MobyObjectDecompositionImpl.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/MobyObjectDecompositionImpl.java	2005/05/12 05:19:20	1.4
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/MobyObjectDecompositionImpl.java	2005/07/22 05:26:43	1.5
@@ -21,7 +21,7 @@
 import javax.xml.parsers.ParserConfigurationException;
 
 import org.biomoby.client.CentralImpl;
-import org.biomoby.client.ui.graphical.applets.util.PrimitiveTypes;
+import org.biomoby.shared.PrimitiveTypes;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
@@ -41,7 +41,7 @@
  */
 public class MobyObjectDecompositionImpl implements MobyObjectDecomposition {
 
-    private static final String URL_OBJECT = "http://mobycentral.cbr.nrc.ca:8090/authority/metadata?lsid=";
+    private static final String URL_OBJECT = "http://mobycentral.icapture.ubc.ca:8090/authority/metadata?lsid=";
 
     /* (non-Javadoc)
      * @see org.biomoby.client.gui.util.MobyObjectDecomposition#getFlattenedPrims(java.lang.String)
@@ -80,7 +80,7 @@
                             "mobyPred:articleName").item(0);
                     String localName = child.getLocalName();
                     String articleName = (articleNode == null) ? ""
-                            : articleNode.getTextContent();
+                            : getTextContent(articleNode);
                     //                  TODO - recurse if object is not a primitive
                     if (isPrimitive(localName)) {
                         MobyDataObject mdsi = createPrimitiveType(
@@ -103,7 +103,7 @@
                                 "mobyPred:articleName").item(0);
                         String localName = child.getLocalName();
                         String articleName = (articleNode == null) ? ""
-                                : articleNode.getTextContent();
+                                : getTextContent(articleNode);
                         //                      TODO - recurse if object is not a primitive
                         if (isPrimitive(localName)) {
                             MobyDataObject mdsi = createPrimitiveType(
@@ -154,6 +154,52 @@
         return convertArrayListToMoby(list);
     }
 
+    /**
+     * Method to simulate DOM level 3 (Java 1.5 default) method for recursively getting text children from a Node.
+     * A wrapper around getTextContent(Node, boolean) with boolean = true, since DOM 3 getTextContent() ignores 
+     * things that match Text.isContentElementWhitespace().
+     */
+    public static String getTextContent(Node parent){
+	return getTextContent(parent, true);
+    }
+
+    /**
+     * Grabs the text and CDATA children of a Node (not necessairly contiguous) and returns them concatenated.
+     * Added by Paul Gordon.  
+     * Simulates ignoring of DOM 3 Text.isContentElementWhitespace() fully, I *think*.
+     * 
+     * @param parent the node who's text children should be concatenated
+     * @param includeChildrensText if true, recursively appends children's text depth first
+     *
+     * @return non-normalized text contents of the passed-in Node, and its children if so requested
+     */
+    public static String getTextContent(Node parent, boolean includeChildrensText){
+	if(parent.hasChildNodes()){
+	    java.util.regex.Pattern nonWhitespacePattern = Pattern.compile("[^ \t\r\n]");
+	    StringBuffer textbuffer = new StringBuffer();
+	    
+	    for(Node node = parent.getFirstChild(); node != null; node = node.getNextSibling()){
+		// Append all the contiguous text data
+		if(node instanceof org.w3c.dom.Text || node instanceof org.w3c.dom.CDATASection){ 
+		    // Get rid of whitespace
+		    String value = node.getNodeValue();
+		    // Add if not just whitespace.
+		    if(nonWhitespacePattern.matcher(value).find()){
+			textbuffer.append(value);
+		    }
+		}
+		// Get contents of children elements too (recursive)
+		else{
+		    textbuffer.append(getTextContent(node, includeChildrensText));
+		}
+	    }
+	    return textbuffer.toString();
+	}
+	else{
+	    return "";
+	}
+    }
+
     /* (non-Javadoc)
      * @see org.biomoby.client.gui.util.MobyObjectDecomposition#getObjectComposition(java.lang.String)
      */




More information about the MOBY-guts mailing list