001/* 
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.  
018*/
019package org.apache.wiki.parser;
020
021import org.apache.wiki.api.core.Context;
022import org.apache.wiki.api.core.Page;
023import org.jdom2.Document;
024
025import java.lang.ref.WeakReference;
026
027/**
028 *  Stores the DOM tree of a rendered WikiPage. This class extends the org.jdom.Document to provide some extra metadata
029 *  specific to JSPWiki.
030 *  <p>
031 *  The document is not stored as metadata in the WikiPage because otherwise it could not be cached separately.
032 *  
033 *  @since  2.4
034 */
035public class WikiDocument extends Document {
036
037    private static final long serialVersionUID = 1L;
038    
039    private final Page m_page;
040    private String m_wikiText;
041    private WeakReference< Context > m_context;
042    
043    /**
044     *  Creates a new WikiDocument for a specific page.
045     * 
046     *  @param page The page to which this document refers to.
047     */
048    public WikiDocument( final Page page )
049    {
050        m_page = page;
051    }
052    
053    /**
054     *  Set the WikiMarkup for this document.
055     *  
056     *  @param data The WikiMarkup
057     */
058    public void setPageData( final String data )
059    {
060        m_wikiText = data;
061    }
062    
063    /**
064     *  Returns the wikimarkup used to render this document.
065     *  
066     *  @return The WikiMarkup
067     */
068    public String getPageData()
069    {
070        return m_wikiText;
071    }
072    
073    /**
074     *  Return the WikiPage for whom this WikiDocument exists.
075     *  
076     *  @return The WikiPage
077     */
078    public Page getPage()
079    {
080        return m_page;
081    }
082
083    /**
084     *  Set the WikiContext in which the WikiDocument is rendered. The WikiContext is stored in a WeakReference, which means that it
085     *  can be garbagecollected away.  This is to allow for caching of the WikiDocument without having to carry the WikiContext around
086     *  for a long time.
087     *  
088     *  @param ctx A WikiContext.
089     */
090    public void setContext( final Context ctx )
091    {
092        m_context = new WeakReference<>( ctx );
093    }
094    
095    /**
096     * Returns the wiki context for this document. This method
097     * may return <code>null</code> if the associated wiki session
098     * had previously been garbage-collected.
099     * @return the wiki context
100     */
101    public Context getContext()
102    {
103        return m_context.get();
104    }
105
106}