KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > tools > content > CmsTagReplaceThread


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/content/CmsTagReplaceThread.java,v $
3  * Date : $Date: 2006/03/27 14:52:27 $
4  * Version: $Revision: 1.2 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.workplace.tools.content;
33
34 import org.opencms.file.CmsFile;
35 import org.opencms.file.CmsObject;
36 import org.opencms.file.CmsProperty;
37 import org.opencms.file.CmsResource;
38 import org.opencms.file.CmsResourceFilter;
39 import org.opencms.file.CmsUser;
40 import org.opencms.i18n.CmsMessageContainer;
41 import org.opencms.lock.CmsLock;
42 import org.opencms.main.CmsException;
43 import org.opencms.main.CmsLog;
44 import org.opencms.main.OpenCms;
45 import org.opencms.report.A_CmsReportThread;
46 import org.opencms.report.I_CmsReport;
47 import org.opencms.util.CmsStringUtil;
48 import org.opencms.xml.CmsXmlException;
49 import org.opencms.xml.content.CmsXmlContent;
50 import org.opencms.xml.content.CmsXmlContentFactory;
51 import org.opencms.xml.types.I_CmsXmlContentValue;
52
53 import java.util.Iterator JavaDoc;
54 import java.util.List JavaDoc;
55 import java.util.Locale JavaDoc;
56
57 import org.apache.commons.logging.Log;
58
59 import org.htmlparser.util.ParserException;
60
61 /**
62  * Replaces HTML tags of xmlpage resources using the corresponding settings object.
63  * <p>
64  *
65  * @author Achim Westermann
66  *
67  * @version $Revision: 1.2 $
68  *
69  * @since 6.1.8
70  */

71 public class CmsTagReplaceThread extends A_CmsReportThread {
72
73     /** The log object for this class. */
74     private static final Log LOG = CmsLog.getLog(CmsTagReplaceThread.class);
75
76     private Throwable JavaDoc m_error;
77
78     private CmsProperty m_markerProperty;
79
80     private CmsTagReplaceSettings m_settings;
81
82     /**
83      * Creates a replace html tag Thread.
84      * <p>
85      *
86      * @param cms the current cms context.
87      *
88      * @param settings the settings needed to perform the operation.
89      */

90     public CmsTagReplaceThread(CmsObject cms, CmsTagReplaceSettings settings) {
91
92         super(cms, Messages.get().getBundle().key(Messages.GUI_TAGREPLACE_THREAD_NAME_0));
93         initHtmlReport(cms.getRequestContext().getLocale());
94         m_settings = settings;
95         m_markerProperty = new CmsProperty(
96             CmsTagReplaceSettings.PROPERTY_CONTENTOOLS_TAGREPLACE,
97             null,
98             m_settings.getPropertyValueTagReplaceID(),
99             true);
100     }
101
102     /**
103      * @see org.opencms.report.A_CmsReportThread#getError()
104      */

105     public Throwable JavaDoc getError() {
106
107         return m_error;
108     }
109
110     /**
111      * @see org.opencms.report.A_CmsReportThread#getReportUpdate()
112      */

113     public String JavaDoc getReportUpdate() {
114
115         return getReport().getReportUpdate();
116     }
117
118     /**
119      * @see java.lang.Runnable#run()
120      */

121     public void run() {
122
123         getReport().println(
124             Messages.get().container(Messages.RPT_TAGREPLACE_BEGIN_1, m_settings.getWorkPath()),
125             I_CmsReport.FORMAT_HEADLINE);
126         try {
127             // change the element locales
128
replaceTags();
129         } catch (CmsException e) {
130             getReport().println(
131                 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_FAILED_0),
132                 I_CmsReport.FORMAT_ERROR);
133             getReport().println(e.getMessageContainer(), I_CmsReport.FORMAT_ERROR);
134             if (LOG.isErrorEnabled()) {
135                 LOG.error(e.getMessageContainer(), e);
136             }
137         } catch (Throwable JavaDoc f) {
138             getReport().println(
139                 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_FAILED_0),
140                 I_CmsReport.FORMAT_ERROR);
141             getReport().println(f);
142             if (LOG.isErrorEnabled()) {
143                 LOG.error(f);
144             }
145         }
146
147         // append runtime statistics to report
148
getReport().print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_STAT_0));
149         getReport().println(
150             org.opencms.report.Messages.get().container(
151                 org.opencms.report.Messages.RPT_STAT_DURATION_1,
152                 getReport().formatRuntime()));
153         getReport().println(Messages.get().container(Messages.RPT_TAGREPLACE_END_0), I_CmsReport.FORMAT_HEADLINE);
154     }
155
156     /**
157      * Checks the shared property {@link CmsTagReplaceSettings#PROPERTY_CONTENTOOLS_TAGREPLACE} if
158      * it has the value of this configuration ({@link CmsTagReplaceSettings#getPropertyValueTagReplaceID()}).
159      * <p>
160      *
161      * @param resource the resource to test.
162      *
163      * @return true if the property with the value was found.
164      *
165      * @throws CmsException if reading a property fails.
166      */

