testcmsxmlcontentschemamodifications.java
来自「找了很久才找到到源代码」· Java 代码 · 共 504 行 · 第 1/2 页
JAVA
504 行
"http://www.opencms.org/test3.xsd",
"/testCombinedNestedSchemaNodesA.html",
"xmlcontent-definition-3.xsd",
"xmlcontent-4.xml",
"xmlcontent-definition-3.mod1.xsd",
"xmlcontent-4.mod1.xml");
runTestWithChangedSchema(
"http://www.opencms.org/test3.xsd",
"/testCombinedNestedSchemaNodesB.html",
"xmlcontent-definition-3.mod1.xsd",
"xmlcontent-4.mod1.xml",
"xmlcontent-definition-3.mod2.xsd",
"xmlcontent-4.mod2.xml");
}
/**
* Test re-arranging nodes in the XML schema.<p>
*
* @throws Exception in case the test fails
*/
public void testReArrangeSchemaNodes() throws Exception {
echo("Test re-arranging nodes in the XML schema");
runTestWithChangedSchema(
SCHEMA_SYSTEM_ID_1,
"/testReArrangeSchemaNodes.html",
"xmlcontent-definition-1.xsd",
"xmlcontent-1.xml",
"xmlcontent-definition-1.mod3.xsd",
"xmlcontent-1.mod3.xml");
}
/**
* Test removing nodes from the XML schema.<p>
*
* @throws Exception in case the test fails
*/
public void testRemoveSchemaNodes() throws Exception {
echo("Testing removing nodes from an XML schema");
runTestWithChangedSchema(
SCHEMA_SYSTEM_ID_1,
"/testRemoveSchemaNodes.html",
"xmlcontent-definition-1.xsd",
"xmlcontent-1.xml",
"xmlcontent-definition-1.mod2.xsd",
"xmlcontent-1.mod2.xml");
}
/**
* Demo test for using the XML content correcting API.<p>
*
* @throws Exception in case the test fails
*/
public void testUsageDemo() throws Exception {
CmsObject cms = getCmsObject();
CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms);
// setup steps for demo test 1:
// filenames to use
String changedSchema = "org/opencms/xml/content/xmlcontent-definition-1.mod4.xsd";
String originalFile = "org/opencms/xml/content/xmlcontent-1.xml";
String filename = "/testUsageDemo.html";
// cache the changed content definition
cacheSchema(resolver, SCHEMA_SYSTEM_ID_1, changedSchema);
// read the XML content from the test directory, usually this would be from the VFS
String content = CmsFileUtil.readFile(originalFile, CmsEncoder.ENCODING_UTF_8);
// DEMO TEST 1:
// assumtion: the schema definition of an existing XML content has changed
// the XML content is unmarshaled (using the changed schema) and then validated
// after validation fails, it is corrected using the API and then saved to the VFS
// unmarshal the XML content (the schema is already "wrong")
CmsXmlContent xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver);
// output the XML content (unmodified version)
System.out.println(xmlcontent.toString());
// validate the XML structure - must be invalid because of the schema change
Exception ex = null;
try {
xmlcontent.validateXmlStructure(resolver);
} catch (CmsXmlException e) {
ex = e;
}
// required exception must haven been thrown or the test is a failure
assertNotNull(ex);
// enable "auto correction mode" - this is required or the XML structure will not be fully corrected
xmlcontent.setAutoCorrectionEnabled(true);
// now correct the XML
xmlcontent.correctXmlStructure(cms);
// output the XML content (modified version)
System.out.println(xmlcontent.toString());
// check again if the XML is correct - this time it must work without exception
xmlcontent.validateXmlStructure(resolver);
// demo test 1 is finished
// setup steps for demo test 2:
// write the content to the VFS
cms.createResource(
filename,
OpenCms.getResourceManager().getResourceType("xmlcontent").getTypeId(),
xmlcontent.toString().getBytes(xmlcontent.getEncoding()),
Collections.EMPTY_LIST);
// change the XML schema definition for the XML content
// it's important that the schema is changed _before_ the content is unmarshaled
// in a "real world" use case this should be no problem as the schema will have been changed in another request
changedSchema = "org/opencms/xml/content/xmlcontent-definition-1.mod5.xsd";
// update the XML schema cache
cacheSchema(resolver, SCHEMA_SYSTEM_ID_1, changedSchema);
// DEMO TEST 2:
// assumption: a file is to be corrected automatically while writing it to the VFS
// for this, a special OpenCms request context attribute has been introduced
// if this is set to a Boolean.TRUE object, the XML content is always corrected while saving it to the VFS
CmsFile file = cms.readFile(filename);
// this is not normally required, but illustrates the differences in the API behaviour
// trying to write the file now (with the changed schema but XML still using the old schema) is not possible
// this is because of the build-in validation while writing
ex = null;
try {
file = cms.writeFile(file);
} catch (CmsXmlException e) {
ex = e;
}
// required exception must haven been thrown or the test is a failure
assertNotNull(ex);
// now set the "automatic correction" request context attribute
cms.getRequestContext().setAttribute(CmsXmlContent.AUTO_CORRECTION_ATTRIBUTE, Boolean.TRUE);
// write the file again - the correction will now be done automatically while writing (no exceptions)
file = cms.writeFile(file);
// output the XML content (modified version)
System.out.println(new String(file.getContents()));
// demo test 2 is finished
}
/**
* Compares a given XML content with the contents of a file in the RFS and asserts the XML is identical.<p>
*
* @param xmlcontent the XML content to compare
* @param filename the RFS name of the file to compare the XML content with
* @param resolver the XML entitiy resolver to use
*
* @throws Exception if something goes wrong
*/
private void assertXmlContent(CmsXmlContent xmlcontent, String filename, CmsXmlEntityResolver resolver)
throws Exception {
System.out.println(xmlcontent.toString());
String content = CmsFileUtil.readFile(filename, CmsEncoder.ENCODING_UTF_8);
assertEquals(CmsXmlUtils.unmarshalHelper(xmlcontent.toString(), resolver), CmsXmlUtils.unmarshalHelper(
content,
resolver));
}
/**
* Updates the OpenCms XML entity resolver cache with a changed XML schema id.<p>
*
* @param resolver the OpenCms XML entity resolver to use
* @param id the XML schema id to update in the resolver
* @param filename the name of the file in the RFS where to read the new schema content from
*
* @throws Exception if something goes wrong
*/
private void cacheSchema(CmsXmlEntityResolver resolver, String id, String filename) throws Exception {
// fire "clear cache" event to clear up previously cached schemas
OpenCms.fireCmsEvent(new CmsEvent(I_CmsEventListener.EVENT_CLEAR_CACHES, new HashMap()));
// read the XML from the given file and store it in the resolver
String content = CmsFileUtil.readFile(filename, CmsEncoder.ENCODING_UTF_8);
CmsXmlContentDefinition definition = CmsXmlContentDefinition.unmarshal(content, id, resolver);
CmsXmlEntityResolver.cacheSystemId(id, definition.getSchema().asXML().getBytes(CmsEncoder.ENCODING_UTF_8));
}
/**
* Reads an XML content with the original schema, then changes the schema and corrects the XML content
* with the new schema.<p>
*
* @param schemaId the XML schema id for the schema cache
* @param filename the filename to write the XML content to in the OpenCms VFS
* @param originalSchema the location of the original schema in the RFS
* @param originalFile the location of the original XML file in the RFS
* @param changedSchema the location of the changed schema in the RFS
* @param changedFile the location of the changed XML file in the RFS (for comparison)
*
* @throws Exception if something goes wrong
*/
private void runTestWithChangedSchema(
String schemaId,
String filename,
String originalSchema,
String originalFile,
String changedSchema,
String changedFile) throws Exception {
CmsObject cms = getCmsObject();
// append default path to the filenames
originalSchema = "org/opencms/xml/content/" + originalSchema;
originalFile = "org/opencms/xml/content/" + originalFile;
changedSchema = "org/opencms/xml/content/" + changedSchema;
changedFile = "org/opencms/xml/content/" + changedFile;
CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms);
// cache the ORIGINAL content definition
cacheSchema(resolver, schemaId, originalSchema);
// now create the XML content
String content = CmsFileUtil.readFile(originalFile, CmsEncoder.ENCODING_UTF_8);
CmsXmlContent xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver);
// save the XML content to the VFS
cms.createResource(
filename,
OpenCms.getResourceManager().getResourceType("xmlcontent").getTypeId(),
xmlcontent.toString().getBytes(xmlcontent.getEncoding()),
Collections.EMPTY_LIST);
CmsFile file = cms.readFile(filename);
CmsXmlContent xmlcontent2 = CmsXmlContentFactory.unmarshal(cms, file);
// enable the XML auto correction mode on save
cms.getRequestContext().setAttribute(CmsXmlContent.AUTO_CORRECTION_ATTRIBUTE, Boolean.TRUE);
// write the file, it should not be changed
cms.writeFile(file);
assertXmlContent(xmlcontent2, originalFile, resolver);
// cache the CHANGED content definition
cacheSchema(resolver, schemaId, changedSchema);
// write the file again, it should be changed according to the changed schema
cms.writeFile(file);
file = cms.readFile(filename);
xmlcontent2 = CmsXmlContentFactory.unmarshal(cms, file);
assertXmlContent(xmlcontent2, changedFile, resolver);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?