KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > comparison > CmsXmlDocumentComparison


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/comparison/CmsXmlDocumentComparison.java,v $
3  * Date : $Date: 2006/03/30 09:30:14 $
4  * Version: $Revision: 1.4 $
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.comparison;
33
34 import org.opencms.file.CmsFile;
35 import org.opencms.file.CmsObject;
36 import org.opencms.file.types.CmsResourceTypeXmlPage;
37 import org.opencms.main.CmsException;
38 import org.opencms.xml.I_CmsXmlDocument;
39 import org.opencms.xml.content.CmsXmlContent;
40 import org.opencms.xml.content.CmsXmlContentFactory;
41 import org.opencms.xml.content.I_CmsXmlContentValueVisitor;
42 import org.opencms.xml.page.CmsXmlPageFactory;
43 import org.opencms.xml.types.I_CmsXmlContentValue;
44
45 import java.util.ArrayList JavaDoc;
46 import java.util.Iterator JavaDoc;
47 import java.util.List JavaDoc;
48 import java.util.Locale JavaDoc;
49
50 /**
51  * A comparison of properties, attributes and elements of xml documents.<p>
52  *
53  * @author Jan Baudisch
54  */

55 public class CmsXmlDocumentComparison extends CmsResourceComparison {
56
57     /**
58      * Visitor that collects the xpath expressions of xml contents.<p>
59      */

60     class CmsXmlContentElementPathExtractor implements I_CmsXmlContentValueVisitor {
61
62         /** The paths to the elements in the xml content. */
63         private List JavaDoc m_elementPaths;
64
65         /**
66          * Creates a CmsXmlContentElementPathExtractor.<p>
67          */

68         CmsXmlContentElementPathExtractor() {
69
70             m_elementPaths = new ArrayList JavaDoc();
71         }
72
73         /**
74          *
75          * @see org.opencms.xml.content.I_CmsXmlContentValueVisitor#visit(org.opencms.xml.types.I_CmsXmlContentValue)
76          */

77         public void visit(I_CmsXmlContentValue value) {
78
79             // only add simple types
80
if (value.isSimpleType()) {
81                 m_elementPaths.add(new CmsXmlContentElementComparison(
82                     value.getLocale().toString(),
83                     value.getPath(),
84                     value.getTypeName()));
85             }
86         }
87
88         /**
89          * Returns the elementPaths.<p>
90          *
91          * @return the elementPaths
92          */

93         List JavaDoc getElementPaths() {
94
95             return m_elementPaths;
96         }
97     }
98
99     /** The compared elements.<p> */
100     private List JavaDoc m_elements;
101
102     /**
103      * Creates a new xml document comparison.<p>
104      *
105      * @param cms the CmsObject to use
106      * @param res1 the first file to compare
107      * @param res2 the second file to compare
108      *
109      * @throws CmsException if something goes wrong
110      */

111     public CmsXmlDocumentComparison(CmsObject cms, CmsFile res1, CmsFile res2)
112     throws CmsException {
113
114         I_CmsXmlDocument resource1;
115         I_CmsXmlDocument resource2;
116
117         List JavaDoc elements1 = null;
118         List JavaDoc elements2 = null;
119
120         if (res1.getTypeId() == CmsResourceTypeXmlPage.getStaticTypeId()
121             && res2.getTypeId() == CmsResourceTypeXmlPage.getStaticTypeId()) {
122             resource1 = CmsXmlPageFactory.unmarshal(cms, res1);
123             resource2 = CmsXmlPageFactory.unmarshal(cms, res2);
124             elements1 = getElements(resource1);
125             elements2 = getElements(resource2);
126         } else {
127             resource1 = CmsXmlContentFactory.unmarshal(cms, res1);
128             CmsXmlContentElementPathExtractor visitor = new CmsXmlContentElementPathExtractor();
129             ((CmsXmlContent)resource1).visitAllValuesWith(visitor);
130             elements1 = visitor.getElementPaths();
131             resource2 = CmsXmlContentFactory.unmarshal(cms, res2);
132             visitor = new CmsXmlContentElementPathExtractor();
133             ((CmsXmlContent)resource2).visitAllValuesWith(visitor);
134             elements2 = visitor.getElementPaths();
135         }
136
137         List JavaDoc removed = new ArrayList JavaDoc(elements1);
138         removed.removeAll(elements2);
139         Iterator JavaDoc i = removed.iterator();
140         while (i.hasNext()) {
141             CmsElementComparison elem = (CmsElementComparison)i.next();
142             elem.setStatus(CmsResourceComparison.TYPE_REMOVED);
143             String JavaDoc value = resource1.getValue(elem.getName(), new Locale JavaDoc(elem.getLocale())).getStringValue(cms);
144             elem.setVersion1(value);
145             elem.setVersion2("");
146         }
147         List JavaDoc added = new ArrayList JavaDoc(elements2);
148         added.removeAll(elements1);
149         i = added.iterator();
150         while (i.hasNext()) {
151             CmsElementComparison elem = (CmsElementComparison)i.next();
152             elem.setStatus(CmsResourceComparison.TYPE_ADDED);
153             elem.setVersion1("");
154             String JavaDoc value = resource2.getValue(elem.getName(), new Locale JavaDoc(elem.getLocale())).getStringValue(cms);
155             elem.setVersion2(value);
156         }
157         List JavaDoc union = new ArrayList JavaDoc(elements1);
158         union.retainAll(elements2);
159
160         // find out, which elements were changed
161
i = new ArrayList JavaDoc(union).iterator();
162         while (i.hasNext()) {
163             CmsElementComparison elem = (CmsElementComparison)i.next();
164             String JavaDoc value1 = resource1.getValue(elem.getName(), new Locale JavaDoc(elem.getLocale())).getStringValue(cms);
165             String JavaDoc value2 = resource2.getValue(elem.getName(), new Locale JavaDoc(elem.getLocale())).getStringValue(cms);
166             if (value1 == null) {
167                 value1 = "";
168             }
169             if (value2 == null) {
170                 value2 = "";
171             }
172             elem.setVersion1(value1);
173             elem.setVersion2(value2);
174             if (!value1.equals(value2)) {
175                 elem.setStatus(CmsResourceComparison.TYPE_CHANGED);
176             } else {
177                 elem.setStatus(CmsResourceComparison.TYPE_UNCHANGED);
178             }
179         }
180         m_elements = new ArrayList JavaDoc(removed);
181         m_elements.addAll(added);
182         m_elements.addAll(union);
183     }
184
185     /**
186      * Returns the elements.<p>
187      *
188      * @return the elements
189      */

190     public List JavaDoc getElements() {
191
192         return m_elements;
193     }
194
195     /** Returs a list of all element names of a xml page.<p>
196      *
197      * @param xmlPage the xml page to read the element names from
198      * @return a list of all element names of a xml page
199      */

200     private List JavaDoc getElements(I_CmsXmlDocument xmlPage) {
201
202         List JavaDoc elements = new ArrayList JavaDoc();
203         Iterator JavaDoc locales = xmlPage.getLocales().iterator();
204         while (locales.hasNext()) {
205             Locale JavaDoc locale = (Locale JavaDoc)locales.next();
206             Iterator JavaDoc elementNames = xmlPage.getNames(locale).iterator();
207             while (elementNames.hasNext()) {
208                 String JavaDoc elementName = (String JavaDoc)elementNames.next();
209                 elements.add(new CmsElementComparison(locale.toString(), elementName));
210             }
211         }
212         return elements;
213     }
214 }
215
Popular Tags