3. Casting values

What will happen if we try to assign another value type to a parameter than the parameter requires? Let’s try it out by loading the username with a date value. Modify the bold lines in the process definition and upload the file.

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

   <step id="compose" processor="hello">
      <load parameter="username">
         <date>1104354657683</date> 1
      </load>
      <load parameter="style">
         <string>color:red;font:italic;</string>	
      </load>
   </step>

   ...
</process>
1

Date values are created with the desired time in milliseconds since 1970

When we reload the page we will see that no error arises as you might have expected but the date is shown in a human readable form:

Figure 7.2. Say hello to the current date

Say hello to the current date

As we have not defined a required content type in the last current of our template, Projector tries to cast any given value into a printable form as the template required a printable value. Projector will always do its best to fulfill the requirements of a processor. If you for example feed a processor that requires a number value with a string value it will try to parse the string and create a matching number value. This is very nice because it will enable you to invoke many processors by encoding parameters into the calling URL as we have seen it in our very first examples.

If we would restrict variable in the template to the content type text/plain this would not work any more as Projector would assure that only values that represent plain text would be accepted. Modify the template so that the content type is restricted:

<html>
   <body>
      <?username <h1 <?style style="<%style%>"?>>Hello <%username;required;text/plain%>!</h1>?>
   </body>
</html>

After upload this changes and reloading the page you’ll see that Projector throws a detailed exception that will tell us that it’s illegal to load a date value to the parameter username. In summary it can be said that Projector will always try to cast the values so that they will fit the needs of a processor but it will not do something illegal but will throw an exception instead that will give you detailed information if processing was aborted.