Source Code Cross Referenced for CmsXmlContentRepairThread.java in  » Content-Management-System » opencms » org » opencms » workplace » threads » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Content Management System » opencms » org.opencms.workplace.threads 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.