KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > resource > StringResource


1 /*
2  * $Id: StringResource.java,v 1.3 2004/12/01 07:54:26 hengels Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings.resource;
15
16 import org.wings.RequestURL;
17 import org.wings.Resource;
18 import org.wings.SimpleURL;
19 import org.wings.externalizer.ExternalizeManager;
20 import org.wings.io.Device;
21 import org.wings.session.PropertyService;
22 import org.wings.session.SessionManager;
23
24 import java.io.IOException JavaDoc;
25
26 /**
27  * @author <a HREF="mailto:hengels@mercatis.de">Holger Engels</a>
28  * @version $Revision: 1.3 $
29  */

30 public class StringResource
31         extends Resource {
32     private final String JavaDoc string;
33
34     private String JavaDoc id;
35
36     /**
37      * Flags that influence the behaviour of the externalize manager
38      */

39     protected int externalizerFlags;
40
41     public StringResource(String JavaDoc string) {
42         this(string, "txt", "text/plain");
43     }
44
45     public StringResource(String JavaDoc string, String JavaDoc extension, String JavaDoc mimeType) {
46         this(string, extension, mimeType, ExternalizeManager.FINAL);
47     }
48
49     public StringResource(String JavaDoc string, String JavaDoc extension, String JavaDoc mimeType,
50                           int externalizerFlags) {
51         super(extension, mimeType);
52
53         this.string = string;
54         this.externalizerFlags = externalizerFlags;
55     }
56
57
58     public int getLength() {
59         return string.length();
60     }
61
62     /**
63      * Get the id that identifies this resource as an externalized object.
64      * If the object has not been externalized yet, it will be externalized.
65      *
66      * @return the externalization id
67      */

68     public String JavaDoc getId() {
69         if (id == null) {
70             ExternalizeManager ext = SessionManager.getSession().getExternalizeManager();
71             id = ext.getId(ext.externalize(this, externalizerFlags));
72         }
73         return id;
74     }
75
76     public SimpleURL getURL() {
77         String JavaDoc name = getId();
78
79         // append the sessionid, if not global
80
if ((externalizerFlags & ExternalizeManager.GLOBAL) > 0) {
81             return new SimpleURL(name);
82         } else {
83             RequestURL requestURL = (RequestURL) getPropertyService().getProperty("request.url");
84             requestURL = (RequestURL) requestURL.clone();
85             requestURL.setResource(name);
86             return requestURL;
87         }
88     }
89
90     public final void write(Device out) throws IOException JavaDoc {
91         out.print(string);
92     }
93
94     public int getExternalizerFlags() {
95         return externalizerFlags;
96     }
97
98     private PropertyService propertyService;
99
100     protected PropertyService getPropertyService() {
101         if (propertyService == null)
102             propertyService = (PropertyService) SessionManager.getSession();
103         return propertyService;
104     }
105
106 }
107
108
109
110
111
112
113
Popular Tags