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