You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by si...@apache.org on 2010/02/08 21:07:33 UTC

svn commit: r907775 - /cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/solr/SolrConsumer.java

Author: simonetripodi
Date: Mon Feb  8 20:07:33 2010
New Revision: 907775

URL: http://svn.apache.org/viewvc?rev=907775&view=rev
Log:
optimized strings allocations using constants
strings concatenation replaced with StringBuilder

Modified:
    cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/solr/SolrConsumer.java

Modified: cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/solr/SolrConsumer.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/solr/SolrConsumer.java?rev=907775&r1=907774&r2=907775&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/solr/SolrConsumer.java (original)
+++ cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/solr/SolrConsumer.java Mon Feb  8 20:07:33 2010
@@ -31,12 +31,29 @@
 
 public class SolrConsumer extends AbstractSAXTransformer {
 
+    private static final String FIELD = "field";
+
+    private static final String NAME = "name";
+
+    private static final String BOOST = "boost";
+
+    private static final String NULL = "null";
+
+    private static final String DOC = "doc";
+
+    private static final String DOCS = "docs";
+
     private SolrServer solr = null;
+
     private boolean isNull = false;
+
     private SolrInputDocument doc = new SolrInputDocument();
+
     private float boost = 1.0f;
+
     private String name = null;
-    private String text = "";
+
+    private StringBuilder text = new StringBuilder();
 
     public SolrConsumer(String url) throws MalformedURLException {
         this.solr = new CommonsHttpSolrServer(url);
@@ -48,26 +65,26 @@
 
     @Override
     public void characters(char[] ch, int start, int length) throws SAXException {
-        this.text += new String(ch, start, length);
+        this.text.append(ch, start, length);
         this.getSAXConsumer().characters(ch, start, length);
     }
 
     @Override
     public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
-        if ("field".equals(localName)) {
-            this.text = "";
+        if (FIELD.equals(localName)) {
+            this.text = new StringBuilder();
             this.boost = 1.0f;
 
-            if (atts.getValue("name") != null) {
-                this.name = atts.getValue("name");
+            if (atts.getValue(NAME) != null) {
+                this.name = atts.getValue(NAME);
             }
 
-            if (atts.getValue("boost") != null) {
-                this.boost = Float.parseFloat(atts.getValue("boost"));
+            if (atts.getValue(BOOST) != null) {
+                this.boost = Float.parseFloat(atts.getValue(BOOST));
             }
 
-            if (atts.getValue("null") != null) {
-                this.isNull = StrUtils.parseBoolean(atts.getValue("null"));
+            if (atts.getValue(NULL) != null) {
+                this.isNull = StrUtils.parseBoolean(atts.getValue(NULL));
             }
         }
 
@@ -76,26 +93,27 @@
 
     @Override
     public void endElement(String uri, String localName, String qName) throws SAXException {
-        if ("docs".equals(localName)) {
+        if (DOCS.equals(localName)) {
             try {
                 this.solr.commit();
             } catch (Exception e) {
                 throw new ProcessingException("Unable to commit the Solr documents.", e);
             }
-        } else if ("doc".equals(localName)) {
+        } else if (DOC.equals(localName)) {
             try {
                 this.solr.add(this.doc);
                 this.doc = new SolrInputDocument();
             } catch (Exception e) {
                 throw new ProcessingException("Unable to add the Solr document.", e);
             }
-        } else if ("field".equals(localName)) {
+        } else if (FIELD.equals(localName)) {
             if (!this.isNull && this.text.length() > 0) {
-                this.doc.addField(this.name, this.text, this.boost);
+                this.doc.addField(this.name, this.text.toString(), this.boost);
                 this.boost = 1.0f;
             }
         }
 
         this.getSAXConsumer().endElement(uri, localName, qName);
     }
+
 }