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

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