1 4 package com.openedit.modules.admin.filemanager; 5 6 import java.io.File ; 7 import java.io.Writer ; 8 import java.util.regex.Matcher ; 9 import java.util.regex.Pattern ; 10 11 import org.openedit.repository.ContentItem; 12 import org.openedit.repository.filesystem.StringItem; 13 14 import com.openedit.OpenEditRuntimeException; 15 import com.openedit.page.Page; 16 import com.openedit.page.manage.PageManager; 17 import com.openedit.util.PathUtilities; 18 import com.openedit.util.URLUtilities; 19 20 25 public class Replacer 26 { 27 protected File fieldRootDirectory; 28 protected String fieldFileTypes = "*.html,*.htm"; 29 protected String fieldFindText; 30 protected String fieldReplaceText; 31 protected String fieldSearchPath; 32 protected int fieldNumFilesChanged = 0; 33 protected PageManager fieldPageManager; 34 35 public Replacer() 36 { 37 } 38 39 protected boolean fileMatchesFileTypes( File inFile ) 40 { 41 String fileName = inFile.getName(); 42 String [] fileTypes = getFileTypes().split( "," ); 43 for (int n = 0; n < fileTypes.length; n++) 44 { 45 if ( PathUtilities.match(fileName, fileTypes[n] ) ) 46 { 47 return true; 48 } 49 } 50 return false; 51 } 52 53 protected boolean isFileIncludedInDirectory( File inFile, File inDirectory ) 54 { 55 String filePath = inFile.getAbsolutePath(); 56 String dirPath = inDirectory.getAbsolutePath(); 57 return filePath.startsWith( dirPath ); 58 } 59 60 protected boolean isDirectoryIncluded( File inFile ) 61 { 62 if( inFile.getAbsolutePath().indexOf(".versions") > -1 ) 63 { 64 return false; 65 } 66 File siteDir = getRootDirectory(); 67 if ( !isFileIncludedInDirectory( inFile, siteDir ) ) 68 { 69 return false; 70 } 71 File openEditDir = new File ( siteDir, "openedit" ); 72 if ( isFileIncludedInDirectory( inFile, openEditDir ) ) 73 { 74 return false; 75 } 76 openEditDir = new File ( siteDir, "base" ); 77 if ( isFileIncludedInDirectory( inFile, openEditDir ) ) 78 { 79 return false; 80 } 81 File webInfDir = new File ( siteDir, "WEB-INF" ); 82 if ( isFileIncludedInDirectory( inFile, webInfDir ) ) 83 { 84 return false; 85 } 86 File logsDir = new File ( siteDir, "logs" ); 87 if ( isFileIncludedInDirectory( inFile, logsDir ) ) 88 { 89 return false; 90 } 91 return true; 92 } 93 94 protected void replaceTextInFile( File inFile ) throws Exception 95 { 96 Writer writer = null; 97 try 98 { 99 100 String path = URLUtilities.getPathWithoutContext(getRootDirectory().getAbsolutePath(),inFile.getAbsolutePath(), "index.html"); Page page = getPageManager().getPage(path); 102 103 if ( getSearchPath() != null ) 105 { 106 if ( !path.toLowerCase().startsWith( getSearchPath().toLowerCase() ) ) 110 { 111 return; 112 } 113 } 114 115 if ( page.exists() && !page.isBinary() ) 116 { 117 String content = page.getContent(); 118 Pattern pat = Pattern.compile(getFindText() ); Matcher mat = pat.matcher(content); 120 String newcontent = mat.replaceAll(getReplaceText()); 121 122 if ( !content.equals( newcontent) ) 123 { 124 ContentItem revision = new StringItem(page.getPath(), newcontent,page.getCharacterEncoding() ); 125 revision.setType( ContentItem.TYPE_EDITED ); 126 revision.setMessage( "Replaced text \"" + getFindText() +"\" with \"" + getReplaceText() +"\"."); 127 page.setContentItem(revision); 128 getPageManager().putPage( page ); 129 setNumFilesChanged( getNumFilesChanged() + 1 ); 130 } 131 } 132 } 133 finally 134 { 135 if ( writer != null ) 136 { 137 writer.close(); 138 } 139 } 140 } 141 142 protected void replaceAllInDirectory( File inDirectory ) throws Exception 143 { 144 if ( inDirectory.isDirectory()) 145 { 146 if ( isDirectoryIncluded( inDirectory ) ) 147 { 148 File [] filesInDirectory = inDirectory.listFiles(); 149 if( filesInDirectory != null) 150 { 151 for ( int n = 0; n < filesInDirectory.length; n++ ) 152 { 153 replaceAllInDirectory( filesInDirectory[n] ); 154 } 155 } 156 } 157 } 158 else if ( fileMatchesFileTypes( inDirectory ) ) 159 { 160 replaceTextInFile( inDirectory ); 161 } 162 } 163 public void replaceAll() throws Exception 164 { 165 setNumFilesChanged(0); 166 if ( getSearchPath() == null) 167 { 168 throw new OpenEditRuntimeException("No search path set"); 169 } 170 171 File path = new File ( getRootDirectory(), getSearchPath()); 172 173 if ( isDirectoryIncluded( path ) ) 174 { 175 replaceAllInDirectory( path ); 176 } 177 } 178 179 public String getFileTypes() 180 { 181 return fieldFileTypes; 182 } 183 public void setFileTypes(String inFileTypes) 184 { 185 fieldFileTypes = inFileTypes; 186 } 187 public String getFindText() 188 { 189 return fieldFindText; 190 } 191 public void setFindText(String inFindText) 192 { 193 fieldFindText = inFindText; 194 } 195 public String getReplaceText() 196 { 197 return fieldReplaceText; 198 } 199 public void setReplaceText(String inReplaceText) 200 { 201 fieldReplaceText = inReplaceText; 202 } 203 public File getRootDirectory() 204 { 205 return fieldRootDirectory; 206 } 207 public void setRootDirectory(File inRootDirectory) 208 { 209 fieldRootDirectory = inRootDirectory; 210 } 211 public int getNumFilesChanged() 212 { 213 return fieldNumFilesChanged; 214 } 215 public void setNumFilesChanged(int inNumFilesChanged) 216 { 217 fieldNumFilesChanged = inNumFilesChanged; 218 } 219 public String getSearchPath() 220 { 221 return fieldSearchPath; 222 } 223 public void setSearchPath(String inSearchPath) 224 { 225 fieldSearchPath = inSearchPath; 226 } 227 public PageManager getPageManager() 228 { 229 return fieldPageManager; 230 } 231 public void setPageManager( PageManager pageManager ) 232 { 233 fieldPageManager = pageManager; 234 } 235 } 236 | Popular Tags |