1 31 32 package org.opencms.xml.page; 33 34 import org.opencms.file.CmsFile; 35 import org.opencms.file.CmsObject; 36 import org.opencms.file.CmsPropertyDefinition; 37 import org.opencms.file.CmsResource; 38 import org.opencms.file.CmsResourceFilter; 39 import org.opencms.file.types.CmsResourceTypeXmlPage; 40 import org.opencms.i18n.CmsEncoder; 41 import org.opencms.loader.CmsXmlContentLoader; 42 import org.opencms.main.CmsException; 43 import org.opencms.main.CmsLog; 44 import org.opencms.main.OpenCms; 45 import org.opencms.xml.CmsXmlEntityResolver; 46 import org.opencms.xml.CmsXmlException; 47 import org.opencms.xml.CmsXmlUtils; 48 import org.opencms.xml.I_CmsXmlDocument; 49 import org.opencms.xml.content.CmsXmlContentFactory; 50 import org.opencms.xml.types.I_CmsXmlSchemaType; 51 52 import java.io.UnsupportedEncodingException ; 53 import java.util.Locale ; 54 55 import javax.servlet.ServletRequest ; 56 57 import org.apache.commons.logging.Log; 58 59 import org.dom4j.Document; 60 import org.dom4j.DocumentHelper; 61 import org.dom4j.Element; 62 import org.xml.sax.EntityResolver ; 63 64 73 public final class CmsXmlPageFactory { 74 75 76 private static final Log LOG = CmsLog.getLog(CmsXmlPageFactory.class); 77 78 81 private CmsXmlPageFactory() { 82 83 } 85 86 94 public static Document createDocument(Locale locale) { 95 96 Document doc = DocumentHelper.createDocument(); 97 Element pages = doc.addElement(CmsXmlPage.NODE_PAGES); 98 pages.add(I_CmsXmlSchemaType.XSI_NAMESPACE); 99 pages.addAttribute( 100 I_CmsXmlSchemaType.XSI_NAMESPACE_ATTRIBUTE_NO_SCHEMA_LOCATION, 101 CmsXmlPage.XMLPAGE_XSD_SYSTEM_ID); 102 103 Element page = pages.addElement(CmsXmlPage.NODE_PAGE); 104 page.addAttribute(CmsXmlPage.ATTRIBUTE_LANGUAGE, locale.toString()); 105 106 return doc; 107 } 108 109 118 public static String createDocument(Locale locale, String encoding) { 119 120 try { 121 return CmsXmlUtils.marshal(createDocument(locale), encoding); 122 } catch (CmsXmlException e) { 123 LOG.error(Messages.get().getBundle().key(Messages.ERR_XML_PAGE_FACT_CREATE_DOC_0), e); 125 return null; 126 } 127 } 128 129 142 public static CmsXmlPage unmarshal(byte[] xmlData, String encoding, EntityResolver resolver) throws CmsXmlException { 143 144 return new CmsXmlPage(CmsXmlUtils.unmarshalHelper(xmlData, resolver), encoding); 145 } 146 147 156 public static CmsXmlPage unmarshal(CmsObject cms, CmsFile file) throws CmsXmlException { 157 158 return CmsXmlPageFactory.unmarshal(cms, file, true); 159 } 160 161 176 public static CmsXmlPage unmarshal(CmsObject cms, CmsFile file, boolean keepEncoding) throws CmsXmlException { 177 178 byte[] content = file.getContents(); 179 180 String fileName = cms.getSitePath(file); 181 boolean allowRelative = false; 182 try { 183 allowRelative = Boolean.valueOf( 184 cms.readPropertyObject(fileName, CmsXmlPage.PROPERTY_ALLOW_RELATIVE, false).getValue()).booleanValue(); 185 } catch (CmsException e) { 186 } 188 189 String encoding = null; 190 try { 191 encoding = cms.readPropertyObject(fileName, CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING, true).getValue(); 192 } catch (CmsException e) { 193 } 195 if (encoding == null) { 196 encoding = OpenCms.getSystemInfo().getDefaultEncoding(); 197 } else { 198 encoding = CmsEncoder.lookupEncoding(encoding, null); 199 if (encoding == null) { 200 throw new CmsXmlException(Messages.get().container(Messages.ERR_XML_PAGE_FACT_INVALID_ENC_1, fileName)); 201 } 202 } 203 204 CmsXmlPage newPage; 205 if (content.length > 0) { 206 if (keepEncoding) { 208 newPage = unmarshal(content, encoding, new CmsXmlEntityResolver(cms)); 210 } else { 211 try { 214 String contentStr = new String (content, encoding); 215 newPage = unmarshal(contentStr, encoding, new CmsXmlEntityResolver(cms)); 216 } catch (UnsupportedEncodingException e) { 217 throw new CmsXmlException(Messages.get().container( 219 Messages.ERR_XML_PAGE_FACT_INVALID_ENC_1, 220 fileName), e); 221 } 222 } 223 } else { 224 newPage = new CmsXmlPage(cms.getRequestContext().getLocale(), encoding); 226 } 227 228 newPage.setFile(file); 229 newPage.setAllowRelativeLinks(allowRelative); 230 231 return newPage; 232 } 233 234 246 public static CmsXmlPage unmarshal(CmsObject cms, CmsResource resource, ServletRequest req) throws CmsException { 247 248 String rootPath = resource.getRootPath(); 249 250 if (resource.getTypeId() != CmsResourceTypeXmlPage.getStaticTypeId()) { 251 throw new CmsXmlException(Messages.get().container( 253 Messages.ERR_XML_PAGE_FACT_NO_XMLPAGE_TYPE_1, 254 cms.getSitePath(resource))); 255 } 256 257 CmsXmlPage page = (CmsXmlPage)req.getAttribute(rootPath); 259 260 if (page == null) { 261 page = unmarshal(cms, CmsFile.upgrade(resource, cms)); 263 req.setAttribute(rootPath, page); 265 } 266 267 return page; 268 } 269 270 282 public static I_CmsXmlDocument unmarshal(CmsObject cms, String filename, ServletRequest req) throws CmsException { 283 284 String rootPath = cms.getRequestContext().addSiteRoot(filename); 286 287 I_CmsXmlDocument doc = (I_CmsXmlDocument)req.getAttribute(rootPath); 289 290 if (doc != null) { 291 return doc; 292 } 293 294 CmsFile file = cms.readFile(filename, CmsResourceFilter.IGNORE_EXPIRATION); 296 297 if (file.getTypeId() == CmsResourceTypeXmlPage.getStaticTypeId()) { 298 doc = CmsXmlPageFactory.unmarshal(cms, file); 300 } else if ((OpenCms.getResourceManager().getLoader(file) instanceof CmsXmlContentLoader)) { 301 doc = CmsXmlContentFactory.unmarshal(cms, file); 303 } else { 304 throw new CmsXmlException(Messages.get().container(Messages.ERR_XML_PAGE_FACT_NO_XML_DOCUMENT_1, file)); 306 } 307 308 req.setAttribute(rootPath, doc); 310 311 return doc; 312 } 313 314 327 public static CmsXmlPage unmarshal(String xmlData, String encoding, EntityResolver resolver) throws CmsXmlException { 328 329 return new CmsXmlPage(CmsXmlUtils.unmarshalHelper(xmlData, resolver), encoding); 330 } 331 } | Popular Tags |