⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 versioningfileprovider.java

📁 wiki建站资源 java编写的 很好用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
         version    pagedir       olddir          none       empty         empty           1         Main.txt (1)  empty           2         Main.txt (2)  1.txt           3         Main.txt (3)  1.txt, 2.txt    */    public synchronized void putPageText( WikiPage page, String text )        throws ProviderException    {        //        //  This is a bit complicated.  We'll first need to        //  copy the old file to be the newest file.        //        File pageDir = findOldPageDir( page.getName() );        if( !pageDir.exists() )        {            pageDir.mkdirs();        }        int  latest  = findLatestVersion( page.getName() );        try        {            //            // Copy old data, if one exists.            //            File oldFile = findPage( page.getName() );            // Figure out which version should the old page be?            // Numbers should always start at 1.            // "most recent" = -1 ==> 1            // "first"       = 1  ==> 2            int versionNumber = (latest > 0) ? latest : 1;            if( oldFile != null && oldFile.exists() )            {                InputStream in = new BufferedInputStream( new FileInputStream( oldFile ) );                File pageFile = new File( pageDir, Integer.toString( versionNumber )+FILE_EXT );                OutputStream out = new BufferedOutputStream( new FileOutputStream( pageFile ) );                FileUtil.copyContents( in, out );                out.close();                in.close();                //                // We need also to set the date, since we rely on this.                //                pageFile.setLastModified( oldFile.lastModified() );                //                // Kludge to make the property code to work properly.                //                versionNumber++;            }            //            //  Let superclass handler writing data to a new version.            //            super.putPageText( page, text );            //            //  Finally, write page version data.            //            // FIXME: No rollback available.            Properties props = getPageProperties( page.getName() );            props.setProperty( versionNumber+".author", (page.getAuthor() != null) ? page.getAuthor() : "unknown" );            putPageProperties( page.getName(), props );        }        catch( IOException e )        {            log.error( "Saving failed", e );        }    }    public WikiPage getPageInfo( String page, int version )        throws ProviderException    {        int latest = findLatestVersion(page);        int realVersion;        WikiPage p = null;        if( version == WikiPageProvider.LATEST_VERSION ||            version == latest ||             (version == 1 && latest == -1) )        {            //            // Yes, we need to talk to the top level directory            // to get this version.            //            // I am listening to Press Play On Tape's guitar version of            // the good old C64 "Wizardry" -tune at this moment.            // Oh, the memories...            //            realVersion = (latest >= 0) ? latest : 1;            p = super.getPageInfo( page, WikiPageProvider.LATEST_VERSION );            if( p != null )            {                p.setVersion( realVersion );            }        }        else        {            //            //  The file is not the most recent, so we'll need to            //  find it from the deep trenches of the "OLD" directory            //  structure.            //            realVersion = version;            File dir = findOldPageDir( page );            if( !dir.exists() || !dir.isDirectory() )            {                return null;            }            File file = new File( dir, version+FILE_EXT );            if( file != null && file.exists() )            {                p = new WikiPage( page );                p.setLastModified( new Date(file.lastModified()) );                p.setVersion( version );            }        }        //        //  Get author and other metadata information        //  (Modification date has already been set.)        //        if( p != null )        {            try            {                Properties props = getPageProperties( page );                String author = props.getProperty( realVersion+".author" );                if( author != null )                {                    p.setAuthor( author );                }            }            catch( IOException e )            {                log.error( "Cannot get author for page"+page+": ", e );            }        }        return p;    }    /**     *  FIXME: Does not get user information.     */    public List getVersionHistory( String page )        throws ProviderException    {        ArrayList list = new ArrayList();        int latest = findLatestVersion( page );        // list.add( getPageInfo(page,WikiPageProvider.LATEST_VERSION) );                for( int i = latest; i > 0; i-- )        {            WikiPage info = getPageInfo( page, i );            if( info != null )            {                list.add( info );            }        }        return list;    }    /**     *  Removes the relevant page directory under "OLD" -directory as well,     *  but does not remove any extra subdirectories from it.  It will only     *  touch those files that it thinks to be WikiPages.     */    // FIXME: Should log errors.    public void deletePage( String page )        throws ProviderException    {        super.deletePage( page );        File dir = findOldPageDir( page );        if( dir.exists() && dir.isDirectory() )        {            File[] files = dir.listFiles( new WikiFileFilter() );            for( int i = 0; i < files.length; i++ )            {                files[i].delete();            }            File propfile = new File( dir, PROPERTYFILE );            if( propfile.exists() )            {                propfile.delete();            }            dir.delete();        }    }    public void deleteVersion( String page, int version )        throws ProviderException    {        File dir = findOldPageDir( page );        int latest = findLatestVersion( page );        if( version == WikiPageProvider.LATEST_VERSION ||            version == latest ||             (version == 1 && latest == -1) )        {            //            //  Delete the properties            //            try            {                Properties props = getPageProperties( page );                props.remove( ((latest > 0) ? latest : 1)+".author" );                putPageProperties( page, props );            }            catch( IOException e )            {                log.error("Unable to modify page properties",e);                throw new ProviderException("Could not modify page properties");            }            // We can let the FileSystemProvider take care            // of the actual deletion            super.deleteVersion( page, WikiPageProvider.LATEST_VERSION );                        //            //  Copy the old file to the new location            //            latest = findLatestVersion( page );                        File pageDir = findOldPageDir( page );            File previousFile = new File( pageDir, Integer.toString(latest)+FILE_EXT );            try            {                if( previousFile != null && previousFile.exists() )                {                    InputStream in = new BufferedInputStream( new FileInputStream( previousFile ) );                    File pageFile = findPage(page);                    OutputStream out = new BufferedOutputStream( new FileOutputStream( pageFile ) );                    FileUtil.copyContents( in, out );                    out.close();                    in.close();                    //                    // We need also to set the date, since we rely on this.                    //                    pageFile.setLastModified( previousFile.lastModified() );                }            }            catch( IOException e )            {                log.fatal("Something wrong with the page directory - you may have just lost data!",e);            }                                    return;        }        File pageFile = new File( dir, ""+version+FILE_EXT );        if( pageFile.exists() )        {            if( !pageFile.delete() )            {                log.error("Unable to delete page.");            }        }        else        {            throw new NoSuchVersionException("Page "+page+", version="+version);        }    }    // FIXME: This is kinda slow, we should need to do this only once.    public Collection getAllPages() throws ProviderException    {        Collection pages = super.getAllPages();        Collection returnedPages = new ArrayList();                for( Iterator i = pages.iterator(); i.hasNext(); )        {            WikiPage page = (WikiPage) i.next();                        WikiPage info = getPageInfo( page.getName(), WikiProvider.LATEST_VERSION );             returnedPages.add( info );        }                return returnedPages;    }        public String getProviderInfo()    {        return "";    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -