geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chir...@apache.org
Subject svn commit: r429359 - /geronimo/xbean/site/editing-custom-xml.html
Date Mon, 07 Aug 2006 14:55:11 GMT
Author: chirino
Date: Mon Aug  7 07:55:11 2006
New Revision: 429359

URL: http://svn.apache.org/viewvc?rev=429359&view=rev
Log:
Latest export from confluence

Modified:
    geronimo/xbean/site/editing-custom-xml.html

Modified: geronimo/xbean/site/editing-custom-xml.html
URL: http://svn.apache.org/viewvc/geronimo/xbean/site/editing-custom-xml.html?rev=429359&r1=429358&r2=429359&view=diff
==============================================================================
--- geronimo/xbean/site/editing-custom-xml.html (original)
+++ geronimo/xbean/site/editing-custom-xml.html Mon Aug  7 07:55:11 2006
@@ -130,8 +130,7 @@
 <!--          
             <div class="pagetitle">Editing Custom XML</div>
 -->
-            <DIV class="wiki-content">
-<P>XBean supports custom XML languages for different libraries of Java code. This page
details some guidelines.</P>
+            <DIV class="wiki-content"><P>XBean supports custom XML languages
for different libraries of Java code. This page details some guidelines.</P>
 
 <H3><A name="EditingCustomXML-References"></A>References</H3>
 
@@ -139,9 +138,127 @@
 
 <P>e.g. consider this example</P>
 
+<DIV class="code"><DIV class="codeContent">
+<PRE class="code-xml"><SPAN class="code-tag">&lt;beans <SPAN class="code-keyword">xmlns:p</SPAN>=<SPAN
class="code-quote">&quot;http://xbean.apache.org/schemas/pizza&quot;</SPAN>&gt;</SPAN>
 
