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 5014 daigle
package edu.ucsb.nceas.metacat.replication;
29 522 berkley
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 5014 daigle
  private Vector<String> indivUpdate = new Vector<String>();
44
  private Vector<Vector<String>> updates = new Vector<Vector<String>>();
45 6118 leinfelder
  private Vector<String> indivSystemMetadata = new Vector<String>();
46
  private Vector<Vector<String>> systemMetadataEntries = new Vector<Vector<String>>();
47 5014 daigle
  private Vector<String> indivDelete = new Vector<String>();
48
  private Vector<Vector<String>> deletes = new Vector<Vector<String>>();
49
  private Vector<String> indivRevision = new Vector<String>();
50
  private Vector<Vector<String>> revisions = new Vector<Vector<String>>();
51 532 berkley
  private String server;
52 1036 tao
  private String dataFile;
53 554 berkley
  private boolean update = false;
54
  private boolean delete = false;
55 2608 tao
  private boolean revision = false;
56 6118 leinfelder
  private boolean systemMetadata = false;
57 522 berkley
  String currentTag = new String();
58 1584 tao
  StringBuffer textBuffer = null;
59 522 berkley
60
  public ReplMessageHandler()
61
  {
62
  }
63
64
  /**
65
   * This method starts a new vector for each updatedDocument tag.
66
   */
67
  public void startElement(String uri, String localName, String qName,
68
                           Attributes attributes) throws SAXException
69
  {
70 1584 tao
    textBuffer = new StringBuffer();
71 522 berkley
    currentTag = localName;
72
    if(localName.equals("updatedDocument"))
73
    {
74 5014 daigle
      indivUpdate = new Vector<String>();
75 554 berkley
      update = true;
76 522 berkley
    }
77 554 berkley
    else if(localName.equals("deletedDocument"))
78
    {
79 5014 daigle
      indivDelete = new Vector<String>();
80 554 berkley
      delete = true;
81
    }
82 2608 tao
    else if (localName.equals("revisionDocument"))
83
    {
84 5014 daigle
      indivRevision = new Vector<String>();
85 2608 tao
      revision = true;
86
    }
87 6118 leinfelder
    else if (localName.equals("updatedSystemMetadata"))
88
    {
89
    	indivSystemMetadata = new Vector<String>();
90
    	systemMetadata = true;
91
    }
92 2608 tao
93 522 berkley
  }
94
95
  /**
96
   * This method write the indivUpdate to updates when it finds the end of
97
   */
98
  public void endElement(String uri, String localName, String qName)
99
              throws SAXException
100
  {
101 2608 tao
    if(currentTag.equals("docid") && update)
102 522 berkley
    {
103 1584 tao
      indivUpdate.add(textBuffer.toString());
104 522 berkley
    }
105 554 berkley
    else if(currentTag.equals("docid") && delete)
106 522 berkley
    {
107 1584 tao
      indivDelete.add(textBuffer.toString());
108 554 berkley
    }
109 2608 tao
    else if (currentTag.equals("docid") && revision)
110
    {
111
      indivRevision.add(textBuffer.toString());
112
    }
113 6118 leinfelder
    else if (currentTag.equals("guid") && systemMetadata)
114
    {
115
      indivSystemMetadata.add(textBuffer.toString());
116
      indivSystemMetadata.add(server);
117
    }
118 554 berkley
119 577 berkley
    if(currentTag.equals("rev") && update)
120
    {
121 1584 tao
      indivUpdate.add(textBuffer.toString());
122 577 berkley
      indivUpdate.add(server);
123
    }
124
    else if(currentTag.equals("rev") && delete)
125
    {
126 1584 tao
      indivDelete.add(textBuffer.toString());
127 577 berkley
      indivDelete.add(server);
128
    }
129 2608 tao
    else if(currentTag.equals("rev") && revision)
130
    {
131
      indivRevision.add(textBuffer.toString());
132
      indivRevision.add(server);
133
    }
134 577 berkley
135 554 berkley
    if(currentTag.equals("date_updated") && update)
136
    {
137 1584 tao
      indivUpdate.add(textBuffer.toString());
138 532 berkley
      indivUpdate.add(server);
139 522 berkley
    }
140 554 berkley
    else if(currentTag.equals("date_updated") && delete)
141
    {
142 1584 tao
      indivDelete.add(textBuffer.toString());
143 554 berkley
      indivDelete.add(server);
144
    }
145 2608 tao
    else if(currentTag.equals("date_updated") && revision)
146
    {
147
      indivRevision.add(textBuffer.toString());
148
      indivRevision.add(server);
149
    }
150 554 berkley
151 532 berkley
    if(currentTag.equals("server"))
152
    {
153 1584 tao
      server = textBuffer.toString();
154 532 berkley
    }
155 1036 tao
156
    //Adding data file attribute into indivUpdate vector
157
    if (currentTag.equals("datafile"))
158
    {
159 1584 tao
      dataFile = textBuffer.toString();
160 2608 tao
      if (update)
161
      {
162
        indivUpdate.add(dataFile);
163
      }
164
      else if (revision)
165
      {
166
        indivRevision.add(dataFile);
167
      }
168
169
170 1036 tao
    }
171 1584 tao
172
    if(localName.equals("updatedDocument"))
173
    {
174 5014 daigle
      updates.add(new Vector<String>(indivUpdate));
175 1584 tao
      update = false;
176
    }
177
    else if(localName.equals("deletedDocument"))
178
    {
179 5014 daigle
      deletes.add(new Vector<String>(indivDelete));
180 1584 tao
      delete = false;
181
    }
182 2608 tao
    else if (localName.equals("revisionDocument"))
183
    {
184 5014 daigle
       revisions.add(new Vector<String>(indivRevision));
185 2608 tao
       revision = false;
186
    }
187 6118 leinfelder
    else if (localName.equals("updatedSystemMetadata"))
188
    {
189
       systemMetadataEntries.add(new Vector<String>(indivSystemMetadata));
190
       systemMetadata = false;
191
    }
192 522 berkley
  }
193
194 1584 tao
  /**
195
   * Take the data out of the docid and date_updated fields
196
   */
197
  public void characters(char[] ch, int start, int length) throws SAXException
198
  {
199
    textBuffer.append(new String(ch, start,length));
200
  }
201
202 5014 daigle
  public Vector<Vector<String>> getUpdatesVect()
203 522 berkley
  {
204
    return updates;
205
  }
206
207 5014 daigle
  public Vector<Vector<String>> getDeletesVect()
208 554 berkley
  {
209
    return deletes;
210
  }
211 2608 tao
212 5014 daigle
  public Vector<Vector<String>> getRevisionsVect()
213 2608 tao
  {
214
     return revisions;
215
  }
216 3897 tao
217 6118 leinfelder
  public Vector<Vector<String>> getSystemMetadataVect()
218
  {
219
     return systemMetadataEntries;
220
  }
221
222 3897 tao
  /**
223
   * Gets the server name
224
   * @return
225
   */
226
  public String getServerName()
227
  {
228
	  return server;
229
  }
230 522 berkley
}