Wiki source code of Development-Custom Templates

Last modified by Pascal Robert on 2010/09/13 00:27

Hide last authors
Pascal Robert 4.1 1 == Petite Abeille ==
smmccraw 1.1 2
Quinton Dolan 3.1 3 //The following solution is more involved than what is strictly needed. It may avoid the debug message, he is using some quasi-private stuff.//
smmccraw 1.1 4
Pascal Robert 4.1 5 === It can be quite easy ===
smmccraw 1.1 6
Quinton Dolan 3.1 7 {{code}}
smmccraw 1.1 8
Quinton Dolan 3.1 9 protected WOElement myTemplate = null;
10 public WOElement template() {
11 if ( myTemplate == null ) {
Pascal Robert 4.1 12 myTemplate = WOComponent.templateWithHTMLString( html, wod, null );
smmccraw 1.1 13 }
Quinton Dolan 3.1 14 return myTemplate;
15 }
smmccraw 1.1 16
Quinton Dolan 3.1 17 {{/code}}
smmccraw 1.1 18
19 A More Involved Solution:
20
Pascal Robert 4.1 21 Here is a little example on how to overwrite WOComponent.template() so
Pascal Robert 6.1 22 you can load the html template and wod from wherever you want )eg a jar
23 file):
smmccraw 1.1 24
25 * First you will need to overwrite WOComponent.template()
26
Quinton Dolan 3.1 27 {{code}}
smmccraw 1.1 28
Quinton Dolan 3.1 29 public WOElement template() {
30 return Component.templateForComponent( this );
31 }
smmccraw 1.1 32
Quinton Dolan 3.1 33 {{/code}}
smmccraw 1.1 34
35 * Now, templateForComponent could look like this:
36
Quinton Dolan 3.1 37 {{code}}
smmccraw 1.1 38
Quinton Dolan 3.1 39 public static WOElement templateForComponent(WOComponent aComponent) {
40 if ( aComponent != null ){
41 String aName = aComponent.name();
42 WOElement anElement = (WOElement) _elementCache.get( aName );
43 if ( anElement == null ) {
44 String anHTMLString = Component.componentTemplateWithExtension( aComponent, ".html" );
45 String aDescriptionString = Component.componentTemplateWithExtension( aComponent, ".wod" );
46 anElement = WOComponent.templateWithHTMLString ( anHTMLString, aDescriptionString, null );
47 if ( aComponent.isCachingEnabled() == true ) {
48 _elementCache.put( aName, anElement );
smmccraw 1.1 49 }
50 }
Quinton Dolan 3.1 51 return anElement;
52 }
53 throw new IllegalArgumentException ( "Component.templateForComponent: null component." );
54 }
smmccraw 1.1 55
Quinton Dolan 3.1 56 {{/code}}
smmccraw 1.1 57
Pascal Robert 6.1 58 * The componentTemplateWithExtension method could look like the following:
smmccraw 1.1 59
Quinton Dolan 3.1 60 {{code}}
smmccraw 1.1 61
Quinton Dolan 3.1 62 private static String componentTemplateWithExtension(WOComponent aComponent, String anExtension) {
63 if ( anExtension != null ) {
64 String aResource = aComponent.name() + anExtension;
65 InputStream anInputStream = Component.componentStreamForResource( aComponent, aResource );
66 if ( anInputStream != null ) {
67 StringBuffer aBuffer = new StringBuffer();
68 try {
69 BufferedInputStream aBufferedStream = new BufferedInputStream( anInputStream );
70 InputStreamReader aStreamReader = new InputStreamReader( aBufferedStream );
71 int aChar = -1;
72 while ( ( aChar = aStreamReader.read() ) != -1 ) {
73 aBuffer.append( (char) aChar );
74 }
75 anInputStream.close();
76 } catch(Exception anException) {
77 Log.warning( anException );
78 }
79 return aBuffer.toString();
80 }
81 throw new RuntimeException ( "Component.componentTemplateWithExtension: resource not found: '" + aResource + "'." );
82 }
83 }
84 throw new IllegalArgumentException ( "Component.componentTemplateWithExtension: null extension." );
85 }
smmccraw 1.1 86
Quinton Dolan 3.1 87 {{/code}}
smmccraw 1.1 88
Quinton Dolan 3.1 89 * And finally componentStreamForResource could be something along these:
smmccraw 1.1 90
Quinton Dolan 3.1 91 {{code}}
smmccraw 1.1 92
Quinton Dolan 3.1 93 private static InputStream componentStreamForResource(WOComponent aComponent, String aResource) {
94 if ( aResource != null ) {
95 File aDirectory = new File( System.getProperty( "user.dir" ) );
Pascal Robert 4.1 96 File aFile = new File( aDirectory, aResource );
Quinton Dolan 3.1 97 if ( aFile.exists() == true ) {
98 try {
99 return new FileInputStream( aFile );
100 } catch(Exception anException) {
101 Log.warning( anException);
smmccraw 1.1 102 }
103 }
Quinton Dolan 3.1 104 return aComponent.getClass().getResourceAsStream( aResource );
105 }
106 throw new IllegalArgumentException( "Component.componentStreamForResource: null resource." );
107 }
smmccraw 1.1 108
Quinton Dolan 3.1 109 {{/code}}