KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/content/CmsTagReplaceSettings.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) 2006 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.CmsObject;
35 import org.opencms.i18n.CmsMessageContainer;
36 import org.opencms.main.CmsIllegalArgumentException;
37 import org.opencms.main.CmsLog;
38 import org.opencms.util.CmsStringUtil;
39
40 import java.util.Comparator JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.List JavaDoc;
43 import java.util.Map JavaDoc;
44 import java.util.Set JavaDoc;
45 import java.util.SortedMap JavaDoc;
46 import java.util.TreeMap JavaDoc;
47 import java.util.TreeSet JavaDoc;
48 import java.util.Vector JavaDoc;
49
50 import org.apache.commons.logging.Log;
51
52 import org.htmlparser.Attribute;
53 import org.htmlparser.NodeFactory;
54 import org.htmlparser.PrototypicalNodeFactory;
55 import org.htmlparser.Tag;
56 import org.htmlparser.util.ParserException;
57
58 /**
59  * Bean to hold the settings needed for the operation of replacing HTML Tags of xmlpage resources in
60  * the OpenCms VFS.
61  * <p>
62  *
63  * @author Achim Westermann
64  *
65  * @version $Revision: 1.2 $
66  *
67  * @since 6.1.7
68  *
69  */

70 public final class CmsTagReplaceSettings {
71
72     /**
73      * Property for the tag-replace contentool to know the files that have been processed before in
74      * case of early terminaton in previous runs.
75      */

76     public static final String JavaDoc PROPERTY_CONTENTOOLS_TAGREPLACE = "contentools.tagreplace";
77
78     /** The log object for this class. */
79     private static final Log LOG = CmsLog.getLog(CmsTagReplaceSettings.class);
80
81     /** Needed to verify if a path String denotes a folder in VFS. */
82     private final CmsObject m_cms;
83
84     /** The tags that should be deleted. */
85     private final Set JavaDoc m_deleteTags;
86
87     /** Used to create Tag instances for tags to delete of the proper type in a convenient way. */
88     private NodeFactory m_nodeFactory;
89
90     /**
91      * The value of the shared {@link #PROPERTY_CONTENTOOLS_TAGREPLACE} to set on resources that
92      * have been processed with these settings.
93      */

94     private String JavaDoc m_propertyValueTagReplaceID;
95
96     /**
97      * A map containing lower case tag names of tags to replace as keys and the replacement tag
98      * names as their corresponding values.
99      */

100     private SortedMap JavaDoc m_tags2replacementTags;
101
102     /** The root of all content files to process. */
103     private String JavaDoc m_workPath;
104
105     /**
106      * Bean constructor with cms object for path validation.
107      * <p>
108      *
109      * @param cms used to test the working path for valididty.
110      */

111     public CmsTagReplaceSettings(CmsObject cms) {
112
113         // Treemap guarantees no duplicate keys (ambiguous replacements) and the same default
114
// property ID's for the same replacement strings due to the ordering:
115
m_tags2replacementTags = new TreeMap JavaDoc();
116         m_cms = cms;
117         // all tags are registered for creation
118
m_nodeFactory = new PrototypicalNodeFactory();
119         m_deleteTags = new TreeSet JavaDoc(new Comparator JavaDoc() {
120
121             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
122
123                 return o1.getClass().getName().compareTo(o2.getClass().getName());
124             }
125         });
126     }
127
128     /**
129      * Returns the value of the shared {@link #PROPERTY_CONTENTOOLS_TAGREPLACE} to set on resources
130      * that have been processed with these settings.
131      * <p>
132      *
133      * @return the value of the shared {@link #PROPERTY_CONTENTOOLS_TAGREPLACE} to set on resources
134      * that have been processed with these settings.
135      */

136     public String JavaDoc getPropertyValueTagReplaceID() {
137
138         return m_propertyValueTagReplaceID;
139     }
140
141     /**
142      * Returns the replacements to perform in form of a comma-separated List of "key=value" tokens.
143      * <p>
144      *
145      * @return the replacements to perform in form of a comma-separated List of "key=value" tokens.
146      */

