Child pages
  • Development-Custom Templates
Skip to end of metadata
Go to start of metadata

Petite Abeille

The following solution is more involved than what is strictly needed. It may avoid the debug message, he is using some quasi-private stuff.

It can be quite easy

protected WOElement myTemplate = null;
public WOElement template() {
  if ( myTemplate == null ) {
      myTemplate = WOComponent.templateWithHTMLString( html, wod, null );
  }
  return myTemplate;
}

A More Involved Solution:

Here is a little example on how to overwrite WOComponent.template() so
you can load the html template and wod from wherever you want )eg a jar
file):

  • First you will need to overwrite WOComponent.template()
public WOElement template() {
  return Component.templateForComponent( this );
}
  • Now, templateForComponent could look like this:
public static WOElement templateForComponent(WOComponent aComponent) {
if ( aComponent != null ){
  String aName = aComponent.name();
  WOElement anElement = (WOElement) _elementCache.get( aName );
  if ( anElement == null ) {
    String anHTMLString = Component.componentTemplateWithExtension( aComponent, ".html" );
    String aDescriptionString = Component.componentTemplateWithExtension( aComponent, ".wod" );
    anElement = WOComponent.templateWithHTMLString ( anHTMLString, aDescriptionString, null );
    if ( aComponent.isCachingEnabled() == true ) {
      _elementCache.put( aName, anElement );
    }
  }
  return anElement;
}
throw new IllegalArgumentException ( "Component.templateForComponent: null component." );
}
  • The componentTemplateWithExtension method could look like the following:
private static String componentTemplateWithExtension(WOComponent aComponent, String anExtension) {
if ( anExtension != null ) {
  String aResource = aComponent.name() + anExtension;
  InputStream anInputStream = Component.componentStreamForResource( aComponent, aResource );
  if ( anInputStream != null ) {
    StringBuffer aBuffer = new StringBuffer();
    try {
      BufferedInputStream  aBufferedStream = new BufferedInputStream( anInputStream );
      InputStreamReader  aStreamReader = new InputStreamReader( aBufferedStream );
      int aChar = -1;
      while ( ( aChar = aStreamReader.read() ) != -1 ) {
        aBuffer.append( (char) aChar );
      }
        anInputStream.close();
      } catch(Exception anException) {
        Log.warning( anException );
      }
      return aBuffer.toString();
    }
    throw new RuntimeException ( "Component.componentTemplateWithExtension: resource not found: '" +  aResource + "'." );
  }
}
throw new IllegalArgumentException ( "Component.componentTemplateWithExtension: null extension." );
}
  • And finally componentStreamForResource could be something along these:
private static InputStream componentStreamForResource(WOComponent aComponent, String aResource) {
if ( aResource != null ) {
  File aDirectory = new File( System.getProperty( "user.dir" ) );
  File aFile = new File( aDirectory, aResource );
  if ( aFile.exists() == true ) {
    try {
      return new FileInputStream( aFile );
    } catch(Exception anException) {
      Log.warning( anException);
    }
  }
  return aComponent.getClass().getResourceAsStream( aResource );
}
throw new IllegalArgumentException( "Component.componentStreamForResource: null resource." );
}
  • No labels