<?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>OcpSoft &#187; OpenSource</title> <atom:link href="http://ocpsoft.com/cat/opensource/feed/" rel="self" type="application/rss+xml" /><link>http://ocpsoft.com</link> <description>&#34;Simple Software&#34;</description> <lastBuildDate>Tue, 27 Jul 2010 15:07:50 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0</generator> <item><title>Vote for PrettyFaces at JUDCon, JBoss World, Red Hat Summit!</title><link>http://ocpsoft.com/opensource/vote-for-prettyfaces-at-judcon-jboss-world-red-hat-summit/</link> <comments>http://ocpsoft.com/opensource/vote-for-prettyfaces-at-judcon-jboss-world-red-hat-summit/#comments</comments> <pubDate>Thu, 20 May 2010 20:23:42 +0000</pubDate> <dc:creator>Lincoln</dc:creator> <category><![CDATA[OpenSource]]></category> <category><![CDATA[PrettyFaces]]></category><guid
isPermaLink="false">http://ocpsoft.com/?p=1327</guid> <description><![CDATA[As everyone knows, or should know, one of the best conferences of our time will be taking place in Boston, MA between June 21st and June 25th, 2010. What people might not know, is that PrettyFaces has a chance to present there! All we need is your vote. It&#8217;s called JBoss World, and if you [...]]]></description> <content:encoded><![CDATA[<p>As everyone knows, or should know, one of the <a
title="Best Technology Conference Ever" href="http://www.redhat.com/promo/summit/2010/" target="_blank">best conferences</a> of our time will be taking place in <a
title="Best City Ever" href="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=Seaport+Hotel+%26+World+Trade+Center,+Boston&amp;sll=42.358431,-71.059773&amp;sspn=0.308494,0.724411&amp;g=boston,+ma&amp;ie=UTF8&amp;hq=Seaport+Hotel+%26+World+Trade+Center,&amp;hnear=Boston,+MA&amp;ll=42.352138,-71.041803&amp;spn=0.03698,0.090551&amp;z=14&amp;iwloc=A" target="_blank">Boston, MA</a> between June 21st and June 25th, 2010. What people might not know, is that PrettyFaces has a chance to present there! All we need is <em>your vote</em>. It&#8217;s called JBoss World, and if you haven&#8217;t already, you can still <a
href="https://www.eiseverywhere.com/ereg/newreg.php?eventid=9127" target="_blank">purchase</a> passes at a <a
href="https://www.eiseverywhere.com/ereg/newreg.php?eventid=9127" target="_blank">reduced rate</a>.</p><h3>Voting ends Friday May 21st, so act now!</h3><ol><li>You&#8217;ll need to <a
title="Register for an Account on JBoss Community" href="http://www.jboss.org/index.html?op=checkage&amp;module=user" target="_blank">register for an account</a> on <a
title="Register to Vote at JUDCon!" href="http://www.jboss.org/index.html?op=checkage&amp;module=user" target="_blank">JBoss.org</a> (Remember when creating an account that you&#8217;ll need to use a valid email  address for registration confirmation!)</li><li><a
href="https://community.jboss.org/en/judcon" target="_blank">Vote</a> &#8211; for &#8220;<strong>Beautiful Java EE: Use PrettyFaces to Harness SEO,  Improve User Exp, and Ease Dev by Lincoln Baxter</strong>&#8221; &#8211; otherwise known as &#8220;Beautiful Java EE: Next-generation web application for next-generation web users.&#8221;</li><li>If you have problems voting after registration, log out, clear your cookies, log in, then navigate back to the contest and vote.</li></ol><p><center><br
/> <a
href="https://community.jboss.org/en/judcon" target="_blank"><img
class="size-full wp-image-1328 aligncenter" title="Vote for Lincoln &amp; PrettyFaces at JUDCon, 2010" src="http://ocpsoft.com/wp-content/uploads/2010/05/lincoln-prettyfaces.jpg" alt="" width="380" height="303" /></a></p><h3>Do your part: <a
href="https://community.jboss.org/en/judcon" target="_blank">Vote</a>!</h3><p></center></p><p/> ]]></content:encoded> <wfw:commentRss>http://ocpsoft.com/opensource/vote-for-prettyfaces-at-judcon-jboss-world-red-hat-summit/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>JSF 2.0 Cross-field Form Validation &#8211; Simple in Reality</title><link>http://ocpsoft.com/java/jsf-2-0-cross-field-form-validation-simpl-in-reality/</link> <comments>http://ocpsoft.com/java/jsf-2-0-cross-field-form-validation-simpl-in-reality/#comments</comments> <pubDate>Wed, 05 May 2010 14:55:50 +0000</pubDate> <dc:creator>Lincoln</dc:creator> <category><![CDATA[Java]]></category> <category><![CDATA[OpenSource]]></category> <category><![CDATA[Seam]]></category><guid
isPermaLink="false">http://ocpsoft.com/?p=1254</guid> <description><![CDATA[I&#8217;d like to start by saying that using JSF by itself can sometimes feel trying to pull your own teeth out with a pair of tweezers, but there&#8217;s hope. JSF was designed to be a platform of extensions &#8211; a foundation for building web-frameworks, and that it&#8217;s done very well. JSF 2.0 addresses most of [...]]]></description> <content:encoded><![CDATA[<p>I&#8217;d like to start by saying that using JSF by itself can sometimes feel trying to pull your own teeth out with a pair of tweezers, but there&#8217;s hope. JSF was designed to be a platform of extensions &#8211; a foundation for building web-frameworks, and that it&#8217;s done very well. JSF 2.0 addresses most of the concerns about usability (so there&#8217;s less tooth pulling,) and provides even more extensibility. That&#8217;s where <a
target="_blank" href="http://seamframework.org/Seam3/FacesModule">Seam Faces</a> comes in, that&#8217;s where <a
href="http://ocpsoft.com/prettyfaces/">PrettyFaces</a> comes in.</p><p>On many occasions you might find yourself  needing to compare the values of multiple input fields on a given page submit: confirming a password; re-enter password;  address lookups; and so on. Performing cross-field form validation is simple &#8211; just place Seam&#8217;s  &lt;s:validateForm&gt; component in the form you wish to validate, then attach your custom Validator.<br
/> <span
id="more-1254"></span><br
/> I&#8217;d like to introduce you to Seam&#8217;s intuitive answer, taken directly out of the <a
target="_blank" href="http://docs.jboss.org/seam/3/faces/reference/snapshot/en-US/html_single/#validateForm">reference manual</a>. If you want to try it out, you can <a
target="_blank" href="http://seamframework.org/Seam3/FacesModule">check out</a> the source or use a snapshot:</p><div
class="wp_syntax"><div
class="code"><pre class="xml" style="font-family:monospace;">        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.jboss.seam.faces<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>seam-faces<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${seam-faces-version}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div><h2>Seam Faces&#8217; &lt;s:validateForm&gt;</h2><div
class="wp_syntax"><div
class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:form</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;locationForm&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:inputText</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;city&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{bean.city}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:inputText</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;state&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{bean.state}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:inputText</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;zip&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{bean.zip}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:commandButton</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Submit&quot;</span> <span style="color: #000066;">action</span>=<span style="color: #ff0000;">&quot;#{bean.submitPost}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:validateForm</span> <span style="color: #000066;">validatorId</span>=<span style="color: #ff0000;">&quot;locationValidator&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h:form<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div><p>The corresponding Validator for the example above would look  something like this:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;">@FacesValidator<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;locationValidator&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LocationValidator <span style="color: #000000; font-weight: bold;">implements</span> Validator
<span style="color: #009900;">&#123;</span>
   @Inject
   Directory directory<span style="color: #339933;">;</span>
&nbsp;
   @Inject
   @InputField
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Object</span> city<span style="color: #339933;">;</span>
&nbsp;
   @Inject
   @InputField
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Object</span> state<span style="color: #339933;">;</span>
&nbsp;
   @Inject
   @InputField
   <span style="color: #000000; font-weight: bold;">private</span> ZipCode zip<span style="color: #339933;">;</span>
&nbsp;
   @Override
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> validate<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> FacesContext context, <span style="color: #000000; font-weight: bold;">final</span> UIComponent comp, <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Object</span> values<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ValidatorException
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>directory.<span style="color: #006633;">exists</span><span style="color: #009900;">&#40;</span>city, state, zip<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
         <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> ValidatorException<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> FacesMessage<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Sorry, that location is not in our database. Please try again.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><h2>Tip &#8211; You may inject the correct type directly.</h2><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;">@Inject
@InputField
<span style="color: #000000; font-weight: bold;">private</span> ZipCode zip<span style="color: #339933;">;</span></pre></div></div><p>Notice that the IDs of the inputText components  match the IDs of your Validator @InputFields; each @Inject @InputField member will be injected with  the value of the form input field who&#8217;s ID matches the name of the variable.</p><p>In other words &#8211; the name of the @InputField  annotated member variable will automatically be matched to the ID of the input component, unless overridden by  using a field ID alias (see below.)</p><div
class="wp_syntax"><div
class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:form</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;locationForm&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:inputText</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;cityId&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{bean.city}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:inputText</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;stateId&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{bean.state}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:inputText</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;zip&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{bean.zip}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:commandButton</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Submit&quot;</span> <span style="color: #000066;">action</span>=<span style="color: #ff0000;">&quot;#{bean.submitPost}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:validateForm</span> <span style="color: #000066;">fields</span>=<span style="color: #ff0000;">&quot;city=cityId state=stateId&quot;</span> <span style="color: #000066;">validatorId</span>=<span style="color: #ff0000;">&quot;locationValidator&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h:form<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div><p>The field with ID &#8220;zip&#8221; will still be referenced  normally; you need only specify aliases for fields that differ in name from the Validator  @InputFields.</p><h2>Tip &#8211; Using @InputField</h2><p>Using <code>@InputField("customID")</code> with an ID override can also be used to specify a custom ID, instead of using the default: the name of the  field. This gives you the ability to change the name of the private field, without  worrying about changing the name of input fields in the View itself.</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;">@Inject
@InputField<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;state&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> sectorTwo<span style="color: #339933;">;</span></pre></div></div><h2>A few last thoughts</h2><p>First, as of the current version, cross-field validation does not work unless you are using the &lt;s:validateForm&gt; component. As soon as a new version of <a
target="_blank" href="http://seamframework.org/Weld/PortableExtensionsPackage">weld-extensions</a> is released, however, this functionality will work even without the &lt;s:validateForm&gt; component, and you&#8217;ll be able to reference cross-fields in any JSF validator!</p><p>For right now, however, you&#8217;ll have to deal with using the component &#8211; but &#8211; it&#8217;s really not that complicated if you think about it <img
src='http://ocpsoft.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> Happy coding!</p> ]]></content:encoded> <wfw:commentRss>http://ocpsoft.com/java/jsf-2-0-cross-field-form-validation-simpl-in-reality/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Using Embedded Glassfish with Maven</title><link>http://ocpsoft.com/java/using-embedded-glassfish-with-maven/</link> <comments>http://ocpsoft.com/java/using-embedded-glassfish-with-maven/#comments</comments> <pubDate>Tue, 23 Mar 2010 18:12:06 +0000</pubDate> <dc:creator>Lincoln</dc:creator> <category><![CDATA[Java]]></category> <category><![CDATA[OpenSource]]></category><guid
isPermaLink="false">http://ocpsoft.com/?p=1162</guid> <description><![CDATA[Three steps to add Embedded GlassFish to any existing project POM. (Upcoming &#8211; Part 2: Using Embedded JBoss AS with Maven) Using Embedded Glassfish The Embedded Glassfish project is hosted on java.net, and as it is described, is very simple: &#8220;This project develops APIs and tools around &#8220;embedded GlassFish v3&#8243;. Embedded GFv3 is a delivery [...]]]></description> <content:encoded><![CDATA[<p>Three steps to add Embedded GlassFish to any existing project POM. (Upcoming &#8211; Part 2: Using Embedded JBoss AS with Maven)</p><h3>Using Embedded Glassfish</h3><p>The <a
target="_blank" href="https://embedded-glassfish.dev.java.net/">Embedded Glassfish</a> project is hosted on <a
target="_blank" href="https://embedded-glassfish.dev.java.net/">java.net</a>, and as it is described, is very simple: &#8220;This project develops APIs and tools around &#8220;embedded GlassFish v3&#8243;. Embedded GFv3 is a delivery vehicle of GFv3 so that applications and tools can use GFv3 just as a library, inside their JVM, instead of having GFv3 as the overload of the entire JVM process. (And no, embedded GFv3 is not meant to be run on JavaME.)&#8221;<br
/> <span
id="more-1162"></span></p><h4>1. You&#8217;ll need to add this plugin repository to your POM:</h4><div
class="wp_syntax"><div
class="code"><pre class="xml" style="font-family:monospace;">        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pluginRepositories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pluginRepository<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Glassfish<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://download.java.net/maven/glassfish/<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/pluginRepository<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/pluginRepositories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div><h4>2. Then add the following plugin to your POM:</h4><div
class="wp_syntax"><div
class="code"><pre class="xml" style="font-family:monospace;">          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.glassfish<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>maven-embedded-glassfish-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>3.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
					<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goalPrefix<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>glassfish<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goalPrefix<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
					<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;app<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>test.war<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/app<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
					<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;port<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>8080<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/port<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
					<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;contextRoot<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/contextRoot<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;executions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
					<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
						<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;phase<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>install<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/phase<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
						<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
							<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>run<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
						<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
					<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/executions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div><h4>3. Commands</h4><ul><li><strong>embedded-glasfish:run</strong> &#8211; starts the embedded server if it is not already running and deploys the specfied application. The goal keeps the server running, so the user can make changes in the application, reploy and test iteratively.</li><li><strong>embedded-glassfish:start</strong> &#8211; starts the embedded server with the specified parameters</li><li><strong>embedded-glassfish:deploy</strong> &#8211; deploys the specified application</li><li><strong>embedded-glassfish:undeploy</strong> &#8211; undeploys the application identified by the &#8220;name&#8221; parameter. If no name is specifed all applications are undeployed.</li><li><strong>embedded-glassfish:stop</strong> &#8211; stops the embedded server</li><li><strong>embedded-glassfish:admin</strong> &#8211; executes the specified admin command on the embedded server. For example, you could create a jdbc resource.</li></ul><h3>Reference:</h3><p><a
