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
 *
9
 *   '$Author$'
10
 *     '$Date$'
11
 * '$Revision$'
12 669 jones
 *
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 522 berkley
 */
27
28
package edu.ucsb.nceas.metacat;
29
30 2608 tao
31 522 berkley
import java.util.Vector;
32
33
import org.xml.sax.Attributes;
34
import org.xml.sax.SAXException;
35
import org.xml.sax.helpers.DefaultHandler;
36
37
/**
38 561 berkley
 * A Class implementing callback bethods for the SAX parser to
39 522 berkley
 * call when processing the XML messages from the replication handler
40
 */
41
public class ReplMessageHandler extends DefaultHandler
42
{
43
  private Vector updates = new Vector();
44
  private Vector indivUpdate = new Vector();
45 554 berkley
  private Vector indivDelete = new Vector();
46 2608 tao
  private Vector indivRevision = new Vector();
47 554 berkley
  private Vector deletes = new Vector();
48 2608 tao
  private Vector revisions = new Vector();
49 532 berkley
  private String server;
50 1036 tao
  private String dataFile;
51 554 berkley
  private boolean update = false;
52
  private boolean delete = false;
53 2608 tao
  private boolean revision = false;
54 522 berkley
  String currentTag = new String();
55 1584 tao
  StringBuffer textBuffer = null;
56 522 berkley
57
  public ReplMessageHandler()
58
  {
59
  }
60
61
  /**
62
   * This method starts a new vector for each updatedDocument tag.
63
   */
64
  public void startElement(String uri, String localName, String qName,
65
                           Attributes attributes) throws SAXException
66
  {
67 1584 tao
    textBuffer = new StringBuffer();
68 522 berkley
    currentTag = localName;
69
    if(localName.equals("updatedDocument"))
70
    {
71
      indivUpdate = new Vector();
72 554 berkley
      update = true;
73 522 berkley
    }
74 554 berkley
    else if(localName.equals("deletedDocument"))
75
    {
76
      indivDelete = new Vector();
77
      delete = true;
78
    }
79 2608 tao
    else if (localName.equals("revisionDocument"))
80
    {
81
      indivRevision = new Vector();
82
      revision = true;
83
    }
84
85 522 berkley
  }
86
87
  /**
88
   * This method write the indivUpdate to updates when it finds the end of
89
   */
90
  public void endElement(String uri, String localName, String qName)
91
              throws SAXException
92
  {
93 2608 tao
    if(currentTag.equals("docid") && update)
94 522 berkley
    {
95 1584 tao
      indivUpdate.add(textBuffer.toString());
96 522 berkley
    }
97 554 berkley
    else if(currentTag.equals("docid") && delete)
98 522 berkley
    {
99 1584 tao
      indivDelete.add(textBuffer.toString());
100 554 berkley
    }
101 2608 tao
    else if (currentTag.equals("docid") && revision)
102
    {
103
      indivRevision.add(textBuffer.toString());
104
    }
105 554 berkley
106 577 berkley
    if(currentTag.equals("rev") && update)
107
    {
108 1584 tao
      indivUpdate.add(textBuffer.toString());
109 577 berkley
      indivUpdate.add(server);
110
    }
111
    else if(currentTag.equals("rev") && delete)
112
    {
113 1584 tao
      indivDelete.add(textBuffer.toString());
114 577 berkley
      indivDelete.add(server);
115
    }
116 2608 tao
    else if(currentTag.equals("rev") && revision)
117
    {
118
      indivRevision.add(textBuffer.toString());
119
      indivRevision.add(server);
120
    }
121 577 berkley
122 554 berkley
    if(currentTag.equals("date_updated") && update)
123
    {
124 1584 tao
      indivUpdate.add(textBuffer.toString());
125 532 berkley
      indivUpdate.add(server);
126 522 berkley
    }
127 554 berkley
    else if(currentTag.equals("date_updated") && delete)
128
    {
129 1584 tao
      indivDelete.add(textBuffer.toString());
130 554 berkley
      indivDelete.add(server);
131
    }
132 2608 tao
    else if(currentTag.equals("date_updated") && revision)
133
    {
134
      indivRevision.add(textBuffer.toString());
135
      indivRevision.add(server);
136
    }
137 554 berkley
138 532 berkley
    if(currentTag.equals("server"))
139
    {
140 1584 tao
      server = textBuffer.toString();
141 532 berkley
    }
142 1036 tao
143
    //Adding data file attribute into indivUpdate vector
144
    if (currentTag.equals("datafile"))
145
    {
146 1584 tao
      dataFile = textBuffer.toString();
147 2608 tao
      if (update)
148
      {
149
        indivUpdate.add(dataFile);
150
      }
151
      else if (revision)
152
      {
153
        indivRevision.add(dataFile);
154
      }
155
156
157 1036 tao
    }
158 1584 tao
159
    if(localName.equals("updatedDocument"))
160
    {
161
      updates.add(new Vector(indivUpdate));
162
      update = false;
163
    }
164
    else if(localName.equals("deletedDocument"))
165
    {
166
      deletes.add(new Vector(indivDelete));
167
      delete = false;
168
    }
169 2608 tao
    else if (localName.equals("revisionDocument"))
170
    {
171
       revisions.add(new Vector(indivRevision));
172
       revision = false;
173
    }
174 522 berkley
  }
175
176 1584 tao
  /**
177
   * Take the data out of the docid and date_updated fields
178
   */
179
  public void characters(char[] ch, int start, int length) throws SAXException
180
  {
181
    textBuffer.append(new String(ch, start,length));
182
  }
183
184 554 berkley
  public Vector getUpdatesVect()
185 522 berkley
  {
186
    return updates;
187
  }
188
189 554 berkley
  public Vector getDeletesVect()
190
  {
191
    return deletes;
192
  }
193 2608 tao
194
  public Vector getRevisionsVect()
195
  {
196
     return revisions;
197
  }
198 3897 tao
199
  /**
200
   * Gets the server name
201
   * @return
202
   */
203
  public String getServerName()
204
  {
205
	  return server;
206
  }
207 522 berkley
}