147     public SortedMap JavaDoc getReplacements() {
148
149         return m_tags2replacementTags;
150     }
151
152     /**
153      * Returns the path under which files will be processed recursively.
154      * <p>
155      *
156      * @return the path under which files will be processed recursively.
157      */

158     public String JavaDoc getWorkPath() {
159
160         return m_workPath;
161     }
162
163     /**
164      * Sets the value of the shared {@link #PROPERTY_CONTENTOOLS_TAGREPLACE} to set on resources
165      * that have been processed with these settings.
166      * <p>
167      *
168      * @param propertyValueTagreplaceID the value of the shared
169      * {@link #PROPERTY_CONTENTOOLS_TAGREPLACE} to set on resources that have been
170      * processed with these settings.
171      *
172      * @throws CmsIllegalArgumentException if the argument is not valid.
173      */

174     public void setPropertyValueTagReplaceID(String JavaDoc propertyValueTagreplaceID) throws CmsIllegalArgumentException {
175
176         if (CmsStringUtil.isEmptyOrWhitespaceOnly(propertyValueTagreplaceID)) {
177             m_propertyValueTagReplaceID = getDefaultTagReplaceID();
178         } else {
179             m_propertyValueTagReplaceID = propertyValueTagreplaceID;
180         }
181     }
182
183     /**
184      * Sets the replacements to perform in form of a comma-separated List of "key=value" tokens.
185      * <p>
186      *
187      * @param replacements the replacements to perform in form of a comma-separated List of
188      * "key=value" tokens.
189      *
190      * @throws CmsIllegalArgumentException if the argument is not valid.
191      */

192     public void setReplacements(SortedMap JavaDoc replacements) throws CmsIllegalArgumentException {
193
194         Iterator JavaDoc itMappings = replacements.entrySet().iterator();
195         Map.Entry JavaDoc entry;
196         String JavaDoc key, value;
197         while (itMappings.hasNext()) {
198             entry = (Map.Entry JavaDoc)itMappings.next();
199             key = (String JavaDoc)entry.getKey();
200             value = (String JavaDoc)entry.getValue();
201             if (CmsStringUtil.isEmptyOrWhitespaceOnly(value)) {
202                 // removal
203
Tag deleteTag;
204                 String JavaDoc tagName = (key).toLowerCase().trim();
205                 try {
206                     Vector JavaDoc attributeList = new Vector JavaDoc(1);
207                     Attribute tagNameAttribute = new Attribute();
208                     tagNameAttribute.setName(tagName);
209                     attributeList.add(tagNameAttribute);
210                     deleteTag = m_nodeFactory.createTagNode(null, 0, 0, attributeList);
211                     m_deleteTags.add(deleteTag);
212                     itMappings.remove();
213                 } catch (ParserException e) {
214                     CmsMessageContainer container = Messages.get().container(
215                         Messages.GUI_ERR_TAGREPLACE_TAGNAME_INVALID_1,
216                         tagName);
217                     throw new CmsIllegalArgumentException(container, e);
218                 }
219             } else {
220                 // nop
221
}
222             m_tags2replacementTags = replacements;
223         }
224         // if setPropertyValueTagReplaceID has been invoked earlier with empty value
225
// due to missing user input:
226
if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_propertyValueTagReplaceID)) {
227             // trigger computation of default ID by empty value:
228
setPropertyValueTagReplaceID(null);
229         }
230     }
231
232     /**
233      * Sets the path under which files will be processed recursively.
234      * <p>
235      *
236      * @param workPath the path under which files will be processed recursively.
237      *
238      * @throws CmsIllegalArgumentException if the argument is not valid.
239      */

