001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixxml;
021:
022: import java.util.ArrayList;
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.Map;
026:
027: import javax.servlet.http.Cookie;
028: import javax.servlet.http.HttpServletResponse;
029:
030: import org.w3c.dom.Document;
031: import org.w3c.dom.Element;
032:
033: import de.schlund.pfixxml.util.Xml;
034:
035: /**
036: *
037: *
038: */
039: public class SPDocument {
040:
041: //~ Instance/static variables ..................................................................
042:
043: private Document document;
044: private HashMap<String, Object> propertiesmap;
045: private boolean updateable = true;
046: // private boolean nostore = false;
047: private String pagename = null;
048: private Variant variant = null;
049: private String xslkey = null;
050: private long timestamp = System.currentTimeMillis();
051: private int error = 0;
052: private String errortext = null;
053: private String contenttype = null;
054: private HashMap<String, String> header = new HashMap<String, String>();
055: private ArrayList<Cookie> cookies = new ArrayList<Cookie>();
056: private String redirectURL = null;
057: private boolean trailLogged;
058:
059: //~ Methods ....................................................................................
060:
061: // Pagename is the preferred way to specify the target
062: public void setPagename(String pagename) {
063: this .pagename = pagename;
064: }
065:
066: public String getPagename() {
067: return pagename;
068: }
069:
070: public void setVariant(Variant variant) {
071: this .variant = variant;
072: }
073:
074: public Variant getVariant() {
075: return variant;
076: }
077:
078: // public void setNostore(boolean nostore) {
079: // this.nostore = nostore;
080: // }
081: //
082: // public boolean getNostore() {
083: // return nostore;
084: // }
085:
086: public void setResponseContentType(String type) {
087: contenttype = type;
088: }
089:
090: public String getResponseContentType() {
091: return contenttype;
092: }
093:
094: public void setResponseErrorText(String err) {
095: errortext = err;
096: }
097:
098: public String getResponseErrorText() {
099: return errortext;
100: }
101:
102: public void setResponseError(int err) {
103: error = err;
104: }
105:
106: public int getResponseError() {
107: if (redirectURL == null) {
108: return error;
109: } else {
110: return HttpServletResponse.SC_MOVED_TEMPORARILY;
111: }
112: }
113:
114: public void addResponseHeader(String key, String val) {
115: header.put(key, val);
116: }
117:
118: public HashMap<String, String> getResponseHeader() {
119: if (redirectURL == null) {
120: return header;
121: } else {
122: HashMap<String, String> newheader = new HashMap<String, String>();
123: newheader.put("Location", redirectURL);
124: return newheader;
125: }
126: }
127:
128: public void storeFrameAnchors(Map<String, String> anchors) {
129: if (document == null) {
130: throw new RuntimeException(
131: "*** Can't store anchors into a null Document ***");
132: }
133: Element root = document.getDocumentElement();
134: for (Iterator<String> i = anchors.keySet().iterator(); i
135: .hasNext();) {
136: String frame = i.next();
137: String anchor = anchors.get(frame);
138: Element elem = document.createElement("frameanchor");
139: elem.setAttribute("frame", frame);
140: elem.setAttribute("anchor", anchor);
141: root.appendChild(elem);
142: }
143: }
144:
145: /**
146: * Returns timestamp that was created on construction
147: * of the document. Is <b>not</b> guaranteed to be unique.
148: *
149: * @return a <code>long</code> value
150: */
151: public long getTimestamp() {
152: return timestamp;
153: }
154:
155: public void addCookie(Cookie cookie) {
156: cookies.add(cookie);
157: }
158:
159: public ArrayList<Cookie> getCookies() {
160: return cookies;
161: }
162:
163: public String getXSLKey() {
164: return xslkey;
165: }
166:
167: public Document getDocument() {
168: return document;
169: }
170:
171: public HashMap<String, Object> getProperties() {
172: return propertiesmap;
173: }
174:
175: public boolean docIsUpdateable() {
176: return updateable;
177: }
178:
179: public void setDocIsUpdateable(boolean upd) {
180: updateable = upd;
181: }
182:
183: public void setDocument(Document newDocument) {
184: document = newDocument;
185: }
186:
187: public void setProperties(HashMap<String, Object> newPropertiesmap) {
188: propertiesmap = newPropertiesmap;
189: }
190:
191: public void setProperty(String key, Object value) {
192: if (propertiesmap == null) {
193: propertiesmap = new HashMap<String, Object>();
194: }
195: propertiesmap.put(key, value);
196: }
197:
198: /**
199: * Describe <code>setXSLKey</code> method here.
200: *
201: * @param xslkey a <code>String</code> value
202: */
203: public void setXSLKey(String xslkey) {
204: this .xslkey = xslkey;
205: }
206:
207: /**
208: * Sets an URL to use for redirection.
209: * This will cause the {@link #getResponseError()} and
210: * {@link #getResponseHeader()} methods to return special
211: * values.
212: *
213: * @param redirectURL Complete URL string
214: */
215: public void setRedirect(String redirectURL) {
216: this .redirectURL = redirectURL;
217: }
218:
219: public boolean isRedirect() {
220: return redirectURL != null;
221: }
222:
223: /**
224: * Resets the redirect URL set via {@link #setRedirect(String)}.
225: * Should be called after serving the document the first time
226: * (which effectively means after doing the redirect).
227: */
228: public void resetRedirectURL() {
229: this .redirectURL = null;
230: }
231:
232: public boolean getTrailLogged() {
233: return trailLogged;
234: }
235:
236: public void setTrailLogged() {
237: trailLogged = true;
238: }
239:
240: /**
241: * Describe <code>toString</code> method here.
242: *
243: * @return a <code>String</code> value
244: */
245: public String toString() {
246: Document tmp = document;
247: StringBuffer sw = new StringBuffer();
248: sw.append("\n");
249: if (tmp == null) {
250: sw.append("null\n");
251: } else {
252: sw.append("[class: " + tmp.getClass().getName() + "]\n");
253: sw.append(Xml.serialize(tmp, true, true));
254: }
255: return sw.toString();
256: }
257: }
|