167     private boolean isProcessedBefore(CmsResource resource) throws CmsException {
168
169         CmsProperty testProp = getCms().readPropertyObject(
170             resource,
171             CmsTagReplaceSettings.PROPERTY_CONTENTOOLS_TAGREPLACE,
172             false);
173         if (testProp.isNullProperty()) {
174             return false;
175         } else {
176             String JavaDoc testValue = testProp.getResourceValue();
177             if (CmsStringUtil.isEmptyOrWhitespaceOnly(testValue)) {
178                 return false;
179             } else {
180                 return testValue.equals(m_settings.getPropertyValueTagReplaceID());
181             }
182         }
183     }
184
185     private void replaceTags() throws CmsException {
186
187         I_CmsReport report = getReport();
188         report.print(Messages.get().container(Messages.RPT_TAGREPLACE_READ_RESOURCES_1, m_settings.getWorkPath()));
189         report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
190         if (LOG.isDebugEnabled()) {
191             LOG.debug(Messages.get().getBundle().key(Messages.RPT_TAGREPLACE_READ_RESOURCES_1, m_settings.getWorkPath()));
192         }
193         CmsResourceFilter filter = CmsResourceFilter.ALL.addRequireType(OpenCms.getResourceManager().getResourceType(
194             "xmlpage").getTypeId());
195         List JavaDoc resources = getCms().readResources(m_settings.getWorkPath(), filter, true);
196         if (LOG.isDebugEnabled()) {
197             LOG.debug(Messages.get().getBundle().key(
198                 Messages.LOG_TAGREPLACE_READ_RESOURCES_OK_1,
199                 m_settings.getWorkPath()));
200         }
201         report.println(
202             org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
203             I_CmsReport.FORMAT_OK);
204         Integer JavaDoc size = new Integer JavaDoc(resources.size());
205         Iterator JavaDoc itResources = resources.iterator();
206         CmsResource resource;
207         int count = 1;
208         while (itResources.hasNext()) {
209             resource = (CmsResource)itResources.next();
210             replaceTags(resource, size, new Integer JavaDoc(count));
211             count++;
212         }
213     }
214
215     /**
216      * Replaces all replacement mappings configured in the internal {@link CmsTagReplaceSettings}
217      * instance in the content of the given resource.
218      * <p>
219      *
220      * No modifications will be done:
221      * <ol>
222      * <li>the resource is locked by another user.</li>
223      * <li>the special marker property with the value that stands for the replacement configuration
224      * is set on the resource (shared).</li>
225      * <li>locking of the non-locked resource fails.</li>
226      * <li>Loading of the content fails.</li>
227      * <li>Unmarshalling fails.</li>
228      * <li>Unexpected exception while replacing occur.</li>
229      * <li>Marshalling of XML fails.</li>
230      * <li>Writing of the marker property fails.</li>
231      * <li>Writing of the file fails.</li>
232      * </ol>
233      * <p>
234      *
235      * @param resource denotes the content to process.
236      *
237      * @param totalJobCount for fancy report writing.
238      *
239      * @param actualJobCount for even fancier report writing.
240      *
241      * @throws CmsException if sth. goes wrong.
242      */

