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.markdown.nodes;
020
021import org.apache.commons.lang3.StringUtils;
022
023import com.vladsch.flexmark.ast.Link;
024
025
026/**
027 * Flexmark node responsible of handling JSPWiki links.
028 */
029public class JSPWikiLink extends Link {
030
031    private final String wikiLink;
032    private final boolean hasRef;
033
034    public JSPWikiLink( final Link other ) {
035        super( other.getChars(),
036               other.getTextOpeningMarker(),
037               other.getText(),
038               other.getTextClosingMarker(),
039               other.getLinkOpeningMarker(),
040               other.getUrl(),
041               other.getTitleOpeningMarker(),
042               other.getTitle(),
043               other.getTitleClosingMarker(),
044               other.getLinkClosingMarker()
045        );
046        if( StringUtils.isEmpty( getUrl().toString() ) ) { // empty link == link.getText() is a wiki page
047            setUrl( getText() );
048            hasRef = false;
049        } else {
050            hasRef = true;
051        }
052
053        wikiLink = getUrl().toString();
054    }
055
056    /**
057     * {@inheritDoc}
058     */
059    @Override
060    public String toStringAttributes() {
061        return super.toStringAttributes() + ", wikiLink=" + wikiLink + ", hasRef=" + hasRef;
062    }
063
064    /**
065     * getter.
066     *
067     * @return wikiLink field.
068     */
069    public String getWikiLink() {
070        return wikiLink;
071    }
072
073    /**
074     * getter.
075     *
076     * @return hasRef field.
077     */
078    public boolean hasRef() {
079        return hasRef;
080    }
081
082}