KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/xml/types/A_CmsXmlContentValue.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.util.CmsFileUtil;
39 import org.opencms.util.CmsStringUtil;
40 import org.opencms.widgets.I_CmsWidgetParameter;
41 import org.opencms.xml.CmsXmlContentDefinition;
42 import org.opencms.xml.CmsXmlUtils;
43 import org.opencms.xml.I_CmsXmlDocument;
44
45 import java.util.List JavaDoc;
46 import java.util.Locale JavaDoc;
47
48 import org.apache.commons.logging.Log;
49
50 import org.dom4j.Element;
51
52 /**
53  * Base class for XML content value implementations.<p>
54  *
55  * @author Alexander Kandzior
56  *
57  * @version $Revision: 1.35 $
58  *
59  * @since 6.0.0
60  */

61 public abstract class A_CmsXmlContentValue implements I_CmsXmlContentValue, I_CmsWidgetParameter {
62
63     /** The log object for this class. */
64     private static final Log LOG = CmsLog.getLog(A_CmsXmlContentValue.class);
65
66     /** The default value for nodes of this value. */
67     protected String JavaDoc m_defaultValue;
68
69     /** The XML content instance this value belongs to. */
70     protected I_CmsXmlDocument m_document;
71
72     /** The XML element node that contains this value. */
73     protected Element m_element;
74
75     /** The locale this value was generated for. */
76     protected Locale JavaDoc m_locale;
77
78     /** The maximum occurences of this value according to the parent schema. */
79     protected int m_maxOccurs;
80
81     /** The minimum occurences of this value according to the parent schema. */
82     protected int m_minOccurs;
83
84     /** The configured XML node name of this value. */
85     protected String JavaDoc m_name;
86
87     /** The content definition this schema type belongs to. */
88     private CmsXmlContentDefinition m_contentDefinition;
89
90     /** Optional localized key prefix identificator. */
91     private String JavaDoc m_prefix = null;
92
93     /**
94      * Default constructor for a xml content type
95      * that initializes some internal values.<p>
96      */

97     protected A_CmsXmlContentValue() {
98
99         m_minOccurs = 0;
100         m_maxOccurs = Integer.MAX_VALUE;
101     }
102
103     /**
104      * Initializes the required members for this XML content value.<p>
105      *
106      * @param document the XML content instance this value belongs to
107      * @param element the XML element that contains this value
108      * @param locale the locale this value is created for
109      * @param type the type instance to create the value for
110      */

111     protected A_CmsXmlContentValue(I_CmsXmlDocument document, Element element, Locale JavaDoc locale, I_CmsXmlSchemaType type) {
112
113         m_element = element;
114         m_name = element.getName();
115         m_document = document;
116         m_locale = locale;
117         m_minOccurs = type.getMinOccurs();
118         m_maxOccurs = type.getMaxOccurs();
119         m_contentDefinition = type.getContentDefinition();
120     }
121
122     /**
123      * Initializes the schema type descriptor values for this type descriptor.<p>
124      *
125      * @param name the name of the XML node containing the value according to the XML schema
126      * @param minOccurs minimum number of occurences of this type according to the XML schema
127      * @param maxOccurs maximum number of occurences of this type according to the XML schema
128      */

129     protected A_CmsXmlContentValue(String JavaDoc name, String JavaDoc minOccurs, String JavaDoc maxOccurs) {
130
131         m_name = name;
132         m_minOccurs = 1;
133         if (CmsStringUtil.isNotEmpty(minOccurs)) {
134             try {
135                 m_minOccurs = Integer.valueOf(minOccurs).intValue();
136             } catch (NumberFormatException JavaDoc e) {
137                 // ignore
138
}
139         }
140         m_maxOccurs = 1;
141         if (CmsStringUtil.isNotEmpty(maxOccurs)) {
142             if (CmsXmlContentDefinition.XSD_ATTRIBUTE_VALUE_UNBOUNDED.equals(maxOccurs)) {
143                 m_maxOccurs = Integer.MAX_VALUE;
144             } else {
145                 try {
146                     m_maxOccurs = Integer.valueOf(maxOccurs).intValue();
147                 } catch (NumberFormatException JavaDoc e) {
148                     // ignore
149
}
150             }
151         }
152     }
153
154     /**
155      * Appends an element XML representation of this type to the given root node.<p>
156      *
157      * @param root the element to append the XML to
158      */

159     public void appendXmlSchema(Element root) {
160
161         Element element = root.addElement(CmsXmlContentDefinition.XSD_NODE_ELEMENT);
162         element.addAttribute(CmsXmlContentDefinition.XSD_ATTRIBUTE_NAME, getName());
163         element.addAttribute(CmsXmlContentDefinition.XSD_ATTRIBUTE_TYPE, getTypeName());
164         if ((getMinOccurs() > 1) || (getMinOccurs() == 0)) {
165             element.addAttribute(CmsXmlContentDefinition.XSD_ATTRIBUTE_MIN_OCCURS, String.valueOf(getMinOccurs()));
166         }
167         if (getMaxOccurs() > 1) {
168             if (getMaxOccurs() == Integer.MAX_VALUE) {
169                 element.addAttribute(
170                     CmsXmlContentDefinition.XSD_ATTRIBUTE_MAX_OCCURS,
171                     CmsXmlContentDefinition.XSD_ATTRIBUTE_VALUE_UNBOUNDED);
172             } else {
173                 element.addAttribute(CmsXmlContentDefinition.XSD_ATTRIBUTE_MAX_OCCURS, String.valueOf(getMaxOccurs()));
174             }
175         }
176     }
177
178     /**
179      * @see java.lang.Comparable#compareTo(java.lang.Object)
180      */

181     public int compareTo(Object JavaDoc obj) {
182
183         if (obj == this) {
184             return 0;
185         }
186         if (obj instanceof I_CmsXmlSchemaType) {
187             return getTypeName().compareTo(((I_CmsXmlSchemaType)obj).getTypeName());
188         }
189         return 0;
190     }
191
192     /**
193      * @see java.lang.Object#equals(java.lang.Object)
194      */

195     public boolean equals(Object JavaDoc obj) {
196
197         if (obj == this) {
198             return true;
199         }
200         if (obj instanceof I_CmsXmlSchemaType) {
201             I_CmsXmlSchemaType other = (I_CmsXmlSchemaType)obj;
202             return (getName().equals(other.getName())
203                 && getTypeName().equals(other.getTypeName())
204                 && (getMinOccurs() == other.getMinOccurs()) && (getMaxOccurs() == other.getMaxOccurs()));
205         }
206         return false;
207     }
208
209     /**
210      * @see org.opencms.xml.types.I_CmsXmlSchemaType#generateXml(org.opencms.file.CmsObject, org.opencms.xml.I_CmsXmlDocument, org.dom4j.Element, java.util.Locale)
211      */

212     public Element generateXml(CmsObject cms, I_CmsXmlDocument document, Element root, Locale JavaDoc locale) {
213
214         Element element = root.addElement(getName());
215         // get the default value from the content handler
216
String JavaDoc defaultValue = document.getContentDefinition().getContentHandler().getDefault(cms, this, locale);
217         if (defaultValue != null) {
218             try {
219                 I_CmsXmlContentValue value = createValue(document, element, locale);
220                 value.setStringValue(cms, defaultValue);
221             } catch (CmsRuntimeException e) {
222                 // should not happen if default value is correct
223
LOG.error(
224                     Messages.get().getBundle().key(Messages.ERR_XMLCONTENT_INVALID_ELEM_DEFAULT_1, defaultValue),
225                     e);
226                 element.clearContent();
227             }
228         }
229         return element;
230     }
231
232     /**
233      * @see org.opencms.xml.types.I_CmsXmlSchemaType#getContentDefinition()
234      */

235     public CmsXmlContentDefinition getContentDefinition() {
236
237         return m_contentDefinition;
238     }
239
240     /**
241      * @see org.opencms.widgets.I_CmsWidgetParameter#getDefault(org.opencms.file.CmsObject)
242      */

243     public String JavaDoc getDefault(CmsObject cms) {
244
245         return m_contentDefinition.getContentHandler().getDefault(cms, this, this.getLocale());
246     }
247
248     /**
249      * @see org.opencms.xml.types.I_CmsXmlSchemaType#getDefault(java.util.Locale)
250      */

251     public String JavaDoc getDefault(Locale JavaDoc locale) {
252
253         return m_defaultValue;
254     }
255
256     /**
257      * @see org.opencms.xml.types.I_CmsXmlContentValue#getDocument()
258      */

259     public I_CmsXmlDocument getDocument() {
260
261         return m_document;
262     }
263
264     /**
265      * @see org.opencms.xml.types.I_CmsXmlContentValue#getElement()
266      */

267     public Element getElement() {
268
269         return m_element;
270     }
271
272     /**
273      * @see org.opencms.widgets.I_CmsWidgetParameter#getId()
274      */

275     public String JavaDoc getId() {
276
277         StringBuffer JavaDoc result = new StringBuffer JavaDoc(128);
278         result.append(getTypeName());
279         result.append('.');
280         // the '[', ']' and '/' chars from the xpath are invalid for html id's
281
result.append(getPath().replace('[', '_').replace(']', '_').replace('/', '.'));
282         result.append('.');
283         result.append(getIndex());
284         return result.toString();
285     }
286
287     /**
288      * @see org.opencms.xml.types.I_CmsXmlContentValue#getIndex()
289      */

290     public int getIndex() {
291
292         return m_element.getParent().elements(m_element.getQName()).indexOf(m_element);
293     }
294
295     /**
296      * @see org.opencms.widgets.I_CmsWidgetParameter#getKey()
297      */

298     public String JavaDoc getKey() {
299
300         StringBuffer JavaDoc result = new StringBuffer JavaDoc(128);
301         if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_prefix)) {
302             result.append(m_prefix);
303             result.append('.');
304         }
305         result.append(m_contentDefinition.getInnerName());
306         result.append('.');
307         result.append(getName());
308         return result.toString();
309     }
310
311     /**
312      * @see org.opencms.xml.types.I_CmsXmlContentValue#getLocale()
313      */

