KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > modules > admin > filemanager > Replacer


1 /*
2  * Created on Nov 17, 2004
3  */

4 package com.openedit.modules.admin.filemanager;
5
6 import java.io.File JavaDoc;
7 import java.io.Writer JavaDoc;
8 import java.util.regex.Matcher JavaDoc;
9 import java.util.regex.Pattern JavaDoc;
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 /**
21  * In eclipse userdata=".*?" finds all attributes named userdata
22  * @author dbrown
23  *
24  */

25 public class Replacer
26 {
27     protected File JavaDoc fieldRootDirectory;
28     protected String JavaDoc fieldFileTypes = "*.html,*.htm";
29     protected String JavaDoc fieldFindText;
30     protected String JavaDoc fieldReplaceText;
31     protected String JavaDoc fieldSearchPath;
32     protected int fieldNumFilesChanged = 0;
33     protected PageManager fieldPageManager;
34
35     public Replacer()
36     {
37     }
38
39     protected boolean fileMatchesFileTypes( File JavaDoc inFile )
40     {
41         String JavaDoc fileName = inFile.getName();
42         String JavaDoc[] 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 JavaDoc inFile, File JavaDoc inDirectory )
54     {
55         String JavaDoc filePath = inFile.getAbsolutePath();
56         String JavaDoc dirPath = inDirectory.getAbsolutePath();
57         return filePath.startsWith( dirPath );
58     }
59
60     protected boolean isDirectoryIncluded( File JavaDoc inFile )
61     {
62         if( inFile.getAbsolutePath().indexOf(".versions") > -1 )
63         {
64             return false;
65         }
66         File JavaDoc siteDir = getRootDirectory();
67         if ( !isFileIncludedInDirectory( inFile, siteDir ) )
68         {
69             return false;
70         }
71         File JavaDoc openEditDir = new File JavaDoc( siteDir, "openedit" );
72         if ( isFileIncludedInDirectory( inFile, openEditDir ) )
73         {
74             return false;
75         }
76         openEditDir = new File JavaDoc( siteDir, "base" );
77         if ( isFileIncludedInDirectory( inFile, openEditDir ) )
78         {
79             return false;
80         }
81         File JavaDoc webInfDir = new File JavaDoc( siteDir, "WEB-INF" );
82         if ( isFileIncludedInDirectory( inFile, webInfDir ) )
83         {
84             return false;
85         }
86         File JavaDoc logsDir = new File JavaDoc( siteDir, "logs" );
87         if ( isFileIncludedInDirectory( inFile, logsDir ) )
88         {
89             return false;
90         }
91         return true;
92     }
93
94     protected void replaceTextInFile( File JavaDoc inFile ) throws Exception JavaDoc
95     {
96         Writer JavaDoc writer = null;
97         try
98         {
99             
100             String JavaDoc path = URLUtilities.getPathWithoutContext(getRootDirectory().getAbsolutePath(),inFile.getAbsolutePath(), "index.html"); //This is an off API to use for this. TODO: Replace with PathUtilities
101
Page page = getPageManager().getPage(path);
102             
103             //is this in the search path
104
if ( getSearchPath() != null )
105             {
106                 //make sure we are within the search path
107
//inFile = "/sub/index.html" searchpath="/" or "/sub" or "/sub/index.html"
108
//then it passes
109
if ( !path.toLowerCase().startsWith( getSearchPath().toLowerCase() ) )
110                 {
111                     return;
112                 }
113             }
114             
115             if ( page.exists() && !page.isBinary() )
116             {
117                 String JavaDoc content = page.getContent();
118                 Pattern JavaDoc pat = Pattern.compile(getFindText() );// (?s)(?m), Pattern.DOTALL | Pattern.MULTILINE);
119
Matcher JavaDoc mat = pat.matcher(content);
120                 String JavaDoc 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 JavaDoc inDirectory ) throws Exception JavaDoc
143     {
144         if ( inDirectory.isDirectory())
145         {
146             if ( isDirectoryIncluded( inDirectory ) )
147             {
148                 File JavaDoc[] 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 JavaDoc
164     {
165         setNumFilesChanged(0);
166         if ( getSearchPath() == null)
167         {
168             throw new OpenEditRuntimeException("No search path set");
169         }
170         
171         File JavaDoc path = new File JavaDoc( getRootDirectory(), getSearchPath());
172         
173         if ( isDirectoryIncluded( path ) )
174         {
175             replaceAllInDirectory( path );
176         }
177     }
178
179     public String JavaDoc getFileTypes()
180     {
181         return fieldFileTypes;
182     }
183     public void setFileTypes(String JavaDoc inFileTypes)
184     {
185         fieldFileTypes = inFileTypes;
186     }
187     public String JavaDoc getFindText()
188     {
189         return fieldFindText;
190     }
191     public void setFindText(String JavaDoc inFindText)
192     {
193         fieldFindText = inFindText;
194     }
195     public String JavaDoc getReplaceText()
196     {
197         return fieldReplaceText;
198     }
199     public void setReplaceText(String JavaDoc inReplaceText)
200     {
201         fieldReplaceText = inReplaceText;
202     }
203     public File JavaDoc getRootDirectory()
204     {
205         return fieldRootDirectory;
206     }
207     public void setRootDirectory(File JavaDoc 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 JavaDoc getSearchPath()
220     {
221         return fieldSearchPath;
222     }
223     public void setSearchPath(String JavaDoc 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