cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Santiago Gala <sg...@hisitech.com>
Subject [PATCH] [C2] SQLTransformer.java
Date Fri, 20 Apr 2001 19:13:06 GMT
I had problems, some weeks ago, with SQLTransformer.java (specifically 
with the SimpleSQL sample).

I think I have already sent this patch, but it was not taken into 
account. I'm rather sure that changes in this file are needed for the 
demo to run. Further, I implemented the recycle() method there, and 
commented two variables that were already available in an ancestor class.

I think this transformer is deprecated. If so, either take it out of C2, 
with the sample, or put a "@deprecated Use ESQL instead" in the class 
javadocs.

The problem is that I'm beginning to have a dirty C2 cvs tree, and I 
don't like having "M" in my updates :).



Index: src/org/apache/cocoon/transformation/SQLTransformer.java
===================================================================
RCS file: 
/home/cvspublic/xml-cocoon/src/org/apache/cocoon/transformation/Attic/SQLTransformer.java,v
retrieving revision 1.1.2.23
diff -u -r1.1.2.23 SQLTransformer.java
--- src/org/apache/cocoon/transformation/SQLTransformer.java 
2001/04/17 03:55:17     1.1.2.23
+++ src/org/apache/cocoon/transformation/SQLTransformer.java 
2001/04/20 18:53:47
@@ -90,7 +90,7 @@
      protected Vector queries = new Vector();

      /** The offset of the current query in the queries list **/
-    protected int current_query_index = -1;
+    protected int current_query_index = 0;

      /** The name of the value element we're currently receiving **/
      protected String current_name;
@@ -102,8 +102,8 @@
      protected StringBuffer current_value = new StringBuffer();

      /** SAX producing state information **/
-    protected XMLConsumer xml_consumer;
-    protected LexicalHandler lexical_handler;
+    //protected XMLConsumer xml_consumer;
+    //protected LexicalHandler lexical_handler;

      protected ComponentSelector dbSelector = null;
      protected ComponentManager manager;
@@ -171,6 +171,7 @@
       * This will be the meat of SQLTransformer, where the query is run.
       */
      protected void executeQuery(int index) throws SAXException {
+        log.debug( "Executing query: " + index );
          this.contentHandler.startPrefixMapping("",my_uri);
          AttributesImpl attr = new AttributesImpl();
          Query query = (Query) queries.elementAt(index);
@@ -198,11 +199,14 @@
          this.contentHandler.endPrefixMapping("");
      }