target="_blank" href="http://blogs.sun.com/sirajg/entry/using_maven_plugin_for_v3">http://blogs.sun.com/sirajg/entry/using_maven_plugin_for_v3</a></p> ]]></content:encoded> <wfw:commentRss>http://ocpsoft.com/java/using-embedded-glassfish-with-maven/feed/</wfw:commentRss> <slash:comments>9</slash:comments> </item> <item><title>Improving Community &#8211; The power of good, centralized documentation.</title><link>http://ocpsoft.com/opensource/improving-community-the-power-of-good-centralized-documentation/</link> <comments>http://ocpsoft.com/opensource/improving-community-the-power-of-good-centralized-documentation/#comments</comments> <pubDate>Thu, 04 Mar 2010 19:15:14 +0000</pubDate> <dc:creator>Lincoln</dc:creator> <category><![CDATA[OpenSource]]></category><guid
isPermaLink="false">http://ocpsoft.com/?p=1141</guid> <description><![CDATA[Read the original post on in.relation.to If you&#8217;ve ever programmed in PHP, Ruby, Perl, Groovy, and probably others, then you know how nice it is to have a central, official space for reference documentation and inline community feedback. This is something that has sorely been missing from the JEE community, and something that has caused [...]]]></description> <content:encoded><![CDATA[<div
class="featured"><center>Read the original post on <a
target="_blank" href="http://in.relation.to/Bloggers/ImprovingCommunityThePowerOfGoodCentralizedDocumentation">in.relation.to</a></center></div><p>If you&#8217;ve ever programmed in PHP, Ruby, Perl, Groovy, and probably others, then you know how nice it is to have a central, official space for reference documentation and inline community feedback.</p><p>This is something that has sorely been missing from the JEE community, and something that has caused many disparate websites to attempt putting forward a weak effort in providing useful documentation &#8212; you leave the community part aside to think that this is a good thing for any open-source technology.<br
/> <span
id="more-1141"></span></p><h3>A successful open-source community documentation initiative has:</h3><ul><li>Comprehensive documentation provided on a central, official website (<a
href="http://seamframework.org">http://seamframework.org</a>)</li><li>Inline comments and user feedback.</li><li> The most common paths on the website are the most visible, intuitive paths.</li><li>Fluent navigation and document hierarchies. (The URL matches the Breadcrumbs matches the content. If users get lost, users give up.)</li><li>Accurate and relevant official information, well vetted information.</li></ul><p>I&#8217;m sure there is more, but that&#8217;s what I have at the top of my head.</p><h3>Making progress happen:</h3><p>In kick-starting my (hopefully long) tenure with Red Hat, I&#8217;m focusing on improving community documentation for the Seam Framework, and for Java EE as a whole. I&#8217;ve started by updated the Seam Framework website to more clearly display the links to Seam and Weld JIRAs; you previously had to do a little digging.</p><p>All of my efforts can be tracked here:<a
href="https://jira.jboss.org/jira/browse/JBSEAM-4585"> https://jira.jboss.org/jira/browse/JBSEAM-4585</a></p><p>Please, I ask if you find any outstanding problems, or points of pain with the Seam or Weld documentation, to add it to this Jira. If you can&#8217;t, then comment here and I&#8217;ll see that it&#8217;s addressed.</p><p>SeamFramework.org needs work, and I do think that the community really belongs on <a
href="http://jboss.org">jboss.org</a>, in order to fully get credit for their hard work and great achievements. Jboss.org also provides much of the functionality that I&#8217;m looking for in a community management tool, but we&#8217;ll see what everyone wants <img
src='http://ocpsoft.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></p><p>Happy communities make happy software!</p> ]]></content:encoded> <wfw:commentRss>http://ocpsoft.com/opensource/improving-community-the-power-of-good-centralized-documentation/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Announcing &#8211; PrettyFaces 2.0.4: URL Rewriting for JSF 2.0</title><link>http://ocpsoft.com/opensource/announcing-prettyfaces-2-0-4-url-rewriting-for-javaserver-faces-jsf2/</link> <comments>http://ocpsoft.com/opensource/announcing-prettyfaces-2-0-4-url-rewriting-for-javaserver-faces-jsf2/#comments</comments> <pubDate>Fri, 19 Feb 2010 22:31:15 +0000</pubDate> <dc:creator>Lincoln</dc:creator> <category><![CDATA[OpenSource]]></category> <category><![CDATA[PrettyFaces]]></category><guid
isPermaLink="false">http://ocpsoft.com/?p=1041</guid> <description><![CDATA[We are proud to introduce version the 2.0.4 series of PrettyFaces: Add PrettyFaces to an existing JSF2 application less than 5 minutes: Non-invasive! &#8211; never change your application internals, simply lay PrettyFaces on to an existing application. Zero configuration* in a Servlet 3.0 environment &#8211; PrettyFaces core classes are automatically registered with the servlet container [...]]]></description> <content:encoded><![CDATA[<div
class="featured"><center>We are <strong>proud</strong> to introduce version the 2.0.4 series of <a
href="http://ocpsoft.com/prettyfaces/" title="SEO | Dynamic Parameters | Bookmarks | Navigation | JSF | JSF2 | UrlRewrite">PrettyFaces</a>:</center></div><p><span
id="more-1041"></span></p><h3>Add PrettyFaces to an existing JSF2 application less than 5 minutes:</h3><ul><li><strong>Non-invasive!</strong> &#8211; never change your application internals, simply lay PrettyFaces on to an existing application.</li><li><strong>Zero configuration</strong>* in a Servlet 3.0 environment &#8211; PrettyFaces core classes are automatically registered with the servlet container and JavaServer Faces. (<em>No need to modify web.xml!</em>)</li><li><strong>Rapid development</strong> via automatic configuration reloading (when java.faces.PROJECT_STAGE = Development)</li></ul><ul><li><strong>Custom Inbound/outbound URL rewriting rules</strong> &#8211; with built in support for canonicalization such as appending or removing trailing slashes, lowercasing, custom rewrites, and more.</li><li><strong>Enhanced SEO and Rewriting support</strong> &#8211; including the ability to choose 301, 302, or internal redirects.</li><li><strong>Outbound URL mapping rewriting</strong> &#8211; removes the need for &lt;pretty:link&gt; now &lt;h:link&gt; can be used to generate pretty, SEO-friendly URLs.</li><li><strong>Named pattern / path-parameters</strong> &#8211; for integration with JSF2 <f:viewParam> and non-JSF applications &#8211; not all applications support EL, but they all understand ServletRequest.getParameter(&#8230;)</li></ul><div
class="featured"><center>Read the <a
target="_blank" href="http://ocpsoft.com/wp-content/uploads/2010/02/prettyfaces-2.0.4-reference.pdf">reference guide</a>.<br
/></center></div><h3>Maven Dependency</h3><div
class="wp_syntax"><div
class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.ocpsoft<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>ocpsoft-pretty-faces<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2.0.4<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div><p><center
class="featured"><object
width="640" height="505"><param
name="movie" value="http://www.youtube.com/v/1kicjmLsN3w&#038;hl=en_US&#038;fs=1&#038;color1=0x006699&#038;color2=0x54abd6"></param><param
name="allowFullScreen" value="true"></param><param
name="allowscriptaccess" value="always"></param><embed
src="http://www.youtube.com/v/1kicjmLsN3w&#038;hl=en_US&#038;fs=1&#038;color1=0x006699&#038;color2=0x54abd6" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="505"></embed></object><br
/><h3>Why not try <a
href="http://ocpsoft.com/prettyfaces/" title="SEO | Dynamic Parameters | Bookmarks | Navigation | JSF | JSF2 | UrlRewrite">PrettyFaces</a> now?.</h3><p></center></p> ]]></content:encoded> <wfw:commentRss>http://ocpsoft.com/opensource/announcing-prettyfaces-2-0-4-url-rewriting-for-javaserver-faces-jsf2/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Safely add / modify Servlet request parameter values</title><link>http://ocpsoft.com/opensource/how-to-safely-add-modify-servlet-request-parameter-values/</link> <comments>http://ocpsoft.com/opensource/how-to-safely-add-modify-servlet-request-parameter-values/#comments</comments> <pubDate>Fri, 05 Feb 2010 04:35:29 +0000</pubDate> <dc:creator>Lincoln</dc:creator> <category><![CDATA[OpenSource]]></category><guid
isPermaLink="false">http://ocpsoft.com/?p=1049</guid> <description><![CDATA[PrettyFaces Sometimes things are worth writing about. While working on the PrettyFaces: bookmarking, and SEO extensions for JSF / JSF2, I came across a need to modify the current request parameters in order to &#8220;trick&#8221; the system into thinking that additional query parameters had been supplied. Naively, I tried: request.getParameterMap&#40;&#41;.put&#40;&#34;name&#34;, new String&#91;&#93;&#123;&#34;value1&#34;, &#34;value2&#34;&#125;&#41;; But that [...]]]></description> <content:encoded><![CDATA[<h1 style="display:none;"><a
href="http://ocpsoft.com/prettyfaces/"  title="SEO | Dynamic Parameters | Bookmarks | and Navigation for JSF | JSF2">PrettyFaces</a></h1><h2>Sometimes things are worth writing about.</h2><p>While working on the <a
href="http://ocpsoft.com/prettyfaces/"  title="SEO | Dynamic Parameters | Bookmarks | and Navigation for JSF | JSF2">PrettyFaces</a>: bookmarking, and SEO extensions for JSF / JSF2, I came across a need to modify the current request parameters in order to &#8220;trick&#8221; the system into thinking that additional query parameters had been supplied.</p><h3>Naively, I tried:</h3><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;">    request.<span style="color: #006633;">getParameterMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;value1&quot;</span>, <span style="color: #0000ff;">&quot;value2&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p>But that doesn&#8217;t work, because you aren&#8217;t allowed to modify request parameter values once the request has begun processing:</p><div
class="wp_syntax"><div
class="code"><pre class="text" style="font-family:monospace;">    java.lang.IllegalStateException: Cannot find message associated with key parameterMap.locked
	at org.apache.catalina.util.ParameterMap.put(ParameterMap.java:213)</pre></div></div><p><span
id="more-1049"></span></p><p>This means we need to wrap the current request with our own special <code>HttpServletRequestWrapper</code> that will provide the ability to merge in some extra parameters up front.</p><h3>The magic is quite simple:</h3><p>But be careful! If you call <code>request.getParameterMap()</code> or any method that would call <code>request.getReader()</code> and begin reading, you will prevent any further calls to <code>request.setCharacterEncoding(...)</code>, which is a big deal if you are processing i18n requests, or requests with a special content type.</p><p>You need to ensure that the new parameters are merged with the original request parameters only when the parameters are requested, so that everyone still has a chance to set the encoding. Otherwise you get nasty warnings like this:</p><div
class="wp_syntax"><div
class="code"><pre class="text" style="font-family:monospace;">WARNING: PWC4011: Unable to set request character encoding to UTF-8 from context 
/ocpsoft-pretty-faces-tests, because request parameters have already been read, or 
ServletRequest.getReader() has already been called</pre></div></div><h3>Our little HttpServletRequestWrapper:</h3><p>You could simply provide a modifiable map implementation, but that would break the contract of the request parameter map.</p><p>So using a bit of lazy processing, here&#8217;s how we avoid reading the parameters until they are actually requested, while still maintaining the immutable nature of the Map.</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*
 * PrettyFaces is an OpenSource JSF library to create bookmarkable URLs.
 * Copyright (C) 2010 - Lincoln Baxter, III &lt;lincoln@ocpsoft.com&gt; This program
 * is free software: you can redistribute it and/or modify it under the terms of
 * the GNU Lesser General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version. This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details. You should have received a copy of the GNU Lesser General
 * Public License along with this program. If not, see the file COPYING.LESSER
 * or visit the GNU website at &lt;http://www.gnu.org/licenses/&gt;.
 */</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Collections</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Enumeration</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Map</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.TreeMap</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequestWrapper</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PrettyFacesWrappedRequest <span style="color: #000000; font-weight: bold;">extends</span> HttpServletRequestWrapper
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> Map<span style="color: #339933;">&lt;</span>String, <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&gt;</span> modifiableParameters<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> Map<span style="color: #339933;">&lt;</span>String, <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&gt;</span> allParameters <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Create a new request wrapper that will merge additional parameters into
     * the request object without prematurely reading parameters from the
     * original request.
     * 
     * @param request
     * @param additionalParams
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> PrettyFacesWrappedRequest<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> HttpServletRequest request, 
                                                    <span style="color: #000000; font-weight: bold;">final</span> Map<span style="color: #339933;">&lt;</span>String, <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&gt;</span> additionalParams<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span>request<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        modifiableParameters <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TreeMap<span style="color: #339933;">&lt;</span>String, <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        modifiableParameters.<span style="color: #006633;">putAll</span><span style="color: #009900;">&#40;</span>additionalParams<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getParameter<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> strings <span style="color: #339933;">=</span> getParameterMap<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>name<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>strings <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> strings<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> Map<span style="color: #339933;">&lt;</span>String, <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&gt;</span> getParameterMap<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>allParameters <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            allParameters <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TreeMap<span style="color: #339933;">&lt;</span>String, <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            allParameters.<span style="color: #006633;">putAll</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">getParameterMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            allParameters.<span style="color: #006633;">putAll</span><span style="color: #009900;">&#40;</span>modifiableParameters<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #666666; font-style: italic;">//Return an unmodifiable collection because we need to uphold the interface contract.</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #003399;">Collections</span>.<span style="color: #006633;">unmodifiableMap</span><span style="color: #009900;">&#40;</span>allParameters<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> Enumeration<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> getParameterNames<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #003399;">Collections</span>.<span style="color: #006633;">enumeration</span><span style="color: #009900;">&#40;</span>getParameterMap<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">keySet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> getParameterValues<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> getParameterMap<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>Once you have your handy request wrapper, you can get working! Keep in mind that you&#8217;ll need to do a servlet forward if you want your entire system to gain access to your new parameters. Otherwise, you can just pass this new wrapped object around as needed.</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">//&quot;request&quot; is the current HttpServletRequest</span>
    Map<span style="color: #339933;">&lt;</span>String, <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&gt;</span> extraParams <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TreeMap<span style="color: #339933;">&lt;</span>String, <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    HttpServletRequest wrappedRequest <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PrettyFacesWrappedRequest<span style="color: #009900;">&#40;</span>request, extraParams<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    request.<span style="color: #006633;">getRequestDispatcher</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;url&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">forward</span><span style="color: #009900;">&#40;</span>wrappedRequest, response<span style="color: #009900;">&#41;</span></pre></div></div><p>Happy hacking! <img