+  <SPAN class="code-tag">&lt;bean id=<SPAN class="code-quote">&quot;topping&quot;</SPAN>
class=<SPAN class="code-quote">&quot;java.lang.String&quot;</SPAN>&gt;</SPAN>
+    <SPAN class="code-tag">&lt;constructor-arg&gt;</SPAN>
+      <SPAN class="code-tag">&lt;value&gt;</SPAN>Salami<SPAN class="code-tag">&lt;/value&gt;</SPAN>
+    <SPAN class="code-tag">&lt;/constructor-arg&gt;</SPAN>
+  <SPAN class="code-tag">&lt;/bean&gt;</SPAN>
+
+  <SPAN class="code-tag">&lt;p:pizza id=<SPAN class="code-quote">&quot;pizzaService&quot;</SPAN>
myTopping=<SPAN class="code-quote">&quot;#topping&quot;</SPAN> cheese=<SPAN
class="code-quote">&quot;##Edam&quot;</SPAN> size=<SPAN class="code-quote">&quot;17&quot;</SPAN>/&gt;</SPAN>
+  
+<SPAN class="code-tag">&lt;/beans&gt;</SPAN></PRE>
+</DIV></DIV>
+
+<P>The topping bean is configured using the regular Spring way; the pizza bean is configured
using XBean XML; however notice that the pizza bean is configured via the reference #topping.
If you really do want to configure a property with a string which starts with # then escape
it as ## as we are doing with the cheese property in this example. i.e. the cheese property
will be set to the string &quot;#Edam&quot;</P>
+
+<H3><A name="EditingCustomXML-PropertyEditorSupport"></A>PropertyEditor
Support</H3>
+
+<P>Sometimes you want to have property that takes a value that is more human friendly
to set.  For example, the ammount of beer remainaing in a keg.  Folks tend to think in different
units of measurment, but you POJO probably just want a an ammount in a specific unit.  Since
XBean 2.6, it supports a per property PropertyEditor.  You can conifigure a PropertyEditor
that should be used for a specific property when it is converting the user configured text
property value to the actual value that will be set on the property.  This gives you an oppertunity
to let the user use values like &quot;1000 ml&quot; or &quot;25 l&quot; or
&quot;300 pints&quot;, and the PropertyEditor would take care of converting all those
values into ml units.</P>
+
+<P>Here&apos;s an example of what the user would configure:</P>
+
+<DIV class="code"><DIV class="codeContent">
+<PRE class="code-xml"><SPAN class="code-tag">&lt;beans <SPAN class="code-keyword">xmlns:x</SPAN>=<SPAN
class="code-quote">&quot;http://xbean.apache.org/schemas/pizza&quot;</SPAN>&gt;</SPAN>
+
+  <SPAN class="code-tag">&lt;x:keg id=<SPAN class="code-quote">&quot;ml1000&quot;</SPAN>
remaining=<SPAN class="code-quote">&quot;1000 ml&quot;</SPAN>/&gt;</SPAN>
+  <SPAN class="code-tag">&lt;x:keg id=<SPAN class="code-quote">&quot;pints5&quot;</SPAN>
remaining=<SPAN class="code-quote">&quot;5 pints&quot;</SPAN>/&gt;</SPAN>
+  <SPAN class="code-tag">&lt;x:keg id=<SPAN class="code-quote">&quot;liter20&quot;</SPAN>
remaining=<SPAN class="code-quote">&quot;20 liter&quot;</SPAN>/&gt;</SPAN>
+  <SPAN class="code-tag">&lt;x:keg id=<SPAN class="code-quote">&quot;empty&quot;</SPAN>
remaining=<SPAN class="code-quote">&quot;0&quot;</SPAN>/&gt;</SPAN>
+
+<SPAN class="code-tag">&lt;/beans&gt;</SPAN></PRE>
+</DIV></DIV>
+
+<P>Here&apos;s how the bean developer would setup the PropertyEditor for the &quot;remaining&quot;
property:</P>
+
+<DIV class="code"><DIV class="codeContent">
+<PRE class="code-java">/**
+ * @org.apache.xbean.XBean element=<SPAN class="code-quote">&quot;keg&quot;</SPAN>

+ * 
+ * Used to verify that property PropertyEditors work correctly.
+ * 
+ * @author chirino
+ */
+<SPAN class="code-keyword">public</SPAN> class KegService {
+	
+    <SPAN class="code-keyword">private</SPAN> <SPAN class="code-object">long</SPAN>
remaining;
+
+	/**
+	 * Gets the amount of beer remaining in the keg (in ml)
+	 * 
+	 * @param remaining
+	 */
+	<SPAN class="code-keyword">public</SPAN> <SPAN class="code-object">long</SPAN>
getRemaining() {
+		<SPAN class="code-keyword">return</SPAN> remaining;
+	}
+
+	/**
+	 * Sets the amount of beer remaining in the keg (in ml)
+	 * 
+     * @org.apache.xbean.Property propertyEditor=<SPAN class="code-quote">&quot;org.apache.xbean.spring.example.MilliLittersPropertyEditor&quot;</SPAN>
+	 * @param remaining
+	 */
+	<SPAN class="code-keyword">public</SPAN> void setRemaining(<SPAN class="code-object">long</SPAN>
remaining) {
+		<SPAN class="code-keyword">this</SPAN>.remaining = remaining;
+	}
+	
+	<SPAN class="code-keyword">public</SPAN> <SPAN class="code-object">long</SPAN>
dispense( <SPAN class="code-object">long</SPAN> amount ) {
+		<SPAN class="code-keyword">this</SPAN>.remaining -= amount;
+		<SPAN class="code-keyword">return</SPAN> <SPAN class="code-keyword">this</SPAN>.remaining;
+	}
+
+}</PRE>
+</DIV></DIV>
+
+<P>And here is how the PropertyEditor was implemented:</P>
+
+<DIV class="code"><DIV class="codeContent">
+<PRE class="code-java">/**
+ * 
+ * Used to verify that per property PropertyEditors work correctly.
+ * 
+ * @author chirino
+ */
+<SPAN class="code-keyword">public</SPAN> class MilliLittersPropertyEditor <SPAN
class="code-keyword">extends</SPAN> PropertyEditorSupport {
+
+	<SPAN class="code-keyword">public</SPAN> void setAsText(<SPAN class="code-object">String</SPAN>
text) <SPAN class="code-keyword">throws</SPAN> IllegalArgumentException {
+
+		Pattern p = Pattern.compile(<SPAN class="code-quote">&quot;^(\\d+)\\s*(l(iter)?)?$&quot;</SPAN>,
Pattern.CASE_INSENSITIVE);
+		Matcher m = p.matcher(text);
+		<SPAN class="code-keyword">if</SPAN>( m.matches() ) {
+			setValue(<SPAN class="code-keyword">new</SPAN> <SPAN class="code-object">Long</SPAN>(<SPAN
class="code-object">Long</SPAN>.parseLong(m.group(1))*1000));
+			<SPAN class="code-keyword">return</SPAN>;
+		}
+		
+		p = Pattern.compile(<SPAN class="code-quote">&quot;^(\\d+)\\s*(ml)?$&quot;</SPAN>,
Pattern.CASE_INSENSITIVE);
+		m = p.matcher(text);
+		<SPAN class="code-keyword">if</SPAN>( m.matches() ) {
+			setValue(<SPAN class="code-keyword">new</SPAN> <SPAN class="code-object">Long</SPAN>(<SPAN
class="code-object">Long</SPAN>.parseLong(m.group(1))));
+			<SPAN class="code-keyword">return</SPAN>;
+		}
+
+		p = Pattern.compile(<SPAN class="code-quote">&quot;^(\\d+)\\s*pints?$&quot;</SPAN>,
Pattern.CASE_INSENSITIVE);
+	    m = p.matcher(text);
+		<SPAN class="code-keyword">if</SPAN>( m.matches() ) {
+			<SPAN class="code-object">long</SPAN> pints = <SPAN class="code-object">Long</SPAN>.parseLong(m.group(1));
+			setValue(<SPAN class="code-keyword">new</SPAN> <SPAN class="code-object">Long</SPAN>(
(<SPAN class="code-object">long</SPAN>)(pints * 1750) ));
+			<SPAN class="code-keyword">return</SPAN>;
+		}
+		
+		<SPAN class="code-keyword">throw</SPAN> <SPAN class="code-keyword">new</SPAN>
IllegalArgumentException(<SPAN class="code-quote">&quot;Could convert not to <SPAN
class="code-object">long</SPAN> (in ml) <SPAN class="code-keyword">for</SPAN>
&quot;</SPAN>+ text);		
+	}
+
+	<SPAN class="code-keyword">public</SPAN> <SPAN class="code-object">String</SPAN>
getAsText() {
+		<SPAN class="code-object">Long</SPAN> value = (<SPAN class="code-object">Long</SPAN>)
getValue();
+		<SPAN class="code-keyword">return</SPAN> (value != <SPAN class="code-keyword">null</SPAN>
? value.toString() : &quot;&quot;);
+	}
+}</PRE>
+</DIV></DIV>
 
