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 6.1
edited by Pascal Robert
on 2010/09/13 00:27
on 2010/09/13 00:27
Change comment:
Migrated to Confluence 4.0
Summary
-
Page properties (3 modified, 0 added, 0 removed)
Details
- Page properties
-
- Title
-
... ... @@ -1,1 +1,1 @@ 1 - Programming__WebObjects-Web Applications-Development-Custom Templates1 +Development-Custom Templates - Author
-
... ... @@ -1,1 +1,1 @@ 1 -XWiki. smmccraw1 +XWiki.probert - Content
-
... ... @@ -1,114 +1,109 @@ 1 -== Petite Abeille 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 -=== It can be quite easy 5 +=== 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 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): 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): 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 60 - * ThecomponentTemplateWithExtension methodcould look likethe following:56 +{{/code}} 61 61 62 - {{panel}}58 +* The componentTemplateWithExtension method could look like the following: 63 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 -__} 60 +{{code}} 88 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 - FileaDirectory= new File( System.getProperty("user.dir"));99 - FileaFile= newFile( aDirectory,aResource );100 - if(aFile.exists()== true){101 - try {102 - returnnew FileInputStream( aFile62 +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(); 103 103 } catch(Exception anException) { 104 - Log.warning( anException); 77 + Log.warning( anException ); 105 105 } 79 + return aBuffer.toString(); 106 106 } 107 - re turnaComponent.getClass().getResourceAsStream(aResource );81 + throw new RuntimeException ( "Component.componentTemplateWithExtension: resource not found: '" + aResource + "'." ); 108 108 } 109 - throw new IllegalArgumentException( "Component.componentStreamForResource: null resource." ); 110 - } 83 +} 84 +throw new IllegalArgumentException ( "Component.componentTemplateWithExtension: null extension." ); 85 +} 111 111 112 -{{/ panel}}87 +{{/code}} 113 113 114 -Category:WebObjects 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}}