314     public Locale JavaDoc getLocale() {
315
316         return m_locale;
317     }
318
319     /**
320      * @see org.opencms.xml.types.I_CmsXmlContentValue#getMaxIndex()
321      */

322     public int getMaxIndex() {
323
324         return m_element.getParent().elements(m_element.getQName()).size();
325     }
326
327     /**
328      * Returns the maximum occurences of this type.<p>
329      *
330      * @return the maximum occurences of this type
331      */

332     public int getMaxOccurs() {
333
334         return m_maxOccurs;
335     }
336
337     /**
338      * Returns the minimum occurences of this type.<p>
339      *
340      * @return the minimum occurences of this type
341      */

342     public int getMinOccurs() {
343
344         return m_minOccurs;
345     }
346
347     /**
348      * Returns the name.<p>
349      *
350      * @return the name
351      */

352     public String JavaDoc getName() {
353
354         return m_name;
355     }
356
357     /**
358      * @see org.opencms.xml.types.I_CmsXmlContentValue#getPath()
359      */

360     public String JavaDoc getPath() {
361
362         String JavaDoc path = m_element.getUniquePath();
363         // must remove the first 2 nodes because these are not required for XML content values
364
int pos = path.indexOf('/', path.indexOf('/', 1) + 1) + 1;
365         path = path.substring(pos);
366
367         // ensure all path elements have an index, even though this may not be required
368
return CmsXmlUtils.createXpath(path, 1);
369     }
370
371     /**
372      * @see org.opencms.xml.types.I_CmsXmlContentValue#getPlainText(org.opencms.file.CmsObject)
373      */