src='http://ocpsoft.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> As a final note, I&#8217;d like to say that <a
href="http://java.sun.com/developer/technicalArticles/JavaEE/JavaEE6Overview.html">JEE 6</a> has really turned out to be a great experience. Very pleasant to program on. Well done, SUN!</p> ]]></content:encoded> <wfw:commentRss>http://ocpsoft.com/opensource/how-to-safely-add-modify-servlet-request-parameter-values/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>One year&#8217;s time</title><link>http://ocpsoft.com/opensource/one-years-time/</link> <comments>http://ocpsoft.com/opensource/one-years-time/#comments</comments> <pubDate>Thu, 28 Jan 2010 17:53:39 +0000</pubDate> <dc:creator>Derek</dc:creator> <category><![CDATA[JSF]]></category> <category><![CDATA[JSF2]]></category> <category><![CDATA[OCPSoft]]></category> <category><![CDATA[OpenSource]]></category> <category><![CDATA[PrettyFaces]]></category><guid
isPermaLink="false">http://ocpsoft.com/?p=788</guid> <description><![CDATA[It is amazing what can be done in exactly one year&#8217;s time.  January 17th will have been the 2nd anniversary since Lincoln Baxter and I started OcpSoft.  It&#8217;s been one seriously fun, and wild, ride with JavaServer Faces and the open-source community.  I want to take a moment to talk about my partner&#8217;s success story [...]]]></description> <content:encoded><![CDATA[<p>It is amazing what can be done in exactly one year&#8217;s time.  January 17th will have been the 2nd anniversary since Lincoln Baxter and I started OcpSoft.  It&#8217;s been one seriously fun, and wild, ride with <a
href="http://www.javaserverfaces.org">JavaServer Faces</a> and the open-source community.  I want to take a moment to talk about my partner&#8217;s success story and what he has accomplished in just <em>one</em> short year.</p><p><span
id="more-788"></span></p><h3>The Beginning</h3><p>When we started OcpSoft, we were working on what is now <a
title="ScrumShark" href="http://ocpsoft.com/scrumshark/" target="_blank">ScrumShark</a> &#8211; using JSF 1.2 , Hibernate, and many other technologies.  At first it was very overwhelming, challenging to figure out which technologies to use; though, even after we had chosen our technology stack, we fought with poor documentation, lack of support, and integration as we explored new features of the stack.</p><p>During that time Lincoln saw a need for pretty URLs, because what was currently on the market wasn&#8217;t intuitive, and was painful to work with.  The enhancements we requested weren&#8217;t getting approved or were too slow coming, even providing patches.  So Lincoln set out to do it himself, and as he puts it: &#8220;destroyed Thanksgiving 2008.&#8221;</p><h3>PrettyFaces</h3><p>Thanks to <a
href="http://ocpsoft.com/prettyfaces/">PrettyFaces</a>, Lincoln dug-in to JSF and started to really understand the inner workings of both problem and solution; within a few weeks of releasing version one, we were seeing many supporters.  Soon after, Lincoln was contacted by Dan Allen from JBoss/Red Hat, and started assisting him with an enhancement to JSF&#8217;s navigation system.</p><p>During that same time Lincoln convinced us &#8211; at OcpSoft &#8211; to start using JSF 2 Beta to build ScrumShark.  JSF 2 was undergoing continuous changes at that time, and the specification wasn&#8217;t even public, so we were seeing different results in our application every week.  Lincoln started posting the issues on the JSF 2 mailing list, and quickly became known as the JSF 2 tester, bug-finder.  Without <a
title="ScrumShark" href="http://ocpsoft.com/scrumshark/" target="_blank">ScrumShark</a>, and Lincoln willing to take a chance on a bleeding edge technology, I don&#8217;t think the JSF 2 team would have found and fixed all the bugs as fast as they did.  Not too long after this, he joined the JSF Expert Group and was presenting <a
href="http://ocpsoft.com/prettyfaces/jsfsummit-2009-prettyfaces-makes-an-appearance/" target="_blank">PrettyFaces at JSFSummit</a>.</p><h3>Congratulations Lincoln!</h3><p>He just got a job with <a
target="_blank" href="http://jboss.org">JBoss</a> at <a
target="_blank" href="http://www.redhat.com">Red Hat</a>, as a Senior Software Engineer. I am <em>proud </em>of of what my partner and best friend Lincoln has accomplished.</p> ]]></content:encoded> <wfw:commentRss>http://ocpsoft.com/opensource/one-years-time/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>JSF 1.2 Components (Book Review)</title><link>http://ocpsoft.com/opensource/jsf-1-2-components-book-review/</link> <comments>http://ocpsoft.com/opensource/jsf-1-2-components-book-review/#comments</comments> <pubDate>Wed, 20 Jan 2010 06:56:21 +0000</pubDate> <dc:creator>Lincoln</dc:creator> <category><![CDATA[JSF2]]></category> <category><![CDATA[OpenSource]]></category> <category><![CDATA[Technology]]></category> <category><![CDATA[JSF]]></category><guid
isPermaLink="false">http://ocpsoft.com/?p=983</guid> <description><![CDATA[As a vocal blogger, I feel responsible for promoting and sharing the good work of others, whether that be technology, creative work, or in this case: a book. I will take no exception to that philosophy when it comes to the JavaServer Faces framework. For a quick read, try the summary. If you are intrigued, [...]]]></description> <content:encoded><![CDATA[<p>As a vocal blogger, I feel responsible for promoting and sharing the good work of others, whether that be technology, creative work, or in this case: a book. I will take no exception to that philosophy when it comes to the <a
target="_blank" href="http://www.javaserverfaces.org">JavaServer Faces</a> framework. For a quick read, try the summary. If you are intrigued, read on! I hope you find this review valuable.<br
/> <span
id="more-983"></span></p><p><strong>In summary:</strong> I would recommend buying this reference if you are a consumer, designer, UI-developer who is working with existing component libraries, Facelets or JSF 2.0, and not so much focused on creating custom Java-components of your own. I don&#8217;t, for every-day web-site components, see why you would need much more than what is featured between the covers of this book. Ian Hlavats does a fantastic job of making bringing JSF to the everyday development setting.<br
/> <br/></p><table><tr><td><a
target="_blank" href="http://www.packtpub.com/jsf-1-2-components-develop-advanced-ajax-enabled-applications/book?utm_source=ocpsoft.com&#038;utm_medium=bookrev&#038;utm_content=blog&#038;utm_campaign=mdb_002041"><img
src="http://ecx.images-amazon.com/images/I/51zwgyza8HL._SL500_AA240_.jpg"></img></a></td><td><h4><a
target="_blank" href="http://www.packtpub.com/jsf-1-2-components-develop-advanced-ajax-enabled-applications/book?utm_source=ocpsoft.com&#038;utm_medium=bookrev&#038;utm_content=blog&#038;utm_campaign=mdb_002041">JSF 1.2 Components</a></h4><p>By Ian Hlavats<br
/> <br/><br
/> Download Chapter 2: <a
target="_blank" href="http://www.packtpub.com/files/7627-developing-jsf-components-sample-chapter-2-facelets-components.pdf">Facelets Components</a> as a PDF</td></tr></table><p><br/></p><h3>In detail:</h3><p>Initially hesitant to buy a reference on components, I have to admit that I was surprised to learn so much by reading through this book. As a member of the JavaServer Faces 2.0 expert group, and a full-time software engineer, components are not my strong-suit. If you&#8217;re at all like me, then you&#8217;re probably asking yourself the same question I did:</p><p>&#8220;What can I learn in this book that I can&#8217;t find on Google? JavaServer Faces has a huge online community, and there&#8217;s a lot of information available already.&#8221;</p><p>It&#8217;s a valid question, but you should keep reading because this book offers something important that I rarely find in blog entries or wikis: &#8220;features you wouldn&#8217;t think to look for; features that you wish you&#8217;d thought of, or don&#8217;t know how to make yourself.&#8221; I wish I had found this book when I started using JSF, because I&#8217;d probably have been more comfortable, and much more productive with our component side of the life-cycle, so to speak; I discovered features of the framework, and pre-made UI-tools that I wish I&#8217;d known about years ago.</p><h5>Things you&#8217;ll learn about:</h5><p>Modal windows, menus, wizards and workflows, AJAX anything with Ajax4JSF, i18n and localization, gCal-like schedules with Apache Tomahawk, multi-field validation, calendars, charts and graphs, file-uploads, user permissions/security, skinning, styling, and all major component libraries are covered in this book (save PrimeFaces, which gets a mere mention.)</p><p>If you think you&#8217;re looking at &#8220;Just another book on JSF component writing,&#8221; that&#8217;s not entirely the case. This book provides real solutions, real examples on how to get started writing JSF applications using existing component libraries and plug-ins. There are two types of chapters in this book: tutorials and references. The tutorials will get you up and running with a quick example, while the reference sections go in depth on usage of individual components within each library.</p><p>You&#8217;ll also (as a bonus, in my opinion) get some appetite-whetting information on the JBoss Seam framework, which is a user-friendly and business-oriented extension to the Java EE technologies. I didn&#8217;t get a very good picture of all of the concepts until I actually looked at the code samples (downloadable here, or just follow the link in the preface,) but that&#8217;s hardly a strong criticism; though, it may have helped if the code samples were more explicitly, frequently referenced.</p><h5>In the end &#8220;The title doesn&#8217;t lie&#8221;:</h5><ul><li>I haven&#8217;t seen a more comprehensive book on component libraries and component writing made available to date.</li><li>JSF2 component writing has been simplified, but if you want to supplement your app with everyday things like in-place editing, accordions, and more, you&#8217;re going to want good examples; this book provides examples in abundance (even examples of JSF2 EzComp, which is a dream to use. UI like it was meant to be)</li><li>Even if components and UI design are not your strong-point, that is exactly why you should read this book; it makes component-based design easy to understand, and easy to implement.</li></ul><p>The author covers many of the advancements in component writing and simplified configuration that are provided by JSF 2.0, but I would have liked to hear some information on the component behaviors model, and mention of integration with frameworks like CDI (JSR-299) and Spring; however, this is not entirely relevant to component writing, and plenty of information exists on sites like <a
target="_blank" href="http://www.javaserverfaces.org">www.javaserverfaces.org</a> or Google.</p><p>My one complaint is that for a book titled &#8220;JSF 1.2 Components,&#8221; it did not explain, or teach me how to create custom Java-based components, which are essential for complex behavior and interaction at some level. JSF 2.0 has done a good job of making Java-based components unnecessary, but this book is titled JSF 1.2 components, thus, I would have liked to see how to write a Java-based component in JSF 1.2.</p><div
class="featured" style="text-align: center;"> <a
target="_blank" href="http://www.packtpub.com/jsf-1-2-components-develop-advanced-ajax-enabled-applications/book?utm_source=ocpsoft.com&#038;utm_medium=bookrev&#038;utm_content=blog&#038;utm_campaign=mdb_002041">JSF 1.2 Components</a> on Packt Publishing<br
/> Download Chapter 2: <a
target="_blank" href="http://www.packtpub.com/files/7627-developing-jsf-components-sample-chapter-2-facelets-components.pdf">Facelets Components</a> as a PDF</div><h3>End notes:</h3><p> Make sure you check out <a
target="_blank" href="http://www.javaserverfaces.org">www.javaserverfaces.org</a>. It&#8217;s a great starting point and reference for JSF that you will undoubtably bookmark if doing a lot of work, and doing the work with this book. Also check out <a
href="http://ocpsoft.com/prettyfaces/">http://ocpsoft.com/prettyfaces/</a> for more information on Pretty URLs in JSF, and building client-facing JSF web applications.</p> ]]></content:encoded> <wfw:commentRss>http://ocpsoft.com/opensource/jsf-1-2-components-book-review/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Guide to Regular Expressions in Java (Part 1)</title><link>http://ocpsoft.com/opensource/guide-to-regular-expressions-in-java-part-1/</link> <comments>http://ocpsoft.com/opensource/guide-to-regular-expressions-in-java-part-1/#comments</comments> <pubDate>Sun, 10 Jan 2010 01:12:23 +0000</pubDate> <dc:creator>Lincoln</dc:creator> <category><![CDATA[OpenSource]]></category><guid
isPermaLink="false">http://ocpsoft.com/?p=822</guid> <description><![CDATA[Often unknown, or heralded as confusing, regular expressions have defined the standard for powerful text manipulation and search. Without them, many of the applications we know today would not function. This two-part series explores the basics of regular expressions in Java, and provides tutorial examples in the hopes of spreading love for our pattern-matching friends. [...]]]></description> <content:encoded><![CDATA[<p>Often unknown, or heralded as confusing, regular expressions have defined the standard for powerful text manipulation and search. Without them, many of the applications we know today would not function. This two-part series explores the basics of regular expressions in Java, and provides tutorial examples in the hopes of spreading love for our pattern-matching friends. (Read <a
href="http://ocpsoft.com/opensource/guide-to-regular-expressions-in-java-part-2/">part two</a>.)<br
/> <span
id="more-822"></span></p><h2>Part 1: What are Regular Expressions?</h2><p>Regular expressions are a language of string patterns built in to most modern programming languages, including <a
target="_blank" href="http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html">Java 1.4 onward</a>; they can be used for: searching, extracting, and modifying text. This chapter will cover basic syntax and use.</p><p><a
name="contents"></a></p><h2>Table of Contents:</h2><div
class="featured"><div
class="inside"><ol><li><h4><a
href="#syntax">Syntax</a></h4><ul><li><a
href="#charclasses">Character Classes</a></li><li><a
href="#quantifiers">Quantifiers</a></li><li><a
href="#metachars">Meta-characters</a></li></ul></li><li><h4><a
href="#examples">Examples</a></h4><ul><li><a
href="#basic">Basic Expressions</a></li><li><a
href="#grouping">Basic Grouping</a></li><li><a
href="#matching">Matching/Validating</a></li><li><a
href="#capture">Extracting/Capturing</a></li><li><a
href="#modify">Modifying/Substitution</a></li></ul></li><li><h4><a
href="#conclude">Conclusion</a></h4></li></ol><div
class="clearer"></div></div><div
class="inside" style="margin-top: 15px;"> This article is part one in the series: &#8220;Guide to Regular Expressions in Java.&#8221; Read <a
href="http://ocpsoft.com/opensource/guide-to-regular-expressions-in-java-part-2/">part two</a>.</div></div><p><a
name="syntax"></a><br
/><h2><a
href="#contents">1. Syntax</a></h2><p>Regular expressions, by definition, are string patterns that describe text. These descriptions can then be used in nearly infinite ways. The basic language constructs include character classes, quantifiers, and meta-characters.</p><p><a
name="charclasses"></a></p><h4><a
href="#contents">1.1. Character Classes</a></h4><p>Character classes are used to define the content of the pattern. E.g. what should the pattern look for?</p><div
class="wp_syntax"><div
class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #339933;">.</span>  	Dot<span style="color: #339933;">,</span> any character <span style="color: #009900;">&#40;</span>may <span style="color: #b1b100;">or</span> may <span style="color: #b1b100;">not</span> match line terminators<span style="color: #339933;">,</span> <span style="color: #000066;">read</span> on<span style="color: #009900;">&#41;</span>
<span style="color: #0000ff;">\d</span>  	A digit<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span>
<span style="color: #0000ff;">\D</span>  	A non<span style="color: #339933;">-</span>digit<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #339933;">^</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span>
<span style="color: #0000ff;">\s</span>  	A whitespace character<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span> <span style="color: #0000ff;">\t</span><span style="color: #0000ff;">\n</span><span style="color: #0000ff;">\x0B</span><span style="color: #0000ff;">\f</span><span style="color: #0000ff;">\r</span><span style="color: #009900;">&#93;</span>
<span style="color: #0000ff;">\S</span>  	A non<span style="color: #339933;">-</span>whitespace character<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #339933;">^</span><span style="color: #0000ff;">\s</span><span style="color: #009900;">&#93;</span>
<span style="color: #0000ff;">\w</span>  	A word character<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>a<span style="color: #339933;">-</span>zA<span style="color: #339933;">-</span>Z_0<span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span>
<span style="color: #0000ff;">\W</span>  	A non<span style="color: #339933;">-</span>word character<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #339933;">^</span><span style="color: #0000ff;">\w</span><span style="color: #009900;">&#93;</span></pre></div></div><p>However; notice that in Java, you will need to “double escape” these backslashes.</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span> pattern <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>d <span style="color: #000099; font-weight: bold;">\\</span>D <span style="color: #000099; font-weight: bold;">\\</span>W <span style="color: #000099; font-weight: bold;">\\</span>w <span style="color: #000099; font-weight: bold;">\\</span>S <span style="color: #000099; font-weight: bold;">\\</span>s&quot;</span><span style="color: #339933;">;</span></pre></div></div><p><a
name="quantifiers"></a></p><h4><a
href="#contents">1.2. Quantifiers</a></h4><p>Quantifiers can be used to specify the number or length that part of a pattern should match or repeat. A quantifier will bind to the expression group to its immediate left.</p><div
class="wp_syntax"><div
class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #339933;">*</span>      Match <span style="color: #cc66cc;">0</span> <span style="color: #b1b100;">or</span> more <span style="color: #000066;">times</span>
<span style="color: #339933;">+</span>      Match <span style="color: #cc66cc;">1</span> <span style="color: #b1b100;">or</span> more <span style="color: #000066;">times</span>
<span style="color: #339933;">?</span>      Match <span style="color: #cc66cc;">1</span> <span style="color: #b1b100;">or</span> <span style="color: #cc66cc;">0</span> <span style="color: #000066;">times</span>
<span style="color: #009900;">&#123;</span>n<span style="color: #009900;">&#125;</span>    Match exactly n <span style="color: #000066;">times</span>
<span style="color: #009900;">&#123;</span>n<span style="color: #339933;">,</span><span style="color: #009900;">&#125;</span>   Match at least n <span style="color: #000066;">times</span>
<span style="color: #009900;">&#123;</span>n<span style="color: #339933;">,</span><span style="color: #000066;">m</span><span style="color: #009900;">&#125;</span>  Match at least n but <span style="color: #b1b100;">not</span> more than <span style="color: #000066;">m</span> <span style="color: #000066;">times</span></pre></div></div><p><a
name="metachars"></a></p><h4><a
href="#contents">1.3. Meta-characters</a></h4><p>Meta-characters are used to group, divide, and perform special operations in patterns.</p><div
class="wp_syntax"><div
class="code"><pre class="perl" style="font-family:monospace;">\   	Quote the <span style="color: #b1b100;">next</span> meta<span style="color: #339933;">-</span>character
<span style="color: #339933;">^</span>   	Match the beginning of the line
<span style="color: #339933;">.</span>   	Match any character <span style="color: #009900;">&#40;</span>except newline<span style="color: #009900;">&#41;</span>
$   	Match the end of the line <span style="color: #009900;">&#40;</span><span style="color: #b1b100;">or</span> before newline at the end<span style="color: #009900;">&#41;</span>
<span style="color: #339933;">|</span>   	Alternation <span style="color: #009900;">&#40;</span>‘<span style="color: #b1b100;">or</span>’ statement<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>  	Grouping
<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>  	Custom character class</pre></div></div><p><a
name="examples"></a></p><h2><a
href="#contents">2. Examples</a></h2><p><a
name="basic"></a></p><h4><a
href="#contents">2.1. Basic Expressions</a></h4><p>Every string is a regular expression. For example, the string, <code>“I lost my wallet”</code>, is a regular expression that will match the text, <code>“I lost my wallet”</code>, and will ignore everything else.</p><p>What if we want to be able to find more things that we lost? We can replace <code>wallet</code> with an expression that will match any word.</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #0000ff;">&quot;I lost my <span style="color: #000099; font-weight: bold;">\\</span>w+&quot;</span></pre></div></div><p>As you can see, this pattern uses both a <b>character class</b> and a <b>quantifier</b>. <code>“\w”</code> says match a word character, and <code>“+”</code> says match one or more. So when combined, the pattern says “match one or more word characters.”</p><p>Now the pattern will match any word in place of “wallet”. E.g. <code>“I lost my sablefish”</code>, <code>“I lost my parrot”</code>, but it will not match <code>“I lost my: trooper”</code>, because as soon as the expression finds the <code>":"</code> character, which is not a word character, it will stop matching.</p><p>If we want the expression to be able to handle this situation, then we need to make a small change.</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #0000ff;">&quot;I lost my:? <span style="color: #000099; font-weight: bold;">\\</span>w+&quot;</span></pre></div></div><p>Now the expression will allow an optional <code>":"</code> directly after the word ‘my’.</p><p><a
name="grouping"></a></p><h4><a
href="#contents">2.2. Basic Grouping</a></h4><p>An important feature of regular expressions is the ability to group sections of a pattern, and provide alternate matches.</p><div
class="wp_syntax"><div
class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #339933;">|</span>   	Alternation <span style="color: #009900;">&#40;</span>‘<span style="color: #b1b100;">or</span>’ statement<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>  	Grouping</pre></div></div><p>These two meta-characters are core parts of flexible regular expressions. For instance, in the first example we lost our wallet. What if we knew exactly which types of objects we had lost, and we wanted to find those objects but nothing else?</p><p>We can use a group <code>()</code>, with an ‘or’ meta-character in order to specify a list of expressions to allow in our match.</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #0000ff;">&quot;I lost my:? (wallet|cell phone|car|marbles)&quot;</span></pre></div></div><p>The new expression will now match the beginning of the string <code>“I lost my”</code>, an optional <code>":"</code>, and then any one of the expressions in the group, separated by alternators, <code>"|"</code>; any one of the following: ‘wallet’, ‘cell phone’, ‘car’, or our ‘marbles’ would be a match.</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #0000ff;">&quot;I lost my wallet&quot;</span>		matches
<span style="color: #0000ff;">&quot;I lost my wallets&quot;</span>		matches		the ‘s’ is not needed, is ignored
<span style="color: #0000ff;">&quot;I lost my: car&quot;</span>		matches
<span style="color: #0000ff;">&quot;I lost my- car&quot;</span>		doesn’t match	‘<span style="color: #339933;">-</span>‘ is not allowed in our pattern
<span style="color: #0000ff;">&quot;I lost my: cell&quot;</span>		doesn’t match	all of ‘cell phone’ is needed
<span style="color: #0000ff;">&quot;I lost my: cell phone&quot;</span>		matches
<span style="color: #0000ff;">&quot;I lost my cell phone&quot;</span>		matches
<span style="color: #0000ff;">&quot;I lost my marbles&quot;</span>		matches</pre></div></div><p>As you can see, the combinations for matches quickly become very large. This is not the complete set, as there are several more phrases that would match our simple pattern.</p><p><a
name="matching"></a></p><h4><a
href="#contents">2.3. Matching/Validating</a></h4><p>Regular expressions make it possible to find all instances of text that match a certain pattern, and return a Boolean value if the pattern is found/not found. (This can be used to validate input such as phone numbers, social security numbers, email addresses, web form input data, scrub data, and much more. Eg. If the pattern is found in a String, and the pattern matches a SSN, then the string is an SSN)</p><h5>Sample code:</h5><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ValidateDemo <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> input <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		input.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;123-45-6789&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		input.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;9876-5-4321&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		input.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;987-65-4321 (attack)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		input.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;987-65-4321 &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		input.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;192-83-7465&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> ssn <span style="color: #339933;">:</span> input<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ssn.<span style="color: #006633;">matches</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;^(<span style="color: #000099; font-weight: bold;">\\</span>d{3}-?<span style="color: #000099; font-weight: bold;">\\</span>d{2}-?<span style="color: #000099; font-weight: bold;">\\</span>d{4})$&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Found good SSN: &quot;</span> <span style="color: #339933;">+</span> ssn<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p><strong>This produces the following output:</strong></p><blockquote><p><code>Found good SSN: 123-45-6789<br
/> Found good SSN: 192-83-7465</code></p></blockquote><h5>Dissecting the pattern:</h5><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #0000ff;">&quot;^(<span style="color: #000099; font-weight: bold;">\\</span>d{3}-?<span style="color: #000099; font-weight: bold;">\\</span>d{2}-?<span style="color: #000099; font-weight: bold;">\\</span>d{4})$&quot;</span></pre></div></div><div
class="wp_syntax"><div
class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #339933;">^</span>		match the beginning of the line
<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 		group everything within the parenthesis as group <span style="color: #cc66cc;">1</span>
<span style="color: #0000ff;">\d</span><span style="color: #009900;">&#123;</span>n<span style="color: #009900;">&#125;</span>		match n digits<span style="color: #339933;">,</span> where n is a number equal to <span style="color: #b1b100;">or</span> greater than zero
<span style="color: #339933;">-?</span>		optionally match a dash
$		match the end of the line</pre></div></div><p><a
name="capture"></a></p><h4><a
href="#contents">2.4. Extracting/Capturing</a></h4><p>Specific values can be selected out of a large complex body of text. These values can be used in the application.</p><h5>Sample code:</h5><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.regex.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ExtractDemo <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">String</span> input <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I have a cat, but I like my dog better.&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		Pattern p <span style="color: #339933;">=</span> Pattern.<span style="color: #006633;">compile</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;(mouse|cat|dog|wolf|bear|human)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Matcher m <span style="color: #339933;">=</span> p.<span style="color: #006633;">matcher</span><span style="color: #009900;">&#40;</span>input<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> animals <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>m.<span style="color: #006633;">find</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Found a &quot;</span> <span style="color: #339933;">+</span> m.<span style="color: #006633;">group</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			animals.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>m.<span style="color: #006633;">group</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p><strong>This produces the following output:</strong></p><blockquote><p><code>Found a cat.<br
/> Found a dog.</code></p></blockquote><h5>Dissecting the pattern:</h5><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #0000ff;">&quot;(mouse|cat|dog|wolf|bear|human)&quot;</span></pre></div></div><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>		group everything within the parenthesis as group <span style="color: #cc66cc;">1</span>
mouse		match the text ‘mouse’
<span style="color: #339933;">|</span>		alternation<span style="color: #339933;">:</span> match any one of the sections of <span style="color: #000000; font-weight: bold;">this</span> group
cat		match the text ‘cat’
&nbsp;
<span style="color: #666666; font-style: italic;">//...and so on</span></pre></div></div><p><a
name="modify"></a></p><h4><a
href="#contents">2.5. Modifying/Substitution</a></h4><p>Values in text can be replaced with new values, for example, you could replace all instances of the word <code>‘clientId=’</code>, followed by<em> a number</em>, with a mask to hide the original text. (See below)</p><p>For sanitizing log files, URI strings and parameters, and form data, this can be a useful method of filtering sensitive information. A simple, reusable utility class can be used to encapsulate this into a more streamlined method.</p><h5>Sample code:</h5><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.regex.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ReplaceDemo <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">String</span> input <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;User clientId=23421. Some more text clientId=33432. This clientNum=100&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		Pattern p <span style="color: #339933;">=</span> Pattern.<span style="color: #006633;">compile</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;(clientId=)(<span style="color: #000099; font-weight: bold;">\\</span>d+)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Matcher m <span style="color: #339933;">=</span> p.<span style="color: #006633;">matcher</span><span style="color: #009900;">&#40;</span>input<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #003399;">StringBuffer</span> result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringBuffer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>m.<span style="color: #006633;">find</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Masking: &quot;</span> <span style="color: #339933;">+</span> m.<span style="color: #006633;">group</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			m.<span style="color: #006633;">appendReplacement</span><span style="color: #009900;">&#40;</span>result, m.<span style="color: #006633;">group</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;***masked***&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		m.<span style="color: #006633;">appendTail</span><span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p><strong>This produces the following output:</strong></p><blockquote><p> <code>Masking: 23421<br
/> Masking: 33432<br
/> User clientId=***masked***. Some more text clientId=***masked***. This clientNum=100.</code></p></blockquote><h5>Dissecting the pattern:</h5><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #0000ff;">&quot;(clientId=)(<span style="color: #000099; font-weight: bold;">\\</span>d+)&quot;</span></pre></div></div><div
class="wp_syntax"><div
class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #009900;">&#40;</span>clientId<span style="color: #339933;">=</span><span style="color: #009900;">&#41;</span> 	group everything within the parenthesis as group <span style="color: #cc66cc;">1</span>
clientId<span style="color: #339933;">=</span>	match the text ‘clientId<span style="color: #339933;">=</span>’
<span style="color: #009900;">&#40;</span>\<span style="color: #0000ff;">\d</span><span style="color: #339933;">+</span><span style="color: #009900;">&#41;</span>		group everything within the parenthesis as group <span style="color: #cc66cc;">2</span>
\<span style="color: #0000ff;">\d</span><span style="color: #339933;">+</span>		match one <span style="color: #b1b100;">or</span> more digits</pre></div></div><p><font
color="red">**</font> Notice how groups begin numbering at 1, and increment by one for each new group. However; groups may contain groups, in which case the outer group begins at one, group two will be the next inner group. When referencing group 0, you will be given the entire chunk of text that matched the regex.</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #009900;">&#40;</span>  <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span>  <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>		<span style="color: #666666; font-style: italic;">//and so on</span>
 <span style="color: #cc66cc;">1</span>  <span style="color: #cc66cc;">2</span>   <span style="color: #cc66cc;">3</span>  <span style="color: #cc66cc;">4</span>   <span style="color: #cc66cc;">5</span>		<span style="color: #666666; font-style: italic;">//0 = everything the pattern matched</span></pre></div></div><p><a
