[opencms-dev] Programmatically create Structured-Content with addValue : ClassCastException

Marc Johnen opencms at johnen.biz
Mon Mar 23 21:29:49 CET 2009


Hello everyone,

I try to programatically create a page with Structured-Content.
For that I unmarshall a file created with the article.xsd but
with no content (Control-Code "Template").
Then I try to add the elements I need. Everything is going
well till I try to add the element "link" below "int_link",
this results in an "ClassCastException" (see exception at bottom).
Is it because of the missing attribute?

Any hint would be very welcome.

Greetings
Marc


Control-Code "Template":

<?xml version="1.0" encoding="UTF-8"?>

<Articles xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="opencms://system/modules/de.mymodule/schemas/article.xsd">
  <Article language="en">
    <title/>
    <sub_title/>
    <teaser/>
    <page>
      <text name="text0">
        <links/>
        <content/>
      </text>
    </page>
    <image/>
    <image_text/>
    <servicebox_title/>
    <servicebox_show_left>false</servicebox_show_left>
    <servicebox_show_below>false</servicebox_show_below>
  </Article>
</Articles>

Control-Code, manualy created Article:

<?xml version="1.0" encoding="UTF-8"?>

<Articles xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="opencms://system/modules/de.mymodule/schemas/article.xsd">
  <Article language="en">
    <title><![CDATA[mytitle]]></title>
    <sub_title><![CDATA[mysubtitle]]></sub_title>
    <teaser><![CDATA[teasing.]]></teaser>
    <page>
      <text name="text0">
        <links/>
        <content><![CDATA[mycontent]]></content>
      </text>
    </page>
    <image><![CDATA[/sites/mysite/_image_gallery/mypic.jpg]]></image>
    <image_text><![CDATA[imagetext]]></image_text>
    <servicebox_title><![CDATA[Lesen Sie auch:]]></servicebox_title>
    <servicebox_show_left>true</servicebox_show_left>
    <servicebox_show_below>true</servicebox_show_below>
    <servicebox_topic>
      <title/>
      <link>
        <link_target><![CDATA[_self]]></link_target>
        <link_title><![CDATA[more]]></link_title>
        <int_link>
          <link type="WEAK">
            <target><![CDATA[/sites/mysite/index.html]]></target>
            <uuid>638aa556-17ba-11de-b2ab-8d16e8b7cfa1</uuid>
          </link>
        </int_link>
      </link>
    </servicebox_topic>
  </Article>
</Articles>


Code:

			CmsResource xmlContentResource = cmso.readResource(template);
			CmsFile xmlContentFile = CmsFile.upgrade(xmlContentResource, cmso);
			CmsXmlContent xmlContent = CmsXmlContentFactory.unmarshal(cmso,
xmlContentFile);
			
			I_CmsXmlContentValue page = xmlContent.getValue("page", locale); 
			I_CmsXmlContentValue text = xmlContent.getValue(page.getPath() + "/" +
"text", locale); 
			I_CmsXmlContentValue c = xmlContent.getValue(text.getPath(), locale);
			c.setStringValue(cmso, content);

			I_CmsXmlContentValue title = xmlContent.getValue("title", locale); 
			I_CmsXmlContentValue t = xmlContent.getValue(title.getPath(), locale);
			t.setStringValue(cmso, titleString);
			
			I_CmsXmlContentValue subheadlineCV = xmlContent.getValue("sub_title",
locale); 
			subheadlineCV.setStringValue(cmso, subheadline);

			I_CmsXmlContentValue articleImageCV = xmlContent.getValue("image",
locale); 
			articleImageCV.setStringValue(cmso, article_image1);

			I_CmsXmlContentValue articleImageTextCV =
xmlContent.getValue("image_text", locale); 
			articleImageTextCV.setStringValue(cmso, article_image_text);
			
			I_CmsXmlContentValue articleTeaserCV = xmlContent.getValue("teaser",
locale); 
			articleTeaserCV.setStringValue(cmso, teaser );

			 String serviceBoxTitle = "servicebox_title";
			 String serviceboxShowLeft = "servicebox_show_left";
			 String serviceboxShowBelow = "servicebox_show_below";
			 String serviceboxTopic = "servicebox_topic";
			 String serviceboxLink_target = "link_target";
			 String serviceboxLink_title = "link_title";
			 String serviceBoxIntLink = "int_link";
			 
			 I_CmsXmlContentValue serviceBoxTitleCV =
xmlContent.getValue(serviceBoxTitle, locale);
			 serviceBoxTitleCV.setStringValue(cmso, "Lesen Sie auch:");
			 I_CmsXmlContentValue serviceboxShowLeftCV =
xmlContent.getValue(serviceboxShowLeft, locale);
			 serviceboxShowLeftCV.setStringValue(cmso, "true");
			 
			 I_CmsXmlContentValue serviceboxShowBelowCV =
xmlContent.getValue(serviceboxShowBelow, locale);
			 serviceboxShowBelowCV.setStringValue(cmso, "true");
			 
			 I_CmsXmlContentValue serviceboxTopicCV = xmlContent.addValue(cmso,
serviceboxTopic, locale, 0);

			 I_CmsXmlContentValue serviceboxLinkTargetCV =
