<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>catify &#187; SDO</title>
	<atom:link href="http://www.catify.com/tag/sdo/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.catify.com</link>
	<description>connecting organizations</description>
	<lastBuildDate>Wed, 26 Sep 2012 14:43:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>SDO handling of simple types for xs:anyType elements</title>
		<link>http://www.catify.com/2010/12/01/sdo-handling-of-simple-types-for-xsanytype-elements/</link>
		<comments>http://www.catify.com/2010/12/01/sdo-handling-of-simple-types-for-xsanytype-elements/#comments</comments>
		<pubDate>Wed, 01 Dec 2010 13:23:47 +0000</pubDate>
		<dc:creator>basti</dc:creator>
				<category><![CDATA[SDO]]></category>
		<category><![CDATA[tech docs]]></category>
		<category><![CDATA[code sample]]></category>

		<guid isPermaLink="false">http://www.catify.com/?p=731</guid>
		<description><![CDATA[In the specification of SDO is defined that &#8220;xs:anyType&#8221;s in xml schema documents are represented in a map of DataObject SDO types. This leads to the problem of assigning simple types like String, Integer and Booleans. By assignment of a simple type to an element of anyType, you receive an ClassCastException at runtime. To solve [...]]]></description>
			<content:encoded><![CDATA[<p>In the specification of SDO is defined that &#8220;xs:anyType&#8221;s in xml schema documents are represented in a map of DataObject SDO types. This leads to the problem of assigning simple types like String, Integer and Booleans. By assignment of a simple type to an element of anyType, you receive an ClassCastException at runtime. To solve this issue, it is necessary to wrap the simple types to SDO DataObject&#8217;s.</p>
<p>Example would be helpful? No problem :)<br />
Assumed we have the following xml schema:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xs:schema</span> <span style="color: #000066;">xmlns:xs</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema xmlns:catify=&quot;</span>http://www.catify.com<span style="color: #ff0000;">&quot; targetNamespace=&quot;</span>http://www.catify.com<span style="color: #ff0000;">&quot;&gt;</span></span> 
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xs:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;document&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;catify:documentType&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xs:complexType</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;documentType&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xs:sequence<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xs:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;property1&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;xs:string&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xs:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;property2&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;xs:anyType&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xs:sequence<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xs:complexType<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xs:schema<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>If we assign a simple java.lang.String value to property1, the result is as expected. But when you try to assign a simple java.lang.String to property2 we run to the implied java.lang.ClassCastException. Remember, SDO treats anyType elements as a map of DataObjects.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span> property1Value <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;property1Value&quot;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> property2Value <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;property2Value&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// register xml schema</span>
XSDHelper.<span style="color: #006633;">INSTANCE</span>.<span style="color: #006633;">define</span><span style="color: #009900;">&#40;</span>getXmlSchema<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// create instance of document defined in xml schema</span>
DataObject documentType <span style="color: #339933;">=</span> DataFactory.<span style="color: #006633;">INSTANCE</span>.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://www.catify.com&quot;</span>, <span style="color: #0000ff;">&quot;documentType&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// set first property -&gt; all is fine</span>
documentType.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;property1&quot;</span>, property1Value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// if we try it this way -&gt; we receive a java.lang.ClassCastException</span>
documentType.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;property2&quot;</span>, property2Value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>So we have to wrap the simple type (java.lang.String in our example) to a DataObject before assigning it to property2.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// create a wrapper for string types</span>
DataObject property2ValueWrapper <span style="color: #339933;">=</span> DataFactory.<span style="color: #006633;">INSTANCE</span>.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// set the value</span>
property2ValueWrapper.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;value&quot;</span>, property2Value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// assign the string wrapper to property2 of document</span>
documentType.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;property2&quot;</span>, property2ValueWrapper<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Lastly we need a mechanism to unwrap the assigned value to work further.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span> simpleTypeValue <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
DataObject property2 <span style="color: #339933;">=</span> documentType.<span style="color: #006633;">getDataObject</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;property2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>property2.<span style="color: #006633;">getType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> SDOWrapperType<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  simpleTypeValue <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> property2.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;value&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The next step is to implement a utility class that provides a wrap and unwrap method to handle simple types as DataObjects. After the information in this post, it must be easy for you to implement such a class. Have fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.catify.com/2010/12/01/sdo-handling-of-simple-types-for-xsanytype-elements/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
