Project

General

Profile

« Previous | Next » 

Revision 16

Added by Matt Jones over 24 years ago

Added code to write element to DB using SAX parser model

View differences:

DBSAXWriter.java
67 67
                         SQLException, 
68 68
                         ClassNotFoundException
69 69
   {
70
    
71 70
     // Open a connection to the database
72 71
     conn = openDBConnection(
73 72
                "oracle.jdbc.driver.OracleDriver",
74 73
                dbstring, user, password);
75 74

  
75
    
76 76
     //
77 77
     // Set up the SAX document handlers for parsing
78 78
     //
......
81 81

  
82 82
        // Use the XMLDocumentHandler interface for namespace support
83 83
        // instead of org.xml.sax.DocumentHandler
84
        XMLDocumentHandler xmlDocHandler = new XMLDocumentHandlerImpl();
84
        XMLDocumentHandler xmlDocHandler = new XMLDocumentHandlerImpl(conn);
85 85

  
86 86
        // For all the other interface use the default provided by
87 87
        // Handler base
......
119 119

  
120 120
   }
121 121
  
122
    private Connection openDBConnection(String dbDriver, String connection,
122
   private Connection openDBConnection(String dbDriver, String connection,
123 123
                String user, String password)
124 124
                throws SQLException, ClassNotFoundException {
125
      // Load the Oracle JDBC driver
126
      Class.forName (dbDriver);
125
     // Load the Oracle JDBC driver
126
     Class.forName (dbDriver);
127 127

  
128
      // Connect to the database
129
      Connection conn = 
130
                 DriverManager.getConnection( connection, user, password);
131
      return conn;
132
    }
128
     // Connect to the database
129
     Connection conn = DriverManager.getConnection( connection, user, password);
130
     return conn;
131
   }
133 132

  
134
 
135
  static public URL fileToURL(File file) 
133
   static public URL fileToURL(File file) 
136 134
   {
137
    String path = file.getAbsolutePath();
138
    String fSep = System.getProperty("file.separator");
139
    if (fSep != null && fSep.length() == 1)
140
      path = path.replace(fSep.charAt(0), '/');
141
    if (path.length() > 0 && path.charAt(0) != '/')
142
      path = '/' + path;
143
    try {
144
      return new URL("file", null, path);
145
    }
146
    catch (java.net.MalformedURLException e) {
147
      /* According to the spec this could only happen if the file
148
	 protocol were not recognized. */
149
      throw new Error("unexpected MalformedURLException");
150
    }
135
     String path = file.getAbsolutePath();
136
     String fSep = System.getProperty("file.separator");
137
     if (fSep != null && fSep.length() == 1)
138
       path = path.replace(fSep.charAt(0), '/');
139
     if (path.length() > 0 && path.charAt(0) != '/')
140
       path = '/' + path;
141
     try {
142
       return new URL("file", null, path);
143
     }
144
     catch (java.net.MalformedURLException e) {
145
       /* According to the spec this could only happen if the file
146
	  protocol were not recognized. */
147
       throw new Error("unexpected MalformedURLException");
148
     }
151 149
  }
152 150

  
153 151
}
154
   /***********************************************************************
155
     Implementation of XMLDocumentHandler interface. Only the new
156
     startElement and endElement interfaces are implemented here. All other
157
     interfaces are implemented in the class HandlerBase.
158
     **********************************************************************/
159 152

  
153
/****************************************************************
154
 * Implementation of XMLDocumentHandler interface.              *
155
 * Other interfaces are implemented in the class HandlerBase.   *
156
 ****************************************************************/
157

  
160 158
class XMLDocumentHandlerImpl extends DefaultXMLDocumentHandler
161 159
{
162 160

  
163
   boolean  debug = false;
164
   boolean  stackCreated = false;
165
   private  Stack 	elementStack;
161
   private boolean 	debug 	= false;
162
   private boolean 	stackCreated = false;
163
   private Stack 	elementStack;
164
   private Connection	conn = null;
166 165

  
167
   public void XMLDocumentHandlerImpl()
166
   public XMLDocumentHandlerImpl(Connection conn)
168 167
   {
168
      System.out.println("\nINITIALIZING HANDLER....\n");
169

  
170
      this.conn = conn;
171

  
172
      // Create the stack for keeping track of element context
173
      // if it doesn't already exist
174
      if (!stackCreated) {
175
        elementStack = new Stack();
176
        stackCreated = true;
177
      }
178

  
169 179
   }
170

  
171
      
180
 
172 181
   public void startElement(NSName name, SAXAttrList atts) throws SAXException 
173 182
   {
174 183

  
......
188 197
      expName = name.getExpandedName();
189 198
      
190 199
      // Create the current element representation
191
      currentElement = new DBSAXElement(1, localName, false, 0);
200
      currentElement = new DBSAXElement(conn, 1, localName, false, 0);
192 201
      System.out.println("Element created:" + currentElement.getTagName());
193 202

  
194 203
      // Add all of the attributes
195 204
      for (int i=0; i<atts.getLength(); i++)
196 205
      {
197 206

  
198
      // Use the methods getQualifiedName(), getLocalName(), getNamespace()
199
      // and getExpandedName() in SAXAttrList interface to get Namespace
200
      // information.
207
         // Use the methods getQualifiedName(), getLocalName(), getNamespace()
208
         // and getExpandedName() in SAXAttrList interface to get Namespace
209
         // information.
201 210

  
202 211
         qName = atts.getQualifiedName(i);
203 212
         localName = atts.getLocalName(i);
......
219 228

  
220 229
      }      
221 230

  
222
      // Create the stack for keeping track of element context
223
      // if it doesn't already exist
224
      if (!stackCreated) {
225
        elementStack = new Stack();
226
        stackCreated = true;
227
      }
228

  
229 231
      // Add the element to the stack, so that any text data can be 
230 232
      // added as it is encountered
231 233
      elementStack.push(currentElement);
......
260 262
      System.out.println("Closing element: " + expName);
261 263
   }
262 264

  
263
   public void db(int flag) {
265
   /** Debug routine */
266
   private void db(int flag) {
264 267
     if (debug) {
265 268
       System.err.println("DEBUG POSITION " + flag);
266 269
     }

Also available in: Unified diff