Project

General

Profile

Revision 99

Added by Matt Jones about 21 years ago

fixed transformation bug, added test method

View differences:

src/edu/ucsb/nceas/metacat/DBTransform.java
28 28
 */
29 29
public class DBTransform {
30 30

  
31
  static  String 	defaultDB = "jdbc:oracle:thin:@localhost:1521:test";
31 32
  private Connection	conn = null;
32 33

  
33 34
  /**
......
54 55
   */
55 56
  public void transformXMLDocument(String doc, String sourcetype, 
56 57
                String targettype, PrintWriter pw) {
57

  
58
    
58 59
    // Look up the stylesheet for this type combination
59 60
    String xsl_system_id = getSystemId("XSL", sourcetype, targettype);
60 61

  
......
73 74
    } else {
74 75
      // No stylesheet registered form this document type, so just return the 
75 76
      // XML stream we were passed
76
      pw.print(doc);
77
      pw.print(sourcetype + "\n");
78
      pw.print(targettype + "\n");
79
      pw.print(xsl_system_id + "\n");
80
      pw.print(doc + "\n");
77 81
    }
78 82
  }
79 83

  
......
90 94
    // Look up the System ID of a particular object
91 95
    PreparedStatement pstmt;
92 96
    String the_system_id = null;
93

  
94 97
    try {
95 98
      pstmt =
96 99
        conn.prepareStatement("SELECT system_id " +
......
102 105
      pstmt.setString(1, objecttype);
103 106
      pstmt.setString(2, sourcetype);
104 107
      pstmt.setString(3, targettype);
105

  
106 108
      pstmt.execute();
107 109
      try {
108 110
        ResultSet rs = pstmt.getResultSet();
......
117 119
            the_system_id = null; 
118 120
          }
119 121
        } catch (SQLException e) {
120
          //System.out.println("Error with next: " + e.getMessage());
122
          System.err.println("Error with next: " + e.getMessage());
121 123
          return ("Error with next: " + e.getMessage());
122 124
        }
123 125
      } catch (SQLException e) {
124
        //System.out.println("Error with getrset: " + e.getMessage());
126
        System.err.println("Error with getrset: " + e.getMessage());
125 127
        return ("Error with getrset: " + e.getMessage());
126 128
      }
127 129
      pstmt.close();
128 130
    } catch (SQLException e) {
129
      //System.out.println("Error getting id: " + e.getMessage());
131
      System.err.println("Error getting id: " + e.getMessage());
130 132
      return ("Error getting id: " + e.getMessage());
131 133
    }
132 134
    return the_system_id;
133 135
  }
136

  
137
  /**
138
   * the main routine used to test the transform utility.
139
   *
140
   * Usage: java transform <user> <password> [dbstring]
141
   *
142
   * @param user the username to use for the database connection
143
   * @param password the password to use for the database connection
144
   * @param dbstring the connection info to use for the database connection
145
   */
146
  static public void main(String[] args) {
147
     
148
     if (args.length < 2)
149
     {
150
        System.err.println("Wrong number of arguments!!!");
151
        System.err.println("USAGE: java transform " +
152
                           "<user> <password> [dbstring]");
153
        return;
154
     } else {
155
        try {
156
                    
157
          String user     = args[0];
158
          String password = args[1];
159
          String dbstring = null;
160

  
161
          if (args.length <= 2) {
162
            dbstring = defaultDB;
163
          } else {
164
            dbstring = args[2];
165
          }
166

  
167
          // Open a connection to the database
168
          Connection dbconn = MetaCatUtil.openDBConnection( 
169
                              "oracle.jdbc.driver.OracleDriver",
170
                              dbstring, user, password);
171

  
172
          // Create a test document
173
          StringBuffer testdoc = new StringBuffer();
174
          testdoc.append("<?xml version=\"1.0\"?>");
175
          testdoc.append("<eml-dataset><metafile_id>NCEAS-0001</metafile_id>");
176
          testdoc.append("<dataset_id>DS001</dataset_id>");
177
          testdoc.append("<title>My test doc</title></eml-dataset>");
178

  
179
          // Transform the document to the new doctype
180
          DBTransform dbt = new DBTransform(dbconn);
181
          dbt.transformXMLDocument(testdoc.toString(), 
182
                                   "-//NCEAS//eml-dataset//EN", 
183
                                   "-//W3C//HTML//EN", 
184
                                   new PrintWriter(System.out));
185

  
186
        } catch (Exception e) {
187
          System.err.println("EXCEPTION HANDLING REQUIRED");
188
          System.err.println(e.getMessage());
189
          e.printStackTrace(System.err);
190
        }
191
     }
192
  }
193
  
194
  public void dbg(int position) {
195
    System.err.println("Debug flag: " + position);
196
  }
197

  
134 198
}
DBTransform.java
28 28
 */
29 29
public class DBTransform {
30 30

  
31
  static  String 	defaultDB = "jdbc:oracle:thin:@localhost:1521:test";
31 32
  private Connection	conn = null;
32 33

  
33 34
  /**
......
54 55
   */
55 56
  public void transformXMLDocument(String doc, String sourcetype, 
56 57
                String targettype, PrintWriter pw) {
57

  
58
    
58 59
    // Look up the stylesheet for this type combination
59 60
    String xsl_system_id = getSystemId("XSL", sourcetype, targettype);
60 61

  
......
73 74
    } else {
74 75
      // No stylesheet registered form this document type, so just return the 
75 76
      // XML stream we were passed
76
      pw.print(doc);
77
      pw.print(sourcetype + "\n");
78
      pw.print(targettype + "\n");
79
      pw.print(xsl_system_id + "\n");
80
      pw.print(doc + "\n");
77 81
    }
78 82
  }
79 83

  
......
90 94
    // Look up the System ID of a particular object
91 95
    PreparedStatement pstmt;
92 96
    String the_system_id = null;
93

  
94 97
    try {
95 98
      pstmt =
96 99
        conn.prepareStatement("SELECT system_id " +
......
102 105
      pstmt.setString(1, objecttype);
103 106
      pstmt.setString(2, sourcetype);
104 107
      pstmt.setString(3, targettype);
105

  
106 108
      pstmt.execute();
107 109
      try {
108 110
        ResultSet rs = pstmt.getResultSet();
......
117 119
            the_system_id = null; 
118 120
          }
119 121
        } catch (SQLException e) {
120
          //System.out.println("Error with next: " + e.getMessage());
122
          System.err.println("Error with next: " + e.getMessage());
121 123
          return ("Error with next: " + e.getMessage());
122 124
        }
123 125
      } catch (SQLException e) {
124
        //System.out.println("Error with getrset: " + e.getMessage());
126
        System.err.println("Error with getrset: " + e.getMessage());
125 127
        return ("Error with getrset: " + e.getMessage());
126 128
      }
127 129
      pstmt.close();
128 130
    } catch (SQLException e) {
129
      //System.out.println("Error getting id: " + e.getMessage());
131
      System.err.println("Error getting id: " + e.getMessage());
130 132
      return ("Error getting id: " + e.getMessage());
131 133
    }
132 134
    return the_system_id;
133 135
  }
136

  
137
  /**
138
   * the main routine used to test the transform utility.
139
   *
140
   * Usage: java transform <user> <password> [dbstring]
141
   *
142
   * @param user the username to use for the database connection
143
   * @param password the password to use for the database connection
144
   * @param dbstring the connection info to use for the database connection
145
   */
146
  static public void main(String[] args) {
147
     
148
     if (args.length < 2)
149
     {
150
        System.err.println("Wrong number of arguments!!!");
151
        System.err.println("USAGE: java transform " +
152
                           "<user> <password> [dbstring]");
153
        return;
154
     } else {
155
        try {
156
                    
157
          String user     = args[0];
158
          String password = args[1];
159
          String dbstring = null;
160

  
161
          if (args.length <= 2) {
162
            dbstring = defaultDB;
163
          } else {
164
            dbstring = args[2];
165
          }
166

  
167
          // Open a connection to the database
168
          Connection dbconn = MetaCatUtil.openDBConnection( 
169
                              "oracle.jdbc.driver.OracleDriver",
170
                              dbstring, user, password);
171

  
172
          // Create a test document
173
          StringBuffer testdoc = new StringBuffer();
174
          testdoc.append("<?xml version=\"1.0\"?>");
175
          testdoc.append("<eml-dataset><metafile_id>NCEAS-0001</metafile_id>");
176
          testdoc.append("<dataset_id>DS001</dataset_id>");
177
          testdoc.append("<title>My test doc</title></eml-dataset>");
178

  
179
          // Transform the document to the new doctype
180
          DBTransform dbt = new DBTransform(dbconn);
181
          dbt.transformXMLDocument(testdoc.toString(), 
182
                                   "-//NCEAS//eml-dataset//EN", 
183
                                   "-//W3C//HTML//EN", 
184
                                   new PrintWriter(System.out));
185

  
186
        } catch (Exception e) {
187
          System.err.println("EXCEPTION HANDLING REQUIRED");
188
          System.err.println(e.getMessage());
189
          e.printStackTrace(System.err);
190
        }
191
     }
192
  }
193
  
194
  public void dbg(int position) {
195
    System.err.println("Debug flag: " + position);
196
  }
197

  
134 198
}
Makefile
82 82
	java -cp $(CPATH) XMLValidate \
83 83
		test.xml
84 84

  
85
trantest:
86
	java -cp $(CPATH) edu.ucsb.nceas.metacat.DBTransform \
87
		$(USER) $(PW) 
88

  
85 89
javadoc:
86 90
	javadoc -classpath $(CPATH) -d docs \
87 91
		-sourcepath ./classes edu.ucsb.nceas.metacat

Also available in: Unified diff