1 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 ; 46 import java.util.Locale ; 47 48 import org.apache.commons.logging.Log; 49 50 import org.dom4j.Element; 51 52 61 public abstract class A_CmsXmlContentValue implements I_CmsXmlContentValue, I_CmsWidgetParameter { 62 63 64 private static final Log LOG = CmsLog.getLog(A_CmsXmlContentValue.class); 65 66 67 protected String m_defaultValue; 68 69 70 protected I_CmsXmlDocument m_document; 71 72 73 protected Element m_element; 74 75 76 protected Locale m_locale; 77 78 79 protected int m_maxOccurs; 80 81 82 protected int m_minOccurs; 83 84 85 protected String m_name; 86 87 88 private CmsXmlContentDefinition m_contentDefinition; 89 90 91 private String m_prefix = null; 92 93 97 protected A_CmsXmlContentValue() { 98 99 m_minOccurs = 0; 100 m_maxOccurs = Integer.MAX_VALUE; 101 } 102 103 111 protected A_CmsXmlContentValue(I_CmsXmlDocument document, Element element, Locale 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 129 protected A_CmsXmlContentValue(String name, String minOccurs, String 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 e) { 137 } 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 e) { 148 } 150 } 151 } 152 } 153 154 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 181 public int compareTo(Object 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 195 public boolean equals(Object 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 212 public Element generateXml(CmsObject cms, I_CmsXmlDocument document, Element root, Locale locale) { 213 214 Element element = root.addElement(getName()); 215 String 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 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 235 public CmsXmlContentDefinition getContentDefinition() { 236 237 return m_contentDefinition; 238 } 239 240 243 public String getDefault(CmsObject cms) { 244 245 return m_contentDefinition.getContentHandler().getDefault(cms, this, this.getLocale()); 246 } 247 248 251 public String getDefault(Locale locale) { 252 253 return m_defaultValue; 254 } 255 256 259 public I_CmsXmlDocument getDocument() { 260 261 return m_document; 262 } 263 264 267 public Element getElement() { 268 269 return m_element; 270 } 271 272 275 public String getId() { 276 277 StringBuffer result = new StringBuffer (128); 278 result.append(getTypeName()); 279 result.append('.'); 280 result.append(getPath().replace('[', '_').replace(']', '_').replace('/', '.')); 282 result.append('.'); 283 result.append(getIndex()); 284 return result.toString(); 285 } 286 287 290 public int getIndex() { 291 292 return m_element.getParent().elements(m_element.getQName()).indexOf(m_element); 293 } 294 295 298 public String getKey() { 299 300 StringBuffer result = new StringBuffer (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 314 public Locale getLocale() { 315 316 return m_locale; 317 } 318 319 322 public int getMaxIndex() { 323 324 return m_element.getParent().elements(m_element.getQName()).size(); 325 } 326 327 332 public int getMaxOccurs() { 333 334 return m_maxOccurs; 335 } 336 337 342 public int getMinOccurs() { 343 344 return m_minOccurs; 345 } 346 347 352 public String getName() { 353 354 return m_name; 355 } 356 357 360 public String getPath() { 361 362 String path = m_element.getUniquePath(); 363 int pos = path.indexOf('/', path.indexOf('/', 1) + 1) + 1; 365 path = path.substring(pos); 366 367 return CmsXmlUtils.createXpath(path, 1); 369 } 370 371 374 public String getPlainText(CmsObject cms) { 375 376 return null; 377 } 378 379 382 public boolean hasError() { 383 384 return false; 385 } 386 387 390 public int hashCode() { 391 392 return getTypeName().hashCode(); 393 } 394 395 398 public boolean isSimpleType() { 399 400 return true; 402 } 403 404 407 public void moveDown() { 408 409 int index = getIndex(); 410 if (index > 0) { 411 moveValue(false); 413 getDocument().initDocument(); 414 } 415 } 416 417 420 public void moveUp() { 421 422 int index = getIndex(); 423 int maxIndex = getMaxIndex(); 424 if (index < (maxIndex - 1)) { 425 moveValue(true); 427 getDocument().initDocument(); 428 } 429 } 430 431 434 public void setContentDefinition(CmsXmlContentDefinition contentDefinition) { 435 436 m_contentDefinition = contentDefinition; 437 } 438 439 444 public void setDefault(String defaultValue) { 445 446 m_defaultValue = defaultValue; 447 } 448 449 452 public void setKeyPrefix(String prefix) { 453 454 m_prefix = prefix; 455 } 456 457 460 public String toString() { 461 462 StringBuffer result = new StringBuffer (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 value; 471 try { 472 value = "'" + getStringValue(null) + "'"; 473 } catch (Exception e) { 474 value = "(CmsObject required to generate)"; 475 } 476 result.append(", value="); 477 result.append(value); 478 479 return result.toString(); 480 } 481 482 485 public boolean validateValue(String value) { 486 487 return true; 488 } 489 490 497 protected void moveValue(boolean moveUp) { 498 499 Element e = getElement(); 500 Element parent = e.getParent(); 501 List 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 517 protected String readSchemaDefinition(String schemaUri) throws CmsRuntimeException { 518 519 String schemaDefinition; 521 try { 522 schemaDefinition = CmsFileUtil.readFile(schemaUri, CmsEncoder.ENCODING_UTF_8); 523 } catch (Exception e) { 524 throw new CmsRuntimeException(Messages.get().container(Messages.ERR_XMLCONTENT_LOAD_SCHEMA_1, schemaUri), e); 525 } 526 return schemaDefinition; 527 } 528 } | Popular Tags |