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