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