testcmsxmlcontentwithvfs.java
来自「找了很久才找到到源代码」· Java 代码 · 共 1,409 行 · 第 1/5 页
JAVA
1,409 行
// add a new value for the deep cascade
level0Sequence.addValue(cms, 0);
assertEquals(1, level0Sequence.getElementCount());
// 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);
level0Sequence = xmlcontent.getValueSequence("DeepCascade", Locale.ENGLISH);
assertEquals(1, level0Sequence.getElementCount());
}
/**
* Test adding a file reference value to an existing xmlcontent.<p>
*
* @throws Exception in case something goes wrong
*/
public void testAddFileReference() throws Exception {
CmsObject cms = getCmsObject();
echo("Testing adding a file reference value to an existing xmlcontent");
String filename = "/xmlcontent/article_0001.html";
String filename2 = "/xmlcontent/article_0002.html";
CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms);
// now read the XML content
CmsFile file = cms.readFile(filename);
// needs to be written to assure link id correctness
cms.lockResource(filename);
file = cms.writeFile(file);
String content = new String(file.getContents());
CmsXmlContent xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver);
xmlcontent.addValue(cms, "Homepage", Locale.ENGLISH, 0);
CmsXmlVfsFileValue value = (CmsXmlVfsFileValue)xmlcontent.getValue("Homepage", Locale.ENGLISH);
value.setStringValue(cms, filename + "?a=b&c=d#e");
// validate the XML structure
xmlcontent.validateXmlStructure(resolver);
CmsLink link = value.getLink(cms);
assertEquals(link.getTarget(), cms.getRequestContext().addSiteRoot(filename));
assertTrue(link.isInternal());
assertEquals(link.getQuery(), "a=b&c=d");
assertEquals(link.getAnchor(), "e");
assertEquals(link.getStructureId(), file.getStructureId());
assertEquals(value.getStringValue(cms), filename + "?a=b&c=d#e");
CmsResource res2 = cms.readResource(filename2);
List links = cms.getRelationsForResource(filename, CmsRelationFilter.TARGETS);
assertEquals(links.size(), 1);
assertRelation(new CmsRelation(file, res2, CmsRelationType.HYPERLINK), (CmsRelation)links.get(0));
file.setContents(xmlcontent.toString().getBytes());
cms.lockResource(filename);
cms.writeFile(file);
links = cms.getRelationsForResource(filename, CmsRelationFilter.TARGETS);
assertEquals(links.size(), 2);
assertRelation(new CmsRelation(file, file, CmsRelationType.XML_WEAK), (CmsRelation)links.get(0));
assertRelation(new CmsRelation(file, res2, CmsRelationType.HYPERLINK), (CmsRelation)links.get(1));
links = cms.getRelationsForResource(filename, CmsRelationFilter.TARGETS.filterType(CmsRelationType.XML_WEAK));
assertEquals(links.size(), 1);
assertRelation(new CmsRelation(file, file, CmsRelationType.XML_WEAK), (CmsRelation)links.get(0));
links = cms.getRelationsForResource(filename, CmsRelationFilter.TARGETS.filterType(CmsRelationType.HYPERLINK));
assertEquals(links.size(), 1);
assertRelation(new CmsRelation(file, res2, CmsRelationType.HYPERLINK), (CmsRelation)links.get(0));
file = cms.readFile(filename);
content = new String(file.getContents());
xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver);
CmsXmlHtmlValue value2 = (CmsXmlHtmlValue)xmlcontent.getValue("Text", Locale.ENGLISH, 0);
link = value2.getLinkTable().getLink("link0");
assertEquals(link.getTarget(), res2.getRootPath());
assertTrue(link.isInternal());
assertNull(link.getQuery());
assertNull(link.getAnchor());
assertEquals(link.getStructureId(), res2.getStructureId());
}
/**
* Test adding and removing elements from an XML content.<p>
*
* @throws Exception in case something goes wrong
*/
public void testAddRemoveElements() throws Exception {
CmsObject cms = getCmsObject();
echo("Testing adding and removing elements from an XML content");
CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms);
String content;
CmsXmlContent xmlcontent;
// unmarshal content definition
content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-definition-6.xsd", CmsEncoder.ENCODING_UTF_8);
// store content definition in entitiy resolver
CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_6, content.getBytes(CmsEncoder.ENCODING_UTF_8));
// now read the XML content
content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-6.xml", CmsEncoder.ENCODING_UTF_8);
xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver);
CmsXmlContentValueSequence titleSequence;
titleSequence = xmlcontent.getValueSequence("Title", Locale.ENGLISH);
assertEquals("Title", titleSequence.getElementName());
assertEquals(1, titleSequence.getElementCount());
assertEquals(1, titleSequence.getMinOccurs());
assertEquals(5, titleSequence.getMaxOccurs());
assertEquals("This is just a modification test", titleSequence.getValue(0).getStringValue(cms));
CmsXmlStringValue newValue;
newValue = (CmsXmlStringValue)titleSequence.addValue(cms, 0);
assertEquals(2, titleSequence.getElementCount());
assertEquals(newValue, titleSequence.getValue(0));
newValue.setStringValue(cms, "This is another Value!");
// now re-create the XML content from the XML document
content = xmlcontent.toString();
System.out.println(content);
xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver);
// ensure the document structure is as expected
titleSequence = xmlcontent.getValueSequence("Title", Locale.ENGLISH);
assertEquals("Title", titleSequence.getElementName());
assertEquals(2, titleSequence.getElementCount());
assertEquals(1, titleSequence.getMinOccurs());
assertEquals(5, titleSequence.getMaxOccurs());
assertEquals("This is another Value!", titleSequence.getValue(0).getStringValue(cms));
assertEquals("This is just a modification test", titleSequence.getValue(1).getStringValue(cms));
// add an element at the last position
newValue = (CmsXmlStringValue)titleSequence.addValue(cms, 2);
newValue.setStringValue(cms, "This is the last value.");
assertEquals(newValue, titleSequence.getValue(2));
// add another element at the 2nd position
newValue = (CmsXmlStringValue)titleSequence.addValue(cms, 1);
newValue.setStringValue(cms, "This is the 2nd value.");
assertEquals(newValue, titleSequence.getValue(1));
assertEquals(4, titleSequence.getElementCount());
// now re-create the XML content from the XML document
content = xmlcontent.toString();
System.out.println(content);
xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver);
// ensure the document structure is as expected
titleSequence = xmlcontent.getValueSequence("Title", Locale.ENGLISH);
assertEquals("Title", titleSequence.getElementName());
assertEquals(4, titleSequence.getElementCount());
assertEquals(1, titleSequence.getMinOccurs());
assertEquals(5, titleSequence.getMaxOccurs());
assertEquals("This is another Value!", titleSequence.getValue(0).getStringValue(cms));
assertEquals("This is the 2nd value.", titleSequence.getValue(1).getStringValue(cms));
assertEquals("This is just a modification test", titleSequence.getValue(2).getStringValue(cms));
assertEquals("This is the last value.", titleSequence.getValue(3).getStringValue(cms));
// now the optional element
CmsXmlContentValueSequence optionSequence;
optionSequence = xmlcontent.getValueSequence("Option", Locale.ENGLISH);
assertEquals("Option", optionSequence.getElementName());
assertEquals(0, optionSequence.getElementCount());
assertEquals(0, optionSequence.getMinOccurs());
assertEquals(2, optionSequence.getMaxOccurs());
// add an element for the optional element
newValue = (CmsXmlStringValue)optionSequence.addValue(cms, 0);
newValue.setStringValue(cms, "Optional value 1");
assertEquals(newValue, optionSequence.getValue(0));
// add another element
newValue = (CmsXmlStringValue)optionSequence.addValue(cms, 0);
newValue.setStringValue(cms, "Optional value 0");
assertEquals(newValue, optionSequence.getValue(0));
assertEquals(2, optionSequence.getElementCount());
// now re-create the XML content from the XML document
content = xmlcontent.toString();
System.out.println(content);
xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver);
optionSequence = xmlcontent.getValueSequence("Option", Locale.ENGLISH);
assertEquals("Option", optionSequence.getElementName());
assertEquals(2, optionSequence.getElementCount());
assertEquals(0, optionSequence.getMinOccurs());
assertEquals(2, optionSequence.getMaxOccurs());
assertEquals("Optional value 0", optionSequence.getValue(0).getStringValue(cms));
assertEquals("Optional value 1", optionSequence.getValue(1).getStringValue(cms));
optionSequence.removeValue(1);
assertEquals(1, optionSequence.getElementCount());
assertEquals("Optional value 0", optionSequence.getValue(0).getStringValue(cms));
optionSequence.removeValue(0);
assertEquals(0, optionSequence.getElementCount());
// now re-create the XML content from the XML document
content = xmlcontent.toString();
System.out.println(content);
xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver);
titleSequence = xmlcontent.getValueSequence("Title", Locale.ENGLISH);
assertEquals(4, titleSequence.getElementCount());
titleSequence.removeValue(0);
titleSequence.removeValue(2);
assertEquals(2, titleSequence.getElementCount());
assertEquals("This is the 2nd value.", titleSequence.getValue(0).getStringValue(cms));
assertEquals("This is just a modification test", titleSequence.getValue(1).getStringValue(cms));
// now re-create the XML content from the XML document
content = xmlcontent.toString();
System.out.println(content);
}
/**
* Test adding and removing elements from an XML content, including nested elements.<p>
*
* @throws Exception in case something goes wrong
*/
public void testAddRemoveNestedElements() throws Exception {
CmsObject cms = getCmsObject();
echo("Testing adding and removing nested elements from an XML content");
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);
CmsXmlContentValueSequence nestedSequence;
nestedSequence = xmlcontent.getValueSequence("Cascade", Locale.ENGLISH);
assertEquals(1, nestedSequence.getElementCount());
I_CmsXmlContentValue newValue;
newValue = nestedSequence.addValue(cms, 0);
assertNotNull(newValue);
assertFalse(newValue.isSimpleType());
assertEquals(CmsXmlNestedContentDefinition.class.getName(), newValue.getClass().getName());
// re-create the XML content
content = xmlcontent.toString();
System.out.println(content);
xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver);
// validate the XML structure
xmlcontent.validateXmlStructure(resolver);
nestedSequence = xmlcontent.getValueSequence("Cascade", Locale.ENGLISH);
assertEquals(2, nestedSequence.getElementCount());
CmsXmlContentValueSequence deepNestedSequence;
deepNestedSequence = xmlcontent.getValueSequence("DeepCascade", Locale.ENGLISH);
assertEquals(0, deepNestedSequence.getElementCount());
newValue = deepNestedSequence.addValue(cms, 0);
assertNotNull(newValue);
assertFalse(newValue.isSimpleType());
assertEquals(CmsXmlNestedContentDefinition.class.getName(), newValue.getClass().getName());
// re-create the XML content
content = xmlcontent.toString();
System.out.println(content);
xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?