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 03:35:05 -0700 (Mon, 26 Jun 2000) $'
11
 * '$Revision: 203 $'
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

    
35
   static String doctype = null;
36
   private Connection conn = null;
37
   private int pIdCounter = 0;
38
   private long currentElementNo;
39

    
40
   /** Construct an instance of the DBEntityResolver clas
41
    *
42
    * @param conn the JDBC connection to which information is written
43
    */
44
   public DBEntityResolver(Connection conn)
45
   {
46
      this.conn = conn;
47
   }
48
   
49
   
50
   /** 
51
    * The Parser call this method before opening any external entity 
52
    * except the top-level document entity (including the external DTD subset,
53
    * external entities referenced within the DTD, and external entities 
54
    * referenced within the document element)
55
    */
56
   public InputSource resolveEntity (String publicId, String systemId)
57
                throws MalformedURLException, IOException
58
   {
59
     String dbSystemId;
60
     
61
     currentElementNo = DBSAXHandler.elementNo;
62
     
63
     if (currentElementNo == 0) {
64
        if (publicId != null) {
65
            pIdCounter += 1;
66
            doctype = publicId;
67
        } else if ( systemId != null) {
68
            doctype = DBSAXHandler.docname;
69
        }    
70
        // look at the db XML Catalog and get dbSystemId by this doctype
71
        dbSystemId = getDTDSystemID (conn, doctype);
72
        if (dbSystemId == "") {
73
            // register publicId in db and use the provided systemId
74
            if (systemId != "") {
75
                checkURLConnection(systemId);
76
                registerDTDSystemID (conn, doctype, doctype, systemId);
77
                return null;
78
            }
79
        } else {
80
            checkURLConnection(dbSystemId);
81
            return new InputSource(dbSystemId);
82
        } 
83
     }
84
    
85
     // use the default behaviour
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.10.2.2  2000/06/25 23:38:16  jones
179
 * 'Added RCSfile keyword
180
 * '
181
 * 'Revision 1.10.2.1  2000/06/25 23:34:17  jones
182
 * 'Changed documentation formatting, added log entries at bottom of source files
183
 * ''
184
 */
(5-5/19)