Wiki source code of Development-Custom Templates
Last modified by Pascal Robert on 2010/09/13 00:27
Hide last authors
author | version | line-number | content |
---|---|---|---|
![]() |
4.1 | 1 | == Petite Abeille == |
![]() |
1.1 | 2 | |
![]() |
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.// |
![]() |
1.1 | 4 | |
![]() |
4.1 | 5 | === It can be quite easy === |
![]() |
1.1 | 6 | |
![]() |
3.1 | 7 | {{code}} |
![]() |
1.1 | 8 | |
![]() |
3.1 | 9 | protected WOElement myTemplate = null; |
10 | public WOElement template() { | ||
11 | if ( myTemplate == null ) { | ||
![]() |
4.1 | 12 | myTemplate = WOComponent.templateWithHTMLString( html, wod, null ); |
![]() |
1.1 | 13 | } |
![]() |
3.1 | 14 | return myTemplate; |
15 | } | ||
![]() |
1.1 | 16 | |
![]() |
3.1 | 17 | {{/code}} |
![]() |
1.1 | 18 | |
19 | A More Involved Solution: | ||
20 | |||
![]() |
4.1 | 21 | Here is a little example on how to overwrite WOComponent.template() so |
![]() |
6.1 | 22 | you can load the html template and wod from wherever you want )eg a jar |
23 | file): | ||
![]() |
1.1 | 24 | |
25 | * First you will need to overwrite WOComponent.template() | ||
26 | |||
![]() |
3.1 | 27 | {{code}} |
![]() |
1.1 | 28 | |
![]() |
3.1 | 29 | public WOElement template() { |
30 | return Component.templateForComponent( this ); | ||
31 | } | ||
![]() |
1.1 | 32 | |
![]() |
3.1 | 33 | {{/code}} |
![]() |
1.1 | 34 | |
35 | * Now, templateForComponent could look like this: | ||
36 | |||
![]() |
3.1 | 37 | {{code}} |
![]() |
1.1 | 38 | |
![]() |
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 ); | ||
![]() |
1.1 | 49 | } |
50 | } | ||
![]() |
3.1 | 51 | return anElement; |
52 | } | ||
53 | throw new IllegalArgumentException ( "Component.templateForComponent: null component." ); | ||
54 | } | ||
![]() |
1.1 | 55 | |
![]() |
3.1 | 56 | {{/code}} |
![]() |
1.1 | 57 | |
![]() |
6.1 | 58 | * The componentTemplateWithExtension method could look like the following: |
![]() |
1.1 | 59 | |
![]() |
3.1 | 60 | {{code}} |
![]() |
1.1 | 61 | |
![]() |
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 | } | ||
![]() |
1.1 | 86 | |
![]() |
3.1 | 87 | {{/code}} |
![]() |
1.1 | 88 | |
![]() |
3.1 | 89 | * And finally componentStreamForResource could be something along these: |
![]() |
1.1 | 90 | |
![]() |
3.1 | 91 | {{code}} |
![]() |
1.1 | 92 | |
![]() |
3.1 | 93 | private static InputStream componentStreamForResource(WOComponent aComponent, String aResource) { |
94 | if ( aResource != null ) { | ||
95 | File aDirectory = new File( System.getProperty( "user.dir" ) ); | ||
![]() |
4.1 | 96 | File aFile = new File( aDirectory, aResource ); |
![]() |
3.1 | 97 | if ( aFile.exists() == true ) { |
98 | try { | ||
99 | return new FileInputStream( aFile ); | ||
100 | } catch(Exception anException) { | ||
101 | Log.warning( anException); | ||
![]() |
1.1 | 102 | } |
103 | } | ||
![]() |
3.1 | 104 | return aComponent.getClass().getResourceAsStream( aResource ); |
105 | } | ||
106 | throw new IllegalArgumentException( "Component.componentStreamForResource: null resource." ); | ||
107 | } | ||
![]() |
1.1 | 108 | |
![]() |
3.1 | 109 | {{/code}} |