001: package ru.emdev.EmForge.wiki.providers.svnprovider;
002:
003: import java.io.IOException;
004: import java.io.UnsupportedEncodingException;
005: import java.util.Properties;
006:
007: import org.tmatesoft.svn.core.SVNException;
008: import org.tmatesoft.svn.core.io.ISVNEditor;
009: import org.tmatesoft.svn.core.io.SVNRepository;
010:
011: import ru.emdev.EmForge.svn.SvnRepositoryFactory;
012:
013: import com.ecyrd.jspwiki.InternalWikiException;
014: import com.ecyrd.jspwiki.NoRequiredPropertyException;
015: import com.ecyrd.jspwiki.TextUtil;
016: import com.ecyrd.jspwiki.WikiEngine;
017:
018: /** Based class for svn-providers (page and attachment provider)
019: *
020: */
021: public class SvnBaseProvider {
022: protected SvnRepositoryFactory m_repositoryFactory;
023: protected String m_wikiPath;
024: protected WikiEngine m_wikiEngine;
025:
026: public void setSvnRepositoryFactory(
027: SvnRepositoryFactory i_repositoryFactory) {
028: m_repositoryFactory = i_repositoryFactory;
029: }
030:
031: public void setWikiPath(String i_wikiPath) {
032: m_wikiPath = i_wikiPath;
033: }
034:
035: public void setWikiEngine(WikiEngine i_wikiEngine) {
036: m_wikiEngine = i_wikiEngine;
037: }
038:
039: protected SVNRepository getRepository() throws SVNException {
040: return m_repositoryFactory.getSvnRepository();
041: }
042:
043: public void initialize(WikiEngine engine, Properties properties)
044: throws NoRequiredPropertyException, IOException {
045: m_wikiEngine = engine;
046: }
047:
048: /** Make path in repository for specified page
049: *
050: * @param i_pageName
051: * @return
052: */
053: protected String getPagePath(String i_pageName) {
054: return m_wikiPath + "/" + mangleName(i_pageName) + ".txt";
055: }
056:
057: /** Makes path in repsoitory for specified attachment
058: *
059: * @param i_pageName
060: * @param i_attachmentName
061: * @return
062: */
063: protected String getAttachmentPath(String i_pageName,
064: String i_attachmentName) {
065: return m_wikiPath + "/" + i_pageName + "/" + i_attachmentName;
066: }
067:
068: protected String getAttachmentDir(String i_pageName) {
069: return m_wikiPath + "/" + i_pageName;
070: }
071:
072: /** Removed file from repository */
073: protected void deleteFile(String path, String message)
074: throws SVNException {
075: SVNRepository repository = getRepository();
076:
077: // creates editor
078: ISVNEditor editor = repository.getCommitEditor(message, null);
079:
080: // create new file with this contenst
081: editor.openRoot(-1);
082:
083: editor.deleteEntry(path, -1);
084:
085: editor.closeDir();
086:
087: editor.closeEdit();
088: }
089:
090: protected void deleteVersion(String path, int i_version,
091: String message) throws SVNException {
092: SVNRepository repository = getRepository();
093:
094: // creates editor
095: ISVNEditor editor = repository.getCommitEditor(
096: "SVNPageProvider::deleteVersion", null);
097:
098: // create new file with this contenst
099: editor.openRoot(i_version);
100:
101: editor.deleteEntry(path, i_version);
102:
103: editor.closeDir();
104:
105: editor.closeEdit();
106: }
107:
108: // function from AbstractFileProvider
109: protected String mangleName(String pagename) {
110: //pagename = TextUtil.urlEncode( pagename, m_encoding );
111:
112: pagename = TextUtil.replaceString(pagename, "/", "%2F");
113:
114: /*if( m_WindowsHackNeeded )
115: {
116: String pn = pagename.toLowerCase();
117: for( int i = 0; i < WINDOWS_DEVICE_NAMES.length; i++ )
118: {
119: if( WINDOWS_DEVICE_NAMES[i].equals(pn) )
120: {
121: pagename = "$$$" + pagename;
122: }
123: }
124: }*/
125:
126: return pagename;
127: }
128:
129: protected String unmangleName(String filename) {
130: // The exception should never happen.
131: try {
132: /*if( m_WindowsHackNeeded && filename.startsWith( "$$$") && filename.length() > 3 )
133: {
134: filename = filename.substring(3);
135: }*/
136:
137: String unmangledName = TextUtil
138: .urlDecode(filename, "UTF-8");
139:
140: // remove suffix .txt
141: if (unmangledName.endsWith(".txt")) {
142: unmangledName = unmangledName.substring(0,
143: unmangledName.length() - ".txt".length());
144: }
145:
146: return unmangledName;
147: } catch (UnsupportedEncodingException e) {
148: throw new InternalWikiException(
149: "Faulty encoding; should never happen");
150: }
151: }
152: }
|