📄 testcmsxmlcontentwithvfs.java
字号:
vfsValue.setStringValue(cms, vfsValue.getStringValue(cms));
Iterator i;
CmsLinkTable table;
String retranslatedOutput = htmlValue.getStringValue(cms);
assertEquals("Incorrect links in resulting output", "<a href=\"http://www.alkacon.com\">Alkacon</a>\n"
+ "<a href=\"/data/opencms/index.html\">Index page</a>\n"
+ "<a href=\"/data/opencms/folder1/index.html?a=b&c=d#anchor\">Index page</a>\n"
+ "<a href=\"/data/opencms/folder1/index.html?a2=b2&c2=d2\">Index page with unescaped ampersand</a>",
// note that the & in the links appear correctly escaped here
retranslatedOutput.trim());
table = htmlValue.getLinkTable();
assertEquals(4, table.size());
i = table.iterator();
int result = 0;
while (i.hasNext()) {
// iterate all links and check if the required values are found
CmsLink link = (CmsLink)i.next();
if (link.getTarget().equals("/sites/default/index.html") && link.isInternal()) {
result++;
} else if (link.getTarget().equals("http://www.alkacon.com") && !link.isInternal()) {
result++;
} else if (link.getTarget().equals("/sites/default/folder1/index.html")
&& link.getQuery().equals("a=b&c=d") // at this point the & in the link should be unescaped
&& link.getAnchor().equals("anchor")
&& link.isInternal()) {
result++;
} else if (link.getTarget().equals("/sites/default/folder1/index.html")
&& link.getQuery().equals("a2=b2&c2=d2") // at this point the & in the link should be unescaped
&& link.isInternal()) {
result++;
}
}
assertEquals(4, result);
table = vfsValue.getLinkTable();
assertEquals(1, table.size());
CmsLink link = (CmsLink)table.iterator().next();
assertEquals("/sites/default/index.html", link.getTarget());
assertTrue(link.isInternal());
assertEquals("/index.html", vfsValue.getStringValue(cms));
}
/**
* Tests the macros in messages and default values.<p>
*
* @throws Exception in case something goes wrong
*/
public void testMacros() throws Exception {
CmsObject cms = getCmsObject();
echo("Testing macros in the XML content");
CmsUser admin = cms.getRequestContext().currentUser();
admin.setFirstname("Hans");
admin.setLastname("Mustermann");
admin.setEmail("hans.mustermann@germany.de");
admin.setAddress("Heidestra遝 17, M黱chen");
cms.writeUser(admin);
CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms);
String content;
CmsXmlContent xmlcontent;
// unmarshal content definition
content = CmsFileUtil.readFile(
"org/opencms/xml/content/xmlcontent-definition-9.xsd",
CmsEncoder.ENCODING_UTF_8);
// store content definition in entitiy resolver
CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_9, content.getBytes(CmsEncoder.ENCODING_UTF_8));
// now read the XML content
content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-9.xml", CmsEncoder.ENCODING_UTF_8);
xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver);
CmsXmlContentErrorHandler errorHandler;
I_CmsXmlContentValue value1;
value1 = xmlcontent.getValue("Test", Locale.ENGLISH);
value1.setStringValue(cms, "This produces a warning!");
errorHandler = xmlcontent.validate(cms);
assertFalse(errorHandler.hasErrors());
assertTrue(errorHandler.hasWarnings());
value1.setStringValue(cms, "This produces a warning and an error!");
errorHandler = xmlcontent.validate(cms);
assertTrue(errorHandler.hasErrors());
assertTrue(errorHandler.hasWarnings());
assertEquals(1, errorHandler.getErrors().size());
assertEquals(1, errorHandler.getWarnings().size());
value1 = xmlcontent.getValue("Toast", Locale.ENGLISH);
value1.setStringValue(cms, "This produces a warning but no error!");
errorHandler = xmlcontent.validate(cms);
assertTrue(errorHandler.hasErrors());
assertTrue(errorHandler.hasWarnings());
assertEquals(1, errorHandler.getErrors(Locale.ENGLISH).size());
assertEquals(2, errorHandler.getWarnings(Locale.ENGLISH).size());
value1 = xmlcontent.addValue(cms, "Option", Locale.ENGLISH, 0);
assertEquals(
"The author is: Hans Mustermann (Admin), Heidestra遝 17, M黱chen - hans.mustermann@germany.de",
value1.getStringValue(cms));
value1 = xmlcontent.addValue(cms, "Option", Locale.GERMAN, 0);
assertEquals(
"Der Autor ist: Hans Mustermann (Admin), Heidestra遝 17, M黱chen - hans.mustermann@germany.de",
value1.getStringValue(cms));
// output the current document
System.out.println(xmlcontent.toString());
// re-create the document
xmlcontent = CmsXmlContentFactory.unmarshal(xmlcontent.toString(), CmsEncoder.ENCODING_UTF_8, resolver);
// validate the XML structure
xmlcontent.validateXmlStructure(resolver);
errorHandler = xmlcontent.validate(cms);
assertTrue(errorHandler.hasErrors());
assertTrue(errorHandler.hasWarnings());
assertEquals(1, errorHandler.getErrors(Locale.ENGLISH).size());
assertEquals(2, errorHandler.getWarnings(Locale.ENGLISH).size());
}
/**
* Tests the element mappings from the appinfo node.<p>
*
* @throws Exception in case something goes wrong
*/
public void testMappings() throws Exception {
CmsObject cms = getCmsObject();
echo("Testing mapping of values in the XML content");
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);
String resourcename = "/mappingtext.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 = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_TITLE, false);
assertSame(titleProperty, CmsProperty.getNullProperty());
CmsProperty localeProperty = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_LOCALE, false);
assertSame(localeProperty, CmsProperty.getNullProperty());
CmsProperty navImageProperty = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_NAVIMAGE, false);
assertSame(navImageProperty, CmsProperty.getNullProperty());
CmsProperty navInfoProperty = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_NAVINFO, false);
assertSame(navInfoProperty, CmsProperty.getNullProperty());
String titleStr = "This must be the Title";
String navImageStr = "This is the String with xpath String[2]";
String navInfoStr = "Here we have the String with xpath String[3]";
I_CmsXmlContentValue value;
value = xmlcontent.addValue(cms, "String", Locale.ENGLISH, 0);
value.setStringValue(cms, titleStr);
// set values for Title[2] and Title[3]
CmsXmlContentValueSequence seq = xmlcontent.getValueSequence("String", Locale.ENGLISH);
assertEquals(1, seq.getElementCount());
value = seq.addValue(cms, 1);
value.setStringValue(cms, navImageStr);
value = seq.addValue(cms, 2);
value.setStringValue(cms, navInfoStr);
String localeStr = "en";
value = xmlcontent.addValue(cms, "Locale", Locale.ENGLISH, 0);
value.setStringValue(cms, localeStr);
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());
assertEquals(titleStr, titleProperty.getStructureValue());
assertNull(titleProperty.getResourceValue());
navImageProperty = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_NAVIMAGE, false);
assertEquals(navImageStr, navImageProperty.getValue());
assertEquals(navImageStr, navImageProperty.getResourceValue());
assertNull(navImageProperty.getStructureValue());
navInfoProperty = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_NAVINFO, false);
assertEquals(navInfoStr, navInfoProperty.getValue());
assertEquals(navInfoStr, navInfoProperty.getStructureValue());
assertNull(navInfoProperty.getResourceValue());
localeProperty = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_LOCALE, false);
assertEquals(localeStr, localeProperty.getValue());
assertEquals(localeStr, localeProperty.getResourceValue());
assertNull(localeProperty.getStructureValue());
}
/**
* Tests element mappings fom XML content to a property list.<p>
*
* @throws Exception if the test fails
*/
public void testMappingsAsList() throws Exception {
CmsObject cms = getCmsObject();
echo("Testing element mappings fom XML content to a property list");
CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms);
String content;
CmsXmlContent xmlcontent;
// please note: XML schema 8 already in the cache from previous tests
// 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);
String resourcename = "/mappinglist.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 prop;
prop = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_DESCRIPTION, false);
assertSame(prop, CmsProperty.getNullProperty());
I_CmsXmlContentValue value;
CmsXmlContentValueSequence seq = xmlcontent.getValueSequence("VfsFile", Locale.ENGLISH);
assertEquals(0, seq.getElementCount());
String res1 = "/index.html";
String res2 = "/xmlcontent/";
String res3 = "/xmlcontent/article_0001.html";
String res4 = "/folder1/index.html";
String sr = cms.getRequestContext().getSiteRoot();
String propValue = sr + res1 + "|" + sr + res2 + "|" + sr + res3 + "|" + sr + res4;
value = seq.addValue(cms, 0);
value.setStringValue(cms, res1);
value = seq.addValue(cms, 1);
value.setStringValue(cms, res2);
value = seq.addValue(cms, 2);
value.setStringValue(cms, res3);
value = seq.addValue(cms, 3);
value.setStringValue(cms, res4);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -