You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by re...@apache.org on 2009/10/15 09:28:43 UTC

svn commit: r825417 - in /cocoon/cocoon3/trunk/cocoon-optional/src: main/java/org/apache/cocoon/optional/pipeline/components/sax/solr/ test/java/org/apache/cocoon/optional/pipeline/components/sax/solr/ test/resources/org/apache/cocoon/optional/pipeline...

Author: reinhard
Date: Thu Oct 15 07:28:42 2009
New Revision: 825417

URL: http://svn.apache.org/viewvc?rev=825417&view=rev
Log:
fix bug in copying chars
add license header
fix indent

Modified:
    cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/solr/SolrConsumer.java
    cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/solr/SolrConsumerTestCase.java
    cocoon/cocoon3/trunk/cocoon-optional/src/test/resources/org/apache/cocoon/optional/pipeline/components/sax/solr/sample.xml

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=825417&r1=825416&r2=825417&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 Thu Oct 15 07:28:42 2009
@@ -1,157 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
 package org.apache.cocoon.optional.pipeline.components.sax.solr;
 
 import java.net.MalformedURLException;
-import java.util.Arrays;
 
 import org.apache.cocoon.pipeline.ProcessingException;
-import org.apache.cocoon.sax.AbstractSAXProducer;
+import org.apache.cocoon.sax.AbstractSAXTransformer;
 import org.apache.cocoon.sax.SAXConsumer;
 import org.apache.solr.client.solrj.SolrServer;
 import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
 import org.apache.solr.common.SolrInputDocument;
 import org.apache.solr.common.util.StrUtils;
 import org.xml.sax.Attributes;
-import org.xml.sax.Locator;
 import org.xml.sax.SAXException;
 
