Changes for page Development-Custom Templates
Last modified by Pascal Robert on 2010/09/13 00:27
From version 2.1
edited by smmccraw
on 2007/07/08 09:45
on 2007/07/08 09:45
Change comment:
There is no comment for this version
To version 3.1
edited by Quinton Dolan
on 2007/07/12 21:02
on 2007/07/12 21:02
Change comment:
There is no comment for this version
Summary
-
Page properties (2 modified, 0 added, 0 removed)
Details
- Page properties
-
- Author
-
... ... @@ -1,1 +1,1 @@ 1 -XWiki. smmccraw1 +XWiki.qdolan - Content
-
... ... @@ -1,21 +1,20 @@ 1 1 == Petite Abeille == 2 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. 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.// 5 5 6 6 === It can be quite easy === 7 7 8 -{{ panel}}7 +{{code}} 9 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; 9 +protected WOElement myTemplate = null; 10 +public WOElement template() { 11 + if ( myTemplate == null ) { 12 + myTemplate = WOComponent.templateWithHTMLString( html, wod, null ); 16 16 } 14 + return myTemplate; 15 +} 17 17 18 -{{/ panel}}17 +{{/code}} 19 19 20 20 A More Involved Solution: 21 21 ... ... @@ -25,90 +25,88 @@ 25 25 26 26 * First you will need to overwrite WOComponent.template() 27 27 28 -{{ panel}}27 +{{code}} 29 29 30 - 31 - 32 - 29 +public WOElement template() { 30 + return Component.templateForComponent( this ); 31 +} 33 33 34 -{{/ panel}}33 +{{/code}} 35 35 36 36 * Now, templateForComponent could look like this: 37 37 38 -{{ panel}}37 +{{code}} 39 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 - } 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 ); 51 51 } 52 - return anElement; 53 53 } 54 - throw new IllegalArgumentException ( "Component.templateForComponent: null component." ); 55 - 56 -{{/panel}} 57 - 51 + return anElement; 58 58 } 53 +throw new IllegalArgumentException ( "Component.templateForComponent: null component." ); 54 +} 59 59 56 +{{/code}} 57 + 60 60 * The componentTemplateWithExtension method could look like the following: 61 61 62 -{{ panel}}60 +{{code}} 63 63 64 - 65 - _null_)_{66 - ____String_aResource_=_aComponent.name()_+_anExtension;67 - ____InputStream_anInputStream_=_Component.componentStreamForResource(_aComponent,_aResource_);68 - ____if_(_anInputStream_!= null ) {69 - 70 - 71 - 72 - 73 - 74 - _-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 - __}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 +} 88 88 89 -{{/ panel}}87 +{{/code}} 90 90 91 -* //And//finally//componentStreamForResource//could//be//something//along//these// 92 -lines~:// 89 +* And finally componentStreamForResource could be something along these: 93 93 94 -{{ panel}}91 +{{code}} 95 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 - } 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); 106 106 } 107 - return aComponent.getClass().getResourceAsStream( aResource ); 108 108 } 109 - throw new IllegalArgumentException( "Component.componentStreamForResource: null resource." ); 110 - } 104 + return aComponent.getClass().getResourceAsStream( aResource ); 105 +} 106 +throw new IllegalArgumentException( "Component.componentStreamForResource: null resource." ); 107 +} 111 111 112 -{{/ panel}}109 +{{/code}} 113 113 114 114 Category:WebObjects