xmlContent.getValue(serviceboxTopicCV.getPath()+"/link[1]/"+
serviceboxLink_target, locale, 0);
			 String lt = "_self";
			 serviceboxLinkTargetCV.setStringValue(cmso, lt);
			 I_CmsXmlContentValue serviceboxLinkTitleCV =
xmlContent.getValue(serviceboxTopicCV.getPath()+"/link[1]/"+
serviceboxLink_title, locale, 0);
			 String ltitle = "Mehr zu:";
			 serviceboxLinkTitleCV.setStringValue(cmso, ltitle);
			 
			 I_CmsXmlContentValue serviceboxIntLinkCV = xmlContent.addValue(cmso,
serviceboxTopicCV.getPath()+"/link[1]/"+ serviceBoxIntLink, locale, 0);
			 serviceboxIntLinkCV.getPath();
			 
			 I_CmsXmlContentValue serviceboxIntLinkLinkCV = xmlContent.addValue(cmso,
serviceboxIntLinkCV.getPath()+"/link", locale,0);




Exception:

java.lang.ClassCastException: org.opencms.xml.types.CmsXmlVfsFileValue
cannot be cast to org.opencms.xml.types.CmsXmlNestedContentDefinition
[0003465] [tomcat opencms ] 	at
org.opencms.xml.content.CmsXmlContent.addValue(CmsXmlContent.java:247)
[0003466] [tomcat opencms ] 	at
de.mysite.migration.MigrateOpenCms.createArticle(MigrateOpenCms.java:392)
[0003467] [tomcat opencms ] 	at
de.mysite.migration.MigrateOpenCms.migrateArticles(MigrateOpenCms.java:170)
[0003468] [tomcat opencms ] 	at
de.mysite.migration.MigrateOpenCms.<init>(MigrateOpenCms.java:88)
[0003469] [tomcat opencms ] 	at
org.apache.jsp.WEB_002dINF.jsp.offline.system.modules.de_mysite_migration.generieren.migrate_jsp._jspService(migrate_jsp.java:63)
[0003470] [tomcat opencms ] 	at
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
[0003471] [tomcat opencms ] 	at
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
[0003472] [tomcat opencms ] 	at
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
[0003473] [tomcat opencms ] 	at
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
[0003474] [tomcat opencms ] 	at
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
[0003475] [tomcat opencms ] 	at
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
[0003476] [tomcat opencms ] 	at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
[0003477] [tomcat opencms ] 	at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
[0003478] [tomcat opencms ] 	at
org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:679)
[0003479] [tomcat opencms ] 	at
org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:584)
[0003480] [tomcat opencms ] 	at
org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:497)
[0003481] [tomcat opencms ] 	at
org.opencms.flex.CmsFlexRequestDispatcher.includeExternal(CmsFlexRequestDispatcher.java:194)
[0003482] [tomcat opencms ] 	at
org.opencms.flex.CmsFlexRequestDispatcher.include(CmsFlexRequestDispatcher.java:169)
[0003483] [tomcat opencms ] 	at
org.opencms.loader.CmsJspLoader.service(CmsJspLoader.java:1193)
[0003484] [tomcat opencms ] 	at
org.opencms.flex.CmsFlexRequestDispatcher.includeInternalWithCache(CmsFlexRequestDispatcher.java:423)
[0003485] [tomcat opencms ] 	at
org.opencms.flex.CmsFlexRequestDispatcher.include(CmsFlexRequestDispatcher.java:173)
[0003486] [tomcat opencms ] 	at
org.opencms.loader.CmsJspLoader.dispatchJsp(CmsJspLoader.java:1227)
[0003487] [tomcat opencms ] 	at
org.opencms.loader.CmsJspLoader.load(CmsJspLoader.java:1171)
[0003488] [tomcat opencms ] 	at
org.opencms.loader.CmsResourceManager.loadResource(CmsResourceManager.java:964)
[0003489] [tomcat opencms ] 	at
org.opencms.main.OpenCmsCore.showResource(OpenCmsCore.java:1498)
[0003490] [tomcat opencms ] 	at
org.opencms.main.OpenCmsServlet.doGet(OpenCmsServlet.java:152)
[0003491] [tomcat opencms ] 	at
javax.servlet.http.HttpServlet.service(HttpServlet.java:627)
[0003492] [tomcat opencms ] 	at
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
[0003493] [tomcat opencms ] 	at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
[0003494] [tomcat opencms ] 	at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
[0003495] [tomcat opencms ] 	at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
[0003496] [tomcat opencms ] 	at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
[0003497] [tomcat opencms ] 	at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
[0003498] [tomcat opencms ] 	at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
[0003499] [tomcat opencms ] 	at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
[0003500] [tomcat opencms ] 	at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
[0003501] [tomcat opencms ] 	at
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:875)
[0003502] [tomcat opencms ] 	at
org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
[0003503] [tomcat opencms ] 	at
org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
[0003504] [tomcat opencms ] 	at
org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
[0003505] [tomcat opencms ] 	at
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
[0003506] [tomcat opencms ] 	at java.lang.Thread.run(Thread.java:619)

-- 
View this message in context: http://www.nabble.com/Programmatically-create-Structured-Content-with-addValue-%3A-ClassCastException-tp22668421p22668421.html
Sent from the OpenCMS - Dev mailing list archive at Nabble.com.




More information about the opencms-dev mailing list