-    protected static void throwIllegalStateException(String message) {
-        throw new IllegalStateException(my_name + ": "+message);
+    protected void throwIllegalStateException(String message)
+        throws ProcessingException {
+        log.debug( "Error " + message + " in SQLTRansformer " );
+        throw new ProcessingException(my_name + ": "+message);
      }

-    protected void startExecuteQueryElement() {
+    protected void startExecuteQueryElement()
+        throws ProcessingException {
          switch (current_state) {
              case SQLTransformer.STATE_OUTSIDE:
              case SQLTransformer.STATE_INSIDE_EXECUTE_QUERY_ELEMENT:
@@ -216,7 +220,8 @@
          }
      }

-    protected void startValueElement(String name) {
+    protected void startValueElement(String name)
+        throws ProcessingException {
          switch (current_state) {
              case SQLTransformer.STATE_INSIDE_EXECUTE_QUERY_ELEMENT:
                  current_name = name;
@@ -229,7 +234,8 @@
          }
      }

-    protected void startQueryElement(Attributes attributes) {
+    protected void startQueryElement(Attributes attributes)
+        throws ProcessingException {
          switch (current_state) {
              case SQLTransformer.STATE_INSIDE_EXECUTE_QUERY_ELEMENT:
                  current_value.setLength(0);
@@ -244,13 +250,14 @@
          }
      }

-    protected void endQueryElement() {
+    protected void endQueryElement()
+        throws ProcessingException {
          switch (current_state) {
              case SQLTransformer.STATE_INSIDE_QUERY_ELEMENT:
                  if (current_value.length() > 0) {
                      getCurrentQuery().addQueryPart(
                        current_value.toString());
-                    log.debug("QUERY IS \""+
+                    log.debug("ADD TO QUERY \""+
                                             current_value.toString() + 
"\"");

                      current_value.setLength(0);
@@ -262,7 +269,8 @@
          }
      }

-    protected void endValueElement() {
+    protected void endValueElement()
+        throws ProcessingException {
          switch (current_state) {
              case SQLTransformer.STATE_INSIDE_VALUE_ELEMENT:
                  getCurrentQuery().setParameter(current_name,
@@ -270,7 +278,7 @@
                  log.debug("SETTING VALUE ELEMENT name {"+
                                         current_name + "} value {"+
                                         current_value.toString() + "}");
-
+                current_value.setLength( 0 );
                  current_state = 
SQLTransformer.STATE_INSIDE_EXECUTE_QUERY_ELEMENT;
                  break;
              default:
@@ -278,7 +286,8 @@
          }
      }

-    protected void endExecuteQueryElement() throws SAXException {
+    protected void endExecuteQueryElement()
+        throws SAXException, ProcessingException {
          switch (current_state) {
              case SQLTransformer.STATE_INSIDE_EXECUTE_QUERY_ELEMENT:
                  if (current_query_index == 0) {
@@ -295,7 +304,8 @@
          }
      }

-    protected void startAncestorValueElement(Attributes attributes) {
+    protected void startAncestorValueElement(Attributes attributes)
+        throws ProcessingException {
          switch (current_state) {
              case SQLTransformer.STATE_INSIDE_QUERY_ELEMENT:
                  int level = 0;
@@ -303,25 +313,26 @@
                      level = Integer.parseInt( attributes.getValue(my_uri,
 
SQLTransformer.MAGIC_ANCESTOR_VALUE_LEVEL_ATTRIBUTE));
                  } catch (Exception e) {
-            log.debug("SQLTransformer", e);
+                    log.debug("SQLTransformer", e);
                      throwIllegalStateException("Ancestor value 
elements must have a "+
 
SQLTransformer.MAGIC_ANCESTOR_VALUE_LEVEL_ATTRIBUTE + " attribute");
                  }
                  String name = attributes.getValue(my_uri,
 
SQLTransformer.MAGIC_ANCESTOR_VALUE_NAME_ATTRIBUTE);
-                if (name == null) {
+                if (name == null ||
+                    name.trim().equals("") ) {
                      throwIllegalStateException("Ancestor value 
elements must have a "+
 
SQLTransformer.MAGIC_ANCESTOR_VALUE_NAME_ATTRIBUTE + " attribute");
                  }
                  AncestorValue av = new AncestorValue(level, name);
                  log.debug("ANCESTOR VALUE "+level + " "+name);
-
+
                  if (current_value.length() > 0) {
                      getCurrentQuery().addQueryPart(
-                      current_value.toString());
-                    log.debug("QUERY IS \""+
-                                           current_value.toString() + 
"\"");
-
+ 
current_value.toString() ); +                    log.debug("ADD TO 
QUERY: \""+
+                              current_value.toString() + "\"");
+
                      current_value.setLength(0);
                  }
                  getCurrentQuery().addQueryPart(av);
@@ -356,43 +367,52 @@
      }

      public void startElement(String uri, String name, String raw,
-                             Attributes attributes) throws SAXException {
-        if (!uri.equals(my_uri)) {
-            super.startElement(uri, name, raw, attributes);
-            return;
-        }
-        log.debug("RECEIVED START ELEMENT "+name);
-
-        if (name.equals(SQLTransformer.MAGIC_EXECUTE_QUERY)) {
-            startExecuteQueryElement();
-        } else if (name.equals(SQLTransformer.MAGIC_QUERY)) {
-            startQueryElement(attributes);
-        } else if (name.equals(SQLTransformer.MAGIC_ANCESTOR_VALUE)) {
-            startAncestorValueElement(attributes);
-        } else {
-            startValueElement(name);
+                             Attributes attributes)
+        throws SAXException {
+        try {
+            if (!uri.equals(my_uri)) {
+                super.startElement(uri, name, raw, attributes);
+                return;
+            }
+            log.debug("RECEIVED START ELEMENT "+name);
+
+            if (name.equals(SQLTransformer.MAGIC_EXECUTE_QUERY)) {
+                startExecuteQueryElement();
+            } else if (name.equals(SQLTransformer.MAGIC_QUERY)) {
+                startQueryElement(attributes);
+            } else if (name.equals(SQLTransformer.MAGIC_ANCESTOR_VALUE)) {
+                startAncestorValueElement(attributes);
+            } else {
+                startValueElement(name);
+            }
+        } catch (ProcessingException pe ) {
+            throw new SAXException( pe );
          }
      }

      public void endElement(String uri, String name,
                             String raw) throws SAXException {
-        if (!uri.equals(my_uri)) {
-            super.endElement(uri, name, raw);
-            return;
-        }
-        log.debug("RECEIVED END ELEMENT "+name + "("+uri +
-                               ")");
-
-        if (name.equals(SQLTransformer.MAGIC_EXECUTE_QUERY)) {
-            endExecuteQueryElement();
-        } else if (name.equals(SQLTransformer.MAGIC_QUERY)) {
-            endQueryElement();
-        } else if (name.equals(SQLTransformer.MAGIC_ANCESTOR_VALUE)) {
-            endAncestorValueElement();
-        } else if (name.equals(SQLTransformer.MAGIC_VALUE) || 
current_state == SQLTransformer.STATE_INSIDE_VALUE_ELEMENT) {
-            endValueElement();
-        } else {
-            super.endElement(uri, name, raw);
+        try {
+            if (!uri.equals(my_uri)) {
+                super.endElement(uri, name, raw);
+                return;
+            }
+            log.debug("RECEIVED END ELEMENT "+name + "("+uri +
+                      ")");
+
+            if (name.equals(SQLTransformer.MAGIC_EXECUTE_QUERY)) {
+                endExecuteQueryElement();
+            } else if (name.equals(SQLTransformer.MAGIC_QUERY)) {
+                endQueryElement();
+            } else if (name.equals(SQLTransformer.MAGIC_ANCESTOR_VALUE)) {
+                endAncestorValueElement();
+            } else if (name.equals(SQLTransformer.MAGIC_VALUE) || 
current_state == SQLTransformer.STATE_INSIDE_VALUE_ELEMENT) {
+                endValueElement();
+            } else {
+                super.endElement(uri, name, raw);
+            }
+        } catch (ProcessingException pe) {
+            throw new SAXException( pe );
          }
      }

@@ -401,6 +421,7 @@
          if (current_state != SQLTransformer.STATE_INSIDE_VALUE_ELEMENT &&
                  current_state != 
SQLTransformer.STATE_INSIDE_QUERY_ELEMENT) {
              super.characters(ary, start, length);
+            return;
          }
          log.debug("RECEIVED CHARACTERS: "+
                                 new String(ary, start, length));
@@ -467,11 +488,11 @@
          /** And the results' metadata **/
          protected ResultSetMetaData md = null;

-    /** If this query is actually an update (insert, update, delete) **/
-    protected boolean isupdate = false;
+        /** If this query is actually an update (insert, update, 
delete) **/
+        protected boolean isupdate = false;

-    /** If it is an update/etc, the return value (num rows modified) **/
-    protected int rv = -1;
+        /** If it is an update/etc, the return value (num rows 
modified) **/
+        protected int rv = -1;

          /** The parts of the query **/
          protected Vector query_parts = new Vector();
@@ -625,6 +646,51 @@
                  transformer.end("returncode");
                  rv = -1; // we only want the return code shown once.
          }
+        }
+
+
+        /**
+         * We group initialization here, to call it in recycle
+         */
+        public void init() {
+            /** Default parameters that might apply to all queries **/
+            //default_properties = new Properties();
+
+            /** The list of queries that we're currently working on **/
+            queries = new Vector();
+
+            /** The offset of the current query in the queries list **/
+            current_query_index = 0;
+
+            /** The name of the value element we're currently receiving **/
+            current_name = null;
+
+            /** The current state of the event receiving FSM **/
+            current_state = SQLTransformer.STATE_OUTSIDE;
+
+            /** The value of the value element we're currently receiving .
+             *  We reset it to reuse memory
+             **/
+            current_value.setLength( 0 );
+
+            /** SAX producing state information **/
+            //protected XMLConsumer xml_consumer;
+            //protected LexicalHandler lexical_handler;
+
+            dbSelector = null;
+
+        }
+
+
+        /**
+         * Recycle method for pool
+         */
+        public void recycle() {
+            //Fist we clean our state
+            init();
+
+            //Then we recycle up (if we have a Recyclable parent)
+            //super.recycle();
          }

      }


---------------------------------------------------------------------
To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org
For additional commands, email: cocoon-dev-help@xml.apache.org


Mime
View raw message