-<P>The topping bean is configured using the regular Spring way; the pizza bean is configured
using XBean XML; however notice that the pizza bean is configured via the reference #topping.
If you really do want to configure a property with a string which starts with # then escape
it as ## as we are doing with the cheese property in this example. i.e. the cheese property
will be set to the string &quot;#Edam&quot;</P></DIV>
+
+</DIV>
           
                   </DIV>
         </TD>
@@ -151,8 +268,8 @@
     </DIV>
     <DIV id="site-footer">
           Added by     <A href="http://goopen.org/confluence/users/viewuserprofile.action?username=jstrachan">James
Strachan</A>,
-    last edited by     <A href="http://goopen.org/confluence/users/viewuserprofile.action?username=jstrachan">James
Strachan</A> on May 16, 2006
-                  &nbsp;(<A href="http://goopen.org/confluence/pages/diffpages.action?pageId=4788&originalId=5870">view
change</A>)
+    last edited by     <A href="http://goopen.org/confluence/users/viewuserprofile.action?username=chirino">Hiram
Chirino</A> on Aug 07, 2006
+                  &nbsp;(<A href="http://goopen.org/confluence/pages/diffpages.action?pageId=4788&originalId=9582">view
change</A>)
               
       (<A href="http://goopen.org/confluence/pages/editpage.action?pageId=4788">edit
page</A>)
     </DIV>



Mime
View raw message