Changes for page Development-Generating Static Pages
Last modified by Pascal Robert on 2010/09/13 00:37
From 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
To 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
Summary
-
Page properties (2 modified, 0 added, 0 removed)
Details
- Page properties
-
- Author
-
... ... @@ -1,1 +1,1 @@ 1 -XWiki. qdolan1 +XWiki.smmccraw - Content
-
... ... @@ -12,56 +12,55 @@ 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 -{{ code}}15 +{{panel}} 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()); 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 + } 34 34 } 35 -} 36 36 37 -{{/ code}}37 +{{/panel}} 38 38 39 39 An example implementation for the htmlPath() method follows: 40 40 41 -{{ code}}41 +{{panel}} 42 42 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(); 43 + public String htmlPath() { 44 + return NSPathUtilities.stringByAppendingPathComponent(PLUtilities.defaultForKey(null, "docroot", "/Library/WebServer/Documents").toString(), urlPath()); 60 60 } 61 - return result; 62 -} 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 + } 63 63 64 -{{/ code}}63 +{{/panel}} 65 65 66 66 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. 67 67 ... ... @@ -71,32 +71,32 @@ 71 71 72 72 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): 73 73 74 -{{ code}}73 +{{panel}} 75 75 76 -Generic1: WOGenericContainer { 77 - elementName = "a"; 78 - href = nodeHref; 79 - invokeAction = nodeLink; 80 -} 75 + Generic1: WOGenericContainer { 76 + elementName = "a"; 77 + href = nodeHref; 78 + invokeAction = nodeLink; 79 + } 81 81 82 -{{/ code}}81 +{{/panel}} 83 83 84 84 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. 85 85 86 -{{ code}}85 +{{panel}} 87 87 88 -public String nodeHref() { 89 - if (sessionViewMode()) return context().componentActionURL(); 90 - return aNode.urlPath(); 91 -} 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 + } 92 92 93 -public CustomTemplate nodeLink() { 94 - if (aNode != null) 95 - return pageWithName(aNode.name()); 96 -} 97 +{{/panel}} 97 97 98 -{{/code}} 99 - 100 100 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. 101 101 102 102 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().