Programming__WebObjects-Web Applications-Development-Custom Templates

Version 1.1 by smmccraw on 2007/07/08 09:45

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) {
  {
    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."_);
}
}

  • AndfinallycomponentStreamForResourcecouldbesomethingalongthese
    lines:

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." );
  }

Category:WebObjects