-public class SolrConsumer extends AbstractSAXProducer implements SAXConsumer {
-  
-    private SolrServer solr = null;
+public class SolrConsumer extends AbstractSAXTransformer implements SAXConsumer {
 
+    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 = "";
 
     public SolrConsumer(String url) throws MalformedURLException {
-        solr = new CommonsHttpSolrServer(url);
+        this.solr = new CommonsHttpSolrServer(url);
     }
-    
+
     public SolrConsumer(SolrServer server) {
-        solr = server;
+        this.solr = server;
     }
-    
-    public void characters(char[] ch, int start, int length)
-            throws SAXException {
-        text += new String(ch, start, start + length);
-        getSAXConsumer().characters(ch, start, length);
+
+    @Override
+    public void characters(char[] ch, int start, int length) throws SAXException {
+        this.text += new String(ch, start, length);
+        this.getSAXConsumer().characters(ch, start, length);
     }
 
-    public void startElement(String uri, String localName, String qName,
-            Attributes atts) throws SAXException {
+    @Override
+    public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
         if ("field".equals(localName)) {
-            text = "";
-            boost = 1.0f;
+            this.text = "";
+            this.boost = 1.0f;
 
-            if (atts.getValue("name") != null)
-                name = atts.getValue("name");
+            if (atts.getValue("name") != null) {
+                this.name = atts.getValue("name");
+            }
 
-            if (atts.getValue("boost") != null)
-                boost = Float.parseFloat(atts.getValue("boost"));
+            if (atts.getValue("boost") != null) {
+                this.boost = Float.parseFloat(atts.getValue("boost"));
+            }
 
-            if (atts.getValue("null") != null)
-                isNull = StrUtils.parseBoolean(atts.getValue("null"));
+            if (atts.getValue("null") != null) {
+                this.isNull = StrUtils.parseBoolean(atts.getValue("null"));
+            }
 
         }
-        getSAXConsumer().startElement(uri, localName, qName, atts);
+
+        this.getSAXConsumer().startElement(uri, localName, qName, atts);
     }
 
-    public void endElement(String uri, String localName, String qName)
-            throws SAXException {
+    @Override
+    public void endElement(String uri, String localName, String qName) throws SAXException {
         if ("docs".equals(localName)) {
             try {
-               solr.commit();
+                this.solr.commit();
             } catch (Exception e) {
-                throw new ProcessingException(
-                        "Unable to commit the Solr documents.", e);
+                throw new ProcessingException("Unable to commit the Solr documents.", e);
             }
         } else if ("doc".equals(localName)) {
             try {
-                solr.add(doc);
-                doc = new SolrInputDocument();
+                this.solr.add(this.doc);
+                this.doc = new SolrInputDocument();
             } catch (Exception e) {
-                throw new ProcessingException(
-                        "Unable to add the Solr document.", e);
+                throw new ProcessingException("Unable to add the Solr document.", e);
             }
         } else if ("field".equals(localName)) {
-            if (!isNull && text.length() > 0) {
-                doc.addField(name, text, boost);
-                boost = 1.0f;
+            if (!this.isNull && this.text.length() > 0) {
+                this.doc.addField(this.name, this.text, this.boost);
+                this.boost = 1.0f;
             }
         }
-        getSAXConsumer().endElement(uri, localName, qName);
-    }
-
-    public void endPrefixMapping(String prefix) throws SAXException {
-        getSAXConsumer().endPrefixMapping(prefix);
-    }
-
-    public void ignorableWhitespace(char[] ch, int start, int length)
-            throws SAXException {
-        getSAXConsumer().ignorableWhitespace(ch, start, length);
-    }
-
-    public void processingInstruction(String target, String data)
-            throws SAXException {
-        getSAXConsumer().processingInstruction(target, data);
-    }
-
-    public void setDocumentLocator(Locator locator) {
-        getSAXConsumer().setDocumentLocator(locator);
-    }
-
-    public void skippedEntity(String name) throws SAXException {
-        getSAXConsumer().skippedEntity(name);
+        this.getSAXConsumer().endElement(uri, localName, qName);
     }
-
-    public void startDocument() throws SAXException {
-        getSAXConsumer().startDocument();
-    }
-
-    public void startPrefixMapping(String prefix, String uri)
-            throws SAXException {
-        getSAXConsumer().startPrefixMapping(prefix, uri);
-    }
-
-    public void comment(char[] ch, int start, int length) throws SAXException {
-        getSAXConsumer().comment(ch, start, length);
-    }
-
-    public void endCDATA() throws SAXException {
-        getSAXConsumer().endCDATA();
-    }
-
-    public void endDTD() throws SAXException {
-        getSAXConsumer().endDTD();
-    }
-
-    public void endEntity(String name) throws SAXException {
-        getSAXConsumer().endEntity(name);
-    }
-
-    public void startCDATA() throws SAXException {
-        getSAXConsumer().startCDATA();
-    }
-
-    public void startDTD(String name, String publicId, String systemId)
-            throws SAXException {
-        getSAXConsumer().startDTD(name, publicId, systemId);
-    }
-
-    public void startEntity(String name) throws SAXException {
-        getSAXConsumer().startEntity(name);
-    }
-
-    public void endDocument() throws SAXException {
-        getSAXConsumer().endDocument();
-    }
-    
-    public void finish() {
-    }
-
 }

Modified: cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/solr/SolrConsumerTestCase.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/solr/SolrConsumerTestCase.java?rev=825417&r1=825416&r2=825417&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/solr/SolrConsumerTestCase.java (original)
+++ cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/solr/SolrConsumerTestCase.java Thu Oct 15 07:28:42 2009
@@ -1,3 +1,19 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 package org.apache.cocoon.optional.pipeline.components.sax.solr;
 
 import static org.junit.Assert.*;
@@ -8,7 +24,6 @@
 
 import javax.xml.parsers.ParserConfigurationException;
 
-import org.apache.cocoon.optional.pipeline.components.sax.solr.SolrConsumer;
 import org.apache.cocoon.pipeline.NonCachingPipeline;
 import org.apache.cocoon.pipeline.Pipeline;
 import org.apache.cocoon.sax.SAXPipelineComponent;
@@ -32,35 +47,34 @@
 
     public SolrConsumerTestCase() throws ParserConfigurationException, IOException, SAXException {
         SolrResourceLoader loader = new SolrResourceLoader("solr");
-        CoreContainer container = new CoreContainer(loader); 
-        CoreDescriptor descriptor = new CoreDescriptor(container, "cname", "." );
+        CoreContainer container = new CoreContainer(loader);
+        CoreDescriptor descriptor = new CoreDescriptor(container, "cname", ".");
         SolrCore core = container.create(descriptor);
-        container.register( core.getName(), core, false );
-        solr = new EmbeddedSolrServer(container, core.getName());
+        container.register(core.getName(), core, false);
+        this.solr = new EmbeddedSolrServer(container, core.getName());
     }
-  
+
     @Test
     public void testPipelineWithSolrConsumer() throws Exception {
         Pipeline<SAXPipelineComponent> pipeline = new NonCachingPipeline<SAXPipelineComponent>();
-        
+
         pipeline.addComponent(new FileGenerator(this.getClass().getResource("sample.xml")));
-        pipeline.addComponent(new SolrConsumer(solr));
+        pipeline.addComponent(new SolrConsumer(this.solr));
         pipeline.addComponent(new XMLSerializer());
-  
+
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         pipeline.setup(baos);
         pipeline.execute();
-  
+
         SolrQuery query = new SolrQuery();
-        
+
         query.setQuery("title:title");
-        SolrDocumentList results = solr.query(query).getResults();
+        SolrDocumentList results = this.solr.query(query).getResults();
         Iterator<SolrDocument> documents = results.iterator();
-        
+
         assertEquals(results.size(), 3);
         assertEquals(documents.next().getFieldValue("title"), "title 2");
         assertEquals(documents.next().getFieldValue("title"), "title 3");
         assertEquals(documents.next().getFieldValue("title"), "title 1");
-        
     }
 }

Modified: cocoon/cocoon3/trunk/cocoon-optional/src/test/resources/org/apache/cocoon/optional/pipeline/components/sax/solr/sample.xml
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-optional/src/test/resources/org/apache/cocoon/optional/pipeline/components/sax/solr/sample.xml?rev=825417&r1=825416&r2=825417&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-optional/src/test/resources/org/apache/cocoon/optional/pipeline/components/sax/solr/sample.xml (original)
+++ cocoon/cocoon3/trunk/cocoon-optional/src/test/resources/org/apache/cocoon/optional/pipeline/components/sax/solr/sample.xml Thu Oct 15 07:28:42 2009
@@ -16,19 +16,19 @@
   limitations under the License.
 -->
 <docs>
-	<doc>
-		<field name="id">1</field>
-		<field name="title" boost="1">title 1</field>
-		<field name="content">description 1</field>
-	</doc>
-	<doc>
-		<field name="id">2</field>
-		<field name="title" boost="3">title 2</field>
-		<field name="content">description 2</field>
-	</doc>
-	<doc>
-		<field name="id">3</field>
-		<field name="title" boost="2">title 3</field>
-		<field name="content">description 3</field>
-	</doc>
+  <doc>
+    <field name="id">1</field>
+    <field name="title" boost="1">title 1</field>
+    <field name="content">description 1</field>
+  </doc>
+  <doc>
+    <field name="id">2</field>
+    <field name="title" boost="3">title 2</field>
+    <field name="content">description 2</field>
+  </doc>
+  <doc>
+    <field name="id">3</field>
+    <field name="title" boost="2">title 3</field>
+    <field name="content">description 3</field>
+  </doc>
 </docs>
\ No newline at end of file