cmsxmlcontentfactory.java
来自「找了很久才找到到源代码」· Java 代码 · 共 359 行 · 第 1/2 页
JAVA
359 行
* most recent version. Use <code>{@link #unmarshal(CmsObject, CmsResource, ServletRequest)}</code>
* for history support.<p>
*
* @param cms the current cms object
* @param file the file with the XML data to unmarshal
* @param keepEncoding if true, the encoding spefified in the XML header is used,
* otherwise the encoding from the VFS file property is used
*
* @return a XML content instance unmarshalled from the provided file
*
* @throws CmsXmlException if something goes wrong
*/
public static CmsXmlContent unmarshal(CmsObject cms, CmsFile file, boolean keepEncoding) throws CmsXmlException {
byte[] contentBytes = file.getContents();
String filename = cms.getSitePath(file);
String encoding = null;
try {
encoding = cms.readPropertyObject(filename, CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING, true).getValue();
} catch (CmsException e) {
// encoding will be null
}
if (encoding == null) {
encoding = OpenCms.getSystemInfo().getDefaultEncoding();
} else {
encoding = CmsEncoder.lookupEncoding(encoding, null);
if (encoding == null) {
throw new CmsXmlException(Messages.get().container(Messages.ERR_XMLCONTENT_INVALID_ENC_1, filename));
}
}
CmsXmlContent content;
if (contentBytes.length > 0) {
// content is initialized
if (keepEncoding) {
// use the encoding from the content
content = unmarshal(cms, contentBytes, encoding, new CmsXmlEntityResolver(cms));
} else {
// use the encoding from the file property
// this usually only triggered by a save operation
try {
String contentStr = new String(contentBytes, encoding);
content = unmarshal(cms, contentStr, encoding, new CmsXmlEntityResolver(cms));
} catch (UnsupportedEncodingException e) {
// this will not happen since the encodig has already been validated
throw new CmsXmlException(Messages.get().container(Messages.ERR_XMLCONTENT_INVALID_ENC_1, filename));
}
}
} else {
// content is empty
content = new CmsXmlContent(cms, DocumentHelper.createDocument(), encoding, new CmsXmlEntityResolver(cms));
}
// set the file
content.setFile(file);
// call prepare for use content handler and return the result
return content.getContentDefinition().getContentHandler().prepareForUse(cms, content);
}
/**
* Factory method to unmarshal (read) a XML content instance from
* a resource, using the request attributes as cache.<p>
*
* @param cms the current OpenCms context object
* @param resource the resource to unmarshal
* @param req the current request
*
* @return the unmarshaled xml content, or null if the given resource was not of type {@link org.opencms.file.types.CmsResourceTypeXmlContent}
*
* @throws CmsException in something goes wrong
* @throws CmsLoaderException if no loader for the given <code>resource</code> type ({@link CmsResource#getTypeId()}) is available
* @throws CmsXmlException if the given <code>resource</code> is not of type xml content
*/
public static CmsXmlContent unmarshal(CmsObject cms, CmsResource resource, ServletRequest req)
throws CmsXmlException, CmsLoaderException, CmsException {
String rootPath = resource.getRootPath();
if (!CmsResourceTypeXmlContent.isXmlContent(resource)) {
// sanity check: resource must be of type XML content
throw new CmsXmlException(Messages.get().container(
Messages.ERR_XMLCONTENT_INVALID_TYPE_1,
cms.getSitePath(resource)));
}
// try to get the requested content form the current request attributes
// this is also necessary for historic versions that have been loaded
CmsXmlContent content = (CmsXmlContent)req.getAttribute(rootPath);
if (content == null) {
// unmarshal XML structure from the file content
content = unmarshal(cms, cms.readFile(resource));
// store the content as request attribute for future read requests
req.setAttribute(rootPath, content);
}
// return the result
return content;
}
/**
* Factory method to unmarshal (generate) a XML content instance from a XML document.<p>
*
* The given encoding is used when marshalling the XML again later.<p>
*
* <b>Warning:</b><br/>
* This method does not support requested historic versions, it always loads the
* most recent version. Use <code>{@link #unmarshal(CmsObject, CmsResource, ServletRequest)}</code>
* for history support.<p>
*
* @param cms the cms context, if <code>null</code> no link validation is performed
* @param document the XML document to generate the XML content from
* @param encoding the encoding to use when marshalling the XML content later
* @param resolver the XML entitiy resolver to use
*
* @return a XML content instance unmarshalled from the String
*/
public static CmsXmlContent unmarshal(CmsObject cms, Document document, String encoding, EntityResolver resolver) {
CmsXmlContent content = new CmsXmlContent(cms, document, encoding, resolver);
// call prepare for use content handler and return the result
return content.getContentDefinition().getContentHandler().prepareForUse(cms, content);
}
/**
* Factory method to unmarshal (generate) a XML content instance from a String
* that contains XML data.<p>
*
* The given encoding is used when marshalling the XML again later.<p>
*
* <b>Warning:</b><br/>
* This method does not support requested historic versions, it always loads the
* most recent version. Use <code>{@link #unmarshal(CmsObject, CmsResource, ServletRequest)}</code>
* for history support.<p>
*
* @param cms the cms context, if <code>null</code> no link validation is performed
* @param xmlData the XML data in a String
* @param encoding the encoding to use when marshalling the XML content later
* @param resolver the XML entitiy resolver to use
*
* @return a XML content instance unmarshalled from the String
*
* @throws CmsXmlException if something goes wrong
*/
public static CmsXmlContent unmarshal(CmsObject cms, String xmlData, String encoding, EntityResolver resolver)
throws CmsXmlException {
// create the XML content object from the provided String
return unmarshal(cms, CmsXmlUtils.unmarshalHelper(xmlData, resolver), encoding, resolver);
}
/**
* Factory method to unmarshal (generate) a XML content instance from a String
* that contains XML data.<p>
*
* The given encoding is used when marshalling the XML again later.<p>
*
* Since no {@link CmsObject} is available, no link validation is performed!<p>
*
* <b>Warning:</b><br/>
* This method does not support requested historic versions, it always loads the
* most recent version. Use <code>{@link #unmarshal(CmsObject, CmsResource, ServletRequest)}</code>
* for history support.<p>
*
* @param xmlData the XML data in a String
* @param encoding the encoding to use when marshalling the XML content later
* @param resolver the XML entitiy resolver to use
*
* @return a XML content instance unmarshalled from the String
*
* @throws CmsXmlException if something goes wrong
*/
public static CmsXmlContent unmarshal(String xmlData, String encoding, EntityResolver resolver)
throws CmsXmlException {
return unmarshal(null, xmlData, encoding, resolver);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?