KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > xml > types > CmsXmlHtmlValue


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/xml/types/CmsXmlHtmlValue.java,v $
3  * Date : $Date: 2006/03/27 14:53:03 $
4  * Version: $Revision: 1.35 $
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.xml.types;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.i18n.CmsEncoder;
36 import org.opencms.main.CmsLog;
37 import org.opencms.main.CmsRuntimeException;
38 import org.opencms.staticexport.CmsLink;
39 import org.opencms.staticexport.CmsLinkProcessor;
40 import org.opencms.staticexport.CmsLinkTable;
41 import org.opencms.util.CmsHtmlConverter;
42 import org.opencms.util.CmsHtmlExtractor;
43 import org.opencms.util.CmsStringUtil;
44 import org.opencms.xml.I_CmsXmlDocument;
45 import org.opencms.xml.page.CmsXmlPage;
46
47 import java.util.Iterator JavaDoc;
48 import java.util.Locale JavaDoc;
49
50 import org.apache.commons.logging.Log;
51
52 import org.dom4j.Attribute;
53 import org.dom4j.Element;
54 import org.htmlparser.util.ParserException;
55
56 /**
57  * Describes the XML content type "OpenCmsHtml".<p>
58  *
59  * @author Alexander Kandzior
60  *
61  * @version $Revision: 1.35 $
62  *
63  * @since 6.0.0
64  */

