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 122 jones
 * A database aware Class implementing DTDHandler interface for the SAX
24
 * parser to call when processing the XML stream and intercepting notations
25
 * and unparsed entities
26 72 bojilova
 */
27
public class DBDTDHandler implements DTDHandler
28
{
29
   private Connection	conn = null;
30
31
   /** Construct an instance of the DBDTDHandler clas
32
    *
33
    * @param conn the JDBC connection to which information is written
34
    */
35
   public DBDTDHandler(Connection conn)
36
   {
37
      this.conn = conn;
38
   }
39
40
   /** Notation declarations are not signaled */
41
   public void notationDecl(String name, String publicId, String systemId)
42
            throws SAXException
43
   {
44
    System.out.println("from DBDTDHandler.notationDecl");
45 92 bojilova
    System.out.print(name);
46
    System.out.print(publicId);
47
    System.out.print(systemId);
48 72 bojilova
    return;
49
   }
50
51 122 jones
   /** All are reported after startDocument and before first
52
    * startElement event
53
    */
54
   public void unparsedEntityDecl(String name, String publicId,
55
                                  String systemId, String notationName)
56 72 bojilova
            throws SAXException
57
   {
58
    System.out.println("from DBDTDHandler.unparsedEntityDecl");
59
    System.out.print(name);
60
    System.out.print(publicId);
61
    System.out.print(systemId);
62
    System.out.println(notationName);
63
    String doctype = DBEntityResolver.doctype;
64 92 bojilova
    //if ( getEntitySystemID(conn, doctype, publicId) == "" )
65
    //    registerEntityPublicID(conn, doctype, publicId, systemId);
66 72 bojilova
    return;
67
   }
68
69 122 jones
   /**
70
    * Look at db XML Catalog to get System ID (if any) for that Public ID
71
    * and doctype.
72
    * Return empty string if there are not
73
    */
74
/*
75
   private String getEntitySystemID (Connection conn, String doctype,
76
                                     String publicId)
77 72 bojilova
   {
78
        String system_id = "";
79
        Statement stmt;
80
        try {
81
          stmt = conn.createStatement();
82 92 bojilova
          stmt.execute("SELECT system_id FROM xml_catalog " +
83 122 jones
                       "WHERE entry_type = 'ENTITY' AND source_doctype = '" +
84
                        doctype + "' AND public_id = '" + publicId + "'");
85 72 bojilova
          try {
86
            ResultSet rs = stmt.getResultSet();
87
            try {
88
              boolean tableHasRows = rs.next();
89
              if (tableHasRows) {
90
                try {
91
                  system_id = rs.getString(1);
92
                } catch (SQLException e) {
93 122 jones
                  System.out.println("DBDTDHandler.getEntitySystemID() " +
94
                         "- Error with getString: " + e.getMessage());
95 72 bojilova
                }
96
              }
97
            } catch (SQLException e) {
98 122 jones
              System.out.println("DBDTDHandler.getEntitySystemID() " +
99
                         "- Error with next: " + e.getMessage());
100 72 bojilova
            }
101
          } catch (SQLException e) {
102 122 jones
            System.out.println("DBDTDHandler.getEntitySystemID() " +
103
                         "- Error with getrset: " + e.getMessage());
104 72 bojilova
          }
105
          stmt.close();
106
        } catch (SQLException e) {
107 122 jones
          System.out.println("DBDTDHandler.getEntitySystemID() " +
108
                         "- Error getting id: " + e.getMessage());
109 72 bojilova
        }
110
111
        // return the selected System ID
112
        return system_id;
113
   }
114 122 jones
*/
115
   /**
116
    * Register Public ID in db XML Catalog
117
    */
118
/*
119
   private void registerEntityPublicID (Connection conn, String doctype,
120
                                        String publicId, String systemId)
121 72 bojilova
   {
122
        try {
123
          PreparedStatement pstmt;
124
          pstmt = conn.prepareStatement(
125 122 jones
                "INSERT INTO xml_catalog (entity_id, entity_name, " +
126
                "entry_type, source_doctype, public_id, system_id) " +
127 72 bojilova
                "VALUES (null, null, 'ENTITY', ?, ?, ?)");
128
          // Bind the values to the query
129
          pstmt.setString(1, doctype);
130
          pstmt.setString(2, publicId);
131
          pstmt.setString(3, systemId);
132
          // Do the insertion
133
          pstmt.execute();
134
          pstmt.close();
135
        } catch (SQLException e) {
136
          System.out.println(e.getMessage());
137
        }
138
   }
139 122 jones
*/
140
141 72 bojilova
}