Project

General

Profile

1 72 bojilova
/**
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$'
10
 */
11
12 74 jones
package edu.ucsb.nceas.metacat;
13 72 bojilova
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.println(name);
45
    System.out.println(publicId);
46
    System.out.println(systemId);
47
    return;
48
   }
49
50
   /** All are reported after startDocument and before 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
    Connection conn = DBEntityResolver.conn;
60
    String doctype = DBEntityResolver.doctype;
61
    if ( getEntitySystemID(conn, doctype, publicId) == "" )
62
        registerEntityPublicID(conn, doctype, publicId, systemId);
63
    return;
64
   }
65
66
   /** Look at db XML Catalog to get System ID (if any) for that Public ID and doctype.
67
     * Return empty string if there are not */
68
   private String getEntitySystemID (Connection conn, String doctype, String publicId)
69
   {
70
        String system_id = "";
71
        Statement stmt;
72
        try {
73
          stmt = conn.createStatement();
74
          stmt.execute("SELECT system_id FROM xml_catalog_entities " +
75
                       "WHERE entity_type = 'ENTITY' AND source_doctype = '" + doctype + "' AND public_id = '" + publicId + "'");
76
          try {
77
            ResultSet rs = stmt.getResultSet();
78
            try {
79
              boolean tableHasRows = rs.next();
80
              if (tableHasRows) {
81
                try {
82
                  system_id = rs.getString(1);
83
                } catch (SQLException e) {
84
                  System.out.println("DBEntityResolver.getEntitySystemID() - Error with getString: " + e.getMessage());
85
                }
86
              }
87
            } catch (SQLException e) {
88
              System.out.println("DBEntityResolver.getEntitySystemID() - Error with next: " + e.getMessage());
89
            }
90
          } catch (SQLException e) {
91
            System.out.println("DBEntityResolver.getEntitySystemID() - Error with getrset: " + e.getMessage());
92
          }
93
          stmt.close();
94
        } catch (SQLException e) {
95
          System.out.println("DBEntityResolver.getEntitySystemID() - Error getting id: " + e.getMessage());
96
        }
97
98
        // return the selected System ID
99
        return system_id;
100
   }
101
102
   /** Register Public ID in db XML Catalog */
103
   private void registerEntityPublicID (Connection conn, String doctype, String publicId, String systemId)
104
   {
105
        try {
106
          conn.setAutoCommit(false);
107
          PreparedStatement pstmt;
108
          pstmt = conn.prepareStatement(
109
                "INSERT INTO xml_catalog_entities (entity_id, entity_name, entity_type, source_doctype, public_id, system_id) " +
110
                "VALUES (null, null, 'ENTITY', ?, ?, ?)");
111
          // Bind the values to the query
112
          pstmt.setString(1, doctype);
113
          pstmt.setString(2, publicId);
114
          pstmt.setString(3, systemId);
115
          // Do the insertion
116
          pstmt.execute();
117
          pstmt.close();
118
          conn.commit();
119
          conn.setAutoCommit(true);
120
        } catch (SQLException e) {
121
          System.out.println(e.getMessage());
122
        }
123
   }
124
125
}