001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2001-2002 Janne Jalkanen (Janne.Jalkanen@iki.fi)
005:
006: This program is free software; you can redistribute it and/or modify
007: it under the terms of the GNU Lesser General Public License as published by
008: the Free Software Foundation; either version 2.1 of the License, or
009: (at your option) any later version.
010:
011: This program is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public License
017: along with this program; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package com.ecyrd.jspwiki.dav.items;
021:
022: import java.io.ByteArrayInputStream;
023: import java.io.InputStream;
024: import java.io.UnsupportedEncodingException;
025: import java.util.Collection;
026:
027: import org.jdom.Element;
028:
029: import com.ecyrd.jspwiki.WikiContext;
030: import com.ecyrd.jspwiki.WikiEngine;
031: import com.ecyrd.jspwiki.WikiPage;
032: import com.ecyrd.jspwiki.dav.DavPath;
033: import com.ecyrd.jspwiki.dav.DavProvider;
034: import com.ecyrd.jspwiki.dav.WikiDavProvider;
035: import com.ecyrd.jspwiki.parser.MarkupParser;
036:
037: /**
038: * Represents a DAV HTML page item.
039: * @author jalkanen
040: *
041: * @since
042: */
043: public class HTMLPageDavItem extends PageDavItem {
044: private long m_cachedLength = -1;
045:
046: /**
047: * @param provider the DAV provider
048: * @param path the DAV path
049: * @param page the wiki page
050: */
051: public HTMLPageDavItem(DavProvider provider, DavPath path,
052: WikiPage page) {
053: super (provider, path, page);
054: }
055:
056: /**
057: * @see com.ecyrd.jspwiki.dav.items.DavItem#getHref()
058: */
059: public String getHref() {
060: return m_provider.getURL(m_path);
061: }
062:
063: /**
064: * Returns the content type for the item. Always returns
065: * <code>text/html; charset=UTF-8</code>.
066: * @see com.ecyrd.jspwiki.dav.items.DavItem#getContentType()
067: */
068: public String getContentType() {
069: return "text/html; charset=UTF-8";
070: }
071:
072: private byte[] getText() {
073: WikiEngine engine = ((WikiDavProvider) m_provider).getEngine();
074:
075: WikiContext context = new WikiContext(engine, m_page);
076: context.setRequestContext(WikiContext.VIEW);
077:
078: context.setVariable(MarkupParser.PROP_RUNPLUGINS, "false");
079: context.setVariable(WikiEngine.PROP_RUNFILTERS, "false");
080:
081: String text = engine.getHTML(context, m_page);
082:
083: try {
084: return text.getBytes("UTF-8");
085: } catch (UnsupportedEncodingException e) {
086: return null; // Should never happen
087: }
088: }
089:
090: public InputStream getInputStream() {
091: byte[] text = getText();
092:
093: ByteArrayInputStream in = new ByteArrayInputStream(text);
094:
095: return in;
096: }
097:
098: public long getLength() {
099: if (m_cachedLength == -1) {
100: byte[] text = getText();
101:
102: m_cachedLength = text.length;
103: }
104:
105: return m_cachedLength;
106: }
107:
108: public Collection getPropertySet() {
109: Collection set = getCommonProperties();
110:
111: //
112: // Rendering the page for every single time is not really a very good idea.
113: //
114:
115: set.add(new Element("getcontentlength", m_davns).setText(Long
116: .toString(getLength())));
117: set.add(new Element("getcontenttype", m_davns)
118: .setText("text/html; charset=\"UTF-8\""));
119:
120: return set;
121: }
122: }
|