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    */
019    package org.apache.wiki.parser;
020    
021    import java.lang.ref.WeakReference;
022    
023    import org.jdom2.Document;
024    
025    import org.apache.wiki.WikiContext;
026    import org.apache.wiki.WikiPage;
027    
028    /**
029     *  Stores the DOM tree of a rendered WikiPage.  This class
030     *  extends the org.jdom.Document to provide some extra metadata
031     *  specific to JSPWiki.
032     *  <p>
033     *  The document is not stored as metadata in the WikiPage because
034     *  otherwise it could not be cached separately.
035     *  
036     *  @since  2.4
037     */
038    public class WikiDocument extends Document
039    {
040        private static final long serialVersionUID = 1L;
041        
042        private WikiPage                   m_page;
043        private String                     m_wikiText;
044    
045        private WeakReference<WikiContext> m_context;
046        
047        /**
048         *  Creates a new WikiDocument for a specific page.
049         * 
050         *  @param page The page to which this document refers to.
051         */
052        public WikiDocument( WikiPage page )
053        {
054            m_page     = page;
055        }
056        
057        /**
058         *  Set the WikiMarkup for this document.
059         *  
060         *  @param data The WikiMarkup
061         */
062        public void setPageData( String data )
063        {
064            m_wikiText = data;
065        }
066        
067        /**
068         *  Returns the wikimarkup used to render this document.
069         *  
070         *  @return The WikiMarkup
071         */
072        public String getPageData()
073        {
074            return m_wikiText;
075        }
076        
077        /**
078         *  Return the WikiPage for whom this WikiDocument exists.
079         *  
080         *  @return The WikiPage
081         */
082        public WikiPage getPage()
083        {
084            return m_page;
085        }
086    
087        /**
088         *  Set the WikiContext in which the WikiDocument is rendered. The
089         *  WikiContext is stored in a WeakReference, which means that it 
090         *  can be garbagecollected away.  This is to allow for caching of
091         *  the WikiDocument without having to carry the WikiContext around
092         *  for a long time.
093         *  
094         *  @param ctx A WikiContext.
095         */
096        public void setContext( WikiContext ctx )
097        {
098            m_context = new WeakReference<WikiContext>( ctx );
099        }
100        
101        /**
102         * Returns the wiki context for this document. This method
103         * may return <code>null</code> if the associated wiki session
104         * had previously been garbage-collected.
105         * @return the wiki context
106         */
107        public WikiContext getContext()
108        {
109            return m_context.get();
110        }
111    }