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.attachment;
020
021import org.apache.wiki.WikiEngine;
022import org.apache.wiki.WikiPage;
023import org.apache.wiki.api.core.Engine;
024
025
026/**
027 *  Describes an attachment.  Attachments are actually derivatives of a WikiPage, since they do actually have a WikiName as well.
028 */
029public class Attachment extends WikiPage implements org.apache.wiki.api.core.Attachment {
030
031    private String m_fileName;
032    private final String m_parentName;
033    private boolean m_cacheable = true;
034
035    /**
036     * Creates a new attachment.  The final name of the attachment will be a synthesis of the parent page name and the file name.
037     *
038     * @param engine     The Engine which is hosting this attachment.
039     * @param parentPage The page which will contain this attachment.
040     * @param fileName   The file name for the attachment.
041     */
042    public Attachment( final Engine engine, final String parentPage, final String fileName ) {
043        super( engine, parentPage + "/" + fileName );
044        m_parentName = parentPage;
045        m_fileName = fileName;
046    }
047
048    /**
049     * Creates a new attachment.  The final name of the attachment will be a synthesis of the parent page name and the file name.
050     *
051     * @param engine     The Engine which is hosting this attachment.
052     * @param parentPage The page which will contain this attachment.
053     * @param fileName   The file name for the attachment.
054     * @deprecated kept for compatibility with page/attachment providers not using public API. Use {@code Attachment(Engine, String, String)} instead.
055     */
056    @Deprecated
057    public Attachment( final WikiEngine engine, final String parentPage, final String fileName ) {
058        this( ( Engine )engine, parentPage, fileName );
059    }
060
061    /**
062     * Returns a human-readable, only-debugging-suitable description.
063     *
064     * @return A debugging string
065     */
066    @Override
067    public String toString() {
068        return "Attachment [" + getName() + ";mod=" + getLastModified() + "]";
069    }
070
071    /**
072     * Returns the file name of the attachment.
073     *
074     * @return A String with the file name.
075     */
076    @Override
077    public String getFileName() {
078        return m_fileName;
079    }
080
081    /**
082     * Sets the file name of this attachment.
083     *
084     * @param name The name of the attachment.  Must be a legal file name without  the path.
085     */
086    @Override
087    public void setFileName(final String name ) {
088        m_fileName = name;
089    }
090
091    /**
092     * Returns the name of the parent of this Attachment, i.e. the page which contains this attachment.
093     *
094     * @return String depicting the parent of the attachment.
095     */
096    @Override
097    public String getParentName() {
098        return m_parentName;
099    }
100
101    /**
102     * Returns true, if this attachment can be cached by the user agent.  By default attachments are cacheable.
103     *
104     * @return False, if the attachment should not be cached by the user agent.
105     * @since 2.5.34
106     */
107    @Override
108    public boolean isCacheable() {
109        return m_cacheable;
110    }
111
112    /**
113     * Sets this attachment to be cacheable or not.  This mostly concerns things like DynamicAttachments, but it may be useful for
114     * certain AttachmentProviders as well.
115     *
116     * @param value True or false, depending on whether you want this attachment to be cacheable or not.
117     * @since 2.5.34
118     */
119    @Override
120    public void setCacheable(final boolean value ) {
121        m_cacheable = value;
122    }
123
124}