Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A class that handles xml messages passed by the 
4
 *             replication handler
5
 *  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: cjones $'
10
 *     '$Date: 2011-06-07 09:53:46 -0700 (Tue, 07 Jun 2011) $'
11
 * '$Revision: 6124 $'
12
 *
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
 */
27

    
28
package edu.ucsb.nceas.metacat.replication;
29

    
30

    
31
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
 * A Class implementing callback bethods for the SAX parser to
39
 * call when processing the XML messages from the replication handler
40
 */
41
public class ReplMessageHandler extends DefaultHandler 
42
{
43
  private Vector<String> indivUpdate = new Vector<String>();
44
  private Vector<Vector<String>> updates = new Vector<Vector<String>>();
45
  private Vector<String> indivSystemMetadata = new Vector<String>();
46
  private Vector<Vector<String>> systemMetadataEntries = new Vector<Vector<String>>();
47
  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
  private String server;
52
  private String dataFile;
53
  private boolean update = false;
54
  private boolean delete = false;
55
  private boolean revision = false;
56
  private boolean systemMetadata = false;
57
  String currentTag = new String();
58
  StringBuffer textBuffer = null;
59
  
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
    textBuffer = new StringBuffer();
71
    currentTag = localName;
72
    if(localName.equals("updatedDocument"))
73
    {
74
      indivUpdate = new Vector<String>();
75
      update = true;
76
    }
77
    else if(localName.equals("deletedDocument"))
78
    {
79
      indivDelete = new Vector<String>();
80
      delete = true;
81
    }
82
    else if (localName.equals("revisionDocument"))
83
    {
84
      indivRevision = new Vector<String>();
85
      revision = true;
86
    }
87
    else if (localName.equals("updatedSystemMetadata"))
88
    {
89
    	indivSystemMetadata = new Vector<String>();
90
    	systemMetadata = true;
91
    }
92
            
93
  }
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
    if(currentTag.equals("docid") && update)
102
    {
103
      indivUpdate.add(textBuffer.toString());
104
    }
105
    else if(currentTag.equals("docid") && delete)
106
    {
107
      indivDelete.add(textBuffer.toString());
108
    }
109
    else if (currentTag.equals("docid") && revision)
110
    {
111
      indivRevision.add(textBuffer.toString());
112
    }
113
    else if (currentTag.equals("guid") && systemMetadata)
114
    {
115
      indivSystemMetadata.add(textBuffer.toString());
116
      indivSystemMetadata.add(server);
117
    }
118
    
119
    if(currentTag.equals("rev") && update)
120
    {
121
      indivUpdate.add(textBuffer.toString());
122
      indivUpdate.add(server);
123
    }
124
    else if(currentTag.equals("rev") && delete)
125
    {
126
      indivDelete.add(textBuffer.toString());
127
      indivDelete.add(server);
128
    }
129
    else if(currentTag.equals("rev") && revision)
130
    {
131
      indivRevision.add(textBuffer.toString());
132
      indivRevision.add(server);
133
    }
134
    
135
    if(currentTag.equals("date_updated") && update)
136
    {
137
      indivUpdate.add(textBuffer.toString());
138
      indivUpdate.add(server);
139
    }
140
    else if(currentTag.equals("date_updated") && delete)
141
    {
142
      indivDelete.add(textBuffer.toString());
143
      indivDelete.add(server);
144
    }
145
    else if(currentTag.equals("date_updated") && revision)
146
    {
147
      indivRevision.add(textBuffer.toString());
148
      indivRevision.add(server);
149
    }
150
    
151
    if(currentTag.equals("server"))
152
    {
153
      server = textBuffer.toString();
154
    }
155
    
156
    //Adding data file attribute into indivUpdate vector
157
    if (currentTag.equals("datafile"))
158
    {
159
      dataFile = textBuffer.toString();
160
      if (update)
161
      {
162
        indivUpdate.add(dataFile);
163
      }
164
      else if (revision)
165
      {
166
        indivRevision.add(dataFile);   
167
      }
168
          
169
      
170
    }
171
    
172
    if(localName.equals("updatedDocument"))
173
    {
174
      updates.add(new Vector<String>(indivUpdate));
175
      update = false;
176
    }
177
    else if(localName.equals("deletedDocument"))
178
    {
179
      deletes.add(new Vector<String>(indivDelete));
180
      delete = false;
181
    }
182
    else if (localName.equals("revisionDocument"))
183
    {
184
       revisions.add(new Vector<String>(indivRevision));
185
       revision = false;
186
    }
187
    else if (localName.equals("updatedSystemMetadata"))
188
    {
189
       systemMetadataEntries.add(new Vector<String>(indivSystemMetadata));
190
       systemMetadata = false;
191
    }
192
  }
193
  
194
  /**
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
  public Vector<Vector<String>> getUpdatesVect()
203
  {
204
    return updates;
205
  }
206
  
207
  public Vector<Vector<String>> getDeletesVect()
208
  {
209
    return deletes;
210
  }
211
  
212
  public Vector<Vector<String>> getRevisionsVect()
213
  {
214
     return revisions;
215
  }
216
  
217
  public Vector<Vector<String>> getSystemMetadataVect()
218
  {
219
     return systemMetadataEntries;
220
  }
221
  
222
  /**
223
   * Gets the server name
224
   * @return
225
   */
226
  public String getServerName()
227
  {
228
	  return server;
229
  }
230
}
(2-2/7)