name="conclude"></a></p><h2><a
href="#contents">3. Conclusion &#038; Next Steps</a></h2><p>Wrapping up, regular expressions are not difficult to master &#8211; in fact, they are quite easy. My strategy, whenever building a new regular expression, is to start with the simplest, most general match possible. From there, I continuously add more and more complexity until I have matched, substituted, or inserted exactly what I need.</p><p>Don&#8217;t be afraid to &#8220;express&#8221; yourself! When you&#8217;ve got the hang of these techniques, or need something a little fancier, read <a
href="http://ocpsoft.com/opensource/guide-to-regular-expressions-in-java-part-2/">Guide to Regular Expressions in Java (Part 2)</a>.</p> ]]></content:encoded> <wfw:commentRss>http://ocpsoft.com/opensource/guide-to-regular-expressions-in-java-part-1/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Guide to Regular Expressions in Java (Part 2)</title><link>http://ocpsoft.com/opensource/guide-to-regular-expressions-in-java-part-2/</link> <comments>http://ocpsoft.com/opensource/guide-to-regular-expressions-in-java-part-2/#comments</comments> <pubDate>Sun, 10 Jan 2010 01:11:31 +0000</pubDate> <dc:creator>Lincoln</dc:creator> <category><![CDATA[OpenSource]]></category><guid
isPermaLink="false">http://ocpsoft.com/?p=915</guid> <description><![CDATA[Often unknown, or heralded as confusing, regular expressions have defined the standard for powerful text manipulation and search. Without them, many of the applications we know today would not function. This two-part series explores the basics of regular expressions in Java, and provides tutorial examples in the hopes of spreading love for our pattern-matching friends. [...]]]></description> <content:encoded><![CDATA[<p>Often unknown, or heralded as confusing, regular expressions have defined the standard for powerful text manipulation and search. Without them, many of the applications we know today would not function. This two-part series explores the basics of regular expressions in Java, and provides tutorial examples in the hopes of spreading love for our pattern-matching friends. (Read <a
href="http://ocpsoft.com/opensource/guide-to-regular-expressions-in-java-part-1/">part one</a>.)<br
/> <span
id="more-915"></span></p><h2>Part 2: Look-ahead &amp; Configuration flags</h2><p>Have you ever wanted to find something in a string, but you only wanted to find it when it came before another pattern in that string? Or maybe you wanted to find a piece of text that was not followed by another piece of text?</p><p>Normally, with standard string searching, you would have to write a somewhat complex function to perform the exact operation you wanted. This can, however, all be done on one line using regular expressions.</p><p>This chapter will also cover the configuration flags available to modify some of the behaviors of the regular expression patterns language.</p><p><a
name="contents"></a></p><h2>Table of Contents:</h2><div
class="featured"><div
class="inside"><ol><li><h4><a
href="#lookaround">Look-ahead &amp; Look-behind</a></h4></li><li><h4><a
href="#config">Configuring the Matching Engine</a></h4></li><li><h4><a
href="#conclude">Conclusion</a></h4></li></ol><div
class="clearer"></div></div><div
class="inside" style="margin-top: 15px;"> This article is part two in the series: &#8220;Guide to Regular Expressions in Java.&#8221; Read <a
href="http://ocpsoft.com/opensource/guide-to-regular-expressions-in-java-part-1/">part one</a>.</div></div><p><a
name="lookaround"></a><br
/><h2><a
href="#contents">1. Look-ahead &amp; Look-behind</a></h2><p>Look-ahead and look-behind operations use syntax that could be confused with grouping (See <a
href="http://ocpsoft.com/opensource/guide-to-regular-expressions-in-java-part-1/#grouping">Ch. 1 &#8211; Basic Grouping</a>,) but these patterns do not capture values; therefore, using these constructs, no values will be stored for later retrieval, and they do not affect group numbering. Look-ahead operations look forward, starting from their location in the pattern, continuing to the end of the input. Look-behind expressions do not search backwards, but instead start at the beginning of the pattern and continue up to/until the look-behind expression.</p><p>E.g.: The statement <code>“my dog is (?!(green|red))\w+”</code> asserts that <code>‘green’</code> will not the word to the look-ahead’s direct right. In other words: My dog is not green or red, but my dog is blue.</p><h4>Look-ahead/behind constructs <span
class="faded">(non-capturing)</span></h4><div
class="wp_syntax"><div
class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #339933;">?:</span>X<span style="color: #009900;">&#41;</span> 			X<span style="color: #339933;">,</span> as a non<span style="color: #339933;">-</span>capturing group
<span style="color: #009900;">&#40;</span><span style="color: #339933;">?=</span>X<span style="color: #009900;">&#41;</span> 			X<span style="color: #339933;">,</span> via zero<span style="color: #339933;">-</span>width positive look<span style="color: #339933;">-</span>ahead
<span style="color: #009900;">&#40;</span><span style="color: #339933;">?!</span>X<span style="color: #009900;">&#41;</span> 			X<span style="color: #339933;">,</span> via zero<span style="color: #339933;">-</span>width negative look<span style="color: #339933;">-</span>ahead
<span style="color: #009900;">&#40;</span><span style="color: #339933;">?&lt;=</span>X<span style="color: #009900;">&#41;</span> 			X<span style="color: #339933;">,</span> via zero<span style="color: #339933;">-</span>width positive look<span style="color: #339933;">-</span>behind
<span style="color: #009900;">&#40;</span><span style="color: #339933;">?&lt;!</span>X<span style="color: #009900;">&#41;</span> 			X<span style="color: #339933;">,</span> via zero<span style="color: #339933;">-</span>width negative look<span style="color: #339933;">-</span>behind
<span style="color: #009900;">&#40;</span><span style="color: #339933;">?&lt;</span>X<span style="color: #009900;">&#41;</span> 			X<span style="color: #339933;">,</span> as an independent<span style="color: #339933;">,</span> non<span style="color: #339933;">-</span>capturing group</pre></div></div><p>So what does this all mean? What does a look-ahead really do for me?</p><p>Say, for example, we wanted to know if our input string contains the word <code>“incident”</code> but that the word <code>“theft”</code> should not be found anywhere. We can use a negative look-ahead to ensure that there are no occurrences.</p><blockquote><p><code>“.*(?!.*theft)incident.*”</code></p></blockquote><p>This expression exhibits the following behavior:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #0000ff;">&quot;There was a crime incident&quot;</span>			matches
<span style="color: #0000ff;">&quot;The incident involved a theft&quot;</span>			does not match
<span style="color: #0000ff;">&quot;The theft was a serious incident&quot;</span>		does not match</pre></div></div><p>A more complex example is password validation. Let’s say we want to ensure that a password is made at least 8, but at most 12 alphanumeric characters, <em>and at least two numbers, in any position.</em></p><p>We will need to use a look-ahead expression in order to enforce a requirement of two numbers. This look-ahead expression will require any number of characters to be followed by a single digit, followed by any number of characters, and another single digit. E.g.: <code>…4…2</code>, or <code>…42</code>, or <code>42…</code>, or <code>4…2</code>.</p><h3>Example code:</h3><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LookaheadDemo <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> input <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		input.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		input.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;p4ssword&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		input.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;p4ssw0rd&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		input.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;p45sword&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> ssn <span style="color: #339933;">:</span> input<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ssn.<span style="color: #006633;">matches</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;^(?=.*[0-9].*[0-9])[0-9a-zA-Z]{8,12}$&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>ssn <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;: matches&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>ssn <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;: does not match&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><h4>This produces the following output:</h4><blockquote><p><code>password: does not match<br
/> p4ssword: does not match<br
/> p4ssw0rd: matches<br
/> p45sword: matches</code></p></blockquote><h4>Dissecting the pattern:</h4><blockquote><p><code>"^(?=.*[0-9].*[0-9])[0-9a-zA-Z]{8,12}$"</code></p></blockquote><div
class="wp_syntax"><div
class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #339933;">^</span>				match the beginning of the line
<span style="color: #009900;">&#40;</span><span style="color: #339933;">?=.*</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.*</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> 		a look<span style="color: #339933;">-</span>ahead expression<span style="color: #339933;">,</span> requires <span style="color: #cc66cc;">2</span> digits to be present
<span style="color: #339933;">.*</span>				match n characters<span style="color: #339933;">,</span> where n <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">0</span>
<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span>				match a digit from <span style="color: #cc66cc;">0</span> to <span style="color: #cc66cc;">9</span>
<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span>9a<span style="color: #339933;">-</span>zA<span style="color: #339933;">-</span>Z<span style="color: #009900;">&#93;</span>			match any numbers <span style="color: #b1b100;">or</span> letters
<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">8</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">12</span><span style="color: #009900;">&#125;</span>				match <span style="color: #cc66cc;">8</span> to <span style="color: #cc66cc;">12</span> of whatever is specified by the <span style="color: #b1b100;">last</span> group
$				match the end of the line</pre></div></div><p><font
color="red">Note**</font>: Multiple look-ahead operations do not evaluate in a specific order. They must all be satisfied equally, and if they logically contradict each other, the pattern will never match.</p><p><a
name="config"></a><br
/><h2><a
href="#contents">2. Configuring the Matching Engine</a></h2><p>Pattern configuration flags for Java appear very similar to look-ahead operations. Flags are used to configure case sensitivity, multi-line matching, and more. Flags can be specified in collections, or as individual statements. Again, these expressions are not literal, and do not capture values.</p><h5>Configuration flags</h5><div
class="wp_syntax"><div
class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span>idmsux<span style="color: #339933;">-</span>idmsux<span style="color: #009900;">&#41;</span>  	Turns match flags on <span style="color: #339933;">-</span> off <span style="color: #b1b100;">for</span> entire expression
<span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span>idmsux<span style="color: #339933;">-</span>idmsux<span style="color: #339933;">:</span>X<span style="color: #009900;">&#41;</span>   	X<span style="color: #339933;">,</span> as a non<span style="color: #339933;">-</span>capturing group with the given flags on – off</pre></div></div><h5>Case insensitivity mode</h5><div
class="wp_syntax"><div
class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span>i<span style="color: #009900;">&#41;</span>  			Toggle <span style="color: #b1b100;">case</span> insensitivity <span style="color: #009900;">&#40;</span>default<span style="color: #339933;">:</span> off<span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">?-</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">for</span> the text in this group only</pre></div></div><h5>UNIX lines mode</h5><div
class="wp_syntax"><div
class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span>d<span style="color: #009900;">&#41;</span>  			Enables UNIX line mode <span style="color: #009900;">&#40;</span>default<span style="color: #339933;">:</span> off<span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">?-</span>d<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> 
			In this mode<span style="color: #339933;">,</span> only the <span style="color: #ff0000;">'\n'</span> line terminator is recognized in the behavior of <span style="color: #339933;">.,</span> <span style="color: #339933;">^,</span> <span style="color: #b1b100;">and</span> $</pre></div></div><h5>Multi-line mode</h5><div
class="wp_syntax"><div
class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span><span style="color: #000066;">m</span><span style="color: #009900;">&#41;</span>  			Toggle treat newlines as whitespace <span style="color: #009900;">&#40;</span>default<span style="color: #339933;">:</span> off<span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">?-</span><span style="color: #000066;">m</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			The <span style="color: #339933;">^</span> <span style="color: #b1b100;">and</span> $ expressions will <span style="color: #000066;">no</span> longer match to the beginning <span style="color: #b1b100;">and</span> end of a line<span style="color: #339933;">,</span>
			respectively<span style="color: #339933;">,</span> but will match the beginning <span style="color: #b1b100;">and</span> end of the entire input sequence<span style="color: #339933;">/</span>string<span style="color: #339933;">.</span></pre></div></div><h5>Dot-all mode</h5><div
class="wp_syntax"><div
class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span><span style="color: #000066;">s</span><span style="color: #009900;">&#41;</span>  			Toggle dot ‘<span style="color: #339933;">.</span>’ matches any character <span style="color: #009900;">&#40;</span>default<span style="color: #339933;">:</span> off<span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">?-</span>d<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			Normally<span style="color: #339933;">,</span> the dot character will match everything except newline characters<span style="color: #339933;">.</span></pre></div></div><h5>Unicode-case mode</h5><div
class="wp_syntax"><div
class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span>u<span style="color: #009900;">&#41;</span>  			Toggle Unicode standard <span style="color: #b1b100;">case</span> matching <span style="color: #009900;">&#40;</span>default<span style="color: #339933;">:</span> off<span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">?-</span>u<span style="color: #009900;">&#41;</span>
			By default<span style="color: #339933;">,</span> case<span style="color: #339933;">-</span>insensitive matching assumes that only characters 
			in the US<span style="color: #339933;">-</span>ASCII charset are being matched<span style="color: #339933;">.</span></pre></div></div><h5>Comments mode</h5><div
class="wp_syntax"><div
class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span>x<span style="color: #009900;">&#41;</span>  			Allow comments in pattern <span style="color: #009900;">&#40;</span>default<span style="color: #339933;">:</span> off<span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">?-</span>x<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			In this mode<span style="color: #339933;">,</span> whitespace is ignored<span style="color: #339933;">,</span> <span style="color: #b1b100;">and</span> embedded comments starting with <span style="color: #ff0000;">'#'</span>
			are ignored <span style="color: #b1b100;">until</span> the end of a line<span style="color: #339933;">.</span></pre></div></div><h5>Global toggle</h5><p>In order to toggle flags for the entire expression, the statement must be at the head of the expression.</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #0000ff;">&quot;(?idx)^I<span style="color: #000099; font-weight: bold;">\s</span> lost<span style="color: #000099; font-weight: bold;">\s</span> my<span style="color: #000099; font-weight: bold;">\s</span> .+     #this comment and all spaces will be ignored&quot;</span></pre></div></div><p>The above expression will ignore case, and will set the dot ‘.’ character to include newlines.</p><h3>Example code:</h3><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ConfigurationDemo <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">String</span> input <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;My dog is Blue.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933;">+</span>
				<span style="color: #0000ff;">&quot;He is not red or green.&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #003399;">Boolean</span> controlResult <span style="color: #339933;">=</span> input.<span style="color: #006633;">matches</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;(?=.*Green.*).*Blue.*&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">Boolean</span> caseInsensitiveResult <span style="color: #339933;">=</span> input.<span style="color: #006633;">matches</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;(?i)(?=.*Green.*).*Blue.*&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">Boolean</span> dotallResult <span style="color: #339933;">=</span> input.<span style="color: #006633;">matches</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;(?s)(?=.*Green.*).*Blue.*&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">Boolean</span> configuredResult <span style="color: #339933;">=</span> input.<span style="color: #006633;">matches</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;(?si)(?=.*Green.*).*Blue.*&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Control result was: &quot;</span> <span style="color: #339933;">+</span> controlResult<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Case ins. result was: &quot;</span> <span style="color: #339933;">+</span> caseInsensitiveResult<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Dot-all result was: &quot;</span> <span style="color: #339933;">+</span> dotallResult<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Configured result was: &quot;</span> <span style="color: #339933;">+</span> configuredResult<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><h4>This produces the following output:</h4><blockquote><p><code>Control result was: false<br
/> Case insensitive result was: false<br
/> Dot-all result was: false<br
/> Configured result was: true</code></p></blockquote><h4>Dissecting the pattern:</h4><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #0000ff;">&quot;(?si)(?=.*Green.*).*Blue.*&quot;</span></pre></div></div><div
class="wp_syntax"><div
class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span>si<span style="color: #009900;">&#41;</span>			turn on <span style="color: #b1b100;">case</span> insensitivity <span style="color: #b1b100;">and</span> dotall modes
<span style="color: #009900;">&#40;</span><span style="color: #339933;">?=.</span><span style="color: #0000ff;">*Green</span><span style="color: #339933;">.*</span><span style="color: #009900;">&#41;</span> 		‘Green’ must be found somewhere to the right of this look<span style="color: #339933;">-</span>ahead
<span style="color: #339933;">.</span><span style="color: #0000ff;">*Blue</span><span style="color: #339933;">.*</span>		‘Blue’ must be found somewhere in the input</pre></div></div><p>We had to enable multi-line and case-insensitive modes for our pattern to match.</p><p>The look-ahead in this example is very similar to the pattern itself, and in this case, the pattern could be substituted for another look-ahead. Because we don’t care in which order we find these two items, the way this is written, substituting “<em>(?=.*Blue.*)</em>” for “<em>.*Blue.*</em>” would be an acceptable change; however, if we did care in which order we wanted to find these colors, we would need to be more precise with our ordering. If we wanted to ensure that the ‘Green’ came after ‘Blue’ we would need to move the look-ahead as seen below, and so on.</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #0000ff;">&quot;(?si).*Blue.*(?=.*Green.*)&quot;</span></pre></div></div><p><a
name="conclude"></a><br
/><h2><a
href="#contents">3. Conclusion</a></h2><p>Regular expressions provide an extremely flexible and powerful text processing system. Try to imagine doing this work using <strong>String.substring(&#8230;)</strong> or <strong>String.indexOf(&#8230;)</strong>, with loops, nested loops, and dozens of <code>if</code> statements. I don&#8217;t even want to try&#8230; so play around! Think about using regular expressions next time you find yourself doing text or pattern manipulation with looping and other painful methods. Let us know how you do.</p><p>Read the previous article in this series: <a
href="http://ocpsoft.com/opensource/guide-to-regular-expressions-in-java-part-1/">Guide to Regular Expressions in Java (Part 1)</a></p> ]]></content:encoded> <wfw:commentRss>http://ocpsoft.com/opensource/guide-to-regular-expressions-in-java-part-2/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> </channel> </rss>
<!-- Served from: ocpsoft.com @ 2010-07-29 17:12:42 by W3 Total Cache -->