You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-commits@lucene.apache.org by ho...@apache.org on 2006/12/15 04:32:29 UTC

svn commit: r487438 - in /incubator/solr/trunk: CHANGES.txt src/java/org/apache/solr/core/Config.java src/java/org/apache/solr/util/DOMUtil.java

Author: hossman
Date: Thu Dec 14 19:32:28 2006
New Revision: 487438

URL: http://svn.apache.org/viewvc?view=rev&rev=487438
Log:
SOLR-78 - replaced usage of Node.getTextContent with new helper method which allows Solr to work with DOM Level 2 parsers instead of requiring DOM Level 3

Modified:
    incubator/solr/trunk/CHANGES.txt
    incubator/solr/trunk/src/java/org/apache/solr/core/Config.java
    incubator/solr/trunk/src/java/org/apache/solr/util/DOMUtil.java

Modified: incubator/solr/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/incubator/solr/trunk/CHANGES.txt?view=diff&rev=487438&r1=487437&r2=487438
==============================================================================
--- incubator/solr/trunk/CHANGES.txt (original)
+++ incubator/solr/trunk/CHANGES.txt Thu Dec 14 19:32:28 2006
@@ -217,5 +217,8 @@
 13. Added Solr/Lucene versions to "Info" page (hossman)
 14. Explicitly set mime-type of .xsl files in web.xml to
     application/xslt+xml (hossman)
+15. Config parsing should now work useing DOM Level 2 parsers -- Solr
+    previously relied on getTextContent which is a DOM Level 3 addition
+    (Alexander Saar via hossman, SOLR-78)
 
 2006/01/17 Solr open sourced, moves to Apache Incubator

Modified: incubator/solr/trunk/src/java/org/apache/solr/core/Config.java
URL: http://svn.apache.org/viewvc/incubator/solr/trunk/src/java/org/apache/solr/core/Config.java?view=diff&rev=487438&r1=487437&r2=487438
==============================================================================
--- incubator/solr/trunk/src/java/org/apache/solr/core/Config.java (original)
+++ incubator/solr/trunk/src/java/org/apache/solr/core/Config.java Thu Dec 14 19:32:28 2006
@@ -22,6 +22,7 @@
 import org.xml.sax.SAXException;
 import org.apache.solr.core.SolrCore;
 import org.apache.solr.core.SolrException;
+import org.apache.solr.util.DOMUtil;
 
 import javax.xml.parsers.*;
 import javax.xml.xpath.XPath;
@@ -122,10 +123,8 @@
     Node nd = getNode(path,errIfMissing);
     if (nd==null) return null;
 
-    // should do the right thing for both attributes and elements.
-    // Oops, when running in Resin, I get an unsupported operation
-    // exception... need to use Sun default (apache)
-    String txt = nd.getTextContent();
+    String txt = DOMUtil.getText(nd);
+    
     log.fine(name + ' '+path+'='+txt);
     return txt;
 

Modified: incubator/solr/trunk/src/java/org/apache/solr/util/DOMUtil.java
URL: http://svn.apache.org/viewvc/incubator/solr/trunk/src/java/org/apache/solr/util/DOMUtil.java?view=diff&rev=487438&r1=487437&r2=487438
==============================================================================
--- incubator/solr/trunk/src/java/org/apache/solr/util/DOMUtil.java (original)
+++ incubator/solr/trunk/src/java/org/apache/solr/util/DOMUtil.java Thu Dec 14 19:32:28 2006
@@ -17,6 +17,7 @@
 
 package org.apache.solr.util;
 
+import org.w3c.dom.Element;
 import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
@@ -129,17 +130,17 @@
     Object val=null;
 
     if ("str".equals(type)) {
-      val = nd.getTextContent();
+      val = getText(nd);
     } else if ("int".equals(type)) {
-      val = Integer.valueOf(nd.getTextContent());
+      val = Integer.valueOf(getText(nd));
     } else if ("long".equals(type)) {
-      val = Long.valueOf(nd.getTextContent());
+      val = Long.valueOf(getText(nd));
     } else if ("float".equals(type)) {
-      val = Float.valueOf(nd.getTextContent());
+      val = Float.valueOf(getText(nd));
     } else if ("double".equals(type)) {
-      val = Double.valueOf(nd.getTextContent());
+      val = Double.valueOf(getText(nd));
     } else if ("bool".equals(type)) {
-      val = Boolean.valueOf(nd.getTextContent());
+      val = Boolean.valueOf(getText(nd));
     } else if ("lst".equals(type)) {
       val = childNodesToNamedList(nd);
     } else if ("arr".equals(type)) {
@@ -150,4 +151,72 @@
     if (arr != null) arr.add(val);
   }
 
+  /**
+   * Drop in replacement for Node.getTextContent().
+   *
+   * <p>
+   * This method is provided to support the same functionality as
+   * Node.getTextContent() but in a way that is DOM Level 2 compatible.
+   * </p>
+   *
+   * @see <a href="http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-textContent">DOM Object Model Core</a>
+   */
+  public static String getText(Node nd) {
+
+    short type = nd.getNodeType();
+    
+    // for most node types, we can defer to the recursive helper method,
+    // but when asked for the text of these types, we must return null
+    // (Not the empty string)
+    switch (type) {
+      
+    case Node.DOCUMENT_NODE: /* fall through */
+    case Node.DOCUMENT_TYPE_NODE: /* fall through */
+    case Node.NOTATION_NODE: /* fall through */
+      return null;
+    }
+
+    StringBuilder sb = new StringBuilder();
+    getText(nd, sb);
+    return sb.toString();
+  }
+
+  /** @see #getText(Node) */
+  private static void getText(Node nd, StringBuilder buf) {
+    
+    short type = nd.getNodeType();
+
+    switch (type) {
+      
+    case Node.ELEMENT_NODE: /* fall through */
+    case Node.ATTRIBUTE_NODE: /* fall through */
+    case Node.ENTITY_NODE: /* fall through */
+    case Node.ENTITY_REFERENCE_NODE: /* fall through */
+    case Node.DOCUMENT_FRAGMENT_NODE: 
+      NodeList childs = nd.getChildNodes();
+      for (int i = 0; i < childs.getLength(); i++) {
+        Node child = childs.item(i);
+        short childType = child.getNodeType();
+        if (childType != Node.COMMENT_NODE &&
+            childType != Node.PROCESSING_INSTRUCTION_NODE) {
+          getText(child, buf);
+        }
+      }
+      break;
+      
+    case Node.TEXT_NODE: /* fall through */
+    case Node.CDATA_SECTION_NODE: /* fall through */
+    case Node.COMMENT_NODE: /* fall through */
+    case Node.PROCESSING_INSTRUCTION_NODE: /* fall through */
+      buf.append(nd.getNodeValue());
+      break;
+
+    case Node.DOCUMENT_NODE: /* fall through */
+    case Node.DOCUMENT_TYPE_NODE: /* fall through */
+    case Node.NOTATION_NODE: /* fall through */
+    default:
+      /* :NOOP: */
+
+    }
+  }
 }