Project

General

Profile

1 522 berkley
/**
2
 *  '$RCSfile$'
3 669 jones
 *    Purpose: A class that handles xml messages passed by the
4
 *             replication handler
5 522 berkley
 *  Copyright: 2000 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Chad Berkley
8
 *    Release: @release@
9
 *
10
 *   '$Author$'
11
 *     '$Date$'
12
 * '$Revision$'
13 669 jones
 *
14
 * This program is free software; you can redistribute it and/or modify
15
 * it under the terms of the GNU General Public License as published by
16
 * the Free Software Foundation; either version 2 of the License, or
17
 * (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU General Public License
25
 * along with this program; if not, write to the Free Software
26
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27 522 berkley
 */
28
29
package edu.ucsb.nceas.metacat;
30
31
import java.sql.*;
32
import java.util.Stack;
33
import java.util.Vector;
34
import java.util.Enumeration;
35
import java.util.EmptyStackException;
36
37
import org.xml.sax.Attributes;
38
import org.xml.sax.SAXException;
39
import org.xml.sax.SAXParseException;
40
import org.xml.sax.ext.DeclHandler;
41
import org.xml.sax.ext.LexicalHandler;
42
import org.xml.sax.helpers.DefaultHandler;
43
44
/**
45 561 berkley
 * A Class implementing callback bethods for the SAX parser to
46 522 berkley
 * call when processing the XML messages from the replication handler
47
 */
48
public class ReplMessageHandler extends DefaultHandler
49
{
50
  private Vector updates = new Vector();
51
  private Vector indivUpdate = new Vector();
52 554 berkley
  private Vector indivDelete = new Vector();
53
  private Vector deletes = new Vector();
54 532 berkley
  private String server;
55 1036 tao
  private String dataFile;
56 554 berkley
  private boolean update = false;
57
  private boolean delete = false;
58 522 berkley
  String currentTag = new String();
59 1584 tao
  StringBuffer textBuffer = null;
60 522 berkley
61
  public ReplMessageHandler()
62
  {
63
  }
64
65
  /**
66
   * This method starts a new vector for each updatedDocument tag.
67
   */
68
  public void startElement(String uri, String localName, String qName,
69
                           Attributes attributes) throws SAXException
70
  {
71 1584 tao
    textBuffer = new StringBuffer();
72 522 berkley
    currentTag = localName;
73
    if(localName.equals("updatedDocument"))
74
    {
75
      indivUpdate = new Vector();
76 554 berkley
      update = true;
77 522 berkley
    }
78 554 berkley
    else if(localName.equals("deletedDocument"))
79
    {
80
      indivDelete = new Vector();
81
      delete = true;
82
    }
83 522 berkley
  }
84
85
  /**
86
   * This method write the indivUpdate to updates when it finds the end of
87
   */
88
  public void endElement(String uri, String localName, String qName)
89
              throws SAXException
90
  {
91 1584 tao
     if(currentTag.equals("docid") && update)
92 522 berkley
    {
93 1584 tao
      indivUpdate.add(textBuffer.toString());
94 522 berkley
    }
95 554 berkley
    else if(currentTag.equals("docid") && delete)
96 522 berkley
    {
97 1584 tao
      indivDelete.add(textBuffer.toString());
98 554 berkley
    }
99
100 577 berkley
    if(currentTag.equals("rev") && update)
101
    {
102 1584 tao
      indivUpdate.add(textBuffer.toString());
103 577 berkley
      indivUpdate.add(server);
104
    }
105
    else if(currentTag.equals("rev") && delete)
106
    {
107 1584 tao
      indivDelete.add(textBuffer.toString());
108 577 berkley
      indivDelete.add(server);
109
    }
110
111 554 berkley
    if(currentTag.equals("date_updated") && update)
112
    {
113 1584 tao
      indivUpdate.add(textBuffer.toString());
114 532 berkley
      indivUpdate.add(server);
115 522 berkley
    }
116 554 berkley
    else if(currentTag.equals("date_updated") && delete)
117
    {
118 1584 tao
      indivDelete.add(textBuffer.toString());
119 554 berkley
      indivDelete.add(server);
120
    }
121
122 532 berkley
    if(currentTag.equals("server"))
123
    {
124 1584 tao
      server = textBuffer.toString();
125 532 berkley
    }
126 1036 tao
127
    //Adding data file attribute into indivUpdate vector
128
    if (currentTag.equals("datafile"))
129
    {
130 1584 tao
      dataFile = textBuffer.toString();
131 1036 tao
      indivUpdate.add(dataFile);
132
    }
133 1584 tao
134
    if(localName.equals("updatedDocument"))
135
    {
136
      updates.add(new Vector(indivUpdate));
137
      update = false;
138
    }
139
    else if(localName.equals("deletedDocument"))
140
    {
141
      deletes.add(new Vector(indivDelete));
142
      delete = false;
143
    }
144 522 berkley
  }
145
146 1584 tao
  /**
147
   * Take the data out of the docid and date_updated fields
148
   */
149
  public void characters(char[] ch, int start, int length) throws SAXException
150
  {
151
    textBuffer.append(new String(ch, start,length));
152
  }
153
154 554 berkley
  public Vector getUpdatesVect()
155 522 berkley
  {
156
    return updates;
157
  }
158
159 554 berkley
  public Vector getDeletesVect()
160
  {
161
    return deletes;
162
  }
163 522 berkley
}