Project

General

Profile

1
/**
2
 *  '$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
 *    Release: @release@
9
 *
10
 *   '$Author: jones $'
11
 *     '$Date: 2001-01-18 11:52:00 -0800 (Thu, 18 Jan 2001) $'
12
 * '$Revision: 669 $'
13
 *
14
 * This program is free software; you can redistribute it and/or modify
15
 * it under the terms of the GNU General Public License as published by
16
 * the Free Software Foundation; either version 2 of the License, or
17
 * (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU General Public License
25
 * along with this program; if not, write to the Free Software
26
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27
 */
28

    
29
package edu.ucsb.nceas.metacat;
30

    
31
import org.xml.sax.*;
32

    
33
import java.sql.*;
34
import java.net.URL;
35
import java.net.MalformedURLException;
36
import java.util.Stack;
37
import java.util.EmptyStackException;
38

    
39
/** 
40
 * A database aware Class implementing DTDHandler interface for the SAX 
41
 * parser to call when processing the XML stream and intercepting notations 
42
 * and unparsed entities
43
 */
44
public class DBDTDHandler implements DTDHandler
45
{
46
   private Connection	conn = null;
47

    
48
   /** Construct an instance of the DBDTDHandler clas
49
    *
50
    * @param conn the JDBC connection to which information is written
51
    */
52
   public DBDTDHandler(Connection conn)
53
   {
54
      this.conn = conn;
55
   }
56
   
57
   /** Notation declarations are not signaled */
58
   public void notationDecl(String name, String publicId, String systemId)
59
            throws SAXException
60
   {
61
    System.out.println("from DBDTDHandler.notationDecl");
62
    System.out.print(name);
63
    System.out.print(publicId);
64
    System.out.print(systemId);
65
    return;
66
   }
67
   
68
   /** All are reported after startDocument and before first 
69
    * startElement event
70
    */
71
   public void unparsedEntityDecl(String name, String publicId, 
72
                                  String systemId, String notationName)
73
            throws SAXException
74
   {
75
    System.out.println("from DBDTDHandler.unparsedEntityDecl");
76
    System.out.print(name);
77
    System.out.print(publicId);
78
    System.out.print(systemId);
79
    System.out.println(notationName);
80
    //String doctype = DBEntityResolver.doctype;
81
    //if ( getEntitySystemID(conn, doctype, publicId) == "" ) 
82
    //    registerEntityPublicID(conn, doctype, publicId, systemId);
83
    return;
84
   }
85

    
86
   /** 
87
    * Look at db XML Catalog to get System ID (if any) for that Public ID 
88
    * and doctype.
89
    * Return empty string if there are not 
90
    */
91
/*
92
   private String getEntitySystemID (Connection conn, String doctype, 
93
                                     String publicId)
94
   {
95
        String system_id = "";
96
        Statement stmt;
97
        try {
98
          stmt = conn.createStatement();
99
          stmt.execute("SELECT system_id FROM xml_catalog " + 
100
                       "WHERE entry_type = 'ENTITY' AND source_doctype = '" + 
101
                        doctype + "' AND public_id = '" + publicId + "'");
102
          try {
103
            ResultSet rs = stmt.getResultSet();
104
            try {
105
              boolean tableHasRows = rs.next();
106
              if (tableHasRows) {
107
                try {
108
                  system_id = rs.getString(1);
109
                } catch (SQLException e) {
110
                  System.out.println("DBDTDHandler.getEntitySystemID() " +
111
                         "- Error with getString: " + e.getMessage());
112
                }
113
              }
114
            } catch (SQLException e) {
115
              System.out.println("DBDTDHandler.getEntitySystemID() " +
116
                         "- Error with next: " + e.getMessage());
117
            }
118
          } catch (SQLException e) {
119
            System.out.println("DBDTDHandler.getEntitySystemID() " +
120
                         "- Error with getrset: " + e.getMessage());
121
          }
122
          stmt.close();
123
        } catch (SQLException e) {
124
          System.out.println("DBDTDHandler.getEntitySystemID() " +
125
                         "- Error getting id: " + e.getMessage());
126
        }
127

    
128
        // return the selected System ID
129
        return system_id;
130
   }
131
*/
132
   /** 
133
    * Register Public ID in db XML Catalog 
134
    */
135
/*
136
   private void registerEntityPublicID (Connection conn, String doctype, 
137
                                        String publicId, String systemId)
138
   {
139
        try {
140
          PreparedStatement pstmt;
141
          pstmt = conn.prepareStatement(
142
                "INSERT INTO xml_catalog (entity_id, entity_name, " +
143
                "entry_type, source_doctype, public_id, system_id) " +
144
                "VALUES (null, null, 'ENTITY', ?, ?, ?)");
145
          // Bind the values to the query
146
          pstmt.setString(1, doctype);
147
          pstmt.setString(2, publicId);
148
          pstmt.setString(3, systemId);
149
          // Do the insertion
150
          pstmt.execute();
151
          pstmt.close();
152
        } catch (SQLException e) {
153
          System.out.println(e.getMessage());
154
        }
155
   }
156
*/
157

    
158
}
(12-12/43)