/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $Id: SymbolSerializer.java 518460 2007-03-15 03:47:19Z vgritsenko $
*/
package org.apache.xindice.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xindice.xml.SymbolTable;
import org.apache.xindice.xml.SymbolTableSymbols;
import org.apache.xindice.xml.dom.CompressedDocument;
import org.apache.xindice.xml.dom.DOMCompressor;
import org.apache.xindice.xml.dom.DocumentImpl;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.util.Hashtable;
/**
* SymbolSerializer is a utility class for managing SymbolTables in
* the server context of Wire Compression.
*
* @version $Revision: 518460 $, $Date: 2007-03-14 23:47:19 -0400 (Wed, 14 Mar 2007) $
*/
public final class SymbolSerializer {
private static final Log log = LogFactory.getLog(SymbolSerializer.class);
private static final SymbolTableSymbols hcSyms = SymbolTableSymbols.getInstance();
/**
* The collection's SymbolTable
*/
private final SymbolTable syms;
/**
* Last time we caught a SymbolTable modification
*/
private long lastMod;
/**
* Stores a byte representation of the SymbolTable
*/
private byte[] symBytes;
public SymbolSerializer(SymbolTable syms) {
this.syms = syms;
}
/**
* getSymBuffer returns a new Hashtable that includes a
* reference to the SymbolTable's byte array image for wire
* transmission. Depending on whether the client is up to
* date, convertFromDocument will remove this reference.
*
* @return A new Hashtable
*/
public Hashtable getSymBuffer() {
long lm = syms.getLastModified();
if (lm > lastMod) {
DocumentImpl doc = new DocumentImpl();
doc.setSymbols(hcSyms);
synchronized (syms) {
Element elem = syms.streamToXML(doc);
doc.appendChild(elem);
symBytes = DOMCompressor.compress(doc, hcSyms);
lastMod = lm;
}
}
Hashtable result = new Hashtable();
//result.put("timestamp", new Long(lm));
result.put("symbols", symBytes);
return result;
}
/**
* convertFromDocument converts a DOM Document into an
* Hashtable that, depending on the time stamp, possibly
* includes a current image of the managed Collection's Symbol
* Table.
*
* @param doc The Document to Convert
* @param stamp The client's last modified stamp
* @return The Hashtable
*/
public Hashtable convertFromDocument(Document doc, long stamp) {
Hashtable result = getSymBuffer();
byte[] docBytes = ((CompressedDocument) doc).getDataBytes();
result.put("document", docBytes);
/*if ( ((Long) result.get("timestamp")).longValue() == stamp ) {
result.put("symbols", EmptyBytes);
}*/
return result;
}
/**
* getSymbols returns the Symbol Table being managed by this
* Serializer.
*
* @return The Symbol Table
*/
public SymbolTable getSymbols() {
return syms;
}
/**
* getLastModified returns a time stamp of the last server-side
* modified of the Symbol Table. This is used to determine
* whether or not to regenerate the byte stream, and whether
* the client will need a new copy of the Symbol Table.
*
* @return Last modified stamp
*/
public long getLastModified() {
return syms.getLastModified();
}
}