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
 *
9
 *   '$Author: daigle $'
10
 *     '$Date: 2008-07-06 21:25:34 -0700 (Sun, 06 Jul 2008) $'
11
 * '$Revision: 4080 $'
12
 *
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
 */
27

    
28
package edu.ucsb.nceas.metacat;
29

    
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
 * 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
 */
43
public class DBDTDHandler implements DTDHandler
44
{
45
   private DBConnection	connection = null;
46

    
47
   /** Construct an instance of the DBDTDHandler clas
48
    *
49
    * @param conn the JDBC connection to which information is written
50
    */
51
   public DBDTDHandler(DBConnection conn)
52
   {
53
      this.connection = conn;
54
   }
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
    System.out.print(name);
62
    System.out.print(publicId);
63
    System.out.print(systemId);
64
    return;
65
   }
66
   
67
   /** 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
            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
    //String doctype = DBEntityResolver.doctype;
80
    //if ( getEntitySystemID(conn, doctype, publicId) == "" ) 
81
    //    registerEntityPublicID(conn, doctype, publicId, systemId);
82
    return;
83
   }
84

    
85
   /** 
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
 * If this ever gets uncommented, you will need to verify the 
92
 * server url on the front of the system ID.
93
 * 
94
   private String getEntitySystemID (Connection conn, String doctype, 
95
                                     String publicId)
96
   {
97
        String system_id = "";
98
        Statement stmt;
99
        try {
100
          stmt = conn.createStatement();
101
          stmt.execute("SELECT system_id FROM xml_catalog " + 
102
                       "WHERE entry_type = 'ENTITY' AND source_doctype = '" + 
103
                        doctype + "' AND public_id = '" + publicId + "'");
104
          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
                  System.out.println("DBDTDHandler.getEntitySystemID() " +
113
                         "- Error with getString: " + e.getMessage());
114
                }
115
              }
116
            } catch (SQLException e) {
117
              System.out.println("DBDTDHandler.getEntitySystemID() " +
118
                         "- Error with next: " + e.getMessage());
119
            }
120
          } catch (SQLException e) {
121
            System.out.println("DBDTDHandler.getEntitySystemID() " +
122
                         "- Error with getrset: " + e.getMessage());
123
          }
124
          stmt.close();
125
        } catch (SQLException e) {
126
          System.out.println("DBDTDHandler.getEntitySystemID() " +
127
                         "- Error getting id: " + e.getMessage());
128
        }
129

    
130
        // return the selected System ID
131
        return system_id;
132
   }
133
*/
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
   {
141
        try {
142
          PreparedStatement pstmt;
143
          pstmt = conn.prepareStatement(
144
                "INSERT INTO xml_catalog (entity_id, entity_name, " +
145
                "entry_type, source_doctype, public_id, system_id) " +
146
                "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
*/
159

    
160
}
(19-19/68)