1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
package com.opensymphony.sitemesh.compatability;
import com.opensymphony.module.sitemesh.HTMLPage;
import com.opensymphony.sitemesh.Content;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;
import java.util.HashMap;
/**
* Adapts a SiteMesh 3 {@link Content} to a SiteMesh 2 {@link HTMLPage}.
*
* @author Joe Walnes
* @since SiteMesh 3
*/
public class Content2HTMLPage implements HTMLPage {
private final Content content;
private HttpServletRequest request;
public Content2HTMLPage(Content content) {
this.content = content;
}
public Content2HTMLPage(Content content, HttpServletRequest request) {
this.content = content;
this.request = request;
}
public void writePage(Writer out) throws IOException {
content.writeOriginal(out);
}
public String getPage() {
try {
StringWriter writer = new StringWriter();
writePage(writer);
return writer.toString();
} catch (IOException e) {
throw new IllegalStateException("Could not get page " + e.getMessage());
}
}
public void writeBody(Writer out) throws IOException {
content.writeBody(out);
}
public String getBody() {
try {
StringWriter writer = new StringWriter();
writeBody(writer);
return writer.toString();
} catch (IOException e) {
throw new IllegalStateException("Could not get body " + e.getMessage());
}
}
public void writeHead(Writer out) throws IOException {
content.writeHead(out);
}
public String getHead() {
try {
StringWriter writer = new StringWriter();
writeHead(writer);
return writer.toString();
} catch (IOException e) {
throw new IllegalStateException("Could not get head " + e.getMessage());
}
}
public String getTitle() {
return content.getTitle();
}
public int getContentLength() {
return content.originalLength();
}
public String getProperty(String name) {
return content.getProperty(name);
}
public int getIntProperty(String name) {
try {
return Integer.parseInt(noNull(getProperty(name)));
}
catch (NumberFormatException e) {
return 0;
}
}
public long getLongProperty(String name) {
try {
return Long.parseLong(noNull(getProperty(name)));
} catch (NumberFormatException e) {
return 0;
}
}
private String noNull(String property) {
return property == null ? "" : property;
}
public boolean getBooleanProperty(String name) {
String property = getProperty(name);
if (property == null || property.trim().length() == 0) return false;
switch (property.charAt(0)) {
case '1':
case 't':
case 'T':
case 'y':
case 'Y':
return true;
default:
return false;
}
}
public boolean isPropertySet(String name) {
return getProperty(name) != null;
}
public String[] getPropertyKeys() {
return content.getPropertyKeys();
}
public Map getProperties() {
Map result = new HashMap();
String[] keys = content.getPropertyKeys();
for (int i = 0; i < keys.length; i++) {
result.put(keys[i], content.getProperty(keys[i]));
}
return result;
}
public boolean isFrameSet() {
return isPropertySet("frameset") && getProperty("frameset").equalsIgnoreCase("true");
}
public void setFrameSet(boolean frameset) {
addProperty("frameset", frameset ? "true" : "false");
}
/**
* @see com.opensymphony.module.sitemesh.Page#getRequest()
*/
public HttpServletRequest getRequest() {
return request;
}
/**
* Create snapshot of Request.
*
* @see com.opensymphony.module.sitemesh.Page#getRequest()
*/
public void setRequest(HttpServletRequest request) {
this.request = request;
}
public void addProperty(String name, String value) {
content.addProperty(name, value);
}
}
|