📄 testcmsxmlcontentwithvfs.java
字号:
assertEquals(4, seq.getElementCount());
// validate the XML structure
xmlcontent.validateXmlStructure(resolver);
file.setContents(xmlcontent.toString().getBytes(CmsEncoder.ENCODING_ISO_8859_1));
cms.writeFile(file);
// check for written property values as list
prop = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_DESCRIPTION, false);
List list = prop.getValueList();
assertNotNull(list);
assertEquals(4, list.size());
assertEquals(sr + res1, list.get(0));
assertEquals(sr + res2, list.get(1));
assertEquals(sr + res3, list.get(2));
assertEquals(sr + res4, list.get(3));
assertEquals(propValue, prop.getValue());
assertEquals(propValue, prop.getStructureValue());
assertNull(prop.getResourceValue());
CmsProperty prop2;
prop2 = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_KEYWORDS, false);
assertSame(prop2, CmsProperty.getNullProperty());
I_CmsXmlContentValue value2;
CmsXmlContentValueSequence seq2 = xmlcontent.getValueSequence("VfsFile2", Locale.ENGLISH);
assertEquals(0, seq2.getElementCount());
value2 = seq2.addValue(cms, 0);
value2.setStringValue(cms, res1);
value2 = seq2.addValue(cms, 1);
value2.setStringValue(cms, res2);
value2 = seq2.addValue(cms, 2);
value2.setStringValue(cms, res3);
value2 = seq2.addValue(cms, 3);
value2.setStringValue(cms, res4);
assertEquals(4, seq2.getElementCount());
// validate the XML structure
xmlcontent.validateXmlStructure(resolver);
file.setContents(xmlcontent.toString().getBytes(CmsEncoder.ENCODING_ISO_8859_1));
cms.writeFile(file);
// check for written property values as list
prop2 = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_KEYWORDS, false);
List list2 = prop.getValueList();
assertNotNull(list2);
assertEquals(4, list2.size());
assertEquals(sr + res1, list2.get(0));
assertEquals(sr + res2, list2.get(1));
assertEquals(sr + res3, list2.get(2));
assertEquals(sr + res4, list2.get(3));
assertEquals(propValue, prop2.getValue());
assertEquals(propValue, prop2.getResourceValue());
assertNull(prop2.getStructureValue());
CmsProperty prop3;
prop3 = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_NAVTEXT, false);
assertSame(prop3, CmsProperty.getNullProperty());
I_CmsXmlContentValue value3;
CmsXmlContentValueSequence seq3 = xmlcontent.getValueSequence("VfsFile3", Locale.ENGLISH);
assertEquals(0, seq3.getElementCount());
value3 = seq3.addValue(cms, 0);
value3.setStringValue(cms, res1);
value3 = seq3.addValue(cms, 1);
value3.setStringValue(cms, res2);
value3 = seq3.addValue(cms, 2);
value3.setStringValue(cms, res3);
value3 = seq3.addValue(cms, 3);
value3.setStringValue(cms, res4);
assertEquals(4, seq3.getElementCount());
// validate the XML structure
xmlcontent.validateXmlStructure(resolver);
file.setContents(xmlcontent.toString().getBytes(CmsEncoder.ENCODING_ISO_8859_1));
cms.writeFile(file);
// check for written property values as list
prop3 = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_NAVTEXT, false);
List list3 = prop.getValueList();
assertNotNull(list3);
assertEquals(4, list3.size());
assertEquals(sr + res1, list3.get(0));
assertEquals(sr + res2, list3.get(1));
assertEquals(sr + res3, list3.get(2));
assertEquals(sr + res4, list3.get(3));
assertEquals(propValue, prop3.getValue());
assertEquals(propValue, prop3.getStructureValue());
assertNull(prop3.getResourceValue());
}
/**
* Tests the element mappings from the appinfo node for nested XML content.<p>
*
* @throws Exception in case something goes wrong
*/
public void testMappingsOfNestedContent() throws Exception {
CmsObject cms = getCmsObject();
echo("Testing mapping of values in the XML content with nested elements");
CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms);
String content;
CmsXmlContent xmlcontent;
// unmarshal content definition
content = CmsFileUtil.readFile(
"org/opencms/xml/content/xmlcontent-definition-7.xsd",
CmsEncoder.ENCODING_UTF_8);
// store content definition in entitiy resolver
CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_7, content.getBytes(CmsEncoder.ENCODING_UTF_8));
// now read the XML content
content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-7.xml", CmsEncoder.ENCODING_UTF_8);
xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver);
String resourcename = "/mappingtest_nested.html";
// create a file in the VFS with this content (required for mappings to work)
cms.createResource(
resourcename,
OpenCms.getResourceManager().getResourceType("xmlcontent").getTypeId(),
content.getBytes(CmsEncoder.ENCODING_ISO_8859_1),
Collections.EMPTY_LIST);
CmsFile file = cms.readFile(resourcename);
xmlcontent = CmsXmlContentFactory.unmarshal(cms, file);
CmsProperty titleProperty;
titleProperty = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_TITLE, false);
assertSame(titleProperty, CmsProperty.getNullProperty());
String titleStr = "This must be the Title (not nested)";
I_CmsXmlContentValue value;
value = xmlcontent.getValue("Test", Locale.ENGLISH);
assertEquals(value.getStringValue(cms), "Another Test");
value.setStringValue(cms, titleStr);
String descStr = "This must be the Description (which IS nested)";
value = xmlcontent.getValue("Cascade/Toast", Locale.ENGLISH);
assertEquals(value.getStringValue(cms), "Toast");
value.setStringValue(cms, descStr);
file.setContents(xmlcontent.toString().getBytes(CmsEncoder.ENCODING_ISO_8859_1));
cms.writeFile(file);
titleProperty = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_TITLE, false);
assertEquals(titleStr, titleProperty.getValue());
titleProperty = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_DESCRIPTION, false);
assertEquals(descStr, titleProperty.getValue());
}
/**
* Tests the element mappings from the appinfo node if there is more then one locale.<p>
*
* @throws Exception in case something goes wrong
*/
public void testMappingsWithManyLocales() throws Exception {
CmsObject cms = getCmsObject();
echo("Testing mapping of values in the XML content with locales");
CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms);
String content;
CmsXmlContent xmlcontent;
// unmarshal content definition
content = CmsFileUtil.readFile(
"org/opencms/xml/content/xmlcontent-definition-8.xsd",
CmsEncoder.ENCODING_UTF_8);
// store content definition in entitiy resolver
CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_8, content.getBytes(CmsEncoder.ENCODING_ISO_8859_1));
// now read the XML content
content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-8.xml", CmsEncoder.ENCODING_ISO_8859_1);
xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_ISO_8859_1, resolver);
// validate the XML structure
xmlcontent.validateXmlStructure(resolver);
// create "en" property
List properties = new ArrayList();
properties.add(new CmsProperty(CmsPropertyDefinition.PROPERTY_LOCALE, Locale.ENGLISH.toString(), null));
String resourcenameEn = "/mappingtext_en.html";
String resourcenameDe = "/mappingtext_de.html";
// create a file in the VFS with this content (required for mappings to work)
cms.createResource(
resourcenameEn,
OpenCms.getResourceManager().getResourceType("xmlcontent").getTypeId(),
content.getBytes(CmsEncoder.ENCODING_ISO_8859_1),
properties);
// copy the resource as a sibling to "de"
cms.copyResource(resourcenameEn, resourcenameDe, org.opencms.file.CmsResource.COPY_AS_SIBLING);
// now lock the "DE" sibling
cms.changeLock(resourcenameDe);
// add the "DE" locale property to the german version
cms.writePropertyObject(resourcenameDe, new CmsProperty(
CmsPropertyDefinition.PROPERTY_LOCALE,
Locale.GERMAN.toString(),
null));
CmsFile file = cms.readFile(resourcenameDe);
xmlcontent = CmsXmlContentFactory.unmarshal(cms, file);
xmlcontent.addLocale(cms, Locale.GERMAN);
if (!xmlcontent.hasLocale(Locale.ENGLISH)) {
xmlcontent.addLocale(cms, Locale.ENGLISH);
}
String titleStrEn = "This must be the Title in EN";
I_CmsXmlContentValue value;
value = xmlcontent.addValue(cms, "String", Locale.ENGLISH, 0);
value.setStringValue(cms, titleStrEn);
String titleStrDe = "Das ist der Title in DE";
value = xmlcontent.addValue(cms, "String", Locale.GERMAN, 0);
value.setStringValue(cms, titleStrDe);
file.setContents(xmlcontent.toString().getBytes(CmsEncoder.ENCODING_ISO_8859_1));
cms.writeFile(file);
// finally unlock the resource
cms.unlockResource(resourcenameDe);
// now check if the properties have been assigned as required to the locales
CmsProperty titlePropertyEn = cms.readPropertyObject(
resourcenameEn,
CmsPropertyDefinition.PROPERTY_TITLE,
false);
assertEquals(titleStrEn, titlePropertyEn.getValue());
CmsProperty titlePropertyDe = cms.readPropertyObject(
resourcenameDe,
CmsPropertyDefinition.PROPERTY_TITLE,
false);
assertEquals(titleStrDe, titlePropertyDe.getValue());
}
/**
* Test using a nested XML content schema.<p>
*
* @throws Exception in case something goes wrong
*/
public void testNestedSchema() throws Exception {
CmsObject cms = getCmsObject();
echo("Testing for nested XML content schemas");
CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms);
String content;
// unmarshal content definition
content = CmsFileUtil.readFile(
"org/opencms/xml/content/xmlcontent-definition-4.xsd",
CmsEncoder.ENCODING_UTF_8);
CmsXmlContentDefinition definition = CmsXmlContentDefinition.unmarshal(content, SCHEMA_SYSTEM_ID_4, resolver);
// store content definition in entitiy resolver
CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_4, content.getBytes(CmsEncoder.ENCODING_UTF_8));
// now create the XML content
content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-4.xml", CmsEncoder.ENCODING_UTF_8);
CmsXmlContent xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver);
System.out.println(xmlcontent.toString());
// validate the XML structure
xmlcontent.validateXmlStructure(resolver);
assertTrue(xmlcontent.hasValue("Title", Locale.ENGLISH));
assertTrue(xmlcontent.hasValue("Title[1]", Locale.ENGLISH));
assertTrue(xmlcontent.hasValue("Cascade[1]/Html[1]", Locale.ENGLISH));
assertTrue(xmlcontent.hasValue("Cascade[1]/VfsLink[1]", Locale.ENGLISH));
assertTrue(xmlcontent.hasValue("Cascade[1]/VfsLink[2]", Locale.ENGLISH));
assertTrue(xmlcontent.hasValue("Cascade[2]/Html[1]", Locale.ENGLISH));
assertTrue(xmlcontent.hasValue("Cascade[2]/VfsLink[1]", Locale.ENGLISH));
assertTrue(xmlcontent.hasValue("Cascade[2]/Html", Locale.ENGLISH));
assertTrue(xmlcontent.hasValue("Cascade[2]/VfsLink", Locale.ENGLISH));
assertTrue(xmlcontent.hasValue("Cas
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -