TransPythia - How-to...
This section takes a deeper look at the source code of TransPythia and explains how to extend the library to add more transcoding actions.
The source code of the library is available at:
http://dev.w3.org/2009/mobileok-authoring/common/transcoding/
The PHP documentation created from the source code is embedded in the PHP documentation of the mobileOK content authoring library, available at:
http://dev.w3.org/2009/mobileok-authoring/doc/phpdoc/
Transcoding classes
TransPythia is merely a set of TranscodingAction
that can be grouped together for convenience in a Transcoder
instance. TranscodingAction
is an abstract class that features a generic pair/value options mechanism that may be used in concrete implementations to fine-tune the behavior of the transcoding action, as well as a few helper methods to access the properties of the requesting device. The apply
method performs the actual adaptation and must be implemented in concrete subclasses.
Here is the class diagram for the main classes of TransPythia:
![](./images/transcoding.png)
How to extend the library
Extending TransPythia is as easy as providing additional extensions of the TranscodingAction
class. Creating a working TranscodingAction
class is simple: apply
is the only method to implement. This method takes two arguments: the content to be adapted and the evidence to use to identify the requesting device. It must return the adapted content.
New transcoding actions must follow the naming convention of the existing ones: the class must be named TranscodingAction[action name]
.
The following code defines the identity transformation:
class TranscodingActionIdentity extends TranscodingAction {
public function apply($content, $evidence){
return $content;
}
}
It may be instantiated through a Transcoder
with:
$action = $transcoder->newTranscodingAction('Identity');
The TranscodingAction
class provides helper methods to get/set options and retrieve device properties. They should be used to speed up development of new transcoding actions.
The following example replaces all occurrences of the string "cookie" with "donut" when the device does not support cookies:
class TranscodingActionCookie2Donut extends TranscodingAction {
public function apply($content, $evidence) {
// Initialize the properties of the requesting device
$this->initPropertyValues($evidence);
// The transcoding action uses the cookieSupport property
// defined in the DDR Core Vocabulary by default.
// This setting may be overridden through a call to:
// setOption('cookie_support', value).
// In any case, we need to make sure this option is defined.
$this->initProperty(
'cookie_support',
'cookieSupport',
'http://www.w3.org/2008/01/ddr-core-vocabulary',
'webBrowser');
// Property to use to detect whether the device supports cookies?
$property = $this->getOption('cookie_support');
// Retrieve the property value for the requesting device
$supportCookies = $this->getPropertyValuePr($property);
if (!isset($supportCookies) || !$supportCookies->getBoolean()) {
return str_replace('cookie', 'donut', $content);
}
return $content;
}
}