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.htmltowiki;
020
021import org.apache.wiki.api.core.Context;
022import org.apache.wiki.api.core.ContextEnum;
023import org.jdom2.Element;
024
025import java.io.UnsupportedEncodingException;
026import java.net.URLDecoder;
027import java.nio.charset.StandardCharsets;
028
029
030/**
031 * Defines a Wiki configuration to XHtmlToWikiTranslator, including things like URLs.
032 */
033public class XHtmlToWikiConfig {
034
035    private String m_outlink = "outlink";
036    private String m_pageInfoJsp = "PageInfo.jsp";
037    private String m_wikiJspPage = "Wiki.jsp?page=";
038    private String m_editJspPage = "Edit.jsp?page=";
039    private String m_attachPage = "attach?page=";
040    private String m_pageName;
041
042    /**
043     *  Creates a new, empty config object.
044     */
045    public XHtmlToWikiConfig() {
046    }
047
048    /**
049     * The constructor initializes the different internal fields according to the current URLConstructor.
050     *
051     * @param wikiContext A WikiContext
052     */
053    public XHtmlToWikiConfig( final Context wikiContext ) {
054        setWikiContext( wikiContext );
055
056        //  Figure out the actual URLs.
057        //  NB: The logic here will fail if you add something else after the Wiki page name in VIEW or ATTACH
058        m_wikiJspPage = wikiContext.getURL( ContextEnum.PAGE_VIEW.getRequestContext(), "" );
059        m_editJspPage = wikiContext.getURL( ContextEnum.PAGE_EDIT.getRequestContext(), "" );
060        m_attachPage = wikiContext.getURL( ContextEnum.PAGE_ATTACH.getRequestContext(), "" );
061        m_pageInfoJsp = wikiContext.getURL( ContextEnum.PAGE_INFO.getRequestContext(), "" );
062    }
063
064    private void setWikiContext( final Context wikiContext ) {
065        if( wikiContext.getPage() != null ) {
066            setPageName( wikiContext.getPage().getName() + '/' );
067        }
068    }
069
070    /**
071     * Return the URL for the attachments.
072     *
073     * @return URL for attachments.
074     */
075    public String getAttachPage() {
076        return m_attachPage;
077    }
078
079    /**
080     * Set the URL for attachments.
081     *
082     * @param attachPage The attachment URL.
083     */
084    public void setAttachPage( final String attachPage ) {
085        m_attachPage = attachPage;
086    }
087
088    /**
089     * Gets the URL of the outlink image.
090     *
091     * @return The URL of the outlink image.
092     */
093    public String getOutlink() {
094        return m_outlink;
095    }
096
097    /**
098     * Set the outlink URL.
099     *
100     * @param outlink The outlink URL.
101     */
102    public void setOutlink( final String outlink ) {
103        m_outlink = outlink;
104    }
105
106    /**
107     * Get the PageInfo.jsp URI.
108     *
109     * @return The URI for the page info display.
110     */
111    public String getPageInfoJsp() {
112        return m_pageInfoJsp;
113    }
114
115    /**
116     * Set the URI for the page info display.
117     *
118     * @param pageInfoJsp URI for the page info.
119     */
120    public void setPageInfoJsp( final String pageInfoJsp ) {
121        m_pageInfoJsp = pageInfoJsp;
122    }
123
124    /**
125     * Get the page name.
126     *
127     * @return The Page Name.
128     */
129    public String getPageName() {
130        return m_pageName;
131    }
132
133    /**
134     * Set the page name.
135     *
136     * @param pageName The name of the page.
137     */
138    public void setPageName( final String pageName ) {
139        m_pageName = pageName;
140    }
141
142    /**
143     * Get the URI to the Wiki.jsp view.
144     *
145     * @return The URI to the Wiki.jsp.
146     */
147    public String getWikiJspPage() {
148        return m_wikiJspPage;
149    }
150
151    /**
152     * Set the URI to the Wiki.jsp.
153     *
154     * @param wikiJspPage The URI to the Wiki.jsp.
155     */
156    public void setWikiJspPage( final String wikiJspPage ) {
157        m_wikiJspPage = wikiJspPage;
158    }
159
160    /**
161     * Return the URI to the Edit.jsp page.
162     *
163     * @return The URI to the Edit.jsp page.
164     */
165    public String getEditJspPage() {
166        return m_editJspPage;
167    }
168
169    /**
170     * Set the URI to the Edit.jsp page.
171     *
172     * @param editJspPage The Edit.jsp URI.
173     */
174    public void setEditJspPage( final String editJspPage ) {
175        m_editJspPage = editJspPage;
176    }
177
178    public boolean isNotIgnorableWikiMarkupLink( final Element a ) {
179        final String ref = a.getAttributeValue( "href" );
180        final String clazz = a.getAttributeValue( "class" );
181        return ( ref == null || !ref.startsWith( getPageInfoJsp() ) )
182                && ( clazz == null || !clazz.trim().equalsIgnoreCase( getOutlink() ) );
183    }
184
185    public String trimLink( String ref ) {
186        if( ref == null ) {
187            return null;
188        }
189        try {
190            ref = URLDecoder.decode( ref, StandardCharsets.UTF_8.name() );
191            ref = ref.trim();
192            if( ref.startsWith( getAttachPage() ) ) {
193                ref = ref.substring( getAttachPage().length() );
194            }
195            if( ref.startsWith( getWikiJspPage() ) ) {
196                ref = ref.substring( getWikiJspPage().length() );
197
198                // Handle links with section anchors.
199                // For example, we need to translate the html string "TargetPage#section-TargetPage-Heading2"
200                // to this wiki string "TargetPage#Heading2".
201                ref = ref.replaceFirst( ".+#section-(.+)-(.+)", "$1#$2" );
202            }
203            if( ref.startsWith( getEditJspPage() ) ) {
204                ref = ref.substring( getEditJspPage().length() );
205            }
206            if( getPageName() != null ) {
207                if( ref.startsWith( getPageName() ) ) {
208                    ref = ref.substring( getPageName().length() );
209                }
210            }
211        } catch ( final UnsupportedEncodingException e ) {
212            // Shouldn't happen...
213        }
214        return ref;
215    }
216
217}