374     public String JavaDoc getPlainText(CmsObject cms) {
375
376         return null;
377     }
378
379     /**
380      * @see org.opencms.widgets.I_CmsWidgetParameter#hasError()
381      */

382     public boolean hasError() {
383
384         return false;
385     }
386
387     /**
388      * @see java.lang.Object#hashCode()
389      */

390     public int hashCode() {
391
392         return getTypeName().hashCode();
393     }
394
395     /**
396      * @see org.opencms.xml.types.I_CmsXmlSchemaType#isSimpleType()
397      */

398     public boolean isSimpleType() {
399
400         // the abstract base type should be used for simple types only
401
return true;
402     }
403
404     /**
405      * @see org.opencms.xml.types.I_CmsXmlContentValue#moveDown()
406      */

407     public void moveDown() {
408
409         int index = getIndex();
410         if (index > 0) {
411             // only move down if this element is not already at the first index position
412
moveValue(false);
413             getDocument().initDocument();
414         }
415     }
416
417     /**
418      * @see org.opencms.xml.types.I_CmsXmlContentValue#moveUp()
419      */

420     public void moveUp() {
421
422         int index = getIndex();
423         int maxIndex = getMaxIndex();
424         if (index < (maxIndex - 1)) {
425             // only move up if this element is not already at the last index position
426
moveValue(true);
427             getDocument().initDocument();
428         }
429     }
430
431     /**
432      * @see org.opencms.xml.types.I_CmsXmlSchemaType#setContentDefinition(org.opencms.xml.CmsXmlContentDefinition)
433      */

