You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by si...@apache.org on 2011/06/02 20:36:57 UTC
svn commit: r1130712 - in
/commons/sandbox/digester3/trunk/src/examples/api/dbinsert/src/main/java/org/apache/commons/digester3/examples/api/dbinsert:
Main.java Row.java RowInserterRule.java Table.java
Author: simonetripodi
Date: Thu Jun 2 18:36:57 2011
New Revision: 1130712
URL: http://svn.apache.org/viewvc?rev=1130712&view=rev
Log:
code reformatted
Modified:
commons/sandbox/digester3/trunk/src/examples/api/dbinsert/src/main/java/org/apache/commons/digester3/examples/api/dbinsert/Main.java
commons/sandbox/digester3/trunk/src/examples/api/dbinsert/src/main/java/org/apache/commons/digester3/examples/api/dbinsert/Row.java
commons/sandbox/digester3/trunk/src/examples/api/dbinsert/src/main/java/org/apache/commons/digester3/examples/api/dbinsert/RowInserterRule.java
commons/sandbox/digester3/trunk/src/examples/api/dbinsert/src/main/java/org/apache/commons/digester3/examples/api/dbinsert/Table.java
Modified: commons/sandbox/digester3/trunk/src/examples/api/dbinsert/src/main/java/org/apache/commons/digester3/examples/api/dbinsert/Main.java
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/examples/api/dbinsert/src/main/java/org/apache/commons/digester3/examples/api/dbinsert/Main.java?rev=1130712&r1=1130711&r2=1130712&view=diff
==============================================================================
--- commons/sandbox/digester3/trunk/src/examples/api/dbinsert/src/main/java/org/apache/commons/digester3/examples/api/dbinsert/Main.java (original)
+++ commons/sandbox/digester3/trunk/src/examples/api/dbinsert/src/main/java/org/apache/commons/digester3/examples/api/dbinsert/Main.java Thu Jun 2 18:36:57 2011
@@ -39,24 +39,27 @@ import org.apache.commons.digester3.Dige
* <p>
* Usage: java Main example.xml
*/
-public class Main {
-
+public class Main
+{
+
/**
* Main method : entry point for running this example program.
* <p>
* Usage: java Main example.xml
*/
- public static void main(String[] args) {
- if (args.length != 1) {
+ public static void main( String[] args )
+ {
+ if ( args.length != 1 )
+ {
usage();
- System.exit(-1);
+ System.exit( -1 );
}
-
+
String filename = args[0];
-
+
// Create a Digester instance
Digester d = new Digester();
-
+
// Here you would establish a real connection.
// There would also be a finally clause to ensure it is
// closed after parsing terminates, etc.
@@ -64,55 +67,59 @@ public class Main {
// Add rules to the digester that will be triggered while
// parsing occurs.
- addRules(d, connection);
-
+ addRules( d, connection );
+
// Process the input file.
- System.out.println("Parsing commencing...");
- try {
- java.io.File srcfile = new java.io.File(filename);
- d.parse(srcfile);
+ System.out.println( "Parsing commencing..." );
+ try
+ {
+ java.io.File srcfile = new java.io.File( filename );
+ d.parse( srcfile );
}
- catch(java.io.IOException ioe) {
- System.out.println("Error reading input file:" + ioe.getMessage());
- System.exit(-1);
+ catch ( java.io.IOException ioe )
+ {
+ System.out.println( "Error reading input file:" + ioe.getMessage() );
+ System.exit( -1 );
}
- catch(org.xml.sax.SAXException se) {
- System.out.println("Error parsing input file:" + se.getMessage());
- System.exit(-1);
+ catch ( org.xml.sax.SAXException se )
+ {
+ System.out.println( "Error parsing input file:" + se.getMessage() );
+ System.exit( -1 );
}
-
+
// And here there is nothing to do. The digester rules have
// (deliberately) not built a representation of the input, but
// instead processed the data as it was read.
- System.out.println("Parsing complete.");
+ System.out.println( "Parsing complete." );
}
-
- private static void addRules(Digester d, java.sql.Connection conn) {
- //--------------------------------------------------
+ private static void addRules( Digester d, java.sql.Connection conn )
+ {
+
+ // --------------------------------------------------
// when we encounter a "table" tag, do the following:
// Create a new instance of class Table, and push that
// object onto the digester stack of objects. We only need
// this so that when a row is inserted, it can find out what
- // the enclosing tablename was.
+ // the enclosing tablename was.
//
- // Note that the object is popped off the stack at the end of the
- // "table" tag (normal behaviour for ObjectCreateRule). Because we
- // never added the table object to some parent object, when it is
- // popped off the digester stack it becomes garbage-collected. That
+ // Note that the object is popped off the stack at the end of the
+ // "table" tag (normal behaviour for ObjectCreateRule). Because we
+ // never added the table object to some parent object, when it is
+ // popped off the digester stack it becomes garbage-collected. That
// is fine in this situation; we've done all the necessary work and
// don't need the table object any more.
- d.addObjectCreate("database/table", Table.class);
-
+ d.addObjectCreate( "database/table", Table.class );
+
// Map *any* attributes on the table tag to appropriate
// setter-methods on the top object on the stack (the Table
// instance created by the preceeding rule). We only expect one
// attribute, though: a 'name' attribute specifying what table
// we are inserting rows into.
- d.addSetProperties("database/table");
+ d.addSetProperties( "database/table" );
- //--------------------------------------------------
+ // --------------------------------------------------
// When we encounter a "row" tag, invoke methods on the provided
// RowInserterRule instance.
//
@@ -129,19 +136,21 @@ public class Main {
// factory methods to create the rule instance; that's just a
// convenience - and obviously not an option for Rule classes
// that are not part of the digester core implementation.
- RowInserterRule rowInserterRule = new RowInserterRule(conn);
- d.addRule("database/table/row", rowInserterRule);
+ RowInserterRule rowInserterRule = new RowInserterRule( conn );
+ d.addRule( "database/table/row", rowInserterRule );
- //--------------------------------------------------
+ // --------------------------------------------------
// when we encounter a "column" tag, call setColumn on the top
// object on the stack, passing two parameters: the "name"
// attribute, and the text within the tag body.
- d.addCallMethod("database/table/row/column", "addColumn", 2);
- d.addCallParam("database/table/row/column", 0, "name");
- d.addCallParam("database/table/row/column", 1);
+ d.addCallMethod( "database/table/row/column", "addColumn", 2 );
+ d.addCallParam( "database/table/row/column", 0, "name" );
+ d.addCallParam( "database/table/row/column", 1 );
}
- private static void usage() {
- System.out.println("Usage: java Main example.xml");
+ private static void usage()
+ {
+ System.out.println( "Usage: java Main example.xml" );
}
-}
\ No newline at end of file
+
+}
Modified: commons/sandbox/digester3/trunk/src/examples/api/dbinsert/src/main/java/org/apache/commons/digester3/examples/api/dbinsert/Row.java
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/examples/api/dbinsert/src/main/java/org/apache/commons/digester3/examples/api/dbinsert/Row.java?rev=1130712&r1=1130711&r2=1130712&view=diff
==============================================================================
--- commons/sandbox/digester3/trunk/src/examples/api/dbinsert/src/main/java/org/apache/commons/digester3/examples/api/dbinsert/Row.java (original)
+++ commons/sandbox/digester3/trunk/src/examples/api/dbinsert/src/main/java/org/apache/commons/digester3/examples/api/dbinsert/Row.java Thu Jun 2 18:36:57 2011
@@ -23,45 +23,53 @@ import java.util.LinkedList;
/**
* See Main.java.
*/
-public class Row {
-
- /**
- * Alas, we can't just use a Map to store the (name, value) pairs
- * because the output will look weird if we don't preserve the column
- * order. This wouldn't be a problem if we were really inserting into
- * a database; it only matters because we are displaying the SQL statements
- * via stdout instead. The LinkedHashMap class would be nice to use, but
- * that would require java 1.4, so we'll use a list instead, and may as
- * well call the entries in the list 'Column' objects.
- */
- public static class Column {
- private String name, value;
-
- public Column(String name, String value) {
- this.name = name;
- this.value = value;
- }
-
- public String getName() {
- return name;
- }
-
- public String getValue() {
- return value;
- }
- }
-
- private LinkedList columns = new LinkedList();
-
- public Row() {
- }
-
- public void addColumn(String name, String value) {
- columns.add(new Column(name, value));
- }
-
- public List getColumns() {
- return columns;
- }
-}
+public class Row
+{
+ /**
+ * Alas, we can't just use a Map to store the (name, value) pairs
+ * because the output will look weird if we don't preserve the column
+ * order. This wouldn't be a problem if we were really inserting into
+ * a database; it only matters because we are displaying the SQL statements
+ * via stdout instead. The LinkedHashMap class would be nice to use, but
+ * that would require java 1.4, so we'll use a list instead, and may as
+ * well call the entries in the list 'Column' objects.
+ */
+ public static class Column
+ {
+ private String name, value;
+
+ public Column( String name, String value )
+ {
+ this.name = name;
+ this.value = value;
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public String getValue()
+ {
+ return value;
+ }
+ }
+
+ private LinkedList columns = new LinkedList();
+
+ public Row()
+ {
+ }
+
+ public void addColumn( String name, String value )
+ {
+ columns.add( new Column( name, value ) );
+ }
+
+ public List getColumns()
+ {
+ return columns;
+ }
+
+}
Modified: commons/sandbox/digester3/trunk/src/examples/api/dbinsert/src/main/java/org/apache/commons/digester3/examples/api/dbinsert/RowInserterRule.java
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/examples/api/dbinsert/src/main/java/org/apache/commons/digester3/examples/api/dbinsert/RowInserterRule.java?rev=1130712&r1=1130711&r2=1130712&view=diff
==============================================================================
--- commons/sandbox/digester3/trunk/src/examples/api/dbinsert/src/main/java/org/apache/commons/digester3/examples/api/dbinsert/RowInserterRule.java (original)
+++ commons/sandbox/digester3/trunk/src/examples/api/dbinsert/src/main/java/org/apache/commons/digester3/examples/api/dbinsert/RowInserterRule.java Thu Jun 2 18:36:57 2011
@@ -25,14 +25,17 @@ import org.apache.commons.digester3.Rule
/**
* See Main.java.
*/
-public class RowInserterRule extends Rule {
+public class RowInserterRule
+ extends Rule
+{
private Connection conn;
-
- public RowInserterRule(Connection conn) {
+
+ public RowInserterRule( Connection conn )
+ {
this.conn = conn;
}
-
+
/**
* This method is invoked when the start tag for an xml element representing
* a database row is encountered. It pushes a new Row instance onto the
@@ -40,10 +43,11 @@ public class RowInserterRule extends Rul
* can be stored on it.
*/
@Override
- public void begin(String namespace, String name, org.xml.sax.Attributes attrs) {
- getDigester().push(new Row());
+ public void begin( String namespace, String name, org.xml.sax.Attributes attrs )
+ {
+ getDigester().push( new Row() );
}
-
+
/**
* This method is invoked when the end tag for an xml element representing
* a database row is encountered. It pops a fully-configured Row instance
@@ -59,50 +63,52 @@ public class RowInserterRule extends Rul
* valid use of Digester.
*/
@Override
- public void end(String namespace, String name) {
+ public void end( String namespace, String name )
+ {
Row row = getDigester().pop();
Table table = getDigester().peek();
// Obviously, all this would be replaced by code like:
- // stmt = conn.prepareStatement();
- // stmt.setString(n, value);
+ // stmt = conn.prepareStatement();
+ // stmt.setString(n, value);
//
- // Many improvements can then be implemented, such as using the
+ // Many improvements can then be implemented, such as using the
// PreparedStatement.getParameterMetaData method to retrieve
// retrieve parameter types, etc.
-
+
StringBuilder colnames = new StringBuilder();
StringBuilder colvalues = new StringBuilder();
-
- for(Iterator i = row.getColumns().iterator(); i.hasNext();)
+
+ for ( Iterator i = row.getColumns().iterator(); i.hasNext(); )
{
Row.Column column = (Row.Column) i.next();
-
- if (colnames.length() > 0)
+
+ if ( colnames.length() > 0 )
{
- colnames.append(", ");
- colvalues.append(", ");
+ colnames.append( ", " );
+ colvalues.append( ", " );
}
-
- colnames.append("'");
- colnames.append(column.getName());
- colnames.append("'");
-
- colvalues.append("'");
- colvalues.append(column.getValue());
- colvalues.append("'");
+
+ colnames.append( "'" );
+ colnames.append( column.getName() );
+ colnames.append( "'" );
+
+ colvalues.append( "'" );
+ colvalues.append( column.getValue() );
+ colvalues.append( "'" );
}
StringBuilder buf = new StringBuilder();
- buf.append("insert into ");
- buf.append(table.getName());
- buf.append(" (");
- buf.append(colnames.toString());
- buf.append(") values (");
- buf.append(colvalues.toString());
- buf.append(")");
-
+ buf.append( "insert into " );
+ buf.append( table.getName() );
+ buf.append( " (" );
+ buf.append( colnames.toString() );
+ buf.append( ") values (" );
+ buf.append( colvalues.toString() );
+ buf.append( ")" );
+
// here the prepared statement would be executed....
- System.out.println(buf.toString());
+ System.out.println( buf.toString() );
}
+
}
Modified: commons/sandbox/digester3/trunk/src/examples/api/dbinsert/src/main/java/org/apache/commons/digester3/examples/api/dbinsert/Table.java
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/examples/api/dbinsert/src/main/java/org/apache/commons/digester3/examples/api/dbinsert/Table.java?rev=1130712&r1=1130711&r2=1130712&view=diff
==============================================================================
--- commons/sandbox/digester3/trunk/src/examples/api/dbinsert/src/main/java/org/apache/commons/digester3/examples/api/dbinsert/Table.java (original)
+++ commons/sandbox/digester3/trunk/src/examples/api/dbinsert/src/main/java/org/apache/commons/digester3/examples/api/dbinsert/Table.java Thu Jun 2 18:36:57 2011
@@ -20,18 +20,23 @@ package org.apache.commons.digester3.exa
/**
* See Main.java.
*/
-public class Table {
- private String name;
+public class Table
+{
- public Table() {
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getName() {
- return name;
- }
-}
+ private String name;
+ public Table()
+ {
+ }
+
+ public void setName( String name )
+ {
+ this.name = name;
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+}