Project

General

Profile

1 72 bojilova
/**
2 203 jones
 *  '$RCSfile$'
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 72 bojilova
 *
9 203 jones
 *   '$Author$'
10
 *     '$Date$'
11
 * '$Revision$'
12 669 jones
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 72 bojilova
 */
27
28 74 jones
package edu.ucsb.nceas.metacat;
29 72 bojilova
30
import org.xml.sax.*;
31
32
import java.sql.*;
33
import java.net.URL;
34
import java.net.MalformedURLException;
35
import java.util.Stack;
36
import java.util.EmptyStackException;
37
38
/**
39 122 jones
 * A database aware Class implementing DTDHandler interface for the SAX
40
 * parser to call when processing the XML stream and intercepting notations
41
 * and unparsed entities
42 72 bojilova
 */
43
public class DBDTDHandler implements DTDHandler
44
{
45 1216 tao
   private DBConnection	connection = null;
46 72 bojilova
47
   /** Construct an instance of the DBDTDHandler clas
48
    *
49
    * @param conn the JDBC connection to which information is written
50
    */
51 1216 tao
   public DBDTDHandler(DBConnection conn)
52 72 bojilova
   {
53 1216 tao
      this.connection = conn;
54 72 bojilova
   }
55
56
   /** Notation declarations are not signaled */
57
   public void notationDecl(String name, String publicId, String systemId)
58
            throws SAXException
59
   {
60
    System.out.println("from DBDTDHandler.notationDecl");
61 92 bojilova
    System.out.print(name);
62
    System.out.print(publicId);
63
    System.out.print(systemId);
64 72 bojilova
    return;
65
   }
66
67 122 jones
   /** All are reported after startDocument and before first
68
    * startElement event
69
    */
70
   public void unparsedEntityDecl(String name, String publicId,
71
                                  String systemId, String notationName)
72 72 bojilova
            throws SAXException
73
   {
74
    System.out.println("from DBDTDHandler.unparsedEntityDecl");
75
    System.out.print(name);
76
    System.out.print(publicId);
77
    System.out.print(systemId);
78
    System.out.println(notationName);
79 204 jones
    //String doctype = DBEntityResolver.doctype;
80 92 bojilova
    //if ( getEntitySystemID(conn, doctype, publicId) == "" )
81
    //    registerEntityPublicID(conn, doctype, publicId, systemId);
82 72 bojilova
    return;
83
   }
84
85 122 jones
   /**
86
    * Look at db XML Catalog to get System ID (if any) for that Public ID
87
    * and doctype.
88
    * Return empty string if there are not
89
    */
90
/*
91 4080 daigle
 * If this ever gets uncommented, you will need to verify the
92
 * server url on the front of the system ID.
93
 *
94 122 jones
   private String getEntitySystemID (Connection conn, String doctype,
95
                                     String publicId)
96 72 bojilova
   {
97
        String system_id = "";
98
        Statement stmt;
99
        try {
100
          stmt = conn.createStatement();
101 92 bojilova
          stmt.execute("SELECT system_id FROM xml_catalog " +
102 122 jones
                       "WHERE entry_type = 'ENTITY' AND source_doctype = '" +
103
                        doctype + "' AND public_id = '" + publicId + "'");
104 72 bojilova
          try {
105
            ResultSet rs = stmt.getResultSet();
106
            try {
107
              boolean tableHasRows = rs.next();
108
              if (tableHasRows) {
109
                try {
110
                  system_id = rs.getString(1);
111
                } catch (SQLException e) {
112 122 jones
                  System.out.println("DBDTDHandler.getEntitySystemID() " +
113
                         "- Error with getString: " + e.getMessage());
114 72 bojilova
                }
115
              }
116
            } catch (SQLException e) {
117 122 jones
              System.out.println("DBDTDHandler.getEntitySystemID() " +
118
                         "- Error with next: " + e.getMessage());
119 72 bojilova
            }
120
          } catch (SQLException e) {
121 122 jones
            System.out.println("DBDTDHandler.getEntitySystemID() " +
122
                         "- Error with getrset: " + e.getMessage());
123 72 bojilova
          }
124
          stmt.close();
125
        } catch (SQLException e) {
126 122 jones
          System.out.println("DBDTDHandler.getEntitySystemID() " +
127
                         "- Error getting id: " + e.getMessage());
128 72 bojilova
        }
129
130
        // return the selected System ID
131
        return system_id;
132
   }
133 122 jones
*/
134
   /**
135
    * Register Public ID in db XML Catalog
136
    */
137
/*
138
   private void registerEntityPublicID (Connection conn, String doctype,
139
                                        String publicId, String systemId)
140 72 bojilova
   {
141
        try {
142
          PreparedStatement pstmt;
143
          pstmt = conn.prepareStatement(
144 122 jones
                "INSERT INTO xml_catalog (entity_id, entity_name, " +
145
                "entry_type, source_doctype, public_id, system_id) " +
146 72 bojilova
                "VALUES (null, null, 'ENTITY', ?, ?, ?)");
147
          // Bind the values to the query
148
          pstmt.setString(1, doctype);
149
          pstmt.setString(2, publicId);
150
          pstmt.setString(3, systemId);
151
          // Do the insertion
152
          pstmt.execute();
153
          pstmt.close();
154
        } catch (SQLException e) {
155
          System.out.println(e.getMessage());
156
        }
157
   }
158 122 jones
*/
159
160 72 bojilova
}