001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/threads/CmsXmlContentRepairThread.java,v $
003: * Date : $Date: 2008-02-27 12:05:43 $
004: * Version: $Revision: 1.6 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.workplace.threads;
033:
034: import org.opencms.file.CmsFile;
035: import org.opencms.file.CmsObject;
036: import org.opencms.file.CmsResource;
037: import org.opencms.file.CmsResourceFilter;
038: import org.opencms.lock.CmsLock;
039: import org.opencms.main.CmsException;
040: import org.opencms.main.CmsLog;
041: import org.opencms.report.A_CmsReportThread;
042: import org.opencms.report.I_CmsReport;
043: import org.opencms.xml.CmsXmlEntityResolver;
044: import org.opencms.xml.CmsXmlException;
045: import org.opencms.xml.content.CmsXmlContent;
046: import org.opencms.xml.content.CmsXmlContentFactory;
047:
048: import java.util.Iterator;
049: import java.util.List;
050:
051: import org.apache.commons.logging.Log;
052:
053: /**
054: * Repairs XML content resources according to their XSD using the corresponding settings object.<p>
055: *
056: * @author Andreas Zahner
057: *
058: * @version $Revision: 1.6 $
059: *
060: */
061: public class CmsXmlContentRepairThread extends A_CmsReportThread {
062:
063: /** The log object for this class. */
064: private static final Log LOG = CmsLog
065: .getLog(CmsXmlContentRepairThread.class);
066:
067: /** The dialog settings used to configure the repair thread. */
068: private CmsXmlContentRepairSettings m_settings;
069:
070: /**
071: * Creates a repair XML content resources thread.<p>
072: *
073: * @param cms the current cms context
074: * @param settings the settings needed to perform the repair operation
075: */
076: public CmsXmlContentRepairThread(CmsObject cms,
077: CmsXmlContentRepairSettings settings) {
078:
079: super (cms, Messages.get().getBundle().key(
080: Messages.GUI_XMLCONTENTREPAIR_THREAD_NAME_0));
081: initHtmlReport(cms.getRequestContext().getLocale());
082: m_settings = settings;
083: }
084:
085: /**
086: * @see org.opencms.report.A_CmsReportThread#getReportUpdate()
087: */
088: public String getReportUpdate() {
089:
090: return getReport().getReportUpdate();
091: }
092:
093: /**
094: * @see java.lang.Runnable#run()
095: */
096: public void run() {
097:
098: getReport().println(
099: Messages.get().container(
100: Messages.RPT_XMLCONTENTREPAIR_BEGIN_2,
101: m_settings.getResourceType(),
102: m_settings.getVfsFolder()),
103: I_CmsReport.FORMAT_HEADLINE);
104: try {
105: // repair the XML content resources
106: repairXmlContents();
107: } catch (Throwable f) {
108: getReport().println(
109: org.opencms.report.Messages.get().container(
110: org.opencms.report.Messages.RPT_FAILED_0),
111: I_CmsReport.FORMAT_ERROR);
112: getReport().println(f);
113: if (LOG.isErrorEnabled()) {
114: LOG.error(f);
115: }
116: }
117:
118: // append runtime statistics to the report output
119: getReport().print(
120: org.opencms.report.Messages.get().container(
121: org.opencms.report.Messages.RPT_STAT_0));
122: getReport()
123: .println(
124: org.opencms.report.Messages
125: .get()
126: .container(
127: org.opencms.report.Messages.RPT_STAT_DURATION_1,
128: getReport().formatRuntime()));
129: getReport().println(
130: Messages.get().container(
131: Messages.RPT_XMLCONTENTREPAIR_END_0),
132: I_CmsReport.FORMAT_HEADLINE);
133: }
134:
135: /**
136: * Performs the correction of the XML content resources according to their XML schema definition.<p>
137: *
138: * @throws CmsException if reading the list of resources to repair fails
139: */
140: private void repairXmlContents() throws CmsException {
141:
142: // set the resource filter to filter XML contents of the selected type
143: CmsResourceFilter filter = CmsResourceFilter.IGNORE_EXPIRATION
144: .addRequireType(m_settings.getResourceTypeId());
145: String path = CmsResource.getFolderPath(m_settings
146: .getVfsFolder());
147: // get the list of resources to check
148: List resources = getCms().readResources(path, filter,
149: m_settings.isIncludeSubFolders());
150:
151: // set the report counters
152: int count = 0;
153: int resSize = resources.size();
154:
155: // create an entity resolver to use
156: CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(
157: getCms());
158:
159: // iterate the resources
160: Iterator i = resources.iterator();
161: while (i.hasNext()) {
162:
163: count++;
164: CmsResource res = (CmsResource) i.next();
165:
166: // generate report output
167: getReport()
168: .print(
169: org.opencms.report.Messages
170: .get()
171: .container(
172: org.opencms.report.Messages.RPT_SUCCESSION_2,
173: String.valueOf(count),
174: String.valueOf(resSize)),
175: I_CmsReport.FORMAT_NOTE);
176: getReport().print(
177: Messages.get().container(
178: Messages.RPT_PROCESSING_XMLCONTENT_0),
179: I_CmsReport.FORMAT_NOTE);
180: getReport().print(
181: org.opencms.report.Messages.get().container(
182: org.opencms.report.Messages.RPT_ARGUMENT_1,
183: getCms().getSitePath(res)));
184: getReport().print(
185: org.opencms.report.Messages.get().container(
186: org.opencms.report.Messages.RPT_DOTS_0));
187:
188: try {
189:
190: // get the file contents
191: CmsFile file = getCms().readFile(res);
192: // get the XML content
193: CmsXmlContent xmlContent = CmsXmlContentFactory
194: .unmarshal(getCms(), file);
195:
196: // check the XML structure
197: boolean fixFile = m_settings.isForce();
198: if (!fixFile) {
199: try {
200: xmlContent.validateXmlStructure(resolver);
201: } catch (CmsXmlException e) {
202: // XML structure is not valid, this file has to be fixed
203: fixFile = true;
204: }
205: }
206: if (fixFile) {
207:
208: // check the lock state of the file to repair
209: CmsLock lock = getCms().getLock(res);
210: boolean isLocked = false;
211: boolean canWrite = false;
212: if (lock.isNullLock()) {
213: // file is not locked, lock it
214: getCms()
215: .lockResource(getCms().getSitePath(res));
216: isLocked = true;
217: canWrite = true;
218: } else if (lock.isOwnedBy(getCms()
219: .getRequestContext().currentUser())) {
220: // file is locked by current user
221: canWrite = true;
222: }
223:
224: if (canWrite) {
225: // enable "auto correction mode" - this is required or the XML structure will not be fully corrected
226: xmlContent.setAutoCorrectionEnabled(true);
227: // now correct the XML
228: xmlContent.correctXmlStructure(getCms());
229: file.setContents(xmlContent.marshal());
230: // write the corrected file
231: getCms().writeFile(file);
232: } else {
233: // no write operation possible
234: getReport()
235: .println(
236: Messages
237: .get()
238: .container(
239: Messages.RPT_XMLCONTENTREPAIR_NOTLOCKED_0),
240: I_CmsReport.FORMAT_NOTE);
241: }
242:
243: if (isLocked) {
244: // unlock previously locked resource
245: getCms().unlockResource(
246: getCms().getSitePath(res));
247: }
248:
249: if (canWrite) {
250: // successfully repaired XML content, report it
251: getReport()
252: .println(
253: Messages
254: .get()
255: .container(
256: Messages.RPT_XMLCONTENTREPAIR_REPAIRED_0),
257: I_CmsReport.FORMAT_OK);
258: }
259:
260: } else {
261: // nothing to fix, skip this file
262: getReport()
263: .println(
264: Messages
265: .get()
266: .container(
267: Messages.RPT_XMLCONTENTREPAIR_SKIPFILE_0),
268: I_CmsReport.FORMAT_NOTE);
269: }
270:
271: } catch (CmsException e) {
272: // an error occured, show exception on report output
273: getReport().println(e);
274: }
275: }
276: }
277:
278: }
|