240     public void setWorkPath(String JavaDoc workPath) throws CmsIllegalArgumentException {
241
242         if (CmsStringUtil.isEmptyOrWhitespaceOnly(workPath)) {
243             throw new CmsIllegalArgumentException(Messages.get().container(Messages.GUI_ERR_WIDGETVALUE_EMPTY_0));
244         }
245         // test if it is a valid path:
246
if (!m_cms.existsResource(workPath)) {
247             throw new CmsIllegalArgumentException(Messages.get().container(
248                 Messages.GUI_ERR_TAGREPLACE_WORKPATH_1,
249                 workPath));
250         }
251         m_workPath = workPath;
252
253     }
254
255     /**
256      * Returns the Set&lt;{@link org.htmlparser.Tag}&gt; to delete from transformed output.
257      * <p>
258      *
259      * @return the Set&lt;{@link org.htmlparser.Tag}&gt; to delete from transformed output.
260      */

261     protected Set JavaDoc getDeleteTags() {
262
263         return m_deleteTags;
264     }
265
266     /**
267      * Transforms the given Tag into the one it has to become by changing it's name and/or
268      * attributes.
269      * <p>
270      *
271      * @param tag the tag to be transformed.
272      *
273      * @return true if the given tag was modified, false else.
274      *
275      */

276     protected boolean replace(org.htmlparser.Tag tag) {
277
278         boolean result = false;
279         String JavaDoc tagName = tag.getTagName().trim().toLowerCase();
280         String JavaDoc replacementName = (String JavaDoc)m_tags2replacementTags.get(tagName);
281         if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(replacementName)) {
282             // judge this as a bug: getTagName() returns plain name, setter needs leading '/' for
283
// TODO: when updating htmlparser, verify if this has changed / been fixed
284
// closing tags
285
if (tag.isEndTag()) {
286                 replacementName = "/" + replacementName;
287             }
288             tag.setTagName(replacementName);
289             result = true;
290             // clear the attributes too:
291
List JavaDoc attributes = tag.getAttributesEx();
292             Iterator JavaDoc itAttribs = attributes.iterator();
293             // skip the "tagname attribute"....
294
itAttribs.next();
295             Attribute attribute;
296             String JavaDoc attName;
297             while (itAttribs.hasNext()) {
298                 attribute = (Attribute)itAttribs.next();
299                 attName = attribute.getName();
300                 if (CmsStringUtil.isEmptyOrWhitespaceOnly(attName)) {
301                     // this is the case for e.g. <h1 >
302
// -> becomes a tag with an attribute for tag name and a null name attribute
303
// (for the whitespace!)
304
} else {
305                     if (LOG.isDebugEnabled()) {
306                         LOG.debug(Messages.get().getBundle().key(
307                             Messages.LOG_DEBUG_TAGREPLACE_TAG_REMOVE_ATTRIB_2,
308                             attName, tag.getTagName()));
309
310                     }
311                     itAttribs.remove();
312                     if (LOG.isDebugEnabled()) {
313                         LOG.debug(Messages.get().getBundle().key(
314                             Messages.LOG_DEBUG_TAGREPLACE_TAG_REMOVE_ATTRIB_OK_0));
315                     }
316                 }
317             }
318         }
319         return result;
320     }
321
322     /**
323      * Computes the default property value for resources that have to be marked as "processed by
324      * these replacement settings".
325      * <p>
326      *
327      * The default value will be the alphabetically sorted string for replacments or the empty
328      * String if the replacements have not been set before.
329      * <p>
330      *
331      * @return the default property value for resources that have to be marked as "processed by
332      * these replacement settings".
333      */

334     private String JavaDoc getDefaultTagReplaceID() {
335
336         if (m_tags2replacementTags.size() == 0) {
337             return ""; // to know that no replacements were set before and the ID will still have
338
// to be computed later
339
} else {
340             StringBuffer JavaDoc result = new StringBuffer JavaDoc();
341             Map.Entry JavaDoc entry;
342             Iterator JavaDoc itEntries = m_tags2replacementTags.entrySet().iterator();
343             while (itEntries.hasNext()) {
344                 entry = (Map.Entry JavaDoc)itEntries.next();
345                 result.append(entry.getKey()).append('=').append(entry.getValue());
346                 if (itEntries.hasNext()) {
347                     result.append(',');
348                 }
349             }
350             return result.toString();
351
352         }
353     }
354 }
355
Popular Tags