01: /*
02: * Copyright 2000,2005 wingS development team.
03: *
04: * This file is part of wingS (http://wingsframework.org).
05: *
06: * wingS is free software; you can redistribute it and/or modify
07: * it under the terms of the GNU Lesser General Public License
08: * as published by the Free Software Foundation; either version 2.1
09: * of the License, or (at your option) any later version.
10: *
11: * Please see COPYING for the complete licence.
12: */
13: package org.wings.resource;
14:
15: import org.wings.RequestURL;
16: import org.wings.Resource;
17: import org.wings.SimpleURL;
18: import org.wings.externalizer.ExternalizeManager;
19: import org.wings.io.Device;
20: import org.wings.session.PropertyService;
21: import org.wings.session.SessionManager;
22:
23: import java.io.IOException;
24:
25: /**
26: * For externalizing a string as a resource.
27: *
28: * @author Holger Engels
29: */
30: public class StringResource extends Resource {
31: private final String string;
32: private String id;
33:
34: /**
35: * Flags that influence the behaviour of the externalize manager
36: */
37: protected int externalizerFlags;
38:
39: /**
40: * Default c'tor externalizing this resource as <code>text/plain</code> MIME document.
41: * @param string
42: */
43: public StringResource(String string) {
44: this (string, "txt", "text/plain");
45: }
46:
47: public StringResource(String string, String extension,
48: String mimeType) {
49: this (string, extension, mimeType, ExternalizeManager.FINAL);
50: }
51:
52: public StringResource(String string, String extension,
53: String mimeType, int externalizerFlags) {
54: super (extension, mimeType);
55:
56: this .string = string;
57: this .externalizerFlags = externalizerFlags;
58: }
59:
60: public int getLength() {
61: return string.length();
62: }
63:
64: /**
65: * Get the id that identifies this resource as an externalized object.
66: * If the object has not been externalized yet, it will be externalized.
67: *
68: * @return the externalization id
69: */
70: public String getId() {
71: if (id == null) {
72: ExternalizeManager ext = SessionManager.getSession()
73: .getExternalizeManager();
74: id = ext.getId(ext.externalize(this , externalizerFlags));
75: }
76: return id;
77: }
78:
79: public SimpleURL getURL() {
80: String name = getId();
81: RequestURL requestURL = (RequestURL) SessionManager
82: .getSession().getProperty("request.url");
83: requestURL = (RequestURL) requestURL.clone();
84: requestURL.setResource(name);
85: return requestURL;
86: }
87:
88: public final void write(Device out) throws IOException {
89: out.print(string);
90: }
91:
92: public int getExternalizerFlags() {
93: return externalizerFlags;
94: }
95: }
|