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.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 ; 48 import java.util.Locale ; 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 65 public class CmsXmlHtmlValue extends A_CmsXmlContentValue implements I_CmsXmlContentValue { 66 67 68 public static final String TYPE_NAME = "OpenCmsHtml"; 69 70 71 private static final Log LOG = CmsLog.getLog(CmsXmlHtmlValue.class); 72 73 74 private static String m_schemaDefinition; 75 76 77 private static final I_CmsXmlSchemaType TYPE_BASE = new CmsXmlHtmlValue("base", "1", "1"); 78 79 80 private String m_stringValue; 81 82 85 public CmsXmlHtmlValue() { 86 87 } 89 90 97 public CmsXmlHtmlValue(I_CmsXmlDocument document, Element element, Locale locale) { 98 99 super(document, element, locale, TYPE_BASE); 100 } 101 102 110 public CmsXmlHtmlValue(I_CmsXmlDocument document, Element element, Locale locale, I_CmsXmlSchemaType type) { 111 112 super(document, element, locale, type); 113 } 114 115 122 public CmsXmlHtmlValue(String name, String minOccurs, String maxOccurs) { 123 124 super(name, minOccurs, maxOccurs); 125 } 126 127 130 public I_CmsXmlContentValue createValue(I_CmsXmlDocument document, Element element, Locale locale) { 131 132 return new CmsXmlHtmlValue(document, element, locale, this); 133 } 134 135 138 public Element generateXml(CmsObject cms, I_CmsXmlDocument document, Element root, Locale 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 String 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 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 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 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 204 public String getPlainText(CmsObject cms) { 205 206 try { 207 return CmsHtmlExtractor.extractText(this.getStringValue(cms), m_document.getEncoding()); 208 } catch (Exception exc) { 209 return null; 210 } 211 } 212 213 216 public String getSchemaDefinition() { 217 218 if (m_schemaDefinition == null) { 220 m_schemaDefinition = readSchemaDefinition("org/opencms/xml/types/XmlHtmlValue.xsd"); 221 } 222 return m_schemaDefinition; 223 } 224 225 228 public String 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 240 public String getTypeName() { 241 242 return TYPE_NAME; 243 } 244 245 248 public I_CmsXmlSchemaType newInstance(String name, String minOccurs, String maxOccurs) { 249 250 return new CmsXmlHtmlValue(name, minOccurs, maxOccurs); 251 } 252 253 256 public void setStringValue(CmsObject cms, String 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 encoding = m_document.getEncoding(); 263 linkProcessor = m_document.getLinkProcessor(cms, new CmsLinkTable()); 264 265 if (encoding != null) { 266 value = CmsEncoder.adjustHtmlEncoding(value, encoding); 268 } 269 270 String 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 value = linkProcessor.replaceLinks(value); 281 } catch (Exception 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 294 CmsLinkTable linkTable = linkProcessor.getLinkTable(); 295 for (Iterator 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 m_stringValue = null; 319 } 320 321 329 private String 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 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 CmsLinkProcessor linkProcessor = document.getLinkProcessor(cms, linkTable); 344 try { 345 content = linkProcessor.processLinks(content); 346 } catch (ParserException e) { 347 LOG.error(Messages.get().getBundle().key(Messages.ERR_XMLCONTENT_LINK_PROCESS_FAILED_0), e); 349 } 350 } 351 } 352 return content; 353 } 354 } | Popular Tags |