Changes for page Development-Generating Static Pages
Last modified by Pascal Robert on 2010/09/13 00:37
From version 1.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:09
on 2007/07/12 21:09
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
-
... ... @@ -12,55 +12,56 @@ 12 12 13 13 I use the following method within a component to write out the page, which is called from the spidering code; node is the EO that represents a page or node, and htmlPath is an obvious method. 14 14 15 -{{ panel}}15 +{{code}} 16 16 17 - public void writeToHtml() { 18 - WOResponse r = null; 19 - String c = null; 20 - try { 21 - File folder = new File(NSPathUtilities.stringByDeletingLastPathComponent(node().htmlPath())); 22 - folder.mkdirs(); 23 - BufferedWriter out = new BufferedWriter(new FileWriter(new File(node().htmlPath()))); 24 - r = generateResponse(); 25 - if (r == null) NSLog.debug.appendln("writeHtml: no response"); 26 - c = r.contentString(); 27 - if (c == null) NSLog.debug.appendln("writeHtml: no content"); 28 - out.write(c); 29 - out.flush(); 30 - } catch (IOException e) { 31 - System.err.println(e.getMessage()); 32 - } catch (Throwable e) { 33 - System.err.println("writeToHtml(" + node().htmlPath() + "): " + e.getMessage()); 34 - } 17 +public void writeToHtml() { 18 + WOResponse r = null; 19 + String c = null; 20 + try { 21 + File folder = new File(NSPathUtilities.stringByDeletingLastPathComponent(node().htmlPath())); 22 + folder.mkdirs(); 23 + BufferedWriter out = new BufferedWriter(new FileWriter(new File(node().htmlPath()))); 24 + r = generateResponse(); 25 + if (r == null) NSLog.debug.appendln("writeHtml: no response"); 26 + c = r.contentString(); 27 + if (c == null) NSLog.debug.appendln("writeHtml: no content"); 28 + out.write(c); 29 + out.flush(); 30 + } catch (IOException e) { 31 + System.err.println(e.getMessage()); 32 + } catch (Throwable e) { 33 + System.err.println("writeToHtml(" + node().htmlPath() + "): " + e.getMessage()); 35 35 } 35 +} 36 36 37 -{{/ panel}}37 +{{/code}} 38 38 39 39 An example implementation for the htmlPath() method follows: 40 40 41 -{{ panel}}41 +{{code}} 42 42 43 - public String htmlPath() { 44 - return NSPathUtilities.stringByAppendingPathComponent(PLUtilities.defaultForKey(null, "docroot", "/Library/WebServer/Documents").toString(), urlPath()); 43 +public String htmlPath() { 44 + return NSPathUtilities.stringByAppendingPathComponent(PLUtilities.defaultForKey(null, 45 + "docroot", "/Library/WebServer/Documents").toString(), urlPath()); 46 +} 47 + 48 +public String urlPath() { 49 + String fileName = name() + primaryKeyString(this) + ".html"; 50 + return "/" + NSPathUtilities.stringByAppendingPathComponent(site().name(), fileName); 51 +} 52 + 53 +static public String primaryKeyString(EOEnterpriseObject object) { 54 + NSDictionary dict = EOUtilities.primaryKeyForObject(object.editingContext(), object); 55 + String result = ""; 56 + java.util.Enumeration enumerator = dict.objectEnumerator(); 57 + while (enumerator.hasMoreElements()) { 58 + Object anObject = enumerator.nextElement(); 59 + result += anObject.toString(); 45 45 } 46 - 47 - public String urlPath() { 48 - String fileName = name() + primaryKeyString(this) + ".html"; 49 - return "/" + NSPathUtilities.stringByAppendingPathComponent(site().name(), fileName); 50 - } 51 - 52 - static public String primaryKeyString(EOEnterpriseObject object) { 53 - NSDictionary dict = EOUtilities.primaryKeyForObject(object.editingContext(), object); 54 - String result = ""; 55 - java.util.Enumeration enumerator = dict.objectEnumerator(); 56 - while (enumerator.hasMoreElements()) { 57 - Object anObject = enumerator.nextElement(); 58 - result += anObject.toString(); 59 - } 60 - return result; 61 - } 61 + return result; 62 +} 62 62 63 -{{/ panel}}64 +{{/code}} 64 64 65 65 A more intelligent implementation may be appropriate, to build a path string depending on the node hierarchy, or to take a file name from EO attributes, etc. 66 66 ... ... @@ -70,32 +70,32 @@ 70 70 71 71 As it is useful to allow the site to run as a dynamic site as well as the static version, I replace all WOHyperlinks with the following contruction (from the wod file): 72 72 73 -{{ panel}}74 +{{code}} 74 74 75 - 76 - 77 - 78 - 79 - 76 +Generic1: WOGenericContainer { 77 + elementName = "a"; 78 + href = nodeHref; 79 + invokeAction = nodeLink; 80 +} 80 80 81 -{{/ panel}}82 +{{/code}} 82 82 83 83 These two methods have to be sensitive to context, and know when to generate a dynamic, WOHyperlink style, link, and when to generate a static link. In my administration application, used for creating and editing the site, I include a flag that is set according to if we are viewing (dynamic) or generating (static) the site. 84 84 85 -{{ panel}}86 +{{code}} 86 86 87 - public String nodeHref() { 88 - if (sessionViewMode()) return context().componentActionURL(); 89 - return aNode.urlPath(); 90 - } 91 - 92 - public CustomTemplate nodeLink() { 93 - if (aNode != null) 94 - return pageWithName(aNode.name()); 95 - } 88 +public String nodeHref() { 89 + if (sessionViewMode()) return context().componentActionURL(); 90 + return aNode.urlPath(); 91 +} 96 96 97 -{{/panel}} 93 +public CustomTemplate nodeLink() { 94 + if (aNode != null) 95 + return pageWithName(aNode.name()); 96 +} 98 98 98 +{{/code}} 99 + 99 99 The call to pageWithName() takes a component name from the node EO; again, this can be replaced with a more intelligent call that sets values in the component, etc. 100 100 101 101 The generic container build an <A> tag with an href that will either contain the appropriate static url, or will act exactly as a WOHyperlink, depending on the value returned by sessionViewMode().