434     public void setContentDefinition(CmsXmlContentDefinition contentDefinition) {
435
436         m_contentDefinition = contentDefinition;
437     }
438
439     /**
440      * Sets the default value for a node of this type.<p>
441      *
442      * @param defaultValue the default value to set
443      */

444     public void setDefault(String JavaDoc defaultValue) {
445
446         m_defaultValue = defaultValue;
447     }
448
449     /**
450      * @see org.opencms.widgets.I_CmsWidgetParameter#setKeyPrefix(java.lang.String)
451      */

452     public void setKeyPrefix(String JavaDoc prefix) {
453
454         m_prefix = prefix;
455     }
456
457     /**
458      * @see java.lang.Object#toString()
459      */

460     public String JavaDoc toString() {
461
462         StringBuffer JavaDoc result = new StringBuffer JavaDoc(128);
463         result.append(getClass().getName());
464         result.append(": name=");
465         result.append(getName());
466         result.append(", type=");
467         result.append(getTypeName());
468         result.append(", path=");
469         result.append(getPath());
470         String JavaDoc value;
471         try {
472             value = "'" + getStringValue(null) + "'";
473         } catch (Exception JavaDoc e) {
474             value = "(CmsObject required to generate)";
475         }
476         result.append(", value=");
477         result.append(value);
478
479         return result.toString();
480     }
481
482     /**
483      * @see org.opencms.xml.types.I_CmsXmlSchemaType#validateValue(java.lang.String)
484      */

485     public boolean validateValue(String JavaDoc value) {
486
487         return true;
488     }
489
490     /**
491      * Moves this XML content element up or down in the XML document.<p>
492      *
493      * Please note: No check is performed if the move violates the XML document schema!<p>
494      *
495      * @param moveUp if true, move up, otherwise move down
496      */

497     protected void moveValue(boolean moveUp) {
498
499         Element e = getElement();
500         Element parent = e.getParent();
501         List JavaDoc siblings = parent.elements();
502         int idx = siblings.indexOf(e);
503         int newIdx = moveUp ? idx + 1 : idx - 1;
504         siblings.remove(idx);
505         siblings.add(newIdx, e);
506     }
507
508     /**
509      * Convenience method to loads the XML schema definition for this value type from an external file.<p>
510      *
511      * @param schemaUri the schema uri to load the XML schema file from
512      *
513      * @return the loaded XML schema
514      *
515      * @throws CmsRuntimeException if something goes wrong
516      */

517     protected String JavaDoc readSchemaDefinition(String JavaDoc schemaUri) throws CmsRuntimeException {
518
519         // the schema definition is located in a separate file for easier editing
520
String JavaDoc schemaDefinition;
521         try {
522             schemaDefinition = CmsFileUtil.readFile(schemaUri, CmsEncoder.ENCODING_UTF_8);
523         } catch (Exception JavaDoc e) {
524             throw new CmsRuntimeException(Messages.get().container(Messages.ERR_XMLCONTENT_LOAD_SCHEMA_1, schemaUri), e);
525         }
526         return schemaDefinition;
527     }
528 }
Popular Tags