KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > editors > CmsXmlContentWidgetVisitor


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/editors/CmsXmlContentWidgetVisitor.java,v $
3  * Date : $Date: 2006/03/27 14:52:49 $
4  * Version: $Revision: 1.8 $
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.editors;
33
34 import org.opencms.main.CmsLog;
35 import org.opencms.widgets.I_CmsWidget;
36 import org.opencms.widgets.Messages;
37 import org.opencms.xml.CmsXmlException;
38 import org.opencms.xml.content.I_CmsXmlContentValueVisitor;
39 import org.opencms.xml.types.I_CmsXmlContentValue;
40
41 import java.util.ArrayList JavaDoc;
42 import java.util.HashMap JavaDoc;
43 import java.util.List JavaDoc;
44 import java.util.Locale JavaDoc;
45 import java.util.Map JavaDoc;
46
47 import org.apache.commons.logging.Log;
48
49 /**
50  * Visitor implementation that collects the different widgets for all visited values and all widgets for the found values.<p>
51  *
52  * This implementation is needed when creating the html output of the xmlcontent editor
53  * {@link org.opencms.workplace.editors.CmsXmlContentEditor}.<p>
54  *
55  * @author Andreas Zahner
56  *
57  * @version $Revision: 1.8 $
58  *
59  * @since 6.0.0
60  */

61 public class CmsXmlContentWidgetVisitor implements I_CmsXmlContentValueVisitor {
62
63     /** Static reference to the log. */
64     private static final Log LOG = CmsLog.getLog(CmsXmlContentWidgetVisitor.class);
65
66     /** The locale to get the values from. */
67     private Locale JavaDoc m_locale;
68
69     /** The unique widgets found in the xml content. */
70     private List JavaDoc m_uniqueWidgets;
71
72     /** The values corresponding to the found widgets. */
73     private Map JavaDoc m_values;
74
75     /** The widgets found in the xml content. */
76     private Map JavaDoc m_widgets;
77
78     /**
79      * Creates a new widget collector node visitor.<p>
80      */

81     public CmsXmlContentWidgetVisitor() {
82
83         initialize(null);
84     }
85
86     /**
87      * Creates a new widget collector node visitor.<p>
88      *
89      * @param locale the Locale to get the widgets from
90      */

91     public CmsXmlContentWidgetVisitor(Locale JavaDoc locale) {
92
93         initialize(locale);
94     }
95
96     /**
97      * Returns the locale to get the widgets from.<p>
98      *
99      * @return the locale to get the widgets from
100      */

101     public Locale JavaDoc getLocale() {
102
103         return m_locale;
104     }
105
106     /**
107      * Returns the unique widgets that were found in the content.<p>
108      *
109      * @return the unique widgets that were found in the content
110      */

111     public List JavaDoc getUniqueWidgets() {
112
113         return m_uniqueWidgets;
114     }
115
116     /**
117      * Returns all simple values that were found in the content.<p>
118      *
119      * The map key is the complete xpath of the value.<p>
120      *
121      * @return all simple values that were found in the content
122      */

123     public Map JavaDoc getValues() {
124
125         return m_values;
126     }
127
128     /**
129      * Returns all widgets that were found in the content.<p>
130      *
131      * The map key is the complete xpath of the corresponding value.<p>
132      *
133      * @return all widgets that were found in the content
134      */

135     public Map JavaDoc getWidgets() {
136
137         return m_widgets;
138     }
139
140     /**
141      * @see org.opencms.xml.content.I_CmsXmlContentValueVisitor#visit(org.opencms.xml.types.I_CmsXmlContentValue)
142      */

143     public void visit(I_CmsXmlContentValue value) {
144
145         if (LOG.isDebugEnabled()) {
146             LOG.debug(org.opencms.workplace.editors.Messages.get().getBundle().key(
147                 org.opencms.workplace.editors.Messages.LOG_VISITING_1,
148                 value.getPath()));
149         }
150
151         if (value.isSimpleType()) {
152             // only visit simple values
153
boolean useLocale = m_locale != null;
154             if ((useLocale && value.getLocale() == getLocale()) || (!useLocale)) {
155                 try {
156                     // get widget for value
157
I_CmsWidget widget = value.getContentDefinition().getContentHandler().getWidget(value);
158                     if (!m_uniqueWidgets.contains(widget)) {
159                         m_uniqueWidgets.add(widget);
160                     }
161                     m_widgets.put(value.getPath(), widget);
162                     m_values.put(value.getPath(), value);
163                     if (LOG.isDebugEnabled()) {
164                         LOG.debug(Messages.get().getBundle().key(
165                             Messages.LOG_DEBUG_WIDGETCOLLECTOR_ADD_1,
166                             value.getPath()));
167                     }
168                 } catch (CmsXmlException e) {
169                     // should usually not happen
170
if (LOG.isErrorEnabled()) {
171                         LOG.error(Messages.get().getBundle().key(Messages.ERR_WIDGETCOLLECTOR_ADD_1, value), e);
172                     }
173                 }
174             }
175         }
176     }
177
178     /**
179      * Initializes the necessary members of the collector.<p>
180      *
181      * @param locale the Locale to get the widgets from
182      */

183     private void initialize(Locale JavaDoc locale) {
184
185         // start with a new instance of the widgets and unique widgets
186
m_widgets = new HashMap JavaDoc(25);
187         m_uniqueWidgets = new ArrayList JavaDoc(12);
188         m_values = new HashMap JavaDoc(25);
189         // store Locale to use when collecting the widgets
190
m_locale = locale;
191     }
192 }
Popular Tags