Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements org.xml.sax.EntityResolver 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: jones $'
10
 *     '$Date: 2000-06-26 21:31:07 -0700 (Mon, 26 Jun 2000) $'
11
 * '$Revision: 204 $'
12
 */
13

    
14
package edu.ucsb.nceas.metacat;
15

    
16
import org.xml.sax.*;
17

    
18
import java.sql.*;
19
import java.net.URL;
20
import java.net.URLConnection;
21
import java.net.MalformedURLException;
22
import java.io.IOException;
23
import java.util.Stack;
24
import java.util.EmptyStackException;
25

    
26
/** 
27
 * A database aware Class implementing EntityResolver interface for the SAX 
28
 * parser to call when processing the XML stream and intercepting any 
29
 * external entities (including the external DTD subset and external 
30
 * parameter entities, if any) before including them.
31
 */
32
public class DBEntityResolver implements EntityResolver
33
{
34
   private Connection conn = null;
35
   private DBSAXHandler handler;
36

    
37
   /** Construct an instance of the DBEntityResolver clas
38
    *
39
    * @param conn the JDBC connection to which information is written
40
    */
41
   public DBEntityResolver(Connection conn, DBSAXHandler handler)
42
   {
43
      this.conn = conn;
44
      this.handler = handler;
45
   }
46
   
47
   /** 
48
    * The Parser call this method before opening any external entity 
49
    * except the top-level document entity (including the external DTD subset,
50
    * external entities referenced within the DTD, and external entities 
51
    * referenced within the document element)
52
    */
53
   public InputSource resolveEntity (String publicId, String systemId)
54
                throws MalformedURLException, IOException
55
   {
56
     String dbSystemId;
57
     String doctype = null;
58
     
59
     // Only process entities if its the DTD
60
     if (handler.atFirstElement()) {
61
        if (publicId != null) {       // If we have a public ID, use it
62
            doctype = publicId;
63
            MetaCatUtil.debugMessage("DOCTYPE-c: " + doctype);
64
        } else if ( systemId != null) {  // Otherwise, use system id if we can
65
            doctype = handler.getDocname();
66
            MetaCatUtil.debugMessage("DOCTYPE-d: " + doctype);
67
        }    
68
        // look at the db XML Catalog and get dbSystemId for this doctype
69
        dbSystemId = getDTDSystemID (conn, doctype);
70
        if (dbSystemId == "") {
71
            // if it is not found inthe database, then
72
            // register publicId in db and use the provided systemId
73
            if (systemId != "") {
74
                checkURLConnection(systemId);
75
                registerDTDSystemID (conn, doctype, doctype, systemId);
76
                return null;
77
            }
78
        } else {
79
            // If it is in the database, return the systemid fromthe database
80
            checkURLConnection(dbSystemId);
81
            return new InputSource(dbSystemId);
82
        } 
83
     }
84
    
85
     // use the default behaviour for other cases
86
     checkURLConnection(systemId);
87
     return null;
88
   }
89

    
90
   /** 
91
    * Look at db XML Catalog to get System ID (if any) for that doctype.
92
    * Return empty string if there are not 
93
    */
94
   private String getDTDSystemID (Connection conn, String doctype)  {
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 = 'DTD' AND public_id = '" + 
101
                       doctype + "'");
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("DBEntityResolver.getDTDSystemID() " +
111
                             "- Error with getString: " + e.getMessage());
112
                }
113
              }
114
            } catch (SQLException e) {
115
              System.out.println("DBEntityResolver.getDTDSystemID() " +
116
                             "- Error with next: " + e.getMessage());
117
            }
118
          } catch (SQLException e) {
119
            System.out.println("DBEntityResolver.getDTDSystemID() " +
120
                             "- Error with getrset: " + e.getMessage());
121
          }
122
          stmt.close();
123
        } catch (SQLException e) {
124
          System.out.println("DBEntityResolver.getDTDSystemID() " +
125
                             "- Error getting id: " + e.getMessage());
126
          System.exit(1);
127
        }
128

    
129
        // return the selected System ID
130
        return system_id;
131
   }
132

    
133
   /** 
134
    * Register DTD System ID in db XML Catalog 
135
    */
136
   private void registerDTDSystemID (Connection conn, String doctype, 
137
                                     String publicId, String systemId)
138
   {
139
        try {
140
          PreparedStatement pstmt;
141
          pstmt = conn.prepareStatement(
142
                "INSERT INTO xml_catalog " +
143
                "(catalog_id, entry_type, source_doctype, " +
144
                "public_id, system_id) " +
145
                "VALUES (null, 'DTD', ?, ?, ?)");
146
          // Bind the values to the query
147
          pstmt.setString(1, doctype);
148
          pstmt.setString(2, publicId);
149
          pstmt.setString(3, systemId);
150
          // Do the insertion
151
          pstmt.execute();
152
          pstmt.close();
153
        } catch (SQLException e) {
154
          System.out.println(e.getMessage());
155
        }
156
   }
157
   /** 
158
    * Check URL Connection for systemId 
159
    */
160
   private void checkURLConnection (String systemId)
161
                throws MalformedURLException, IOException
162
   {
163
        try {
164
            URLConnection urlConn = (new URL(systemId)).openConnection();
165
            urlConn.connect();
166
        } catch (MalformedURLException e) {
167
            System.out.println("from checkURLConnection(): " + e.getMessage());
168
            throw e;
169
        } catch (IOException e) {
170
            System.out.println("from checkURLConnection(): " + e.getMessage());
171
            throw e;
172
        }    
173
   }   
174
}
175

    
176
/**
177
 * '$Log$
178
 * 'Revision 1.11  2000/06/26 10:35:04  jones
179
 * 'Merged in substantial changes to DBWriter and associated classes and to
180
 * 'the MetaCatServlet in order to accomodate the new UPDATE and DELETE
181
 * 'functions.  The command line tools and the parameters for the
182
 * 'servlet have changed substantially.
183
 * '
184
 * 'Revision 1.10.2.2  2000/06/25 23:38:16  jones
185
 * 'Added RCSfile keyword
186
 * '
187
 * 'Revision 1.10.2.1  2000/06/25 23:34:17  jones
188
 * 'Changed documentation formatting, added log entries at bottom of source files
189
 * ''
190
 */
(6-6/20)