1 31 32 package org.opencms.xml.content; 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.i18n.CmsEncoder; 39 import org.opencms.loader.CmsLoaderException; 40 import org.opencms.loader.CmsXmlContentLoader; 41 import org.opencms.main.CmsException; 42 import org.opencms.main.OpenCms; 43 import org.opencms.xml.CmsXmlContentDefinition; 44 import org.opencms.xml.CmsXmlEntityResolver; 45 import org.opencms.xml.CmsXmlException; 46 import org.opencms.xml.CmsXmlUtils; 47 48 import java.io.UnsupportedEncodingException ; 49 import java.util.Locale ; 50 51 import javax.servlet.ServletRequest ; 52 53 import org.dom4j.Document; 54 import org.dom4j.DocumentHelper; 55 import org.xml.sax.EntityResolver ; 56 57 66 public final class CmsXmlContentFactory { 67 68 71 private CmsXmlContentFactory() { 72 73 } 75 76 89 public static CmsXmlContent createDocument( 90 CmsObject cms, 91 Locale locale, 92 String encoding, 93 CmsXmlContentDefinition contentDefinition) { 94 95 return new CmsXmlContent(cms, locale, encoding, contentDefinition); 97 } 98 99 112 public static CmsXmlContent unmarshal(byte[] xmlData, String encoding, EntityResolver resolver) 113 throws CmsXmlException { 114 115 return unmarshal(CmsXmlUtils.unmarshalHelper(xmlData, resolver), encoding, resolver); 116 } 117 118 127 public static CmsXmlContent unmarshal(CmsObject cms, CmsFile file) throws CmsXmlException { 128 129 return unmarshal(cms, file, true); 130 } 131 132 147 public static CmsXmlContent unmarshal(CmsObject cms, CmsFile file, boolean keepEncoding) throws CmsXmlException { 148 149 byte[] contentBytes = file.getContents(); 150 String filename = cms.getSitePath(file); 151 152 String encoding = null; 153 try { 154 encoding = cms.readPropertyObject(filename, CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING, true).getValue(); 155 } catch (CmsException e) { 156 } 158 if (encoding == null) { 159 encoding = OpenCms.getSystemInfo().getDefaultEncoding(); 160 } else { 161 encoding = CmsEncoder.lookupEncoding(encoding, null); 162 if (encoding == null) { 163 throw new CmsXmlException(Messages.get().container(Messages.ERR_XMLCONTENT_INVALID_ENC_1, filename)); 164 } 165 } 166 167 CmsXmlContent content; 168 if (contentBytes.length > 0) { 169 if (keepEncoding) { 171 content = unmarshal(contentBytes, encoding, new CmsXmlEntityResolver(cms)); 173 } else { 174 try { 177 String contentStr = new String (contentBytes, encoding); 178 content = unmarshal(contentStr, encoding, new CmsXmlEntityResolver(cms)); 179 } catch (UnsupportedEncodingException e) { 180 throw new CmsXmlException(Messages.get().container(Messages.ERR_XMLCONTENT_INVALID_ENC_1, filename)); 182 } 183 } 184 } else { 185 content = new CmsXmlContent(DocumentHelper.createDocument(), encoding, new CmsXmlEntityResolver(cms)); 187 } 188 189 content.setFile(file); 191 return content; 193 } 194 195 209 public static CmsXmlContent unmarshal(CmsObject cms, CmsResource resource, ServletRequest req) 210 throws CmsXmlException, CmsLoaderException, CmsException { 211 212 String rootPath = resource.getRootPath(); 213 214 if (!(OpenCms.getResourceManager().getLoader(resource) instanceof CmsXmlContentLoader)) { 215 throw new CmsXmlException(Messages.get().container( 217 Messages.ERR_XMLCONTENT_INVALID_TYPE_1, 218 cms.getSitePath(resource))); 219 } 220 221 CmsXmlContent content = (CmsXmlContent)req.getAttribute(rootPath); 223 224 if (content == null) { 225 content = unmarshal(cms, CmsFile.upgrade(resource, cms)); 227 req.setAttribute(rootPath, content); 229 } 230 231 return content; 233 } 234 235 245 public static CmsXmlContent unmarshal(Document document, String encoding, EntityResolver resolver) { 246 247 return new CmsXmlContent(document, encoding, resolver); 248 } 249 250 262 public static CmsXmlContent unmarshal(String xmlData, String encoding, EntityResolver resolver) 263 throws CmsXmlException { 264 265 CmsXmlContent content = unmarshal(CmsXmlUtils.unmarshalHelper(xmlData, resolver), encoding, resolver); 267 return content; 269 270 } 271 } | Popular Tags |