243
244     private void replaceTags(CmsResource resource, Integer JavaDoc totalJobCount, Integer JavaDoc actualJobCount) throws CmsException {
245
246         I_CmsReport report = getReport();
247         report.print(org.opencms.report.Messages.get().container(
248             org.opencms.report.Messages.RPT_SUCCESSION_2,
249             actualJobCount,
250             totalJobCount));
251         report.print(Messages.get().container(
252             Messages.RPT_TAGREPLACE_PROCESS_FILE_1,
253             getCms().getRequestContext().removeSiteRoot(resource.getRootPath())));
254         report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
255
256         if (isProcessedBefore(resource)) {
257             report.print(
258                 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_SKIPPED_0),
259                 I_CmsReport.FORMAT_OK);
260             report.println(
261                 Messages.get().container(Messages.RPT_TAGREPLACE_SKIP_REASON_PROPERTY_0),
262                 I_CmsReport.FORMAT_OK);
263             return;
264         }
265
266         if (LOG.isDebugEnabled()) {
267             LOG.debug(Messages.get().getBundle().key(
268                 Messages.LOG_DEBUG_TAGREPLACE_LOCK_RESOURCE_1,
269                 resource.getRootPath()));
270         }
271         try {
272             // checking the lock:
273
if (LOG.isDebugEnabled()) {
274                 LOG.debug(Messages.get().getBundle().key(
275                     Messages.LOG_DEBUG_TAGREPLACE_LOCK_READ_1,
276                     resource.getRootPath()));
277             }
278
279             CmsLock lock = getCms().getLock(resource);
280
281             if (LOG.isDebugEnabled()) {
282                 LOG.debug(Messages.get().getBundle().key(
283                     Messages.LOG_DEBUG_TAGREPLACE_LOCK_READ_1,
284                     resource.getRootPath()));
285             }
286
287             boolean myLock = !lock.isNullLock()
288                 && lock.getUserId().equals(getCms().getRequestContext().currentUser().getId());
289             if (lock.isNullLock() || myLock) {
290                 if (!myLock) {
291                     if (LOG.isDebugEnabled()) {
292                         LOG.debug(Messages.get().getBundle().key(
293                             Messages.LOG_DEBUG_TAGREPLACE_LOCK_RESOURCE_1,
294                             resource.getRootPath()));
295                     }
296                     // obtaining the lock:
297
getCms().lockResource(
298                         getCms().getRequestContext().removeSiteRoot(resource.getRootPath()),
299                         CmsLock.TYPE_EXCLUSIVE);
300                     if (LOG.isDebugEnabled()) {
301                         LOG.debug(Messages.get().getBundle().key(
302                             Messages.LOG_DEBUG_TAGREPLACE_LOCK_RESOURCE_OK_1,
303                             resource.getRootPath()));
304                     }
305                 }
306             } else {
307                 // locked by another user:
308
if (LOG.isDebugEnabled()) {
309                     LOG.debug(Messages.get().getBundle().key(
310                         Messages.LOG_DEBUG_TAGREPLACE_RESOURCE_SKIPPED_1,
311                         resource.getRootPath()));
312                     LOG.debug(Messages.get().getBundle().key(Messages.RPT_TAGREPLACE_SKIP_REASON_LOCKED_0));
313                 }
314                 report.print(
315                     org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_SKIPPED_0),
316                     I_CmsReport.FORMAT_WARNING);
317                 try {
318                     CmsUser locker = getCms().readUser(lock.getUserId());
319                     report.println(Messages.get().container(
320                         Messages.RPT_TAGREPLACE_SKIP_REASON_LOCKED_1,
321                         locker.getName()), I_CmsReport.FORMAT_WARNING);
322                 } catch (Throwable JavaDoc f) {
323                     report.println(
324                         Messages.get().container(Messages.RPT_TAGREPLACE_SKIP_REASON_ERR_LOCK_0),
325                         I_CmsReport.FORMAT_WARNING);
326                     if (LOG.isDebugEnabled()) {
327                         LOG.debug(Messages.get().getBundle().key(
328                             Messages.LOG_DEBUG_TAGREPLACE_RESOURCE_SKIPPED_1,
329                             resource.getRootPath()));
330                         LOG.debug(Messages.get().getBundle().key(Messages.RPT_TAGREPLACE_SKIP_REASON_ERR_LOCK_0));
331                     }
332                 }
333                 return;
334             }
335         } catch (CmsException e) {
336             if (LOG.isErrorEnabled()) {
337                 LOG.error(Messages.get().getBundle().key(
338                     Messages.LOG_WARN_TAGREPLACE_LOCK_RESOURCE_FAILED_1,
339                     resource.getRootPath()), e);
340             }
341             report.print(
342                 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_SKIPPED_0),
343                 I_CmsReport.FORMAT_WARNING);
344             report.println(
345                 Messages.get().container(Messages.RPT_TAGREPLACE_SKIP_REASON_LOCKED_0),
346                 I_CmsReport.FORMAT_WARNING);
347             return;
348         }
349
350         if (LOG.isDebugEnabled()) {
351             LOG.debug(Messages.get().getBundle().key(Messages.LOG_DEBUG_TAGREPLACE_LOAD_FILE_1, resource.getRootPath()));
352         }
353
354         CmsFile file = CmsFile.upgrade(resource, getCms());
355
356         if (LOG.isDebugEnabled()) {
357             LOG.debug(Messages.get().getBundle().key(
358                 Messages.LOG_DEBUG_TAGREPLACE_LOAD_FILE_OK_1,
359                 resource.getRootPath()));
360             LOG.debug(Messages.get().getBundle().key(Messages.LOG_DEBUG_TAGREPLACE_UNMARSHAL_1, resource.getRootPath()));
361         }
362
363         CmsXmlContent xmlcontent = CmsXmlContentFactory.unmarshal(getCms(), file);
364
365         if (LOG.isDebugEnabled()) {
366             LOG.debug(Messages.get().getBundle().key(
367                 Messages.LOG_DEBUG_TAGREPLACE_UNMARSHAL_OK_1,
368                 resource.getRootPath()));
369         }
370
371         List JavaDoc locales = xmlcontent.getLocales();
372         Iterator JavaDoc itLocales = locales.iterator();
373         List JavaDoc elements;
374         Iterator JavaDoc itElements;
375         Locale JavaDoc locale;
376         CmsTagReplaceParser parser = new CmsTagReplaceParser(m_settings);
377         I_CmsXmlContentValue value;
378         int count = 1;
379         while (itLocales.hasNext()) {
380             locale = (Locale JavaDoc)itLocales.next();
381             if (LOG.isDebugEnabled()) {
382                 LOG.debug(Messages.get().getBundle().key(Messages.LOG_DEBUG_TAGREPLACE_LOCALE_1, locale.getLanguage()));
383             }
384
385             elements = xmlcontent.getValues(locale);
386             itElements = elements.iterator();
387             while (itElements.hasNext()) {
388                 value = (I_CmsXmlContentValue)itElements.next();
389                 String JavaDoc content = value.getStringValue(getCms());
390                 if (LOG.isDebugEnabled()) {
391                     LOG.debug(Messages.get().getBundle().key(
392                         Messages.LOG_DEBUG_TAGREPLACE_ELEMENT_2,
393                         value.getPath(),
394                         content));
395                 }
396                 try {
397
398                     parser.process(content, xmlcontent.getEncoding());
399                     value.setStringValue(getCms(), parser.getResult());
400                 } catch (ParserException e) {
401                     CmsMessageContainer container = Messages.get().container(
402                         Messages.ERR_TAGREPLACE_PARSE_4,
403                         new Object JavaDoc[] {
404                             getCms().getRequestContext().removeSiteRoot(resource.getRootPath()),
405                             locale.getLanguage(),
406                             value.getPath(),
407                             parser.getResult()});
408                     throw new CmsXmlException(container, e);
409                 }
410             }
411             count++;
412         }
413
414         if (parser.isChangedContent()) {
415
416             if (LOG.isDebugEnabled()) {
417                 LOG.debug(Messages.get().getBundle().key(
418                     Messages.LOG_DEBUG_TAGREPLACE_MARSHAL_1,
419                     resource.getRootPath()));
420             }
421             byte[] content = xmlcontent.marshal();
422             if (LOG.isDebugEnabled()) {
423                 LOG.debug(Messages.get().getBundle().key(
424                     Messages.LOG_DEBUG_TAGREPLACE_MARSHAL_OK_1,
425                     resource.getRootPath()));
426             }
427
428             // write back the modified xmlcontent:
429
file.setContents(content);
430
431             if (LOG.isDebugEnabled()) {
432                 LOG.debug(Messages.get().getBundle().key(Messages.LOG_DEBUG_TAGREPLACE_WRITE_1, resource.getRootPath()));
433             }
434
435             getCms().writeFile(file);
436
437             if (LOG.isDebugEnabled()) {
438                 LOG.debug(Messages.get().getBundle().key(
439                     Messages.LOG_DEBUG_TAGREPLACE_WRITE_OK_1,
440                     resource.getRootPath()));
441             }
442
443             try {
444                 // set the marker property:
445

446                 if (LOG.isDebugEnabled()) {
447                     LOG.debug(Messages.get().getBundle().key(
448                         Messages.LOG_DEBUG_TAGREPLACE_PROPERTY_WRITE_3,
449                         new Object JavaDoc[] {
450                             m_markerProperty.getName(),
451                             m_markerProperty.getResourceValue(),
452                             resource.getRootPath()}));
453                 }
454                 getCms().writePropertyObject(
455                     getCms().getRequestContext().removeSiteRoot(resource.getRootPath()),
456                     m_markerProperty);
457                 if (LOG.isDebugEnabled()) {
458                     LOG.debug(Messages.get().getBundle().key(Messages.LOG_DEBUG_TAGREPLACE_PROPERTY_WRITE_OK_0));
459                 }
460                 report.println(
461                     org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
462                     I_CmsReport.FORMAT_OK);
463             } catch (CmsException e) {
464                 CmsMessageContainer container = Messages.get().container(
465                     Messages.LOG_ERROR_TAGREPLACE_PROPERTY_WRITE_3,
466                     new Object JavaDoc[] {
467                         m_markerProperty.getName(),
468                         m_markerProperty.getResourceValue(),
469                         resource.getRootPath()});
470                 throw new CmsXmlException(container, e);
471             }
472
473         } else {
474             if (LOG.isDebugEnabled()) {
475                 LOG.debug(Messages.get().container(Messages.LOG_DEBUG_TAGREPLACE_UNLOCK_FILE_1, resource.getRootPath()));
476             }
477             getCms().unlockResource(getCms().getRequestContext().removeSiteRoot(resource.getRootPath()));
478             if (LOG.isDebugEnabled()) {
479                 LOG.debug(Messages.get().container(Messages.LOG_DEBUG_TAGREPLACE_UNLOCK_FILE_OK_0));
480             }
481             report.print(
482                 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_SKIPPED_0),
483                 I_CmsReport.FORMAT_OK);
484             report.println(
485                 Messages.get().container(Messages.RPT_TAGREPLACE_SKIP_REASON_UNMODIFIED_0),
486                 I_CmsReport.FORMAT_OK);
487
488         }
489     }
490 }
Popular Tags