📄 xmlsyncdocument.java
字号:
/**
* @(#)$RCSfile: XMLSyncDocument.java,v $ $Revision: 1.2 $
*
* ====================================================================
* Copyright 2001, Reaxion Corp.,
* 11418 105th PL NE, Kirkland, WA, 98033, USA
* All rights reserved.
* ====================================================================
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (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.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is the Tequila SyncML.
*
* The Initial Developer of the Original Code is Reaxion Corp.
* All Rights Reserved.
*/
package com.reaxion.tequila.syncml.sync;
import java.util.*;
import java.io.*;
import com.reaxion.tequila.syncml.*;
import com.reaxion.tequila.syncml.xml.*;
import com.reaxion.tequila.syncml.xml.node.*;
import com.reaxion.tequila.syncml.xml.node.xpath.XPathAPI;
import com.reaxion.tequila.syncml.util.*;
/**
* The implementation of IXMLSyncDocument
* XML document that could be synchronized should extends this class
*
* @version $1.0$
* @author Oleg A. Oksyuk
*/
public abstract class XMLSyncDocument implements IXMLSyncDocument {
private IXMLNode doc;
private String dbName;
private String fullDbName;
/**
* When add/delete/replace method called with needToSync=true this method is called
* Here we could do something like put the command to stack for latter
* synchronization
*/
protected abstract void addCommand(SyncSyncCommand command);
public XMLSyncDocument(String aPath, String aDbName) {
Log.println("XMLSyncDocument("+aPath+","+aDbName+")");
dbName = aDbName;
fullDbName = (null != aPath) ? aPath+aDbName : aDbName;
// try {
// doc = XmlIO.readXmlNodeFromFile(fullDbName);
// } catch (Exception e) {
// Log.printEx(e);
// }
}
public XMLSyncDocument(String aDbName) {
dbName = aDbName;
fullDbName = aDbName;
// try {
// doc = XmlIO.readXmlNodeFromFile(fullDbName);
// } catch (Exception e) {
// Log.printEx(e);
// }
}
public IXMLNode getDocument() {
return doc;
}
public String getDbName() {
return dbName;
}
public void save() throws IOException {
XmlIO.writeXmlNodeToFile(doc, fullDbName);
Log.println("Document saved successfully");
}
public int put(IXMLNode data, boolean necessarily) throws IOException {
byte [] bts = XmlIO.writeXmlNodeToBytes(data);
doc = XmlIO.readXmlNodeFromBytes(bts);
Log.println("Document class: "+doc.getClass().getName());
return SyncStatuses.OK;
}
public synchronized int add(SyncTarget target, Object data,
boolean necessarily, boolean needToSync) {
IXMLNodeAttr t = getTargetNode(target);
if (!(t instanceof IXMLNode)) {
throw new SyncException("Can not add attribute to attribute");
}
IXMLNode tn = (IXMLNode) t;
if (data instanceof String) {
String str = (String) data;
int nameBegin = str.indexOf("?")+"?".length();
int nameEnd = str.indexOf("=", nameBegin);
int valBegin = str.indexOf("\'", nameEnd);
int valEnd = -1;
if (valBegin < 0) {
valBegin = nameEnd+"=".length();
valEnd = str.length();
} else {
valBegin = valBegin+"\'".length();
valEnd = str.indexOf("\'", valBegin);
}
String name = str.substring(nameBegin, nameEnd);
String value = str.substring(valBegin, valEnd);
IXMLAttr attr = new XmlAttrImpl(tn, name, value);
tn.add(attr);
} else if (data instanceof IXMLNode) {
tn.add((IXMLNode) data);
} else {
throw new SyncException("Wrong dataToAdd");
}
if (needToSync) {
addCommand(new SyncAdd(target, data, !ISyncServer.COMMAND_RESP_NEEDED));
}
return SyncStatuses.OK;
}
public synchronized int delete(SyncTarget target,
boolean necessarily, boolean needToSync) {
IXMLNodeAttr t = getTargetNode(target);
t.delete();
if (needToSync) {
addCommand(new SyncDelete(target, !ISyncServer.COMMAND_RESP_NEEDED));
}
return SyncStatuses.OK;
}
public synchronized int replace(SyncTarget target, String data,
boolean necessarily, boolean needToSync) {
IXMLNodeAttr t = getTargetNode(target);
t.replace(data);
if (needToSync) {
addCommand(new SyncReplace(target, data, !ISyncServer.COMMAND_RESP_NEEDED));
}
return SyncStatuses.OK;
}
public boolean isExists() {
return (doc != null);
}
private IXMLNodeAttr getTargetNode(SyncTarget target) {
if (null == doc) {
throw new NoDocExistException();
}
return XPathAPI.selectSingleXmlNode(doc, target.getURI().getElData().toString().trim());
}
public String toString() {
return dbName;
}
/**
* All debug logs for child classes should be print through this method
*
* @param msg message to log
*/
protected void log(String msg) {
StringBuffer res = new StringBuffer(" ");
res.append(this.getClass().getName());
res.append("[");
res.append(hashCode());
res.append(",");
res.append(dbName);
res.append("].");
res.append(msg);
Log.println(res);
}
}
/* -----------------------------------------------------------------------------
* Change log:
* -----------------------------------------------------------------------------
* $Log: XMLSyncDocument.java,v $
* Revision 1.2 2001/10/17 15:27:42 OlegO
* changed comments for better javadoc
*
* Revision 1.1.1.1 2001/10/11 13:13:32 OlegO
* no message
*
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -