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