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
   private String getEntitySystemID (Connection conn, String doctype,
92
                                     String publicId)
93 72 bojilova
   {
94
        String system_id = "";
95
        Statement stmt;
96
        try {
97
          stmt = conn.createStatement();
98 92 bojilova
          stmt.execute("SELECT system_id FROM xml_catalog " +
99 122 jones
                       "WHERE entry_type = 'ENTITY' AND source_doctype = '" +
100
                        doctype + "' AND public_id = '" + publicId + "'");
101 72 bojilova
          try {
102
            ResultSet rs = stmt.getResultSet();
103
            try {
104
              boolean tableHasRows = rs.next();
105
              if (tableHasRows) {
106
                try {
107
                  system_id = rs.getString(1);
108
                } catch (SQLException e) {
109 122 jones
                  System.out.println("DBDTDHandler.getEntitySystemID() " +
110
                         "- Error with getString: " + e.getMessage());
111 72 bojilova
                }
112
              }
113
            } catch (SQLException e) {
114 122 jones
              System.out.println("DBDTDHandler.getEntitySystemID() " +
115
                         "- Error with next: " + e.getMessage());
116 72 bojilova
            }
117
          } catch (SQLException e) {
118 122 jones
            System.out.println("DBDTDHandler.getEntitySystemID() " +
119
                         "- Error with getrset: " + e.getMessage());
120 72 bojilova
          }
121
          stmt.close();
122
        } catch (SQLException e) {
123 122 jones
          System.out.println("DBDTDHandler.getEntitySystemID() " +
124
                         "- Error getting id: " + e.getMessage());
125 72 bojilova
        }
126
127
        // return the selected System ID
128
        return system_id;
129
   }
130 122 jones
*/
131
   /**
132
    * Register Public ID in db XML Catalog
133
    */
134
/*
135
   private void registerEntityPublicID (Connection conn, String doctype,
136
                                        String publicId, String systemId)
137 72 bojilova
   {
138
        try {
139
          PreparedStatement pstmt;
140
          pstmt = conn.prepareStatement(
141 122 jones
                "INSERT INTO xml_catalog (entity_id, entity_name, " +
142
                "entry_type, source_doctype, public_id, system_id) " +
143 72 bojilova
                "VALUES (null, null, 'ENTITY', ?, ?, ?)");
144
          // Bind the values to the query
145
          pstmt.setString(1, doctype);
146
          pstmt.setString(2, publicId);
147
          pstmt.setString(3, systemId);
148
          // Do the insertion
149
          pstmt.execute();
150
          pstmt.close();
151
        } catch (SQLException e) {
152
          System.out.println(e.getMessage());
153
        }
154
   }
155 122 jones
*/
156
157 72 bojilova
}