001: package com.opensymphony.sitemesh.compatability;
002:
003: import com.opensymphony.module.sitemesh.HTMLPage;
004: import com.opensymphony.sitemesh.Content;
005:
006: import javax.servlet.http.HttpServletRequest;
007: import java.io.IOException;
008: import java.io.StringWriter;
009: import java.io.Writer;
010: import java.util.Map;
011: import java.util.HashMap;
012:
013: /**
014: * Adapts a SiteMesh 3 {@link Content} to a SiteMesh 2 {@link HTMLPage}.
015: *
016: * @author Joe Walnes
017: * @since SiteMesh 3
018: */
019: public class Content2HTMLPage implements HTMLPage {
020:
021: private final Content content;
022:
023: public Content2HTMLPage(Content content) {
024: this .content = content;
025: }
026:
027: public void writePage(Writer out) throws IOException {
028: content.writeOriginal(out);
029: }
030:
031: public String getPage() {
032: try {
033: StringWriter writer = new StringWriter();
034: writePage(writer);
035: return writer.toString();
036: } catch (IOException e) {
037: throw new IllegalStateException("Could not get page "
038: + e.getMessage());
039: }
040: }
041:
042: public void writeBody(Writer out) throws IOException {
043: content.writeBody(out);
044: }
045:
046: public String getBody() {
047: try {
048: StringWriter writer = new StringWriter();
049: writeBody(writer);
050: return writer.toString();
051: } catch (IOException e) {
052: throw new IllegalStateException("Could not get body "
053: + e.getMessage());
054: }
055: }
056:
057: public void writeHead(Writer out) throws IOException {
058: content.writeHead(out);
059: }
060:
061: public String getHead() {
062: try {
063: StringWriter writer = new StringWriter();
064: writeHead(writer);
065: return writer.toString();
066: } catch (IOException e) {
067: throw new IllegalStateException("Could not get head "
068: + e.getMessage());
069: }
070: }
071:
072: public String getTitle() {
073: return content.getTitle();
074: }
075:
076: public int getContentLength() {
077: return content.originalLength();
078: }
079:
080: public String getProperty(String name) {
081: return content.getProperty(name);
082: }
083:
084: public int getIntProperty(String name) {
085: try {
086: return Integer.parseInt(noNull(getProperty(name)));
087: } catch (NumberFormatException e) {
088: return 0;
089: }
090: }
091:
092: public long getLongProperty(String name) {
093: try {
094: return Long.parseLong(noNull(getProperty(name)));
095: } catch (NumberFormatException e) {
096: return 0;
097: }
098: }
099:
100: private String noNull(String property) {
101: return property == null ? "" : property;
102: }
103:
104: public boolean getBooleanProperty(String name) {
105: String property = getProperty(name);
106: if (property == null || property.trim().length() == 0)
107: return false;
108: switch (property.charAt(0)) {
109: case '1':
110: case 't':
111: case 'T':
112: case 'y':
113: case 'Y':
114: return true;
115: default:
116: return false;
117: }
118: }
119:
120: public boolean isPropertySet(String name) {
121: return getProperty(name) != null;
122: }
123:
124: public String[] getPropertyKeys() {
125: return content.getPropertyKeys();
126: }
127:
128: public Map getProperties() {
129: Map result = new HashMap();
130: String[] keys = content.getPropertyKeys();
131: for (int i = 0; i < keys.length; i++) {
132: result.put(keys[i], content.getProperty(keys[i]));
133: }
134: return result;
135: }
136:
137: public boolean isFrameSet() {
138: return isPropertySet("frameset")
139: && getProperty("frameset").equalsIgnoreCase("true");
140: }
141:
142: public void setFrameSet(boolean frameset) {
143: addProperty("frameset", frameset ? "true" : "false");
144: }
145:
146: public HttpServletRequest getRequest() {
147: // TODO: Find a new place for the request to live - don't want to couple Content to Servlet environment.
148: throw new UnsupportedOperationException();
149: }
150:
151: public void setRequest(HttpServletRequest request) {
152: // TODO: Find a new place for the request to live - don't want to couple Content to Servlet environment.
153: throw new UnsupportedOperationException();
154: }
155:
156: public void addProperty(String name, String value) {
157: content.addProperty(name, value);
158: }
159:
160: }
|