65 public class CmsXmlHtmlValue extends A_CmsXmlContentValue implements I_CmsXmlContentValue {
66
67     /** The name of this type as used in the XML schema. */
68     public static final String JavaDoc TYPE_NAME = "OpenCmsHtml";
69
70     /** The log object for this class. */
71     private static final Log LOG = CmsLog.getLog(CmsXmlHtmlValue.class);
72
73     /** The schema definition String is located in a text for easier editing. */
74     private static String JavaDoc m_schemaDefinition;
75
76     /** Base type for single type instances, required for XML pages. */
77     private static final I_CmsXmlSchemaType TYPE_BASE = new CmsXmlHtmlValue("base", "1", "1");
78
79     /** The String value of the element node. */
80     private String JavaDoc m_stringValue;
81
82     /**
83      * Creates a new, empty schema type descriptor of type "OpenCmsHtml".<p>
84      */

85     public CmsXmlHtmlValue() {
86
87         // empty constructor is required for class registration
88
}
89
90     /**
91      * Creates a new XML content value of type "OpenCmsHtml".<p>
92      *
93      * @param document the XML content instance this value belongs to
94      * @param element the XML element that contains this value
95      * @param locale the locale this value is created for
96      */

97     public CmsXmlHtmlValue(I_CmsXmlDocument document, Element element, Locale JavaDoc locale) {
98
99         super(document, element, locale, TYPE_BASE);
100     }
101
102     /**
103      * Creates a new XML content value of type "OpenCmsHtml".<p>
104      *
105      * @param document the XML content instance this value belongs to
106      * @param element the XML element that contains this value
107      * @param locale the locale this value is created for
108      * @param type the type instance to create the value for
109      */

110     public CmsXmlHtmlValue(I_CmsXmlDocument document, Element element, Locale JavaDoc locale, I_CmsXmlSchemaType type) {
111
112         super(document, element, locale, type);
113     }
114
115     /**
116      * Creates a new schema type descriptor for the type "OpenCmsHtml".<p>
117      *
118      * @param name the name of the XML node containing the value according to the XML schema
119      * @param minOccurs minimum number of occurences of this type according to the XML schema
120      * @param maxOccurs maximum number of occurences of this type according to the XML schema
121      */

122     public CmsXmlHtmlValue(String JavaDoc name, String JavaDoc minOccurs, String JavaDoc maxOccurs) {
123
124         super(name, minOccurs, maxOccurs);
125     }
126
127     /**
128      * @see org.opencms.xml.types.A_CmsXmlContentValue#createValue(I_CmsXmlDocument, org.dom4j.Element, Locale)
129      */

130     public I_CmsXmlContentValue createValue(I_CmsXmlDocument document, Element element, Locale JavaDoc locale) {
131
132         return new CmsXmlHtmlValue(document, element, locale, this);
133     }
134
135     /**
136      * @see org.opencms.xml.types.I_CmsXmlSchemaType#generateXml(org.opencms.file.CmsObject, org.opencms.xml.I_CmsXmlDocument, org.dom4j.Element, java.util.Locale)
137      */

138     public Element generateXml(CmsObject cms, I_CmsXmlDocument document, Element root, Locale JavaDoc locale) {
139
140         Element element = root.addElement(getName());
141         int index = element.getParent().elements(element.getQName()).indexOf(element);
142         element.addAttribute(CmsXmlPage.ATTRIBUTE_NAME, getName() + index);
143         element.addElement(CmsXmlPage.NODE_LINKS);
144         element.addElement(CmsXmlPage.NODE_CONTENT);
145
146         // get the default value from the content handler
147
String JavaDoc defaultValue = document.getContentDefinition().getContentHandler().getDefault(cms, this, locale);
148         if (defaultValue != null) {
149             try {
150                 I_CmsXmlContentValue value = createValue(document, element, locale);
151                 value.setStringValue(cms, defaultValue);
152             } catch (CmsRuntimeException e) {
153                 // should not happen if default value is correct
154
LOG.error(
155                     Messages.get().getBundle().key(Messages.ERR_XMLCONTENT_INVALID_ELEM_DEFAULT_1, defaultValue),
156                     e);
157                 element.clearContent();
158             }
159         }
160         return element;
161     }
162
163     /**
164      * Returns the link table of this XML page element.<p>
165      *
166      * @return the link table of this XML page element
167      */

168     public CmsLinkTable getLinkTable() {
169
170         CmsLinkTable linkTable = new CmsLinkTable();
171
172         Element links = m_element.element(CmsXmlPage.NODE_LINKS);
173
174         if (links != null) {
175             for (Iterator JavaDoc i = links.elementIterator(CmsXmlPage.NODE_LINK); i.hasNext();) {
176
177                 Element lelem = (Element)i.next();
178                 Attribute lname = lelem.attribute(CmsXmlPage.ATTRIBUTE_NAME);
179                 Attribute type = lelem.attribute(CmsXmlPage.ATTRIBUTE_TYPE);
180                 Attribute internal = lelem.attribute(CmsXmlPage.ATTRIBUTE_INTERNAL);
181
182                 Element target = lelem.element(CmsXmlPage.NODE_TARGET);
183                 Element anchor = lelem.element(CmsXmlPage.NODE_ANCHOR);
184                 Element query = lelem.element(CmsXmlPage.NODE_QUERY);
185
186                 CmsLink link = new CmsLink(
187                     lelem,
188                     lname.getValue(),
189                     type.getValue(),
190                     (target != null) ? target.getText() : null,
191                     (anchor != null) ? anchor.getText() : null,
192                     (query != null) ? query.getText() : null,
193                     Boolean.valueOf(internal.getValue()).booleanValue());
194
195                 linkTable.addLink(link);
196             }
197         }
198         return linkTable;
199     }
200
201     /**
202      * @see org.opencms.xml.types.I_CmsXmlContentValue#getPlainText(org.opencms.file.CmsObject)
203      */

204     public String JavaDoc getPlainText(CmsObject cms) {
205
206         try {
207             return CmsHtmlExtractor.extractText(this.getStringValue(cms), m_document.getEncoding());
208         } catch (Exception JavaDoc exc) {
209             return null;
210         }
211     }
212
213     /**
214      * @see org.opencms.xml.types.I_CmsXmlSchemaType#getSchemaDefinition()
215      */

216     public String JavaDoc getSchemaDefinition() {
217
218         // the schema definition is located in a separate file for easier editing
219
if (m_schemaDefinition == null) {
220             m_schemaDefinition = readSchemaDefinition("org/opencms/xml/types/XmlHtmlValue.xsd");
221         }
222         return m_schemaDefinition;
223     }
224
225     /**
226      * @see org.opencms.xml.types.I_CmsXmlContentValue#getStringValue(org.opencms.file.CmsObject)
227      */

228     public String JavaDoc getStringValue(CmsObject cms) {
229
230         if (m_stringValue == null) {
231             m_stringValue = createStringValue(cms, m_document);
232         }
233
234         return m_stringValue;
235     }
236
237     /**
238      * @see org.opencms.xml.types.A_CmsXmlContentValue#getTypeName()
239      */

240     public String JavaDoc getTypeName() {
241
242         return TYPE_NAME;
243     }
244
245     /**
246      * @see org.opencms.xml.types.A_CmsXmlContentValue#newInstance(java.lang.String, java.lang.String, java.lang.String)
247      */

248     public I_CmsXmlSchemaType newInstance(String JavaDoc name, String JavaDoc minOccurs, String JavaDoc maxOccurs) {
249
250         return new CmsXmlHtmlValue(name, minOccurs, maxOccurs);
251     }
252
253     /**
254      * @see org.opencms.xml.types.I_CmsXmlContentValue#setStringValue(org.opencms.file.CmsObject, java.lang.String)
255      */

256     public void setStringValue(CmsObject cms, String JavaDoc value) {
257
258         Element content = m_element.element(CmsXmlPage.NODE_CONTENT);
259         Element links = m_element.element(CmsXmlPage.NODE_LINKS);
260         CmsLinkProcessor linkProcessor = null;
261
262         String JavaDoc encoding = m_document.getEncoding();
263         linkProcessor = m_document.getLinkProcessor(cms, new CmsLinkTable());
264
265         if (encoding != null) {
266             // ensure all chars in the given content are valid chars for the selected charset
267
value = CmsEncoder.adjustHtmlEncoding(value, encoding);
268         }
269
270         // remove unnecessary tags if required
271
String JavaDoc contentConversion = m_document.getConversion();
272         if (CmsHtmlConverter.isConversionEnabled(contentConversion)) {
273             CmsHtmlConverter converter = new CmsHtmlConverter(encoding, contentConversion);
274             value = converter.convertToStringSilent(value);
275         }
276
277         if (linkProcessor != null) {
278             try {
279                 // replace links in HTML by macros and fill link table
280
value = linkProcessor.replaceLinks(value);
281             } catch (Exception JavaDoc exc) {
282                 throw new CmsRuntimeException(Messages.get().container(Messages.ERR_HTML_DATA_PROCESSING_0));
283             }
284         }
285
286         content.clearContent();
287         links.clearContent();
288
289         if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(value)) {
290             content.addCDATA(value);
291             if (linkProcessor != null) {
292                 // may be null in case of default value generation (i.e. setStringValue(String) was called)
293

294                 CmsLinkTable linkTable = linkProcessor.getLinkTable();
295                 for (Iterator JavaDoc i = linkTable.iterator(); i.hasNext();) {
296                     CmsLink link = (CmsLink)i.next();
297
298                     Element linkElement = links.addElement(CmsXmlPage.NODE_LINK).addAttribute(
299                         CmsXmlPage.ATTRIBUTE_NAME,
300                         link.getName()).addAttribute(CmsXmlPage.ATTRIBUTE_TYPE, link.getType()).addAttribute(
301                         CmsXmlPage.ATTRIBUTE_INTERNAL,
302                         Boolean.toString(link.isInternal()));
303
304                     linkElement.addElement(CmsXmlPage.NODE_TARGET).addCDATA(link.getTarget());
305
306                     if (link.getAnchor() != null) {
307                         linkElement.addElement(CmsXmlPage.NODE_ANCHOR).addCDATA(link.getAnchor());
308                     }
309
310                     if (link.getQuery() != null) {
311                         linkElement.addElement(CmsXmlPage.NODE_QUERY).addCDATA(link.getQuery());
312                     }
313                 }
314             }
315         }
316
317         // ensure the String value is re-calculated next time
318
m_stringValue = null;
319     }
320
321     /**
322      * Creates the String value for this HTML value element.<p>
323      *
324      * @param cms an initialized instance of a CmsObject
325      * @param document the XML document this value belongs to
326      *
327      * @return the String value for this HTML value element
328      */

329     private String JavaDoc createStringValue(CmsObject cms, I_CmsXmlDocument document) {
330
331         Element data = m_element.element(CmsXmlPage.NODE_CONTENT);
332         Attribute enabled = m_element.attribute(CmsXmlPage.ATTRIBUTE_ENABLED);
333
334         String JavaDoc content = "";
335         if (enabled == null || Boolean.valueOf(enabled.getText()).booleanValue()) {
336
337             content = data.getText();
338
339             CmsLinkTable linkTable = getLinkTable();
340             if (!linkTable.isEmpty()) {
341
342                 // link processing: replace macros with links
343
CmsLinkProcessor linkProcessor = document.getLinkProcessor(cms, linkTable);
344                 try {
345                     content = linkProcessor.processLinks(content);
346                 } catch (ParserException e) {
347                     // should better not happen
348
LOG.error(Messages.get().getBundle().key(Messages.ERR_XMLCONTENT_LINK_PROCESS_FAILED_0), e);
349                 }
350             }
351         }
352         return content;
353     }
354 }
Popular Tags