1 31 32 package org.opencms.file.types; 33 34 import org.opencms.configuration.CmsConfigurationException; 35 import org.opencms.db.CmsSecurityManager; 36 import org.opencms.file.CmsFile; 37 import org.opencms.file.CmsObject; 38 import org.opencms.file.CmsResource; 39 import org.opencms.file.CmsResourceFilter; 40 import org.opencms.loader.CmsXmlPageLoader; 41 import org.opencms.main.CmsException; 42 import org.opencms.main.CmsLog; 43 import org.opencms.main.OpenCms; 44 import org.opencms.security.CmsPermissionSet; 45 import org.opencms.staticexport.CmsLink; 46 import org.opencms.staticexport.CmsLinkTable; 47 import org.opencms.util.CmsHtmlConverter; 48 import org.opencms.validation.I_CmsXmlDocumentLinkValidatable; 49 import org.opencms.xml.CmsXmlEntityResolver; 50 import org.opencms.xml.CmsXmlException; 51 import org.opencms.xml.page.CmsXmlPage; 52 import org.opencms.xml.page.CmsXmlPageFactory; 53 54 import java.util.ArrayList ; 55 import java.util.Collections ; 56 import java.util.Iterator ; 57 import java.util.List ; 58 import java.util.Locale ; 59 60 import org.apache.commons.logging.Log; 61 62 71 public class CmsResourceTypeXmlPage extends A_CmsResourceType implements I_CmsXmlDocumentLinkValidatable { 72 73 74 private static final Log LOG = CmsLog.getLog(CmsResourceTypeXmlPage.class); 75 76 77 private static boolean m_staticFrozen; 78 79 80 private static int m_staticTypeId; 81 82 83 private static final int RESOURCE_TYPE_ID = 6; 84 85 86 private static final String RESOURCE_TYPE_NAME = "xmlpage"; 87 88 91 public CmsResourceTypeXmlPage() { 92 93 super(); 94 m_typeId = RESOURCE_TYPE_ID; 95 m_typeName = RESOURCE_TYPE_NAME; 96 } 97 98 103 public static int getStaticTypeId() { 104 105 return m_staticTypeId; 106 } 107 108 113 public static String getStaticTypeName() { 114 115 return RESOURCE_TYPE_NAME; 116 } 117 118 121 public List findLinks(CmsObject cms, CmsResource resource) { 122 123 List links = new ArrayList (); 124 CmsFile file = null; 125 CmsXmlPage xmlPage = null; 126 List locales = null; 127 List elementNames = null; 128 String elementName = null; 129 CmsLinkTable linkTable = null; 130 CmsLink link = null; 131 132 try { 133 file = cms.readFile( 134 cms.getRequestContext().removeSiteRoot(resource.getRootPath()), 135 CmsResourceFilter.IGNORE_EXPIRATION); 136 } catch (CmsException e) { 137 if (LOG.isErrorEnabled()) { 138 LOG.error(org.opencms.db.Messages.get().getBundle().key( 139 org.opencms.db.Messages.ERR_READ_RESOURCE_1, 140 cms.getSitePath(resource)), e); 141 } 142 143 return Collections.EMPTY_LIST; 144 } 145 146 try { 147 xmlPage = CmsXmlPageFactory.unmarshal(cms, file); 148 locales = xmlPage.getLocales(); 149 150 Iterator i = locales.iterator(); 152 while (i.hasNext()) { 153 Locale locale = (Locale )i.next(); 154 elementNames = xmlPage.getNames(locale); 155 156 Iterator j = elementNames.iterator(); 158 while (j.hasNext()) { 159 elementName = (String )j.next(); 160 linkTable = xmlPage.getLinkTable(elementName, locale); 161 162 Iterator k = linkTable.iterator(); 164 while (k.hasNext()) { 165 link = (CmsLink)k.next(); 166 167 if (link.isInternal()) { 169 links.add(link.getTarget()); 170 } 171 } 172 } 173 } 174 } catch (CmsXmlException e) { 175 if (LOG.isErrorEnabled()) { 176 LOG.error( 177 Messages.get().getBundle().key(Messages.ERR_PROCESS_HTML_CONTENT_1, cms.getSitePath(resource)), 178 e); 179 } 180 181 return Collections.EMPTY_LIST; 182 } 183 184 return links; 185 } 186 187 190 public String getCachePropertyDefault() { 191 192 return "element;locale;"; 193 } 194 195 198 public int getLoaderId() { 199 200 return CmsXmlPageLoader.RESOURCE_LOADER_ID; 201 } 202 203 206 public void initConfiguration(String name, String id, String className) throws CmsConfigurationException { 207 208 if ((OpenCms.getRunLevel() > OpenCms.RUNLEVEL_2_INITIALIZING) && m_staticFrozen) { 209 throw new CmsConfigurationException(Messages.get().container( 211 Messages.ERR_CONFIG_FROZEN_3, 212 this.getClass().getName(), 213 getStaticTypeName(), 214 new Integer (getStaticTypeId()))); 215 } 216 217 if (!RESOURCE_TYPE_NAME.equals(name)) { 218 throw new CmsConfigurationException(Messages.get().container( 220 Messages.ERR_INVALID_RESTYPE_CONFIG_NAME_3, 221 this.getClass().getName(), 222 RESOURCE_TYPE_NAME, 223 name)); 224 } 225 226 m_staticFrozen = true; 228 229 super.initConfiguration(RESOURCE_TYPE_NAME, id, className); 230 m_staticTypeId = m_typeId; 232 } 233 234 237 public boolean isDirectEditable() { 238 239 return true; 240 } 241 242 245 public CmsFile writeFile(CmsObject cms, CmsSecurityManager securityManager, CmsFile resource) throws CmsException { 246 247 securityManager.checkPermissions( 250 cms.getRequestContext(), 251 resource, 252 CmsPermissionSet.ACCESS_WRITE, 253 true, 254 CmsResourceFilter.ALL); 255 256 if (resource.getLength() > 0) { 258 CmsXmlPage xmlPage = CmsXmlPageFactory.unmarshal(cms, resource, false); 260 xmlPage.validateXmlStructure(new CmsXmlEntityResolver(cms)); 263 String contentConversion = CmsHtmlConverter.getConversionSettings(cms, resource); 265 xmlPage.setConversion(contentConversion); 266 resource = xmlPage.correctXmlStructure(cms); 268 } 269 return super.writeFile(cms, securityManager, resource); 271 } 272 273 } | Popular Tags |