📄 xwikircsfilestore.java
字号:
/**
* ===================================================================
*
* Copyright (c) 2003 Ludovic Dubost, All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details, published at
* http://www.gnu.org/copyleft/gpl.html or in gpl.txt in the
* root folder of this distribution.
*
* Created by
* User: Ludovic Dubost
* Date: 24 nov. 2003
* Time: 01:00:44
*/
package com.xpn.xwiki.store;
import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiAttachment;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.doc.XWikiLock;
import com.xpn.xwiki.util.Util;
import org.apache.commons.jrcs.rcs.Archive;
import org.apache.commons.jrcs.rcs.Lines;
import org.apache.commons.jrcs.rcs.Node;
import org.apache.commons.jrcs.rcs.Version;
import java.io.*;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;
public class XWikiRCSFileStore extends XWikiDefaultStore {
private File rcspath;
private File rcsattachmentpath;
public XWikiRCSFileStore() {
}
public XWikiRCSFileStore(XWiki xwiki, XWikiContext context) {
String rcspath = xwiki.ParamAsRealPath("xwiki.store.rcs.path", context);
setPath(rcspath);
String rcsattachementpath = xwiki.ParamAsRealPath("xwiki.store.rcs.attachmentpath", context);
setAttachmentPath(rcsattachementpath);
}
public XWikiRCSFileStore(String rcspath, String rcsattachmentpath) {
setPath(rcspath);
setAttachmentPath(rcsattachmentpath);
}
public void setPath(String rcspath) {
this.rcspath = new File(rcspath);
}
public String getPath() {
return rcspath.toString();
}
public void setAttachmentPath(String rcsattachmentpath) {
this.rcsattachmentpath = new File(rcsattachmentpath);
}
public String getAttachmentPath() {
return rcsattachmentpath.toString();
}
public File getFilePath(XWikiDocument doc, XWikiContext context) {
File webdir = new File(getPath(), doc.getWeb().replace('.','/'));
webdir.mkdirs();
return new File(webdir, doc.getName() + ".txt");
}
public File getVersionedFilePath(XWikiDocument doc, XWikiContext context) {
File webfile = new File(getPath(), doc.getWeb().replace('.','/'));
return new File(webfile, doc.getName() + ".txt,v");
}
public File getAttachmentPath(XWikiAttachment attachment, XWikiContext context) {
File webdir = new File(getAttachmentPath(), attachment.getDoc().getWeb().replace('.','/')
+ "/" + attachment.getDoc().getName());
webdir.mkdirs();
return new File(webdir, attachment.getFilename());
}
public File getVersionedAttachmentPath(XWikiAttachment attachment, XWikiContext context) {
File webdir = new File(getAttachmentPath(), attachment.getDoc().getWeb().replace('.','/')
+ "/" + attachment.getDoc().getName());
webdir.mkdirs();
return new File(webdir, attachment.getFilename() + ",v");
}
public void saveXWikiDoc(XWikiDocument doc, XWikiContext context) throws XWikiException {
//To change body of implemented methods use Options | File Templates.
try {
doc.setStore(this);
// Handle the latest text file
if (doc.isContentDirty()||doc.isMetaDataDirty()) {
doc.setDate(new Date());
doc.incrementVersion();
}
File file = getFilePath(doc, context);
FileWriter wr = new FileWriter(file);
wr.write(getFullContent(doc, context));
wr.flush();
wr.close();
// Now handle the versioned file
if (doc.isContentDirty()||doc.isMetaDataDirty()) {
File vfile = getVersionedFilePath(doc, context);
Archive archive;
Lines lines = new Lines(getFullContent(doc, context));
try {
// Let's try to read the archive
archive = new Archive(vfile.toString());
archive.addRevision(lines.toArray(),"");
} catch (FileNotFoundException e) {
// If we cannot find the file let's create a new archive
archive = new Archive(lines.toArray(),doc.getFullName(),doc.getVersion());
}
// Save back the archive
doc.setRCSArchive(archive);
doc.getRCSArchive().save(vfile.toString());
}
doc.setMostRecent(true);
doc.setNew(false);
} catch (Exception e) {
Object[] args = { doc.getFullName() };
throw new XWikiException( XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_RCS_SAVING_FILE,
"Exception while saving document {0}", e, args);
}
}
public void saveXWikiDoc(XWikiDocument doc, XWikiContext context, boolean bTransaction) throws XWikiException {
saveXWikiDoc(doc, context);
}
public XWikiDocument loadXWikiDoc(XWikiDocument doc, XWikiContext context) throws XWikiException {
//To change body of implemented methods use Options | File Templates.
BufferedReader fr = null;
try {
doc.setStore(this);
File file = getFilePath(doc, context);
if (!file.exists()) {
doc.setNew(true);
return doc;
}
doc.setNew(false);
StringBuffer content = new StringBuffer();
fr = new BufferedReader(new FileReader(file));
String line;
boolean bMetaDataDone = false;
boolean bisXML = false;
line = fr.readLine();
if (line.startsWith("<"))
bisXML = true;
if (bisXML) {
while (true) {
if (line==null) {
fr.close();
doc.fromXML(content.toString());
break;
}
content.append(line);
content.append("\n");
line = fr.readLine();
}
} else
{
while (true) {
if (line==null) {
fr.close();
doc.setContent(content.toString());
doc.setMostRecent(true);
break;
}
if (bMetaDataDone||(parseMetaData(doc,line)==false)) {
content.append(line);
content.append("\n");
}
line = fr.readLine();
}
}
} catch (Exception e) {
Object[] args = { doc.getFullName() };
throw new XWikiException( XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_RCS_READING_FILE,
"Exception while reading document {0}", e, args);
}
return doc;
}
public XWikiDocument loadXWikiDoc(XWikiDocument basedoc,String version, XWikiContext context) throws XWikiException {
XWikiDocument doc = new XWikiDocument(basedoc.getWeb(), basedoc.getName());
try {
doc.setStore(this);
Archive archive = basedoc.getRCSArchive();
if (archive==null) {
File file = getVersionedFilePath(doc, context);
String path = file.toString();
synchronized (path) {
archive = new Archive(path);
}
}
basedoc.setRCSArchive(archive);
Object[] text = (Object[]) archive.getRevision(version);
if (text[0].toString().startsWith("<")) {
StringBuffer content = new StringBuffer();
for (int i=0;i<text.length;i++) {
String line = text[i].toString();
content.append(line);
content.append("\n");
}
doc.fromXML(content.toString());
} else {
StringBuffer content = new StringBuffer();
boolean bMetaDataDone = false;
for (int i=0;i<text.length;i++) {
String line = text[i].toString();
if (bMetaDataDone||(parseMetaData(doc,line)==false)) {
content.append(line);
content.append("\n");
}
doc.setContent(content.toString());
}
// Make sure the document has the same name
// as the new document (in case there was a name change
doc.setName(basedoc.getName());
doc.setWeb(basedoc.getWeb());
}
} catch (Exception e) {
Object[] args = { doc.getFullName(), version.toString() };
throw new XWikiException( XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_RCS_READING_VERSION,
"Exception while reading document {0} version {1}", e, args);
}
return doc;
}
public void deleteXWikiDoc(XWikiDocument doc, XWikiContext context) throws XWikiException {
try {
doc.setStore(this);
File file = getFilePath(doc, context);
List attachlist = doc.getAttachmentList();
for (int i=0;i<attachlist.size();i++) {
XWikiAttachment attachment = (XWikiAttachment) attachlist.get(i);
deleteXWikiAttachment(attachment, context, false);
}
file.delete();
} catch (Exception e) {
Object[] args = { doc.getFullName()};
throw new XWikiException( XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_RCS_DELETING_FILE,
"Exception while deleting document {0} ", e, args);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -