Project

General

Profile

1
/**
2
 *        Name: DBDTDHandler.java
3
 *     Purpose: A Class that implements org.xml.sax.DTDHandler interface
4
 *              for resolving external entities
5
 *   Copyright: 2000 Regents of the University of California and the
6
 *              National Center for Ecological Analysis and Synthesis
7
 *     Authors: Jivka Bojilova
8
 *
9
 *     Version: '$Id: DBDTDHandler.java 92 2000-05-11 23:34:09Z bojilova $'
10
 */
11

    
12
package edu.ucsb.nceas.metacat;
13

    
14
import org.xml.sax.*;
15

    
16
import java.sql.*;
17
import java.net.URL;
18
import java.net.MalformedURLException;
19
import java.util.Stack;
20
import java.util.EmptyStackException;
21

    
22
/** 
23
 * A database aware Class implementing DTDHandler interface for the SAX parser to
24
 * call when processing the XML stream and intercepting notations and unparsed entities
25
 */
26
public class DBDTDHandler implements DTDHandler
27
{
28
   private Connection	conn = null;
29

    
30
   /** Construct an instance of the DBDTDHandler clas
31
    *
32
    * @param conn the JDBC connection to which information is written
33
    */
34
   public DBDTDHandler(Connection conn)
35
   {
36
      this.conn = conn;
37
   }
38
   
39
   /** Notation declarations are not signaled */
40
   public void notationDecl(String name, String publicId, String systemId)
41
            throws SAXException
42
   {
43
    System.out.println("from DBDTDHandler.notationDecl");
44
    System.out.print(name);
45
    System.out.print(publicId);
46
    System.out.print(systemId);
47
    return;
48
   }
49
   
50
   /** All are reported after startDocument and before first startElement event*/
51
   public void unparsedEntityDecl(String name, String publicId, String systemId, String notationName)
52
            throws SAXException
53
   {
54
    System.out.println("from DBDTDHandler.unparsedEntityDecl");
55
    System.out.print(name);
56
    System.out.print(publicId);
57
    System.out.print(systemId);
58
    System.out.println(notationName);
59
    String doctype = DBEntityResolver.doctype;
60
    //if ( getEntitySystemID(conn, doctype, publicId) == "" ) 
61
    //    registerEntityPublicID(conn, doctype, publicId, systemId);
62
    return;
63
   }
64

    
65
   /** Look at db XML Catalog to get System ID (if any) for that Public ID and doctype.
66
     * Return empty string if there are not */
67
   private String getEntitySystemID (Connection conn, String doctype, String publicId)
68
   {
69
        String system_id = "";
70
        Statement stmt;
71
        try {
72
          stmt = conn.createStatement();
73
          stmt.execute("SELECT system_id FROM xml_catalog " + 
74
                       "WHERE entity_type = 'ENTITY' AND source_doctype = '" + doctype + "' AND public_id = '" + publicId + "'");
75
          try {
76
            ResultSet rs = stmt.getResultSet();
77
            try {
78
              boolean tableHasRows = rs.next();
79
              if (tableHasRows) {
80
                try {
81
                  system_id = rs.getString(1);
82
                } catch (SQLException e) {
83
                  System.out.println("DBDTDHandler.getEntitySystemID() - Error with getString: " + e.getMessage());
84
                }
85
              }
86
            } catch (SQLException e) {
87
              System.out.println("DBDTDHandler.getEntitySystemID() - Error with next: " + e.getMessage());
88
            }
89
          } catch (SQLException e) {
90
            System.out.println("DBDTDHandler.getEntitySystemID() - Error with getrset: " + e.getMessage());
91
          }
92
          stmt.close();
93
        } catch (SQLException e) {
94
          System.out.println("DBDTDHandler.getEntitySystemID() - Error getting id: " + e.getMessage());
95
        }
96

    
97
        // return the selected System ID
98
        return system_id;
99
   }
100

    
101
   /** Register Public ID in db XML Catalog */
102
   private void registerEntityPublicID (Connection conn, String doctype, String publicId, String systemId)
103
   {
104
        try {
105
          conn.setAutoCommit(false);
106
          PreparedStatement pstmt;
107
          pstmt = conn.prepareStatement(
108
                "INSERT INTO xml_catalog (entity_id, entity_name, entity_type, source_doctype, public_id, system_id) " +
109
                "VALUES (null, null, 'ENTITY', ?, ?, ?)");
110
          // Bind the values to the query
111
          pstmt.setString(1, doctype);
112
          pstmt.setString(2, publicId);
113
          pstmt.setString(3, systemId);
114
          // Do the insertion
115
          pstmt.execute();
116
          pstmt.close();
117
          conn.commit();
118
          conn.setAutoCommit(true);
119
        } catch (SQLException e) {
120
          System.out.println(e.getMessage());
121
        }
122
   }
123
   
124
}
(2-2/30)